Nginx php-fpm 分离搭建 (上) 未完
最近又重新看了一遍 'nginx入门到精通' 抽点时间 出来搭几个Demo 会有更深体会:
Nginx如何与Php-fpm结合
Nginx不只有处理http请求的功能,还能做反向代理。Nginx通过反向代理功能将动态请求转向后端Php-fpm。
nginx与php-fpm的结合,完整的流程是这样的
环境说明:
OS:centos6.8 x86
主机IP:
191,168.1.26 nginx
191,168.1.27 php-fpm
191,168.1.28 mysql
1 Nginx 安装
yum install gcc* wget -y #都懂得
安装nginx 相关依赖: *-devel (开发包)
# pcre 支持正则表达式 # zlib 支持数据压缩 # openssl支持HTTPS
yum install zlib zlib-devel pcre pcre-devel openssl openssl-devel -y
下载Nginx 源码包:
#cd /usr/src
#下载:nginx-1.8.1.tar.gz 解压 tar -zxvf nginx-1.8.1.tar.gz
cd /usr/src && wget http://mirrors.sohu.com/nginx/nginx-1.8.1.tar.gz && tar -zxvf nginx-1.8.1.tar.gz
cd ./nginx-1.8.1
创建groupadd useradd用户 : www # 创建nginx worker进程工作用户
groupadd -g 700 www
useradd -u 800 -g 700 -s /sbin/nologin www
安装nginx
#--with-http_ssl_module 启用HTTPS加密 #--with-http_stub_status_module 启用nginx状态监控 #--with-http_gzip_static_module 启用静态压缩 #--with-http_realip_module 做代理时获取客户端真实IP
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
--with-http_gzip_static_module --with-http_realip_module --with-pcre --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module
[root@26_Nginx nginx-1.8.1]# make && make install
[root@26_Nginx nginx-1.8.1]# vim /etc/init.d/nginx # 创建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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
killall -9 nginx
}
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
授权file
[root@26_Nginx nginx-1.8.1]# chmod +x /etc/init.d/nginx
Nginx 添加 用户 组
[root@26_Nginx nginx-1.8.1]# vim /usr/local/nginx/conf/nginx.conf
user www www;
service nginx start
添加开机启动项 chkconfig nginx on
临时关闭 防火墙: iptables -F
修改 nginx 配置文件如下:
vim /usr/local/nginx/conf/nginx.conf
配置修正:
location ~ \.php$ {
root /www;
fastcgi_pass 191.168.1.27:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
配置文件检查:
/usr/local/nginx/sbin/nginx -t
[root@26_Nginx nginx-1.8.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1.2 nfs + rpcbin 安装 共享文件存储server
创建网站测试文件:
mkdir /www
创建测试文件:
echo '<?php phpinfo(); ?>' >> /www/index.php
yum install nfs-utils rpcbin -y
修改nfs 配置文件件
echo '/www 191.168.1.0/24' >> /etc/exports
重启 nfs rpcbin 服务:
service rpcbind restart
service nfs restart
两台主机可重启一下 ntpd 对一下时间 yum install ntpd -y
service ntpd restart
2:php—191.168.1.27:
nfs 安装:
yum install nfs-utils rpcbind -y
挂载 nginx 主机 nfs 到 /www
mount -t nfs 191.168.1.26:/www /www
安装 php-fpm 依赖:
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2-devel openssl openssl-devel bzip2-devel libcurl-devel gd -y
EPEL源 没有 libmcrypt , mhash 包
更换EPEL源 :
[root@php_27 ~]# vim /etc/yum.repos.d/epel-centos6.repo [epel] name=Extra Packages for Enterprise Linux 6 - $basearch baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch failovermethod=priority enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 [epel-debuginfo] name=Extra Packages for Enterprise Linux 6 - $basearch - Debug baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 gpgcheck=0 [epel-source] name=Extra Packages for Enterprise Linux 6 - $basearch - Source baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 gpgcheck=0
yum repolist
重新安装
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2-devel openssl openssl-devel bzip2-devel libcurl-devel
创建PHP 运行 组·用户
groupadd -g 700 www
useradd -u 700 -g 700 -s /sbin/nologin www
php --with 一些参数说明:
---------------------------------------------------------------
1 ./configure --prefix=/usr/local/php \ //安装位置 2 --with-mysql \ //支持mysql 3 --with-pdo-mysql \ //支持pdo模块 4 --with-mysqli \ //支持mysqli模块 5 --with-openssl \ //支持openssl模块 6 --enable-fpm \ //支持fpm模式 7 --enable-sockets \ //启用socket支持 8 --enable-sysvshm \ //启用系统共享内存支持 9 --enable-mbstring \ //使多字节字符串的支持 10 --with-freetype-dir \ //设置FreeType安装前缀路径 11 --with-jpeg-dir \ //设置libjpeg安装前缀路径 12 --with-png-dir \ //设置libpng安装前缀路径 13 --with-zlib-dir \ //设置libz安装前缀路径 14 --with-libxml-dir=/usr \ //设置libxml2安装路径 15 --enable-xml \
16 --with-mhash \ //支持mhash 17 --with-mcrypt \ //支持mcrypt 18 --with-config-file-path=/etc \ //配置文件路径 19 --with-config-file-scan-dir=/etc/php.d \ //配置文件扫描路径 20 --with-bz2 \ //支持BZip2 21 --with-curl //支持curl
---------------------------------------------------------------
下载PHP 源码包并解压:
cd /usr/src/ && wget http://mirrors.sohu.com/php/php-5.6.7.tar.gz && tar -zxvf php-5.6.7.tar.gz
cd php-5.6.7
./configure --prefix=/usr/local/php --enable-fpm --enable-ftp --enable-zip --enable-xml --enable-sockets --enable-bcmath --enable-pcntl --enable-shmop --enable-soap --enable-sysvsem --enable-mbstring --enable-mbregex --enable-inline-optimization --enable-maintainer-zts --enable-gd-native-ttf
--with-fpm-user=www --with-fpm-group=www --with-mysql --with-mysqli --with-pdo-mysql --with-openssl --with-freetype-dir --with-iconv-dir --with-jpeg-dir --with-png-dir --with-libxml-dir --with-curl --with-zlib --with-bz2 --with-xmlrpc --with-gd --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d
编译时间有点长:
COPY 启动脚本到 /etc/init.d/
[root@php_27 php-5.6.7]# cp -a sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php_27 php-5.6.7]# chmod +x /etc/init.d/php-fpm
[root@php_27 php-5.6.7]# cp -a php.ini-production /usr/local/php/etc/php.ini
[root@php_27 php-5.6.7]# cd /usr/local/php/etc/
[root@php_27 etc]# cp -a php-fpm.conf.default php-fpm.conf
[root@php_27 etc]# vim php-fpm.conf # 修改监听地址
listen = 191.168.1.27:9000
重启 nginx
3 安装MYSQL #第三台主机 191.168.1.28 安装Mysql
先来安装NFS rpcbind 并且挂载到 nginx php-fpm
yum install nfs-utils rpcbind -y
测试用先关闭 iptables -F
创建/WWW 网站目录 #这里可以使用git管理网站目录
mkdir /www
修改NFS配置文件
echo "/www 191.168.1.1/24(rw,no_root_squash)" >> /etc/exports
重启服务
service rpcbind start
service nfs start
在其它两台主机
showmount -e 191.168.1.28 #error
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
或 clnt_create: RPC: Unknown host的解决方法
第一条为 我 用了 iptables -F 解决 iptables stop
第二条 echo '191.168.1.28 mysql_server' > /etc/hosts
在次 showmount -e mysql_server
在次挂载 OK
mount -t nfs mysql_server:/www /www
mount -l
yum install mysql mysql-server mysql-devel -y