Nginx 0.7.x + PHP 5.2.6(FastCGI)搭建高效能web伺服器

weixin_34377065發表於2008-10-23
#更新元件
sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel
#下載程式原始碼包
cd /opt
wget [url]http://sysoev.ru/nginx/nginx-0.7.17.tar.gz[/url]
wget [url]http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror[/url]
wget [url]http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.9.diff.gz[/url]
#wget [url]http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.26-rc.tar.gz/from/http://mirror.x10.com/mirror/mysql/[/url]
wget [url]http://blog.s135.com/soft/linux/mysql/mysql-5.1.26-rc.tar.gz[/url]
wget [url]http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz[/url]
#wget "[url]http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0[/url]"
wget [url]http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz[/url]
#wget "[url]http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.7.tar.gz?modtime=1194463373&big_mirror=0[/url]"
wget [url]http://mirror.optus.net/sourceforge/m/mc/mcrypt/mcrypt-2.6.7.tar.gz[/url]
wget [url]http://pecl.php.net/get/memcache-2.2.3.tgz[/url]
#wget "[url]http://downloads.sourceforge.net/mhash/mhash-0.9.9.tar.gz?modtime=1175740843&big_mirror=0[/url]"
wget [url]http://mirror.optus.net/sourceforge/m/mh/mhash/mhash-0.9.9.tar.gz[/url]
wget [url]ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz[/url]
wget [url]http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2[/url]

#二、安裝PHP 5.2.6(FastCGI模式)
#  1、編譯安裝PHP 5.2.6所需的支援庫:
cd /opt
tar zxvf libiconv-1.12.tar.gz
cd libiconv-1.12/
./configure --prefix=/usr/local
make
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.7.tar.gz
cd mcrypt-2.6.7/
./configure
make
make install
cd ../
#2、編譯安裝MySQL 5.1.26-rc
cd /opt
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
mkdir -p /usr/local/webserver/mysql/
tar zxvf mysql-5.1.26-rc.tar.gz
cd mysql-5.1.26-rc/
./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
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使用者帳號的身份建立資料表:
/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
#②、啟動MySQL(最後的&表示在後臺執行)
/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &
echo "/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &" >>/etc/rc.local
#3、編譯安裝PHP(FastCGI模式)
cd /opt
tar zxvf php-5.2.6.tar.gz
gzip -cd php-5.2.6-fpm-0.5.9.diff.gz | patch -d php-5.2.6 -p1
cd php-5.2.6/
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --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-mcrypt --with-gd --enable-gd-native-ttf --with-openssl
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.3.tgz
cd memcache-2.2.3/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/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/webserver/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
#配置php.ini
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\n#' /usr/local/webserver/php/etc/php.ini
sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/webserver/php/etc/php.ini
#配置eAccelerator加速PHP:
mkdir -p /usr/local/webserver/eaccelerator_cache
配置eAccelerator加速PHP:
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini
#按shift+g鍵跳到配置檔案的最末尾,加上以下配置資訊:
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="128"
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="300"
eaccelerator.shm_prune_period="120"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
#修改sysctl.conf
sed -i 's#kernel.shmmax = 4294967295#kernel.shmmax = 134217728#' /etc/sysctl.conf
/sbin/sysctl -p
#建立www使用者和組,以及虛擬主機目錄
groupadd www -g 48
useradd -u 48 -g www www
mkdir -p /var/htdoc/www
chmod +w /var/htdoc/www
chown -R www:www /var/htdoc/www
mkdir -p /var/htdoc/mail
chmod +w /var/htdoc/mail
chown -R www:www /var/htdoc/mail
#建立php-fpm配置檔案
rm -f /usr/local/webserver/php/etc/php-fpm.conf
cp /opt/php-fpm.conf  /usr/local/webserver/php/etc/
#啟動php-cgi程式
ulimit -SHn 51200
/usr/local/webserver/php/sbin/php-fpm start
#安裝Nginx 0.7.17
#1、安裝Nginx所需的pcre庫:
cd /opt
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install
cd ../
#2、安裝Nginx
tar zxvf nginx-0.7.17.tar.gz
cd nginx-0.7.17/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
#3、建立Nginx日誌目錄
mkdir -p /var/nginx/logs
chmod +w /var/nginx/logs
chown -R www:www /var/nginx/logs
#建立Nginx配置檔案
rm -f /usr/local/webserver/nginx/conf/nginx.conf
cp /opt/nginx.conf  /usr/local/webserver/nginx/conf/
#nginx虛擬主機配置例項
server
 {
   listen       80;
   server_name  mail.1*****1.com;
   index index.html index.htm index.php;
   root  /var/htdoc/56guider;
   #limit_conn   crawler  20;   
                           
   location ~ .*\.(php|php5)?$
   {     
     #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      30d;
   }
   location ~ .*\.(js|css)?$
   {
     expires      1h;
   }   
   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  /var/nginx/logs/56guider/access.log  access;
     }
 server
 {
   listen       80;
   server_name  mail.2*****2.com;
   index index.html index.htm index.php;
   root  /var/htdoc/chembidworld;
   #limit_conn   crawler  20;   
                           
   location ~ .*\.(php|php5)?$
   {     
     #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      30d;
   }
   location ~ .*\.(js|css)?$
   {
     expires      1h;
   }   
   log_format  chemlog  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
   access_log  /var/nginx/logs/chembidworld/access.log  chemlog;
     }
 server
 {
   listen  80;
   server_name  192.168.8.152;
   location /nginx_status {
   stub_status on;
   access_log   off;
   }
 }
cp /opt/fcgi.conf  /usr/local/webserver/nginx/conf/
#配置開機自動啟動Nginx + PHP
echo "ulimit -SHn 51200" >> /etc/rc.local
echo "/usr/local/webserver/php/sbin/php-fpm start" >> /etc/rc.local
echo "/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf" >> /etc/rc.local
#優化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日誌的指令碼
#1、建立指令碼/usr/local/nginx/sbin/cut_nginx_log.sh
vi /usr/local/nginx/sbin/cut_nginx_log.sh
#輸入以下內容:
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`

#2、設定crontab,每天凌晨00:00切割nginx訪問日誌
crontab -e
#輸入以下內容:
00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh
 
以上配置參考了張宴的[url]http://blog.s135.com/read.php/366.htm[/url]的這篇文章,在此表示感謝!!
 
 
 
 
 
 
 

相關文章