192.168.130.61 nginx
192.168.130.62 mysql
192.168.130.63 php
1、安装nginx(192.168.130.61)
groupadd -r nginx
useradd -r -g nginx nginx
yum -y groupinstall "Development Tools"
yum -y install openssl-devel pcre-devel
wget http://nginx.org/download/nginx-1.15.8.tar.gz
tar zxvf nginx-1.15.8.tar.gz
cd nginx-1.15.8
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-http_addition_module
make && make install
为nginx提供SysV init脚本:
vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
chmod +x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start
2、安装mysql(192.168.130.62)
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
mv /usr/local/mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
创建相关用户及目录
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin mysql
mkdir -p /mydata/data
mkdir -p /mydata/binlog
chown mysql:mysql /mydata/data
chown mysql:mysql /mydata/binlog
服务器启动脚本
chown :mysql /usr/local/mysql -R
cd /usr/local/mysql/
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
创建配置文件
vim /etc/my.cnf
[mysqld]
bind-address = 0.0.0.0
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/mydata/data
log-bin=/mydata/binlog/log-bin
server-id=1
skip-name-resolve
设置环境变量
echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
配置为开机启动
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data
/etc/init.d/mysqld start
chkconfig --add mysqld
chkconfig --list mysqld
netstat -tuanlp | grep 3306
设置root密码,授权远程连接
mysql
SET PASSWORD = PASSWORD('root123');
GRANT ALL ON mysql.* TO 'root'@'localhost' IDENTIFIED BY 'root123';
GRANT ALL ON mysql.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'root123';
GRANT ALL ON mysql.* TO 'testuser'@'192.168.%.%' IDENTIFIED BY 'testpass';
FLUSH PRIVILEGES;
\q
3、安装php-5.6.38(192.168.130.63)
安装依赖包
yum -y groupinstall "Development tools" "Server Platform Development"
yum -y install https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm
yum -y install bzip2-devel libmcrypt-devel libxml2-devel openssl-devel
安装php
wget http://101.110.118.22/am1.php.net/distributions/php-5.6.38.tar.gz
tar xf php-5.6.38.tar.gz
cd php-5.6.38
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
因为我们PHP和MYSQL是分离的,所以替换为如下
./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
make && make install
复制配置文件
cp php.ini-production /etc/php.ini
为php-fpm提供SysV init脚本
cp /root/php-5.6.38/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --list php-fpm
chkconfig php-fpm on
为php-fpm提供配置文件:
useradd nginx
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
user = nginx
group = nginx
listen = 192.168.130.63:9000
启动php-fpm
service php-fpm start
netstat -tuanlp | grep 9000
4、nginx与php整合,php连接数据库
php配置
mkdir -p /var/www/html
chown nginx:nginx /var/www/html
vim /var/www/html/index.php
<?php
$link = mysql_connect('192.168.130.62','testuser','testpass');
if ($link)
echo "连接数据库成功";
else
echo "连接数据库失败";
mysql_close();
phpinfo();
?>
/etc/init.d/php-fpm restart
nginx配置
vim /etc/nginx/nginx.conf
location / {
root /var/www/html; #更改目录
index index.php index.html index.htm; #添加一个index.php
}
location ~ \.php$ {
root /var/www/html;#更改目录
fastcgi_pass 192.168.130.63:9000; #这里为PHP服务器的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
vim /etc/nginx/fastcgi_params
#添加以下这行:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
mkdir -p /var/www/html
chown -R nginx:nginx /var/www/html
touch /var/www/html/index.php
/etc/init.d/nginx restart
5、测试
http://192.168.130.61