linux 下 nginx phpcgi 的安裝及配置

linux 下 nginx phpcgi 的安裝及配置

張映 發表於 2010-04-17

分類目錄: apache/nginx

一,什麼是nginx,它有什麼優點

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不間斷運行,即使運行數個月也不需要重新啓動. 你還能夠 不間斷服務的情況下進行軟件版本的升級.

二,安裝所要軟件

wget http://sysoev.ru/nginx/nginx-0.8.15.tar.gz

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz

wget http://museum.php.net/php5/php-5.2.6.tar.gz

三、安裝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

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

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

nginx fastcgi
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章