Nginx PHP FastCGI 安装配置记录
网址: http://bluedata.org/2008/04/18/nginx-php-fastcgi/
安装 PHP
下载:http://www.php.net/downloads.php
因需要兼容老程序,这里用的还是 PHP4 ,可根据自己的需要更改编译选项。
tar jxvf php-4.4.8.tar.bz2
cd php-4.4.8
./configure \
--prefix=/usr/local/php-fcgi \
--enable-fastcgi \
--enable-discard-path \
--enable-force-cgi-redirect \
--enable-mbstring=all \
--enable-memcache \
--with-config-file-path=/usr/local/php-fcgi/etc \
--with-gd=/usr/local/gd \
--with-zlib \
--with-png \
--with-jpeg \
--with-freetype \
--with-mysql=/usr/local/mysql \
--with-dom
make
make install
cp -f php.ini-dist /usr/local/php-fcgi/etc/php.ini
安装 ZendOptimizer
Zend 的优化器,免费使用,如果加密了 PHP 程序文件,这是必须的。
如果没有使用 Zend 优化器,PHP 进程的内存会难以释放。
下载:http://www.zend.com/en/products/guard/downloads
tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.3-linux-glibc23-i386
./install
cd ..
安装 eAccelerator
PHP scripts 的缓存,对于提高 PHP 程序执行速度很有帮助。
下载:http://eaccelerator.net/
tar jxvf eaccelerator-0.9.5.2.tar.bz2
cd eaccelerator-0.9.5.2
/usr/local/php-fcgi/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php-fcgi/bin/php-config
make
make install
cd ..
安装 memcache
用于缓存 sql 查询,对于降低数据库压力,提升查询速度有很大好处。
下载:http://pecl.php.net/package/memcache
tar zxvf memcache-2.2.3.tgz
cd memcache-2.2.3
/usr/local/php-fcgi/bin/phpize
./configure --with-php-config=/usr/local/php-fcgi/bin/php-config
make
make install
安装 Linghttpd's spawn-fcgi
用 Lighttpd 的 spawn-fcgi 来管理 PHP FastCGI 进程。
下载 Lighttpd: http://www.lighttpd.net/download
yum -y install pcre-devel
(debian: apt-get -y install libpcre3-dev)
tar zxvf lighttpd-1.4.19.tar.bz2
cd lighttpd-1.4.19
./configure --without-bzip2
make
cp -a src/spawn-fcgi /usr/local/php-fcgi/bin/
安装 Nginx
下载:http://nginx.net/
tar zxvf nginx-0.5.35.tar.gz
cd nginx-0.5.35
./configure \
--user=nobody \
--group=nobody \
--prefix=/usr/local/nginx \
--with-http_stub_status_module
make
make install
配置 PHP
vi /usr/local/php-fcgi/etc/php.ini
# 最大执行时间
max_execution_time = 5
# 打开全局变量兼容老程序
register_globals = On
# 默认编码
default_charset = "gbk"
# 扩展模块的目录
extension_dir = "/usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20020429"
# 关闭 mysql 持久连接。高访问量下会把数据库可用连接用完。
mysql.allow_persistent = Off
# 降低连接超时时间
mysql.connect_timeout = 10
# session 文件存储目录,这里为两级目录,需要手动创建。
session.save_path = "2;/tmpfs/phpsession"
# session 过期时间设为一小时。
session.gc_maxlifetime = 3600
# 关闭
session.bug_compat_42 = 0
session.bug_compat_warn = 0
# 开启 eAccelerator
[eAccelerator]
extension="eaccelerator.so"
# 可使用内存设为了 32M,太大会启动不了 PHP。
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmpfs/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
# 在 eAccelerator 之后启用 ZO ,避免 eaccelerator.so 不能加载。
[Zend]
zend_optimizer.optimization_level=1023
zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.3
zend_optimizer.version=3.3.3
zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so
# 开启 memcache
[memcache]
extension=memcache.so
配置 Nginx
建立相应目录
mkdir /var/log/nginx
cd /usr/local/nginx
mv logs conf/vhosts
cd conf
vi nginx.conf
- # 运行 Nginx 的用户
- user nobody nobody;
- # Nginx 的进程数
- worker_processes 8;
- # 错误日志
- error_log /var/log/nginx/error.log;
- # pid 文件
- pid /var/run/nginx.pid;
- events {
- # 使用 epoll(For Linux Kernel 2.6+) 提升 Nginx 性能。
- use epoll;
- # 最大连接数
- worker_connections 4096;
- }
- http {
- # 包含 mime types 的配置文件
- include conf/mime.types;
- # 默认类型
- default_type application/octet-stream;
- # 定义 temp 的路径
- client_body_temp_path /tmpfs/nginx/client_body_temp;
- proxy_temp_path /tmpfs/nginx/proxy_temp;
- fastcgi_temp_path /tmpfs/nginx/fastcgi_temp;
- # 如果 server_name 定义的域名过多,需要增大这个值
- server_names_hash_bucket_size 128;
- # 定义主访问日志格式,因开启了 gzip,加入了 $gzip_ratio
- log_format main '$remote_addr - $remote_user [$time_local] $request '
- '"$status" $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$gzip_ratio"';
- # 默认的访问日志存放文件
- access_log /var/log/nginx/access.log main;
- # Linux Kernel 2.4+ 可通过调用内核级 sendfile() 来提高性能
- sendfile on;
- # FreeBSD 或基于 TCP_CORK 的 Linux 系统可使用
- tcp_nopush on;
- # 只在 keep-alive 连接状态中使用
- tcp_nodelay on;
- # 设置保持连接超时时间 和 header 超时时间(单位:秒)
- keepalive_timeout 60 15;
- # 开启 gzip
- gzip on;
- # 压缩级别 1-9,9 为最高压缩级别
- gzip_comp_level 9;
- # 回送给客户端最小的 gzip 压缩大小
- gzip_min_length 1100;
- # 设置 gzip 缓存的大小,默认是 4-8k 之间
- gzip_buffers 4 8k;
- # gzip http 版本
- gzip_http_version 1.1;
- # 需要使用 gzip 压缩的文件类型
- gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
- # 定义默认主机
- server {
- # 监听端口
- listen 80;
- # 匹配所有域名
- server_name _ *;
- # 默认首页
- index index.php;
- # 根目录路径
- root /home/www;
- # 执行 php 程序文件的设置
- location ~ .*\.php?$ {
- include conf/fcgi.conf;
- }
- # 对于以下文件不记录访问日志
- location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
- access_log off;
- }
- }
- # 包含其它虚拟主机的配置
- include conf/vhosts/*.conf;
- }
vi fcgi.conf
- fastcgi_pass 127.0.0.1:10080;
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;
- fastcgi_param SERVER_SOFTWARE nginx;
- fastcgi_param QUERY_STRING $query_string;
- fastcgi_param REQUEST_METHOD $request_method;
- fastcgi_param CONTENT_TYPE $content_type;
- fastcgi_param CONTENT_LENGTH $content_length;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param SCRIPT_NAME $fastcgi_script_name;
- fastcgi_param REQUEST_URI $request_uri;
- fastcgi_param DOCUMENT_URI $document_uri;
- fastcgi_param DOCUMENT_ROOT $document_root;
- fastcgi_param SERVER_PROTOCOL $server_protocol;
- fastcgi_param REMOTE_ADDR $remote_addr;
- fastcgi_param REMOTE_PORT $remote_port;
- fastcgi_param SERVER_ADDR $server_addr;
- fastcgi_param SERVER_PORT $server_port;
- fastcgi_param SERVER_NAME $server_name;
- # PHP only, required if PHP was built with --enable-force-cgi-redirect
- fastcgi_param REDIRECT_STATUS 200;
再来配置 Discuz! 论坛和 SupeSite/X-Space 的虚拟主机
vi vhosts/main.conf
- server {
- listen 80;
- # 分论坛可使用子域名
- server_name bbs.domain.com *.bbs.domain.com;
- index index.php;
- root /home/www/bbs;
- # 和 Apache Rewrite 规则很相似
- rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1;
- rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
- rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2;
- rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?action=viewpro&$1=$2;
- rewrite ^/tag-(.+)\.html$ /tag.php?name=$1;
- location ~ .*\.php?$ {
- include conf/fcgi.conf;
- }
- location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
- access_log off;
- }
- }
- server {
- listen 80;
- server_name blog.domain.com;
- index index.php;
- root /home/www/blog;
- rewrite ^([0-9]+)/spacelist(.*)$ index.php?$1/action_spacelist$2;
- rewrite ^([0-9]+)/viewspace_(.+)$ index.php?$1/action_viewspace_itemid_$2;
- rewrite ^([0-9]+)/viewbbs_(.+)$ index.php?$1/action_viewbbs_tid_$2;
- rewrite ^([0-9]+)/(.*)$ index.php?$1/$2;
- rewrite ^([0-9]+)$ index.php?$1;
- rewrite ^action_(.+)$ index.php?action_$1;
- rewrite ^category_(.+)$ index.php?action_category_catid_$1;
- rewrite ^itemlist_(.+)$ index.php?action_itemlist_catid_$1;
- rewrite ^viewnews_(.+)$ index.php?action_viewnews_itemid_$1;
- rewrite ^viewthread_(.+)$ index.php?action_viewthread_tid_$1;
- rewrite ^index([\.a-zA-Z0-9]*)$ index.php;
- rewrite ^html/([0-9]+)/viewnews_itemid_([0-9]+)\.html$ index.php?action_viewnews_itemid_$2;
- rewrite ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2;
- rewrite ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2;
- rewrite ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2;
- rewrite ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2;
- rewrite ^/([0-9]+)$ /index.php?uid/$1;
- rewrite ^/action(.+)$ /index.php?action$1;
- rewrite ^/category(.+)$ /index.php?action/category/catid$1;
- rewrite ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1;
- rewrite ^/viewthread(.+)$ /index.php?action/viewthread/tid$1;
- rewrite ^/mygroup(.+)$ /index.php?action/mygroup/gid$1;
- # 归档目录的默认首页设置
- location /archiver/ {
- index index.html;
- }
- location ~ .*\.php?$ {
- include conf/fcgi.conf;
- }
- location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
- access_log off;
- }
- }
用 tmpfs 提升性能
mkdir /tmpfs
在 /etc/fstab 中加入
tmpfs /tmpfs tmpfs size=256m,mode=1777 0 0
mount -a
创建相关目录
vi /usr/local/sbin/mkdir.sh
mkdir /tmpfs/nginx
chown -R nobody:nobody /tmpfs/nginx
chmod -R 1777 /tmpfs/nginx
mkdir /tmpfs/eaccelerator
chown -R nobody:nobody /tmpfs/eaccelerator
chmod -R 1777 /tmpfs/eaccelerator
I="0 1 2 3 4 5 6 7 8 9 a b c d e f"
for acm in $I;
do
for x in $I;
do
mkdir -p /tmpfs/phpsession/$acm/$x;
done;
done
chown -R nobody:nobody /tmpfs/phpsession
chmod -R 1777 /tmpfs/phpsession
chmod +x /usr/local/sbin/mkdir.sh
mkdir.sh
启动 PHP 和 Nginx
/usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 32 -u nobody -g nobody -f /usr/local/php-fcgi/bin/php
监听在 127.0.0.1 的 10080 端口并开启 32 个进程,使用和 Nginx 一样的 nobody 用户。(PHP5 的执行程序是 php-cgi)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重启 PHP 进程可以先 killall php 再启动。
重启 Nginx
kill -HUP `cat /var/run/nginx.pid`
清除超过一小时没有被访问到的 eAccelerator 缓存文件和创建时间超过一小时的 php session 文件,防止占满空间。
vi /etc/crontab
0 * * * * root find /tmpfs/eaccelerator -type f -amin +60 -exec rm {} \;
0 * * * * root find /tmpfs/phpsession -type f -cmin +60 -exec rm {} \;
配置开机自启动
vi /etc/rc.local
/usr/local/sbin/mkdir.sh
/usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 32 -u nobody -g nobody -f /usr/local/php-fcgi/bin/php
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
方便重启 Nginx
vi ~/.bashrc
alias nginx='kill -HUP `cat /var/run/nginx.pid`'
source ~/.bashrc


















对比 Apache 的 mod_cgi ,在高并发状态下,性能肯定也是有相当大的提高的。
使用 Nginx 还达到了一个“动静分离”的效果。
回复