一、安装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
为什么使用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