[服务器] 体验 Nginx + PHP FastCGI + MySQL

来源:互联网 发布:钢铁少女数据 编辑:程序博客网 时间:2024/04/28 08:05
最近 Nginx 很火,主要得益于它对并发请求的强大处理能力,这也是 Apache 以及 iis 的软肋所在,所以在一个比较大型的网络应用中,加入 Nginx 这个新元素就变得必不可少了,这里主要记录下一些主要的安装步骤,以及在安装中出现的问题。

软件版本:Nginx 0.6.32, PHP 5.2.6, MySQL 5.1.26.
安装过程的主要操作记录如下:

(Install required packages ..)
# wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
# wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.26-rc.tar.gz/from/http://mirror.x10.com/mirror/mysql/
# wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.8.diff.gz
# wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
# wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz
# wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
...
# tar -zxvf mysql-5.1.26-rc.tar.gz
# cd mysql-5.1.26-rc
# ./configure --prefix=/usr/local/mysql --without-debug --with-unix-socket-path=/usr/local/mysql/mysql.sock --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client
# make install clean
# chmod +w /usr/local/mysql/
# cd /usr/local/mysql/
# cp share/mysql/my-large.cnf /etc/my.cnf
(Replace port = 3306 , socket = /tmp/mysql.sock in /etc/my.cnf first.)
# bin/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
# cp share/mysql/mysql.server /usr/local/bin/mysql.server
# mysql.server start|status|stop (ok.)
...
# yum install (or apt-get -y install) libxml2-devel (Fix configure error: checking whether libxml build works)
# yum install (or apt-get -y install) curl-devel (Fix configure error: Please reinstall the libcurl distribution)
# yum install (or apt-get -y install) freetype-devel
# yum install (or apt-get -y install) libjpeg-devel
# yum install (or apt-get -y install) libpng-devel
# yum install (or apt-get -y install) mysql-devel
...
# tar -zxvf libiconv-1.12.tar.gz
# cd libiconv-1.12
# ./configure
# make install clean
...
# tar -zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make install clean
...
# tar -zxvf php-5.2.6.tar.gz
# gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1
# cd php-5.2.6
# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-libxml-dir=/usr --enable-xml --with-gd --enable-gd-native-ttf --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-discard-path --enable-safe-mode --enable-calendar --enable-bcmath --enable-shmop --enable-sysvsem --enable-pcntl --with-curl --with-curlwrappers --enable-mbregex  --enable-mbstring --with-mcrypt --with-openssl --disable-debug --disable-rpath
# cp php.ini-dist /etc/php.ini
# make install clean
...
# tar -zxvf xcache-1.2.2.tar.gz
# cd xcache-1.2.2
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache
(Install `apt-get -y install autoconf` if phpize failed.)
# make install clean
...
(Config php.ini ..)
# sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"#' /etc/php.ini
# sed -i 's#display_errors = On#display_errors = Off#' /etc/php.ini (if production envionment)
# vi /etc/php.ini (Add follow config to the end of the file)
... ...
[xcache-common]
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so

[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"

[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo | grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo | grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"

(Config php-fpm.conf)
... ...
<value name="user">nginx</value> (add user nginx:nginx first for nginx server)
<value name="group">nginx</value>
... ...
<value name="max_children">128</value>
... ...
<value name="rlimit_files">51200</value>
... ...
<value name="max_requests">51200</value>
... ...
# ulimit -SHn 51200
# /usr/local/php/sbin/php-fpm start|stop|restart (ok.)

以上安装过程与传统的 php 安装用做 apache 的 mod_php 的方式有一定的区别,特别要注意的是:如果你想同时把 php 安装成为 mod_php 和 fast_cgi 模式的话,是不能成功的,也就是说 --enable-fastcgi 与 --with-apxs2=xxx 选项是不能同时选中的,否则安装会没有效果。

(Intergrate with Nginx ..)
# tar zxvf pcre-7.7.tar.gz
# cd pcre-7.7/
# ./configure
# make install clean

# tar zxvf nginx-0.6.32.tar.gz
# cd nginx-0.6.32/
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
# make install clean

(Set your own conf/nginx.conf ..)
  1. user nginx nginx;
  2. worker_processes  4;
  3. # [ debug | info | notice | warn | error | crit ]
  4. error_log  logs/error.log crit;
  5. pid        logs/nginx.pid;
  6. events {
  7.     # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
  8.     use epoll;
  9.     worker_connections  1024;
  10. }
  11. http {
  12.     include       mime.types;
  13.     default_type  application/octet-stream;
  14.     sendfile      on;
  15.     tcp_nopush    on;
  16.     tcp_nodelay   on;
  17.     keepalive_timeout  60;
  18.     fastcgi_connect_timeout 60;
  19.     fastcgi_send_timeout 180;
  20.     fastcgi_read_timeout 180;
  21.     fastcgi_buffer_size 128k;
  22.     fastcgi_buffers 4 128k;
  23.     fastcgi_busy_buffers_size 128k;
  24.     fastcgi_temp_file_write_size 128k;
  25.     fastcgi_temp_path /dev/shm;
  26.     gzip  on;
  27.     gzip_min_length     1k;
  28.     gzip_buffers      4 8k;
  29.     gzip_http_version  1.1;
  30.     gzip_types        text/plain application/x-javascript text/css text/html application/xml;
  31.     charset utf-8;
  32.     server {
  33.         listen       8000;
  34.         server_name  localhost;
  35.         index        index.html index.htm index.php;
  36.         root         /home/nginx/web/www;
  37.         log_format   access '$remote_addr - $remote_user [$time_local] "$request" '
  38.                             '$status $body_bytes_sent "$http_referer" '
  39.                             '"$http_user_agent" $http_x_forwarded_for';
  40.         access_log   /home/nginx/web/logs/access.log  access;
  41.         error_log    /home/nginx/web/logs/error.log   crit;
  42.         if (-d $request_filename)
  43.         {
  44.             rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  45.         }
  46.         location ~ .*/.phpx?$
  47.         {
  48.             include        fastcgi_params;
  49.             # Add for fastcgi
  50.             fastcgi_param  SCRIPT_FILENAME  /home/nginx/web/www/phps/$fastcgi_script_name;
  51.             # Add End
  52.             fastcgi_pass   127.0.0.1:9000;
  53.             fastcgi_index  index.php;
  54.         }
  55.         error_page  404              /404.html;
  56.         error_page  500 502 503 504  /50x.html;
  57.         location ~ .*/.html?$
  58.         {
  59.             root  /home/nginx/web/www/html;
  60.         }
  61.         location ~ ^/(images|img|javascript|js|css|flash|media|static)/
  62.         {
  63.             root         /home/nginx/web/www/htdocs;
  64.             access_log   off;
  65.             expires      30d;
  66.         }
  67.         location /NginxStatus {
  68.             stub_status           on;
  69.             access_log            on;
  70.             auth_basic            "NginxStatus";
  71.             auth_basic_user_file  htpasswd;
  72.         }
  73.     }
  74.     # HTTPS server
  75.     #
  76.     #server {
  77.     #    listen       443;
  78.     #    server_name  localhost;
  79.     #    ssl                  on;
  80.     #    ssl_certificate      cert.pem;
  81.     #    ssl_certificate_key  cert.key;
  82.     #    ssl_session_timeout  5m;
  83.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;
  84.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  85.     #    ssl_prefer_server_ciphers   on;
  86.     #    location / {
  87.     #        root   html;
  88.     #        index  index.html index.htm;
  89.     #    }
  90.     #}
  91. }
另附上 nginx.server 脚本,用于管理 nginx 服务:
  1. #!/bin/sh
  2. #
  3. # Nginx daemon control script.
  4. # This is an init script for the nginx daemon.
  5. # To use nginx, you must first set up the config file(s).
  6. #
  7. # Written for Slackware Linux by Cherife Li <cherife@dotimes.com>.
  8. ROOTPATH="/usr/local/nginx"
  9. DAEMON="$ROOTPATH/sbin/nginx"
  10. CONF="$ROOTPATH/conf/nginx.conf"
  11. PID="$ROOTPATH/logs/nginx.pid"
  12. nginx_start() {
  13.   # Sanity checks.
  14.   if [ ! -r $CONF ]; then # no config file, exit:
  15.     echo "Please check the nginx config file, exiting..."
  16.     exit 1
  17.   fi
  18.   if [ -s $PID ]; then
  19.     echo "Nging is already running?"
  20.     exit 1
  21.   fi
  22.   echo "Starting Nginx server daemon:"
  23.   if [ -x $DAEMON ]; then
  24.     $DAEMON -c $CONF
  25.   fi
  26. }
  27. nginx_test_conf() {
  28.   echo "Checking configuration for correct syntax and"
  29.   echo "then trying to open files referenced in configuration..."
  30.   $DAEMON -t -c $CONF
  31. }
  32. nginx_term() {
  33.   echo "Shutdown Nginx quickly..."
  34.   kill -TERM $(cat $PID)
  35. }
  36. nginx_stop() {
  37.   echo "Shutdown Nginx gracefully..."
  38.   kill -QUIT $(cat $PID)
  39. }
  40. nginx_reload() {
  41.   echo "Reloading Nginx configuration..."
  42.   kill -HUP $(cat $PID)
  43. }
  44. nginx_upgrade() {
  45.   echo "Upgrading to the new Nginx binary."
  46.   echo "Make sure the Nginx binary has been replaced with new one"
  47.   echo "or Nginx server modules were added/removed."
  48.   kill -USR2 $(cat $PID)
  49.   sleep 3
  50.   kill -QUIT $(cat $PID.oldbin)
  51. }
  52. nginx_restart() {
  53.   nginx_stop
  54.   sleep 5
  55.   nginx_start
  56. }
  57. case "$1" in
  58.   check)
  59.     nginx_test_conf
  60.     ;;
  61.   start)
  62.     nginx_start
  63.     ;;
  64.   term)
  65.     nginx_term
  66.     ;;
  67.   stop)
  68.     nginx_stop
  69.     ;;
  70.   reload)
  71.     nginx_reload
  72.     ;;
  73.   restart)
  74.     nginx_restart
  75.     ;;
  76.   upgrade)
  77.     nginx_upgrade
  78.     ;;
  79.   *)
  80.   echo "usage: $0 {check|start|term|stop|reload|restart|upgrade}"
  81. esac
以上是我在自己的测试服务器上面配置的 Nginx Virtual Host,大家可以参考一下。个人习惯把 mysql.server, nginx.server, php-fpm 这些常用的控制工具 copy 到 /usr/local/bin 下以方便操作。要注意的是,在配置文件中必须写明 fastcgi_param  SCRIPT_FILENAME  /path/to/your/scripts/$fastcgi_script_name; 此项,否则在运行的时候会出现 No input file specified. 错误。

到这里你运行 php-fpm start 和 nginx.server start 就可以正常编写 php 服务器端程序了。至于 php error log 的配置你必须在 php.ini 中单独指定存放文件,这个和 php 作为 apache 的 mod_php 模式时也是有区别的,特此提醒,全文完。