当前位置: 主页 > 服务器技术 > Web服务器 > Linux中配置nginx+php(fastcgi)

Linux中配置nginx+php(fastcgi)

时间:2010-7-23来源:互联网 点击:
    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:
在高连接并发的情况下,Nginx是Apache服务器不错的替代品: Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue作为开发模型.
Nginx作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多.
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
Nginx 是一个 安装非常的简单 , 配置文件 非常简洁(还能够支持perl语法), Bugs非常少的服务器: Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够 不间断服务的情况下进行软件版本的升级.
安装PHP 5.2.6(FastCGI模式)

安装php的时候,需要安装支持php的一些库,不过一般情况,装好linux系统时,这些库基本上都装好,所以支持php安装的软件,就在这儿不说了。如果make && make install时提示出错时,你可以看一下缺少什么,你就装什么。

tar zxvf php-5.2.6.tar.gz
cd php-5.2.6/
./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-zlib-dir –with-freetype-dir=/usr –with-jpeg-dir=/usr –with-png-dir=/usr –enable-gd-native-ttf –with-gd –enable-ftp –with-iconv –with-gettext –with-curl –enable-fastcgi –with-openssl

make && make install
cd /usr/local/php/lib
cp php.ini-dist php.ini

cp /usr/local/php/bin/php  /usr/bin/php-cgi

1,修改php.ini

nano php.ini  按f6然后输入extension_dir

查找/usr/local/php/etc/php.ini中的extension_dir = “./”
修改为extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”

2,启动php-cgi

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -f /usr/bin/php-cgi

nginx配置文件中的  fastcgi_pass 127.0.0.1:9000; 就是根据上面来的

3,查看一下

[zhangy@BlackGhost www]$ ps -e|grep php-cgi
3737 ?        00:00:00 php-cgi
3738 ?        00:00:03 php-cgi
3739 ?        00:00:03 php-cgi
3740 ?        00:00:04 php-cgi
3741 ?        00:00:03 php-cgi
3742 ?        00:00:03 php-cgi

安装nginx

tar zxvf nginx-0.8.35.tar.gz
cd nginx-0.8.35/
./configure –user=zhangy –group=users –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-pcre=/usr/local/pcre
make && make install

1,配置nginx.conf

nano /usr/local/nginx/conf/nginx.conf

user  zhangy users;   
  1.   
  2. worker_processes 10;   
  3.   
  4. error_log  /var/vlogs/nginx_error.log  crit;   
  5.   
  6. pid        /var/vlogs/nginx.pid;   
  7.   
  8. #Specifies the value for maximum file descriptors that can be opened by this process.   
  9. worker_rlimit_nofile 65535;   
  10.   
  11. events   
  12. {   
  13.   use epoll;   
  14.   worker_connections 65535;   
  15. }   
  16.   
  17. http   
  18. {   
  19.   include       mime.types;   
  20.   default_type  application/octet-stream;   
  21.   
  22.   #charset  gb2312;   
  23.   
  24.   server_names_hash_bucket_size 128;   
  25.   client_header_buffer_size 32k;   
  26.   large_client_header_buffers 4 32k;   
  27.   client_max_body_size 8m;   
  28.   
  29.   sendfile on;   
  30.   tcp_nopush     on;   
  31.   
  32.   keepalive_timeout 60;   
  33.   
  34.   tcp_nodelay on;   
  35.   
  36.   fastcgi_connect_timeout 300;   
  37.   fastcgi_send_timeout 300;   
  38.   fastcgi_read_timeout 300;   
  39.   fastcgi_buffer_size 64k;   
  40.   fastcgi_buffers 4 64k;   
  41.   fastcgi_busy_buffers_size 128k;   
  42.   fastcgi_temp_file_write_size 128k;   
  43.   
  44.   gzip on;   
  45.   gzip_min_length  1k;   
  46.   gzip_buffers     4 16k;   
  47.   gzip_http_version 1.0;   
  48.   gzip_comp_level 2;   
  49.   gzip_types       text/plain application/x-javascript text/css application/xml;   
  50.   gzip_vary on;   
  51.   
  52.   #limit_zone  crawler  $binary_remote_addr  10m;   
  53.   
  54.     upstream 127.0.0.1:1081  { server 127.0.0.1:1081; }   
  55.     upstream localhost:1080 { server 127.0.0.1:1080; }   
  56.   
  57.   server   
  58.   {   
  59.     listen       10000;   
  60.     server_name  :10000;   
  61.     index index.html index.htm index.php;   
  62.     root  /home/zhangy/www/metbee/trunk/src/web;   
  63.   
  64.     #limit_conn   crawler  20;       
  65.   
  66.     location ~ .*\.(php|php5)?$   
  67.     {         
  68.   
  69.          proxy_pass http://127.0.0.1:1081;   
  70.          break;   
  71.   
  72.       #fastcgi_pass  unix:/tmp/php-cgi.sock;   
  73.       fastcgi_pass  127.0.0.1:9000;   
  74.       fastcgi_index index.php;   
  75.       include fastcgi.conf;   
  76.     }   
  77.   
  78. #     location /main {   
  79. #         proxy_pass http://fast_test;   
  80. #         break;   
  81. #     }   
  82.   
  83.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$   
  84.     {   
  85.       expires      30d;   
  86.     }   
  87.   
  88.     location ~ .*\.(js|css)?$   
  89.     {   
  90.       expires      1h;   
  91.     }       
  92.   
  93.     log_format  access  ‘$remote_addr - $remote_user [$time_local] "$request" ‘  
  94.               ‘$status $body_bytes_sent "$http_referer" ‘  
  95.               ‘"$http_user_agent" $http_x_forwarded_for‘;   
  96.     access_log  /var/log/metbee.log  access;   
  97.       }   
  98.   
  99.   server   
  100.   {   
  101.     listen       80;   
  102.     server_name  :80;   
  103.     index index.html index.htm blog/index.php;   
  104.     root  /home/zhangy/www;   
  105.   
  106.      location /test {   
  107.   
  108.          proxy_pass http://localhost:1080;   
  109.          break;   
  110.      }   
  111.   
  112. #   location ~ .*\.(php|php5|html)?$   
  113. # location ~ .*   
  114. #    {      
  115.   
  116. if (!-e $request_filename){   
  117.     rewrite ^/tag/(.*) /blog/index.php?tag=$1 last;   
  118.     rewrite ^/page/(\d+)$  /blog/index.php?paged=$1 last;   
  119.     rewrite ^/(.*)/(\d+)\.html /blog/index.php?p=$2 last;   
  120.     rewrite ^/category/(.*) /blog/index.php?category_name=$1 last;   
  121. #   rewrite ^/date/([0-9]{4,4})/([0-9]{1,2})?$ /blog/index.php?year=$1&monthnum=$2&page=$3 last;   
  122. #   rewrite ^/date/([0-9]{4,4})/([0-9]{1,2})?$ /blog/index.php?m=$1$2 last;   
  123.     rewrite ^/newpage(\d+)$  /blog/index.php?page_id=$1 last;   
  124.     rewrite ^/feed$  /blog/index.php?feed=rss2 last;   
  125.     rewrite ^/comment/feed$  /blog/index.php?feed=comment-rss2 last;   
  126.     rewrite http://blog.51yip.com$ http://blog.51yip.com/blog/index.php redirect;   
  127. }   
  128.   
  129. location ~ .*\.(php|php5)?$   
  130.    {   
  131.       #fastcgi_pass  unix:/tmp/php-cgi.sock;   
  132.       fastcgi_pass  127.0.0.1:9000;   
  133.       fastcgi_index index.php;   
  134.       include fastcgi.conf;   
  135.     }   
  136.   
  137.     log_format  wwwlogs  ‘$remote_addr - $remote_user [$time_local] "$request" ‘  
  138.                ‘$status $body_bytes_sent "$http_referer" ‘  
  139.                ‘"$http_user_agent" $http_x_forwarded_for‘;   
  140.     access_log  /var/log/wwwlogs.log  wwwlogs;   
  141.   }   
  142.   
  143.   server   
  144.   {   
  145.     listen       10001;   
  146.     server_name  :10001;   
  147.     index index.html index.htm index.php;   
  148.     root  /mnt/song/fastfds2/data;   
  149.   
  150.     location ~ .*\.(php|php5)?$   
  151.     {   
  152.       #fastcgi_pass  unix:/tmp/php-cgi.sock;   
  153.       fastcgi_pass  127.0.0.1:9000;   
  154.       fastcgi_index index.php;   
  155.       include fastcgi.conf;   
  156.     }   
  157.   
  158.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$   
  159.     {   
  160.       expires      30d;   
  161.     }   
  162.   
  163.     log_format  imagelogs  ‘$remote_addr - $remote_user [$time_local] "$request" ‘  
  164.                ‘$status $body_bytes_sent "$http_referer" ‘  
  165.                ‘"$http_user_agent" $http_x_forwarded_for‘;   
  166.     access_log  /var/log/imagelogs.log  imagelogs;   
  167.   }   
  168.   
  169.   server   
  170.   {   
  171.     listen  11211;   
  172.     server_name :11211;   
  173.   
  174.     location / {   
  175.     stub_status on;   
  176.     access_log   off;   
  177.     }   
  178.   }   
  179. }  

2,fastcgi.conf

cd /usr/local/nginx/conf

nano /usr/local/nginx/conf/fastcgi.conf

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

3,优化linux内核参数

nano /etc/sysctl.conf

在末尾加上下面的东东

net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535

4,启动nginx

ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx

5,查看启动

[zhangy@BlackGhost www]$ ps -e|grep nginx
4070 ?        00:00:00 nginx
4071 ?        00:00:00 nginx
4072 ?        00:00:00 nginx
4073 ?        00:00:00 nginx
4074 ?        00:00:00 nginx
4075 ?        00:00:00 nginx
4076 ?        00:00:00 nginx
4077 ?        00:00:00 nginx
4078 ?        00:00:00 nginx
4079 ?        00:00:00 nginx

开机启动

nano /etc/rc.local

ulimit -SHn 65535
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -f /usr/bin/php-cgi
/usr/local/nginx/sbin/nginx

站长资讯网
. TAG: Linux nginx php fastcgi
推荐内容最近更新人气排行
关于我们 | 友情链接 | 网址推荐 | 常用资讯 | 网站地图 | RSS | 留言