一、安裝Nginx

源碼安裝

yum pcre-devel
wget http://nginx.org/download/nginx-1.0.4.tar.gz
tar zxvf nginx-1.0.4.tar.gz
cd nginx-1.0.4/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make
make install
  • 默認配置文件在<prefix>/conf/nginx下
  • 程序文件在<prefix>/sbin/nginx
  • 日誌放在了<prefix>/logs/


創建www用戶和組:

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

二、安裝FastCGI模式PHP

1、安裝php-fpm

為什麼使用php-fpm?php-fpm是對PHP FastCGI的增進改善。到http://php-fpm.org/下載對應php版本的補丁*。解壓,安裝方法類似:

tar zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1

* PHP 5.3.3 and later無須打補丁步驟,fpm已經集成在PHP官方core里了。

2、重新編譯php

注意加上–enable-fpm參數,加上該參數後不會生成php-cgi二進制程序,被php-fpm替代。

參考參數

./configure --prefix=/home/root/php --enable-fpm --enable-cgi --enable-fastcgi --enable-force-cgi-redirect --with-mysql=/usr/local/mysql --with-pdo-mysql --enable-mbstring --with-gd --enable-gd-native-ttf --enable-gd-jis-conv --with-zlib --with-png-dir --with-jpeg-dir  --with-freetype-dir --enable-sockets --with-iconv --with-config-file-path=/home/root/php/etc --with-pear --with-curl
 
make clean
make
make install

每次make前一定不要忘記make clean,否則總會出現一些莫名其妙的問題。

為避免影響線上系統正常運作,將新PHP的路徑設置為了–prefix=/home/root/php。按Linux系統目錄風格,這不是常規路徑。測試後,可重新編譯到/usr/local/php。編譯前注意備份php.ini避免被覆蓋。

三、配置

1、修改<prefix>/etc/php-fpm.conf

user = www
group = www
pm.max_children = 50
pm.start_servers = 10(去掉注釋)
pm.min_spare_servers = 5
pm.max_spare_servers = 35

如果服務器內存比較小,建議從10開始遞增修改max_children,子進程會佔用比較多的內存。

2、編輯/usr/local/nginx/conf/nginx.conf

worker_processes  2; #有多少個CPU核
worker_connections 1024;  #執行ulimited -n命令查看

這裡說明一下虛擬主機配置

http {
  index index.html index.php;
 
  server {
    listen 30010; #端口
    server_name www.33iq.com 33iq.com; #第一個虛擬主機,www和根域名均適用
    root /var/www/html/33iq.com; #網站目錄
    access_log logs/domain1.access.log main; #日誌文件位置
 
    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/33iq$fastcgi_script_name;
            include        fastcgi_params;
    } #這裡是Nginx反向代理的本質:遇到php結尾的文件,就轉發到9000端口的fastcgi去處理
  }
 
  server {
    listen 30010;
    server_name www.geexun.com; #第二個虛擬主機
    root /var/www/html/geexun;
    access_log  logs/domain2.access.log main;
    #...
  }
}

3、改.htaccess

.htaccess是Apache基於目錄的配置文件,Apache默認會去每個目錄下遞歸搜尋該文件,如果存在,則針對該目錄應用不同的配置規則(主要是rewrite)。由於該方式低效,Nginx並不支持htaccess,不過,它還是支持在nginx.conf里通過include載入外部配置文件的。

晒晒IQ網為例,複製/var/www/html/33iq目錄下的.htaccess

cd /var/www/html/33iq
cp .htaccess .htaccess_nginx
vim .htaccess_nginx

編輯.htaccess_nginx,改好後在/usr/local/nginx/conf/nginx.conf里對應虛擬主機server{…}里添加一句

include /var/www/html/33iq/.htaccess_nginx;

Apache Rewrite規則改寫為Nginx格式:

Apache:
RewriteRule ^/(.*)$ http://www.33iq.com/$1 [L,R=301]
Nginx:
rewrite ^/(.*)$ http://www.33iq.com/$1 permanent;

可見,其實二者區別不大。Nginx配置文件每句話最後有;分號,此外除了兩頭的指令格式不同,中間的正則表達式替換完全一樣。總結規律如下:

Apache Nginx
RewriteRule rewrite
RewriteCond if
[R],[R,L] redirect
[L],[P],[P,L] last

按上面的規律替換修改為Nginx規則後,執行nginx -t命令檢查語法,如果有錯誤,一般是因為rewrite正則表達式裡面的大括號{  }與Ngnix配置文件的大括號server{…}錯誤地配對了,引發衝突。解決辦法是,給條件加上引號:

rewrite "^/xianshou\/([\d]{6,6})\.html$" /xianshou.php?function=sh&number=$1 last;

四、測試

1、啟動PHP FastCGI:

/home/root/php/sbin/php-fpm -c /etc/php.ini (-c 參數指定php.ini路徑)

2、啟動Nginx:

/usr/local/nginx/sbin/nginx

配置開機自動啟動Nginx+PHP:vim /etc/rc.local 在末尾增加以上內容即可。

修改nginx.conf後,不用重啟,平滑過渡新配置

/usr/local/nginx/sbin/nginx -s reload

3、注意防火牆,把測試需要用的30010端口打開

vi /etc/sysconfig/iptables
增加一行
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 30010 -j ACCEPT
重新啟動防火牆服務
/sbin/service iptables restart
查看端口是否開放
/sbin/iptables -L -n

通過www.33iq.com:30010訪問,測試網站功能是否運行正常。遇到502 Bad Gateway等錯誤,可能是後端php-fpm的問題,要學會查看日誌進行分析。

五、線上遷移

修改配置文件/usr/local/nginx/conf/nginx.conf,將nginx的30010測試端口號改為80,然後停掉apache2,重新加載nginx

killall httpd
/usr/local/nginx/sbin/nginx -s reload

 

參考鏈接
http://blog.s135.com/nginx_php_v6/
http://wiki.nginx.org