分享、学习、提高
2010/03/05 09:55
文章作者:Enjoy 转载请注明原文链接。
配置nginx+php很多次了,每次都需要翻阅张宴的多篇博客以及自己的记录,比较麻烦,特意全程记录了下。

vps配置:内存128M,swap 256M,硬盘8G。安装前:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             7.9G  564M  7.0G   8% /

进行一下系统库文件的升级和安装:
yum -y install gcc gcc-c++ autoconf
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel ncurses ncurses-devel
yum -y install make vim-enhanced patch mlocate
ln -s /usr/bin/vim /bin/vi


在根目录创建www目录,下载的软件、mysql的数据、web程序、web日志等都放在www下。
mkdir /www

下载安装文件:
mkdir /www/soft
cd /www/soft
wget http://sysoev.ru/nginx/nginx-0.8.34.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.1.38.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.01.tar.gz
wget http://bart.eaccelerator.net/source/0.9.6/eaccelerator-0.9.6.tar.bz2

安装libiconv:
tar zxf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make
make install
cd ../

安装MYSQL
groupadd mysql
useradd -g mysql mysql
tar zxf mysql-5.1.38.tar.gz
cd mysql-5.1.38/
./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-embedded-server --enable-local-infile
配置可附加参数:--with-plugins=innobase --with-ssl
make
make install
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql

mkdir -p /www/mysql/data
chown -R mysql:mysql /www/mysql

cp support-files/my-medium.cnf /etc/my.cnf
cd ../

建立mysql初始数据表
/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/www/mysql/data --user=mysql
修改下my.cnf
vi /etc/my.cnf
在[mysqld]下面加以下两句
basedir = /usr/local/webserver/mysql
datadir = /www/mysql/data
启动mysql
/usr/local/webserver/mysql/bin/mysqld_safe 2>&1 > /dev/null &

安装PHP
tar zxf 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
cd php-5.2.10/
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-gd --enable-gd-native-ttf --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../
#curl http://pear.php.net/go-pear | /usr/local/webserver/php/bin/php

安装nginx所需要的pcre
tar zxvf pcre-8.01.tar.gz
cd pcre-8.01/
./configure
make ; make install
cd ../


创建www用户
groupadd www
useradd -g www www

创建Nginx日志目录
mkdir /www/logs
chmod +w /www/logs

把/www的所有者设为www用户
chown -R www:www /www/

安装Nginx

tar zxf nginx-0.8.34.tar.gz
cd nginx-0.8.34/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module
make ; make install
cd ../

安装php加速器eaccelerator
tar jxvf eaccelerator-0.9.6.tar.bz2
cd eaccelerator-0.9.6/
/usr/local/webserver/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../

配置eAccelerator:
mkdir /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini
在底部加上以下配置信息
引用
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="1"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
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"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"



修改php配置文件
vi /usr/local/webserver/php/etc/php-fpm.conf
把<value name="user">nobody</value>和<value name="group">nobody</value>去掉注释,并把nobody改为www
如果vps是128M内存,可以让php启用/tmp/php-cgi.sock连接,比较省内存。高于128M内存的话就可以不用改了。
注释掉<value name="listen_address">127.0.0.1:9000</value>
加一句<value name="listen_address">/tmp/php-cgi.sock</value>


防止启动php-cgi进程出错
vi /etc/ld.so.conf
插入一行,内容为:/usr/local/lib
引用
/usr/local/lib
include ld.so.conf.d/*.conf

运行:ldconfig
启动:/usr/local/webserver/php/sbin/php-fpm start


vi /usr/local/webserver/nginx/conf/fcgi.conf
引用
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;


  nginx配置文件
/usr/local/webserver/nginx/conf/nginx.conf
引用
user  www www;

worker_processes 1;

error_log  /www/logs/nginx_error.log  error;

pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 2048;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

  #charset  gb2312;
      
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
      
  sendfile on;
  tcp_nopush     on;

  keepalive_timeout 60;

  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  #limit_zone  crawler  $binary_remote_addr  10m;

  server
  {
    listen       80;
    server_name  www.itlearner.com itlearner.com;
    index index.html index.php;
    root  /www/itlearner.com;

    #limit_conn   crawler  20;    
                            
    location ~ .*\.php?$
    {      
      fastcgi_pass  unix:/tmp/php-cgi.sock;
      #fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      15d;
    }

    location ~ .*\.(js|css)?$
    {
      expires      1d;
    }    

    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /www/logs/access.log  access;
      }

  server
  {
    listen       80;
    server_name  www.9enjoy.com 9enjoy.com;
    index index.php;
    root  /www/9enjoy.com;
    
    access_log  /www/logs/9enjoy.com-access.log access;
    #access_log off;

    location ~ .*\.(php|php5|asp)?$
    {
      fastcgi_pass  unix:/tmp/php-cgi.sock;
      #fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
  }


  server
  {
    listen  80;
    server_name  status.itlearner.com;

    location / {
    stub_status on;
    access_log   off;
    }
  }
}    

看看有没有错误:
/usr/local/webserver/nginx/sbin/nginx -t
平滑重启(nginx0.8.x系列可以这样子了,比之前方便了):
/usr/local/webserver/nginx/sbin/nginx -s reload

时间更改为中国的时间,并同步为标准时间。
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install -y ntp
ntpdate ntp.api.bz

开机自动运行
vi  /etc/rc.local
增加
/usr/local/webserver/mysql/bin/mysqld_safe 2>&1 > /dev/null &
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx

计划任务
vi /etc/crontab
增加
23 3 * * 0 root ntpdate ntp.api.bz

完成!reboot重启一下。
初始状态:128M内存约使用了50M,空闲78M。
硬盘状态:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             7.9G  1.5G  6.1G  20% /

主要参考:
Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小内存VPS服务器上的配置优化[原创]  http://blog.s135.com/post/375/
Nginx 0.7.x + PHP 5.2.8(FastCGI)搭建胜过Apache十倍的Web服务器(第4版)[原创] http://blog.s135.com/post/366/
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]  http://blog.s135.com/nginx_php_v6/
Enjoy:试用nginx记录 http://www.9enjoy.com/post/374/
Enjoy:试用128MVPS,安装nginx+php+mysql及优化 http://www.9enjoy.com/post/424/
发表评论
表情
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写