nginx + php(fastcgi)搭建高性能web服务器[v1]

十一月 20th, 2009 at 18:23

本文在CentOS 5.1\5.3平台通过测试;最后修改于2009-11-26

  • 下载源码 (截止到2009年11月20日最新稳定版)
  • 安装 mysql
  • groupadd mysql
    useradd mysql -g mysql -M -s /bin/false
    tar zxvf mysql-5.1.40.tar.gz
    cd mysql-5.1.40
    ./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --without-debug --with-big-tables \ 
    --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=all --with-pthread \
    --enable-static --enable-assembler --enable-local-infile --with-readline --with-ssl \
    --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static
    make
    make install
     
    以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下两步。
    如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
     
    1、以mysql用户帐号的身份建立数据表:
    chown -R mysql.mysql /usr/local/mysql
    cp support-files/my-large.cnf /etc/my.cnf
    scripts/mysql_install_db --user=mysql
     
    2、加入开机自启动;并启动mysql
    cp support-files/mysql.server /etc/rc.d/init.d/mysqld
    chmod 700 /etc/rc.d/init.d/mysqld
    chkconfig --add mysqld
    chkconfig --level 35 mysqld on
    service mysqld start
  • 安装GD库
    • 安装 zlib

      tar zxvf zlib-1.2.3.tar.gz
      cd zlib-1.2.3
      ./configure --prefix=/usr/local/zlib
      make
      make install
      cd ..

      安装 freetype

      tar zxvf freetype-2.3.8.tar.gz
      cd freetype-2.3.8
      ./configure --prefix=/usr/local/freetype
      make
      make install
      cd ..

      安装 libpng

      tar zxvf libpng-1.2.35.tar.gz
      cd libpng-1.2.35
      cp scripts/makefile.linux makefile
      make test
      make install
      cd ..

      安装 jpeg

      tar zxvf jpegsrc.v6b.tar.gz
      cd jpeg-6b
      mkdir /usr/local/jpeg
      mkdir /usr/local/jpeg/bin
      mkdir /usr/local/jpeg/lib
      mkdir /usr/local/jpeg/include
      mkdir /usr/local/jpeg/man
      mkdir /usr/local/jpeg/man/man1
      ./configure --prefix=/usr/local/jpeg --enable-shared --enable-static
      make
      make install
      cd ..
       
      在RHEL5 64 位下编译jpeg-6b时候报以下错误:
      make: ./libtool: Command not found make: *** [jcapimin.lo] Error 127
      错误分析:由于libtool版本过低导致的,进入jpeg-6b的源码目录,复制相关文件覆盖,然后重新configure。
      cp /usr/share/libtool/config.sub .
      cp /usr/share/libtool/config.guess .

      安装 gd

      依赖gettext,同时需要将-fPIC选项加入到Makefile的CFLAG中,大约230行。 
      tar zxvf gd-2.0.35.tar.gz
      cd gd-2.0.35
      ./configure --prefix=/usr/local/gd --with-jpeg=/usr/local/jpeg --with-freetype=/usr/local/freetype \
      --with-png --with-zlib=/usr/local/zlib
      make
      make install
      cd ..

      编译最新的2.0.35版本,make的时候出现以下错误:
      configure.ac:64: error: possibly undefined macro: AM_ICONV
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
      make: *** [configure] Error 1

      解决办法: configure的时候加上--enable-m4_pattern_allow参数;便能顺利编译安装

  • 安装 libiconv
  • tar zxvf libiconv-1.12.tar.gz
    cd libiconv-1.12
    ./configure
    make
    make install
    cd ..
  • 安装 libmcrypt
  • tar zxvf libmcrypt-2.5.8.tar.gz
    cd libmcrypt-2.5.8
    ./configure --disable-posix-threads
    make
    make check
    make install
    cd libltdl
    ldconfig
    ./configure --enable-ltdl-install
    make
    make install
    cd ..
  • 安装 mhash
  • tar zxvf mhash-0.9.9.9.tar.gz
    cd mhash-0.9.9.9
    ./configure
    make
    make install
    cd ..
  • 安装 mcrypt
  • tar zxvf mcrypt-2.6.8.tar.gz
    cd mcrypt-2.6.8
    LD_LIBRARY_PATH=/usr/local/lib ./configure --with-libiconv-prefix
    make
    make install
    cd ..

    configure时候出现如下错误:
    configure: error: "You need at least libmhash 0.8.15 to compile this program http://mhash.sf.net/"
    前面已经安装过mhash,初步分析为预编译的时候没找到lib文件所在的地址;
    所以在./configure前面加上LD_LIBRARY_PATH=/usr/local/lib

  • 安装 libevent
  • tar -xzvf libevent-1.4.9-stable.tar.gz
    cd /usr/local/libevent-1.4.9-stable/
    ./configure --prefix=/usr/local/libevent
    make
    make install
    cd ..
  • 安装 PHP(fastcgi模式)
  • tar zxvf php-5.2.11.tar.gz
    生成 php-fpm补丁
    tar zxvf php-fpm-0.6~5.2.11.tar.gz
    php-fpm-0.6-5.2.11/generate-fpm-patch
     
    安装Sohusin应用补丁
    gzip -d suhosin-patch-5.2.11-0.9.7.patch.gz
    cd php-5.2.11
    patch -p 1 -i ../suhosin-patch-5.2.11-0.9.7.patch
    cd ..
     
    安装 php-fpm补丁
    cd php-5.2.11
    patch -p1 < ../fpm.patch
    cd ..
     
    cd php-5.2.11
    ./buildconf  --force
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc \
    --with-mysql=/usr/local/mysql  --with-mysqli=/usr/local/mysql/bin/mysql_config \
    --with-freetype-dir=/usr/local/freetype--with-jpeg-dir=/usr/local/jpeg --with-png-dir \
    --with-zlib=/usr/local/zlib --with-gd=/usr/local/gd --with-libevent=/usr/local/libevent \
    --enable-safe-mode --enable-sockets --with-gdbm=/usr/lib --with-iconv-dir --enable-mbstring \
    --enable-mbregex --with-curl --with-curlwrappers --enable-bcmath --with-mhash --with-mcrypt \
    --enable-fastcgi --with-fpm --with-pear --enable-force-cgi-redirect --enable-shmop --enable-xml \
    --enable-sysvsem --enable-inline-optimization --enable-pcntl --enable-discard-path --with-libxml-dir \
    --enable-suhosin --enable-zip 
    make
    make install
    cp php.ini-dist /usr/local/php/etc/php.ini
    cd ..

    # PHP-FPM 0.6 for PHP 5.2.11的编译开关从enable改为了with, 要做相应修改,同时要增加--with-libevent开关
    # 加上--enable-suhosin 韩国的PHP 守护神补丁

    安装PHP5扩展模块

      tar zxvf memcache-2.2.5.tgz
      cd memcache-2.2.5/
      /usr/local/php/bin/phpize
      ./configure --with-php-config=/usr/local/php/bin/php-config
      make
      make install
      cd ..
       
      tar jxvf eaccelerator-0.9.5.3.tar.bz2
      cd eaccelerator-0.9.5.3/
      /usr/local/php/bin/phpize
      ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
      make
      make install
      cd ..
       
      tar zxvf PDO_MYSQL-1.0.2.tgz
      cd PDO_MYSQL-1.0.2/
      /usr/local/php/bin/phpize
      ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
      make
      make install
      cd ..
       
      tar zxvf ImageMagick-6.5.7-9.tar.gz
      cd ImageMagick-6.5.7-9/
      ./configure
      make
      make install
      cd ..
       
      tar zxvf imagick-2.3.0.tgz
      cd imagick-2.3.0/
      /usr/local/php/bin/phpize
      ./configure --with-php-config=/usr/local/php/bin/php-config
      make
      make install
      cd ..

      配置 PHP

      vi /usr/local/php/etc/php.ini
      查找 extension_dir = "./"
      修改为 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
      并在此行后增加以下几行,然后保存:
      extension = "memcache.so"
      extension = "pdo_mysql.so"
      extension = "imagick.so"
      extension = "eaccelerator.so"
       
      eaccelerator.shm_size="64"
      eaccelerator.cache_dir="/var/cache/eaccelerator"
      eaccelerator.enable="1"
      eaccelerator.optimizer="1"
      eaccelerator.check_mtime="1"
      eaccelerator.debug="0"
      eaccelerator.filter=""
      eaccelerator.shm_max="0"
      eaccelerator.shm_ttl="3600"
      eaccelerator.shm_prune_period="3600"
      eaccelerator.shm_only="0"
      eaccelerator.compress="1"
      eaccelerator.compress_level="9"
       
      再查找output_buffering = Off
      修改为output_buffering = On
       
      创建eaccelerator缓存目录
      mkdir -p /var/cache/eaccelerator
      chmod 777 /var/cache/eaccelerator

    修改php-fpm.conf
    使用默认配置文件,根据实际情况调整几个参数即可
    max_children
    rlimit_files
    max_requests

    或者使用本站推荐配置文件;点此 查看

  • 安装 Nginx
  • tar jxvf pcre-8.0.tar.gz
    cd pcre-8.0
    ./configure
    make
    make check
    make install
    cd ..
     
    mkdir /var/cache/nginx
    tar zxvf nginx-0.7.51.tar.gz
    cd nginx-0.7.51
    ./configure --prefix=/usr/local/nginx --http-client-body-temp-path=/var/cache/nginx/client_body_temp \
    --with-select_module --with-poll_module --with-http_sub_module --with-http_stub_status_module \
    --with-http_gzip_static_module --with-http_dav_module --with-http_flv_module --with-debug \
    --with-http_ssl_module --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
    make
    make install
    cd ..

    参考文章:张宴的 Nginx 0.8.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器

    相关文章

    Comments are closed.