最近又從新看了一遍 'nginx入門到精通' 抽點時間 出來搭幾個Demo 會有更深體會:php
Nginx不僅有處理http請求的功能,還能作反向代理。Nginx經過反向代理功能將動態請求轉向後端Php-fpm。html
nginx與php-fpm的結合,完整的流程是這樣的mysql
環境說明:
OS:centos6.8 x86 nginx
主機IP: git
191,168.1.26 nginx正則表達式
191,168.1.27 php-fpmsql
191,168.1.28 mysqlvim
1 Nginx 安裝後端
yum install gcc* wget -y #都懂得
安裝nginx 相關依賴: *-devel (開發包)centos
# 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: 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