国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

Nginx0.5.33+PHP5.2.5(FastCGI)搭建勝過Apache10倍的Web服務器

2024-08-30 12:27:45
字體:
來源:轉載
供稿:網友
修正了PHP iconv和gd庫沖突的BUG,增加了PHP mcrypt、memcache擴展,修改了PHP和Nginx編譯參數,優化了Nginx配置文件,添加了部分功能。

Nginx
("engine x") 是一個高性能的 HTTP 和反向代理服務器,也是一個 IMAPPOP3SMTP 代理服務器。 Nginx 是由 Igo Sysoev 為俄羅斯訪問量第二的 ambe. 站點開發的,它已經在該站點運行超過兩年半了。Igo 將源代碼以類BSD許可證的形式發布。
  Nginx 的中文維基:
http:wiki.codemonges.comNginxChs
  在高并發連接的情況下,Nginx是Apache服務器不錯的替代品。Nginx同時也可以作為7層負載均衡服務器來使用。根據我的測試結果,Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 可以承受3萬以上的并發連接數,相當于同等環境下Apache的10倍。
  根據我的經驗,4GB內存的服務器+Apache(pefok模式)一般只能處理3000個并發連接,因為它們將占用3GB以上的內存,還得為系統預留1GB的內存。我曾經就有兩臺Apache服務器,因為在配置文件中設置的MaxCients為4000,當Apache并發連接數達到3800 時,導致服務器內存和Swap空間用滿而崩潰。
  而這臺 Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服務器在3萬并發連接下,開啟的10個Nginx進程消耗150M內存(15M*10=150M),開啟的64個php-cgi進程消耗1280M內存(20M*64=1280M),加上系統自身消耗的內存,總共消耗不到2GB內存。如果服務器內存較小,完全可以只開啟25個php-cgi進程,這樣 php-cgi消耗的總內存數才500M。
  在3萬并發連接下,訪問Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服務器的PHP程序,仍然速度飛快。下圖為Nginx的狀態監控頁面,顯示的活動連接數為28457(關于Nginx的監控頁配置,會在本文接下來所給出的Nginx配置文件中寫明):

Nginx0.5.33+PHP5.2.5(FastCGI)搭建勝過Apache10倍的Web服務器


以下為 Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服務器在3萬并發連接下,開啟的10個Nginx進程和250個php-cgi進程時的系統負載情況:
  

Nginx0.5.33+PHP5.2.5(FastCGI)搭建勝過Apache10倍的Web服務器


  安裝步驟:
 ?。ㄏ到y要求:Linux 2.6+ 內核,本文中的Linux操作系統為CentOS 4.4)
  一、獲取相關開源程序:
  1、下載程序源碼包到當前目錄:
  本文中提到的所有開源軟件為截止到2007年11月25日的最新穩定版。我將它們打了兩個壓縮包。
  第一個壓縮包:nginx-php-1.zip
  下載地址:
?fileid=2746375
  第二個壓縮包:nginx-php-2.zip
  下載地址:
?fileid=2746370
  附:各開源軟件單獨下載地址(如果下載了以上兩個壓縮包,則無需下載以下軟件):
  

(登錄用戶名、密碼均為blog.s135.com)
  2、解壓縮:
mkdir -p /data0/software
cd /data0/software
unzip nginx-php-1.zip
unzip nginx-php-2.zip
cd /data0/software/nginx-php
  二、安裝PHP 5.2.5(FastCGI模式)
  1、編譯安裝PHP 5.2.5所需的支持庫:
tar zxvf libiconv-1.11.tar.gz
cd libiconv-1.11/
./configure --prefix=/usr/local
make
make install
cd ../
tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5/
./configure
make
make install
cd ../
tar zxvf libpng-1.2.20.tar.gz
cd libpng-1.2.20/
./configure
make
make install
cd ../
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure --enable-static --enable-shared
make
make install
make install-lib
cd ../
tar zxvf libxml2-2.6.30.tar.gz
cd libxml2-2.6.30/
./configure
make
install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../
tar zxvf mhash-0.9.9.tar.gz
cd mhash-0.9.9/
./configure
make
make install
cd ../
cp /usr/local/lib/libmcrypt.* /usr/lib
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
tar zxvf mcrypt-2.6.6.tar.gz
cd mcrypt-2.6.6/
./configure
make
make install
cd ../
  2、編譯安裝MySQL 5.0.45
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.0.45.tar.gz
cd mysql-5.0.45
./configure --prefix=/usr/local/webserver/mysql/ --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client
make && make install
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnf
cd ../
  附:以下為附加步驟,如果你想在這臺服務器上運行MySQL數據庫,則執行以下兩步。如果你只是希望讓PHP支持MySQL擴展庫,能夠連接其他服務器上的MySQL數據庫,那么,以下兩步無需執行。
  ①、以mysql用戶帳號的身份建立數據表:
/usr/local/webserver/mysql/bin/mysql_install_db --defaults-file=/usr/local/webserver/mysql/my.cnf --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=mysql --pid-file=/usr/local/webserver/mysql/mysql.pid --skip-locking --port=3306 --socket=/tmp/mysql.sock
 ?、凇覯ySQL(最后的&表示在后臺運行)
/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &
  3、編譯安裝PHP(FastCGI模式)
tar zxvf php-5.2.5.tar.gz
cd php-5.2.5/
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug --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-force-cgi-redirect --enable-mbstring --with-mcrypt
sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile
make
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../
  4、編譯安裝PHP5擴展模塊
tar zxvf memcache-2.2.1.tgz
cd memcache-2.2.1/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
cd php-5.2.5/ext/gd/
/usr/local/webserver/php/bin/phpize
./configure --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../../../
  5、修改php.ini文件
  手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
  修改為extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
  并在此行后增加以下幾行,然后保存:
  extension = "memcache.so"
  extension = "gd.so"
  自動修改:若嫌手工修改麻煩,可執行以下shell命令,自動完成對php.ini文件的修改:
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/nextension = "gd.so"/n#' /usr/local/webserver/php/etc/php.ini
  6、創建www用戶和組,以及其使用的目錄:
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /data0/vshare/htdocs
chmod +w /data0/vshare/htdocs
chown -R www:www /data0/vshare/htdocs
  7、安裝lighttpd中附帶的spawn-fcgi,用來啟動php-cgi
  注:壓縮包中的spawn-fcgi程序為已經編譯成二進制的版本。
cp spawn-fcgi /usr/local/webserver/php/bin
chmod +x /usr/local/webserver/php/bin/spawn-fcgi
  8、啟動php-cgi進程,監聽127.0.0.1的10080端口,進程數為64(如果服務器內存小于3GB,可以只開啟25個進程),用戶為www:
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/webserver/php/bin/php-cgi
  三、安裝Nginx 0.5.33
  1、安裝Nginx所需的pcre庫:
tar zxvf pcre-7.2.tar.gz
cd pcre-7.2/
./configure
make && make install
cd ../
  2、安裝Nginx
tar zxvf nginx-0.5.33.tar.gz
cd nginx-0.5.33/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module
make && make install
cd ../
  3、創建Nginx日志目錄
mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs
  4、創建Nginx配置文件
  ①、在/usr/local/webserver/nginx/conf/目錄中創建nginx.conf文件:
rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf
  輸入以下內容:
引用
user  www www;
worker_processes 10;
error_log  /data1/logs/nginx_error.log  crit;
#pid        logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
      use epoll;
      worker_connections 51200;
}
http
{
      include       conf/mime.types;
      default_type  application/octet-stream;
      charset  gb2312;

      server_names_hash_bucket_size 128;

      #sendfile on;
      #tcp_nopush     on;
      keepalive_timeout 60;
      tcp_nodelay on;
      gzip on;
      gzip_min_length  1k;
      gzip_buffers     4 8k;
      gzip_http_version 1.1;
      gzip_types       text/plain application/x-javascript text/css text/html application/xml;
      server
      {
              listen       80;
              server_name  blog.s135.com;
              index index.html index.htm index.php;
              root  /data0/vshare/htdocs;
              if (-d $request_filename)
              {
                     rewrite ^/(.*)([^/])$
$host/$1$2/
permanent;
              }

              location ~ .*/.php?$
              {
                   include conf/fcgi.conf;      
                   fastcgi_pass  127.0.0.1:10080;
                   fastcgi_index index.php;
              }
              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  /data1/logs/access.log  access;
      }
      server
      {
              listen  80;
              server_name  status.blog.s135.com;
              location / {
                   stub_status on;
                   access_log   off;
              }
      }
}
  ②、在/usr/local/webserver/nginx/conf/目錄中創建fcgi.conf文件:
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;
  5、啟動Nginx
ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
  四、配置開機自動啟動Nginx + PHP
vi /etc/rc.local
  在末尾增加以下內容:
引用
ulimit -SHn 51200
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/webserver/php/bin/php-cgi
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
  五、優化Linux內核參數
vi /etc/sysctl.conf
  在末尾增加以下內容:
引用
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000
  使配置立即生效:
/sbin/sysctl -p
  六、在不停止Nginx服務的情況下平滑變更Nginx配置
  (1)、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,請執行以下命令檢查配置文件是否正確:
/usr/local/webserver/nginx/sbin/nginx -t
  如果屏幕顯示以下兩行信息,說明配置文件正確:
  the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
  the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
  (2)、這時,輸入以下命令查看Nginx主進程號:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
  屏幕顯示的即為Nginx主進程號,例如:
  6302
  這時,執行以下命令即可使修改過的Nginx配置文件生效:
kill -HUP 6302
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柯坪县| 新竹市| 东乡县| 昌宁县| 平泉县| 兴城市| 新和县| 遵义县| 敦煌市| 交城县| 双柏县| 秦皇岛市| 桂东县| 电白县| 桓台县| 开原市| 垣曲县| 安多县| 莆田市| 永靖县| 永德县| 连江县| 郸城县| 屏东市| 高青县| 泌阳县| 安图县| 杨浦区| 永嘉县| 久治县| 淳化县| 通道| 女性| 施甸县| 岢岚县| 平武县| 苗栗市| 云和县| 商南县| 同仁县| 南澳县|