Nginx PHP FastCGI 安装配置记录

2008-04-18 | 18:26分类:Linux, PHP | 标签: | 4,454 Views

安装 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

  1. # 运行 Nginx 的用户
  2. user  nobody nobody;
  3. # Nginx 的进程数
  4. worker_processes  8;
  5.  
  6. # 错误日志
  7. error_log  /var/log/nginx/error.log;
  8.  
  9. # pid 文件
  10. pid        /var/run/nginx.pid;
  11.  
  12. events {
  13. # 使用 epoll(For Linux Kernel 2.6+) 提升 Nginx 性能。
  14.     use  epoll;
  15. # 最大连接数
  16.     worker_connections  4096;
  17. }
  18.  
  19.  
  20. http {
  21. # 包含 mime types 的配置文件
  22.     include       conf/mime.types;
  23. # 默认类型
  24.     default_type  application/octet-stream;
  25.  
  26. # 定义 temp 的路径
  27.     client_body_temp_path  /tmpfs/nginx/client_body_temp;
  28.     proxy_temp_path        /tmpfs/nginx/proxy_temp;
  29.     fastcgi_temp_path      /tmpfs/nginx/fastcgi_temp;
  30.  
  31. # 如果 server_name 定义的域名过多,需要增大这个值
  32.     server_names_hash_bucket_size  128;
  33.  
  34. # 定义主访问日志格式,因开启了 gzip,加入了 $gzip_ratio
  35.     log_format  main  '$remote_addr - $remote_user [$time_local] $request '
  36.                       '"$status" $body_bytes_sent "$http_referer" '
  37.                       '"$http_user_agent" "$gzip_ratio"';
  38.  
  39. # 默认的访问日志存放文件
  40.     access_log  /var/log/nginx/access.log  main;
  41.  
  42. # Linux Kernel 2.4+ 可通过调用内核级 sendfile() 来提高性能
  43.     sendfile  on;
  44.  
  45. # FreeBSD 或基于 TCP_CORK 的 Linux 系统可使用
  46.     tcp_nopush on;
  47.  
  48. # 只在 keep-alive 连接状态中使用
  49.     tcp_nodelay  on;
  50.  
  51. # 设置保持连接超时时间 和 header 超时时间(单位:秒)
  52.     keepalive_timeout  60 15;
  53.  
  54. # 开启 gzip
  55.     gzip  on;
  56. # 压缩级别 1-9,9 为最高压缩级别
  57.     gzip_comp_level  9;
  58. # 回送给客户端最小的 gzip 压缩大小
  59.     gzip_min_length  1100;
  60. # 设置 gzip 缓存的大小,默认是 4-8k 之间
  61.     gzip_buffers  4 8k;
  62. # gzip http 版本
  63.     gzip_http_version  1.1;
  64. # 需要使用 gzip 压缩的文件类型
  65.     gzip_types  text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  66.  
  67. # 定义默认主机
  68.     server {
  69. # 监听端口
  70.         listen       80;
  71. # 匹配所有域名
  72.         server_name  _ *;
  73. # 默认首页
  74.         index  index.php;
  75. # 根目录路径
  76.         root  /home/www;
  77. # 执行 php 程序文件的设置
  78.         location ~ .*\.php?$ {
  79.             include  conf/fcgi.conf;
  80.         }
  81. # 对于以下文件不记录访问日志
  82.         location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
  83.             access_log  off;
  84.         }
  85.     }
  86.  
  87. # 包含其它虚拟主机的配置
  88.     include  conf/vhosts/*.conf;
  89. }

vi fcgi.conf

  1. fastcgi_pass  127.0.0.1:10080;
  2.  
  3. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
  4. fastcgi_param  SERVER_SOFTWARE    nginx;
  5.  
  6. fastcgi_param  QUERY_STRING       $query_string;
  7. fastcgi_param  REQUEST_METHOD     $request_method;
  8. fastcgi_param  CONTENT_TYPE       $content_type;
  9. fastcgi_param  CONTENT_LENGTH     $content_length;
  10.  
  11. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  12. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
  13. fastcgi_param  REQUEST_URI        $request_uri;
  14. fastcgi_param  DOCUMENT_URI       $document_uri;
  15. fastcgi_param  DOCUMENT_ROOT      $document_root;
  16. fastcgi_param  SERVER_PROTOCOL    $server_protocol;
  17.  
  18. fastcgi_param  REMOTE_ADDR        $remote_addr;
  19. fastcgi_param  REMOTE_PORT        $remote_port;
  20. fastcgi_param  SERVER_ADDR        $server_addr;
  21. fastcgi_param  SERVER_PORT        $server_port;
  22. fastcgi_param  SERVER_NAME        $server_name;
  23.  
  24. # PHP only, required if PHP was built with --enable-force-cgi-redirect
  25. fastcgi_param  REDIRECT_STATUS    200;

再来配置 Discuz! 论坛和 SupeSite/X-Space 的虚拟主机
vi vhosts/main.conf

  1. server {
  2.     listen       80;
  3. # 分论坛可使用子域名
  4.     server_name  bbs.domain.com *.bbs.domain.com;
  5.     index  index.php;
  6.     root  /home/www/bbs;
  7. # 和 Apache Rewrite 规则很相似
  8.     rewrite  ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1;
  9.     rewrite  ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
  10.     rewrite  ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2;
  11.     rewrite  ^/space-(username|uid)-(.+)\.html$ /space.php?action=viewpro&$1=$2;
  12.     rewrite  ^/tag-(.+)\.html$ /tag.php?name=$1;
  13.         location ~ .*\.php?$ {
  14.             include  conf/fcgi.conf;
  15.         }
  16.         location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
  17.             access_log  off;
  18.         }
  19.     }
  20.  
  21.     server {
  22.     listen       80;
  23.     server_name  blog.domain.com;
  24.     index  index.php;
  25.     root  /home/www/blog;
  26.     rewrite  ^([0-9]+)/spacelist(.*)$ index.php?$1/action_spacelist$2;
  27.     rewrite  ^([0-9]+)/viewspace_(.+)$ index.php?$1/action_viewspace_itemid_$2;
  28.     rewrite  ^([0-9]+)/viewbbs_(.+)$ index.php?$1/action_viewbbs_tid_$2;
  29.     rewrite  ^([0-9]+)/(.*)$ index.php?$1/$2;
  30.     rewrite  ^([0-9]+)$ index.php?$1;
  31.     rewrite  ^action_(.+)$ index.php?action_$1;
  32.     rewrite  ^category_(.+)$ index.php?action_category_catid_$1;
  33.     rewrite  ^itemlist_(.+)$ index.php?action_itemlist_catid_$1;
  34.     rewrite  ^viewnews_(.+)$ index.php?action_viewnews_itemid_$1;
  35.     rewrite  ^viewthread_(.+)$ index.php?action_viewthread_tid_$1;
  36.     rewrite  ^index([\.a-zA-Z0-9]*)$ index.php;
  37.     rewrite  ^html/([0-9]+)/viewnews_itemid_([0-9]+)\.html$ index.php?action_viewnews_itemid_$2;
  38.  
  39.     rewrite  ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2;
  40.     rewrite  ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2;
  41.     rewrite  ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2;
  42.     rewrite  ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2;
  43.     rewrite  ^/([0-9]+)$ /index.php?uid/$1;
  44.  
  45.     rewrite  ^/action(.+)$ /index.php?action$1;
  46.     rewrite  ^/category(.+)$ /index.php?action/category/catid$1;
  47.     rewrite  ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1;
  48.     rewrite  ^/viewthread(.+)$ /index.php?action/viewthread/tid$1;
  49.     rewrite  ^/mygroup(.+)$ /index.php?action/mygroup/gid$1;
  50. # 归档目录的默认首页设置
  51.         location /archiver/ {
  52.             index index.html;
  53.         }
  54.         location ~ .*\.php?$ {
  55.             include  conf/fcgi.conf;
  56.         }
  57.         location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
  58.             access_log  off;
  59.         }
  60.     }

用 tmpfs 提升性能
mkdir /tmpfs
在 /etc/fstab 中加入
tmpfs /tmpfs tmpfs size=256m,mode=1777 0 0

mount -a

创建相关目录
vi /usr/local/sbin/mkdir.sh

#/bin/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

  1. 2条评论 关于 “Nginx PHP FastCGI 安装配置记录”

  2. lpfrx 发表于2008-08-02

    nginx跑php不错,但跑perl又怎样呢?

    [Reply]

    bluedata reply on 08月 2nd, 2008:

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

    [Reply]

发表您的评论

您的名字: (*必填)
您的邮箱:(*不会被公布 ,必填)
您的网站: