CENTOS LNMP手動安裝

 

  1. [文章作者:張宴 本文版本:v6.3 最後修改:2010.07.26 轉載請註明原文鏈接:http://blog.s135.com/nginx_php_v6/] 
  2.  安裝步驟: 
  3.   (系統要求:Linux 2.6+ 內核,本文中的Linux操作系統爲CentOS 6.0 )
  4.  
  5.   一、獲取相關開源程序: 
  6.   1、【適用CentOS操作系統】利用CentOS Linux系統自帶的yum命令安裝、升級所需的程序庫(RedHat等其他Linux發行版可從安裝光盤中找到這些程序庫的RPM包,進行安裝): 
  7. [root@bogon soft]# sudo -s 
  8. [root@bogon soft]# LANG=C 
  9. [root@bogon soft]# 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 e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers 
  10.  
  11.   2、【適用CentOS、RedHat及其它Linux操作系統】下載程序源碼包: 
  12.   本文中提到的所有開源軟件爲截止到2011年09月21日的最新穩定版。 
  13.   ①、從軟件的官方網站下載: 
  14. [root@bogon soft]# cd /root/soft 
  15. [root@bogon soft]# wget http://nginx.org/download/nginx-1.0.6.tar.gz
  16. [root@bogon soft]# wget http://cn2.php.net/get/php-5.2.17.tar.bz2/from/cn.php.net/mirror 
  17. [root@bogon soft]# wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz 
  18. [root@bogon soft]# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.16.tar.gz/from/http://gd.tuwien.ac.at/db/mysql/
  19. [root@bogon soft]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
  20. [root@bogon soft]# wget http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2
  21. [root@bogon soft]# wget http://downloads.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
  22. [root@bogon soft]# wget http://pecl.php.net/get/memcache-2.2.6.tgz
  23. [root@bogon soft]# wget http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2
  24. [root@bogon soft]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.bz2
  25. [root@bogon soft]# wget http://downloads.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.zip
  26. [root@bogon soft]# wget ftp://gd.tuwien.ac.at/pub/graphics/ImageMagick/ImageMagick-6.7.2-7.tar.bz2
  27. [root@bogon soft]# wget http://pecl.php.net/get/imagick-3.0.1.tgz  
  28.   
  29.   二、安裝PHP 5.2.14(FastCGI模式) 
  30.   1、編譯安裝PHP 5.2.14所需的支持庫: 
  31. [root@bogon src]# cd libiconv-1.14/
  32. [root@bogon libiconv-1.14]# ./configure --prefix=/usr/local 
  33. [root@bogon libiconv-1.14]# make 
  34. [root@bogon libiconv-1.14]# make install 
  35. [root@bogon libiconv-1.14]# cd ../libmcrypt-2.5.8 
  36.  
  37. [root@bogon libmcrypt-2.5.8]# ./configure 
  38. [root@bogon libmcrypt-2.5.8]# make 
  39. [root@bogon libmcrypt-2.5.8]# make install 
  40. [root@bogon libmcrypt-2.5.8]# /sbin/ldconfig 
  41. [root@bogon libmcrypt-2.5.8]# cd libltdl/ 
  42. [root@bogon libmcrypt-2.5.8]# ./configure --enable-ltdl-install 
  43. [root@bogon libmcrypt-2.5.8]# make 
  44. [root@bogon libmcrypt-2.5.8]# make install 
  45. [root@bogon libmcrypt-2.5.8]# cd ../../mhash-0.9.9.9 
  46.  
  47. [root@bogon mhash-0.9.9.9]# ./configure 
  48. [root@bogon mhash-0.9.9.9]# make 
  49. [root@bogon mhash-0.9.9.9]# make install 
  50. [root@bogon mhash-0.9.9.9]# cd ../mcrypt-2.6.8 
  51.  
  52. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la 
  53. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so 
  54. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 
  55. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 
  56. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a 
  57. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la 
  58. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so 
  59. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 
  60. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 
  61. ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config 
  62.  
  63. [root@bogon mcrypt-2.6.8]# /sbin/ldconfig 
  64. [root@bogon mcrypt-2.6.8]# ./configure 
  65. [root@bogon mcrypt-2.6.8]# make 
  66. [root@bogon mcrypt-2.6.8]# make install 
  67. [root@bogon mcrypt-2.6.8]# cd ../mysql-5.5.16 
  68.  
  69.  
  70.   2、cmake方式安裝MySQL 5.5.3-m3 
  71. [root@bogon mysql-5.5.16]# /usr/sbin/groupadd mysql 
  72. [root@bogon mysql-5.5.16]# /usr/sbin/useradd -g mysql mysql 
  73. [root@bogon mysql-5.5.16]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql\
  74. > -DMYSQL_DATADIR=/var/mysql\
  75. > -DMYSQL_UNIX_ADDR=/var/mysql/mysqld.sock\
  76. > -DDEFAULT_CHARSET=utf8\
  77. > -DWITH_EXTRA_CHARSETS=all\
  78. > -DDEFAULT_COLLATION=utf8_general_ci\
  79. > -DWITH_INNOBASE_STORAGE_ENGINE=1\
  80. > -DWITH_MYISAM_STORAGE_ENGINE=1\
  81. > -DWITH_SPHINX_STORAGE_ENGINE=1\
  82. > -DWITH_INNOBASE_STORAGE_ENGINE=1\
  83. > -DWITH_MEMORY_STORAGE_ENGINE=1\
  84. > -DMYSQL_USER=mysql
  85. [root@bogon mysql-5.5.16]# make
  86. [root@bogon mysql-5.5.16]# make
  87. [root@bogon mysql-5.5.16]# chmod +w /usr/local/mysql/
  88. [root@bogon mysql-5.5.16]# chown -R mysql:mysql /usr/local/mysql
  89. [root@bogon mysql-5.5.16]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
  90. [root@bogon mysql-5.5.16]# mkdir -p /var/mysql/{data,log}
  91. [root@bogon mysql-5.5.16]# chown -R mysql:mysql /var/mysql/
  92. [root@bogon mysql-5.5.16]# cd support-files/
  93. [root@bogon support-files]# cp my-large.cnf /var/mysql/my.cnf
  94. [root@bogon support-files]# cp mysql.server /etc/init.d/mysqld
  95. [root@bogon scripts]# ./mysql_install_db --basedir=/usr/local/mysql/ --datadir=/var/mysql/data/ --defaults-file=/var/mysql/my.cnf --user=mysql
  96.   
  97.  
  98.   3、編譯安裝PHP(FastCGI模式) 
  99. tar zxvf php-5.2.14.tar.gz 
  100. gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1 
  101. cd php-5.2.14/ 
  102. ./configure --prefix=/usr/local/php --with-config-file-scan-dir=/usr/local/php/etc/ --with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/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-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring  --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip  --enable-soap
  103. make ZEND_EXTRA_LIBS='-liconv' 
  104. make install 
  105. cp php.ini-dist /usr/local/webserver/php/etc/php.ini 
  106. cd ../ 
  107.  
  108.  
  109.   4、編譯安裝PHP5擴展模塊 
  110. tar zxvf memcache-2.2.5.tgz 
  111. cd memcache-2.2.5/ 
  112. /usr/local/webserver/php/bin/phpize 
  113. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config 
  114. make 
  115. make install 
  116. cd ../ 
  117.  
  118. tar jxvf eaccelerator-0.9.6.1.tar.bz2 
  119. cd eaccelerator-0.9.6.1/ 
  120. /usr/local/webserver/php/bin/phpize 
  121. ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config 
  122. make 
  123. make install 
  124. cd ../ 
  125.  
  126. tar zxvf PDO_MYSQL-1.0.2.tgz 
  127. cd PDO_MYSQL-1.0.2/ 
  128. /usr/local/webserver/php/bin/phpize 
  129. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql 
  130. make 
  131. make install 
  132. cd ../ 
  133.  
  134. tar zxvf ImageMagick.tar.gz 
  135. cd ImageMagick-6.5.1-2/ 
  136. ./configure 
  137. make 
  138. make install 
  139. cd ../ 
  140.  
  141. tar zxvf imagick-2.3.0.tgz 
  142. cd imagick-2.3.0/ 
  143. /usr/local/webserver/php/bin/phpize 
  144. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config 
  145. make 
  146. make install 
  147. cd ../ 
  148.  
  149.  
  150.   5、修改php.ini文件 
  151.   手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./" 
  152.   修改爲extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/" 
  153.   並在此行後增加以下幾行,然後保存: 
  154.   extension = "memcache.so" 
  155.   extension = "pdo_mysql.so" 
  156.   extension = "imagick.so" 
  157.  
  158.   再查找output_buffering = Off 
  159.   修改爲output_buffering = On 
  160.  
  161.   再查找; cgi.fix_pathinfo=0 
  162.   修改爲cgi.fix_pathinfo=0,防止Nginx文件類型錯誤解析漏洞。 
  163.  
  164.   自動修改:若嫌手工修改麻煩,可執行以下shell命令,自動完成對php.ini文件的修改: 
  165. sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\n#' /usr/local/webserver/php/etc/php.ini 
  166. sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/webserver/php/etc/php.ini 
  167. sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /usr/local/webserver/php/etc/php.ini 
  168. sed -i "s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g" /usr/local/webserver/php/etc/php.ini 
  169.  
  170.  
  171.   6、配置eAccelerator加速PHP: 
  172. mkdir -p /usr/local/webserver/eaccelerator_cache 
  173. vi /usr/local/webserver/php/etc/php.ini 
  174.  
  175.   按shift+g鍵跳到配置文件的最末尾,加上以下配置信息: 
  176. 引用 
  177. [eaccelerator] 
  178. zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" 
  179. eaccelerator.shm_size="64" 
  180. eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache" 
  181. eaccelerator.enable="1" 
  182. eaccelerator.optimizer="1" 
  183. eaccelerator.check_mtime="1" 
  184. eaccelerator.debug="0" 
  185. eaccelerator.filter="" 
  186. eaccelerator.shm_max="0" 
  187. eaccelerator.shm_ttl="3600" 
  188. eaccelerator.shm_prune_period="3600" 
  189. eaccelerator.shm_only="0" 
  190. eaccelerator.compress="1" 
  191. eaccelerator.compress_level="9" 
  192.  
  193.  
  194.  
  195.   7、創建www用戶和組,以及供blog.s135.com和www.s135.com兩個虛擬主機使用的目錄: 
  196. /usr/sbin/groupadd www 
  197. /usr/sbin/useradd -g www www 
  198. mkdir -p /data0/htdocs/blog 
  199. chmod +w /data0/htdocs/blog 
  200. chown -R www:www /data0/htdocs/blog 
  201. mkdir -p /data0/htdocs/www 
  202. chmod +w /data0/htdocs/www 
  203. chown -R www:www /data0/htdocs/www 
  204.  
  205.  
  206.   8、創建php-fpm配置文件(php-fpm是爲PHP打的一個FastCGI管理補丁,可以平滑變更php.ini配置而無需重啓php-cgi): 
  207.   在/usr/local/webserver/php/etc/目錄中創建php-fpm.conf文件: 
  208. rm -f /usr/local/webserver/php/etc/php-fpm.conf 
  209. vi /usr/local/webserver/php/etc/php-fpm.conf 
  210.  
  211.   輸入以下內容(如果您安裝 Nginx + PHP 用於程序調試,請將以下的<value name="display_errors">0</value>改爲<value name="display_errors">1</value>,以便顯示PHP錯誤信息,否則,Nginx 會報狀態爲500的空白錯誤頁): 
  212. <?xml version="1.0" ?> 
  213. <configuration> 
  214.  
  215.   All relative paths in this config are relative to php's install prefix 
  216.  
  217.   <section name="global_options"> 
  218.  
  219.     Pid file 
  220.     <value name="pid_file">/usr/local/webserver/php/logs/php-fpm.pid</value> 
  221.  
  222.     Error log file 
  223.     <value name="error_log">/usr/local/webserver/php/logs/php-fpm.log</value> 
  224.  
  225.     Log level 
  226.     <value name="log_level">notice</value> 
  227.  
  228.     When this amount of php processes exited with SIGSEGV or SIGBUS ... 
  229.     <value name="emergency_restart_threshold">10</value> 
  230.  
  231.     ... in a less than this interval of time, a graceful restart will be initiated. 
  232.     Useful to work around accidental curruptions in accelerator's shared memory. 
  233.     <value name="emergency_restart_interval">1m</value> 
  234.  
  235.     Time limit on waiting child's reaction on signals from master 
  236.     <value name="process_control_timeout">5s</value> 
  237.  
  238.     Set to 'no' to debug fpm 
  239.     <value name="daemonize">yes</value> 
  240.  
  241.   </section> 
  242.  
  243.   <workers> 
  244.  
  245.     <section name="pool"> 
  246.  
  247.       Name of pool. Used in logs and stats. 
  248.       <value name="name">default</value> 
  249.  
  250.       Address to accept fastcgi requests on. 
  251.       Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket' 
  252.       <value name="listen_address">127.0.0.1:9000</value> 
  253.  
  254.       <value name="listen_options"> 
  255.  
  256.         Set listen(2) backlog 
  257.         <value name="backlog">-1</value> 
  258.  
  259.         Set permissions for unix socket, if one used. 
  260.         In Linux read/write permissions must be set in order to allow connections from web server. 
  261.         Many BSD-derrived systems allow connections regardless of permissions. 
  262.         <value name="owner"></value> 
  263.         <value name="group"></value> 
  264.         <value name="mode">0666</value> 
  265.       </value> 
  266.  
  267.       Additional php.ini defines, specific to this pool of workers. 
  268.       <value name="php_defines"> 
  269.         <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> 
  270.         <value name="display_errors">0</value> 
  271.       </value> 
  272.  
  273.       Unix user of processes 
  274.       <value name="user">www</value> 
  275.  
  276.       Unix group of processes 
  277.       <value name="group">www</value> 
  278.  
  279.       Process manager settings 
  280.       <value name="pm"> 
  281.  
  282.         Sets style of controling worker process count. 
  283.         Valid values are 'static' and 'apache-like' 
  284.         <value name="style">static</value> 
  285.  
  286.         Sets the limit on the number of simultaneous requests that will be served. 
  287.         Equivalent to Apache MaxClients directive. 
  288.         Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi 
  289.         Used with any pm_style. 
  290.         <value name="max_children">128</value> 
  291.  
  292.         Settings group for 'apache-like' pm style 
  293.         <value name="apache_like"> 
  294.  
  295.           Sets the number of server processes created on startup. 
  296.           Used only when 'apache-like' pm_style is selected 
  297.           <value name="StartServers">20</value> 
  298.  
  299.           Sets the desired minimum number of idle server processes. 
  300.           Used only when 'apache-like' pm_style is selected 
  301.           <value name="MinSpareServers">5</value> 
  302.  
  303.           Sets the desired maximum number of idle server processes. 
  304.           Used only when 'apache-like' pm_style is selected 
  305.           <value name="MaxSpareServers">35</value> 
  306.  
  307.         </value> 
  308.  
  309.       </value> 
  310.  
  311.       The timeout (in seconds) for serving a single request after which the worker process will be terminated 
  312.       Should be used when 'max_execution_time' ini option does not stop script execution for some reason 
  313.       '0s' means 'off' 
  314.       <value name="request_terminate_timeout">0s</value> 
  315.  
  316.       The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file 
  317.       '0s' means 'off' 
  318.       <value name="request_slowlog_timeout">0s</value> 
  319.  
  320.       The log file for slow requests 
  321.       <value name="slowlog">logs/slow.log</value> 
  322.  
  323.       Set open file desc rlimit 
  324.       <value name="rlimit_files">65535</value> 
  325.  
  326.       Set max core size rlimit 
  327.       <value name="rlimit_core">0</value> 
  328.  
  329.       Chroot to this directory at the start, absolute path 
  330.       <value name="chroot"></value> 
  331.  
  332.       Chdir to this directory at the start, absolute path 
  333.       <value name="chdir"></value> 
  334.  
  335.       Redirect workers' stdout and stderr into main error log. 
  336.       If not set, they will be redirected to /dev/null, according to FastCGI specs 
  337.       <value name="catch_workers_output">yes</value> 
  338.  
  339.       How much requests each process should execute before respawn. 
  340.       Useful to work around memory leaks in 3rd party libraries. 
  341.       For endless request processing please specify 0 
  342.       Equivalent to PHP_FCGI_MAX_REQUESTS 
  343.       <value name="max_requests">1024</value> 
  344.  
  345.       Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect. 
  346.       Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+) 
  347.       Makes sense only with AF_INET listening socket. 
  348.       <value name="allowed_clients">127.0.0.1</value> 
  349.  
  350.       Pass environment variables like LD_LIBRARY_PATH 
  351.       All $VARIABLEs are taken from current environment 
  352.       <value name="environment"> 
  353.         <value name="HOSTNAME">$HOSTNAME</value> 
  354.         <value name="PATH">/usr/local/bin:/usr/bin:/bin</value> 
  355.         <value name="TMP">/tmp</value> 
  356.         <value name="TMPDIR">/tmp</value> 
  357.         <value name="TEMP">/tmp</value> 
  358.         <value name="OSTYPE">$OSTYPE</value> 
  359.         <value name="MACHTYPE">$MACHTYPE</value> 
  360.         <value name="MALLOC_CHECK_">2</value> 
  361.       </value> 
  362.  
  363.     </section> 
  364.  
  365.   </workers> 
  366.  
  367. </configuration> 
  368.   9、啓動php-cgi進程,監聽127.0.0.1的9000端口,進程數爲128(如果服務器內存小於3GB,可以只開啓64個進程),用戶爲www: 
  369. ulimit -SHn 65535 
  370. /usr/local/webserver/php/sbin/php-fpm start 
  371.  
  372.   注:/usr/local/webserver/php/sbin/php-fpm還有其他參數,包括:start|stop|quit|restart|reload|logrotate,修改php.ini後不重啓php-cgi,重新加載配置文件使用reload。 
  373.  
  374.  
  375.   三、安裝Nginx 0.8.46 
  376.   1、安裝Nginx所需的pcre庫: 
  377. tar zxvf pcre-8.10.tar.gz 
  378. cd pcre-8.10/ 
  379. ./configure 
  380. make && make install 
  381. cd ../ 
  382.  
  383.  
  384.   2、安裝Nginx 
  385. tar zxvf nginx-0.8.46.tar.gz 
  386. cd nginx-0.8.46/ 
  387. ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module 
  388. make && make install 
  389. cd ../ 
  390.  
  391.  
  392.   3、創建Nginx日誌目錄 
  393. mkdir -p /data1/logs 
  394. chmod +w /data1/logs 
  395. chown -R www:www /data1/logs 
  396.  
  397.  
  398.   4、創建Nginx配置文件 
  399.   ①、在/usr/local/webserver/nginx/conf/目錄中創建nginx.conf文件: 
  400. rm -f /usr/local/webserver/nginx/conf/nginx.conf 
  401. vi /usr/local/webserver/nginx/conf/nginx.conf 
  402.  
  403.   輸入以下內容: 
  404. 引用 
  405. user  www www; 
  406.  
  407. worker_processes 8; 
  408.  
  409. error_log  /data1/logs/nginx_error.log  crit; 
  410.  
  411. pid        /usr/local/webserver/nginx/nginx.pid; 
  412.  
  413. #Specifies the value for maximum file descriptors that can be opened by this process.  
  414. worker_rlimit_nofile 65535; 
  415.  
  416. events  
  417.   use epoll; 
  418.   worker_connections 65535; 
  419.  
  420. http  
  421.   include       mime.types; 
  422.   default_type  application/octet-stream; 
  423.  
  424.   #charset  gb2312; 
  425.        
  426.   server_names_hash_bucket_size 128; 
  427.   client_header_buffer_size 32k; 
  428.   large_client_header_buffers 4 32k; 
  429.   client_max_body_size 8m; 
  430.        
  431.   sendfile on; 
  432.   tcp_nopush     on; 
  433.  
  434.   keepalive_timeout 60; 
  435.  
  436.   tcp_nodelay on; 
  437.  
  438.   fastcgi_connect_timeout 300; 
  439.   fastcgi_send_timeout 300; 
  440.   fastcgi_read_timeout 300; 
  441.   fastcgi_buffer_size 64k; 
  442.   fastcgi_buffers 4 64k; 
  443.   fastcgi_busy_buffers_size 128k; 
  444.   fastcgi_temp_file_write_size 128k; 
  445.  
  446.   gzip on; 
  447.   gzip_min_length  1k; 
  448.   gzip_buffers     4 16k; 
  449.   gzip_http_version 1.0; 
  450.   gzip_comp_level 2; 
  451.   gzip_types       text/plain application/x-javascript text/css application/xml; 
  452.   gzip_vary on; 
  453.  
  454.   #limit_zone  crawler  $binary_remote_addr  10m; 
  455.  
  456.   server 
  457.   { 
  458.     listen       80; 
  459.     server_name  blog.s135.com; 
  460.     index index.html index.htm index.php; 
  461.     root  /data0/htdocs/blog; 
  462.  
  463.     #limit_conn   crawler  20;     
  464.                               
  465.     location ~ .*\.(php|php5)?$ 
  466.     {       
  467.       #fastcgi_pass  unix:/tmp/php-cgi.sock; 
  468.       fastcgi_pass  127.0.0.1:9000; 
  469.       fastcgi_index index.php; 
  470.       include fcgi.conf; 
  471.     } 
  472.      
  473.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  474.     { 
  475.       expires      30d; 
  476.     } 
  477.  
  478.     location ~ .*\.(js|css)?$ 
  479.     { 
  480.       expires      1h; 
  481.     }     
  482.  
  483.     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' 
  484.               '$status $body_bytes_sent "$http_referer" ' 
  485.               '"$http_user_agent" $http_x_forwarded_for'; 
  486.     access_log  /data1/logs/access.log  access; 
  487.       } 
  488.  
  489.   server 
  490.   { 
  491.     listen       80; 
  492.     server_name  www.s135.com; 
  493.     index index.html index.htm index.php; 
  494.     root  /data0/htdocs/www; 
  495.  
  496.     location ~ .*\.(php|php5)?$ 
  497.     {       
  498.       #fastcgi_pass  unix:/tmp/php-cgi.sock; 
  499.       fastcgi_pass  127.0.0.1:9000; 
  500.       fastcgi_index index.php; 
  501.       include fcgi.conf; 
  502.     } 
  503.  
  504.     log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" ' 
  505.                '$status $body_bytes_sent "$http_referer" ' 
  506.                '"$http_user_agent" $http_x_forwarded_for'; 
  507.     access_log  /data1/logs/wwwlogs.log  wwwlogs; 
  508.   } 
  509.  
  510.   server 
  511.   { 
  512.     listen  80; 
  513.     server_name  status.blog.s135.com; 
  514.  
  515.     location / { 
  516.     stub_status on; 
  517.     access_log   off; 
  518.     } 
  519.   } 
  520.  
  521.  
  522.   ②、在/usr/local/webserver/nginx/conf/目錄中創建fcgi.conf文件: 
  523. vi /usr/local/webserver/nginx/conf/fcgi.conf 
  524.  
  525.   輸入以下內容: 
  526. 引用 
  527. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1; 
  528. fastcgi_param  SERVER_SOFTWARE    nginx; 
  529.  
  530. fastcgi_param  QUERY_STRING       $query_string; 
  531. fastcgi_param  REQUEST_METHOD     $request_method; 
  532. fastcgi_param  CONTENT_TYPE       $content_type; 
  533. fastcgi_param  CONTENT_LENGTH     $content_length; 
  534.  
  535. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name; 
  536. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name; 
  537. fastcgi_param  REQUEST_URI        $request_uri; 
  538. fastcgi_param  DOCUMENT_URI       $document_uri; 
  539. fastcgi_param  DOCUMENT_ROOT      $document_root; 
  540. fastcgi_param  SERVER_PROTOCOL    $server_protocol; 
  541.  
  542. fastcgi_param  REMOTE_ADDR        $remote_addr; 
  543. fastcgi_param  REMOTE_PORT        $remote_port; 
  544. fastcgi_param  SERVER_ADDR        $server_addr; 
  545. fastcgi_param  SERVER_PORT        $server_port; 
  546. fastcgi_param  SERVER_NAME        $server_name; 
  547.  
  548. # PHP only, required if PHP was built with --enable-force-cgi-redirect 
  549. fastcgi_param  REDIRECT_STATUS    200; 
  550.  
  551.  
  552.   5、啓動Nginx 
  553. ulimit -SHn 65535 
  554. /usr/local/webserver/nginx/sbin/nginx 
  555.  
  556.  
  557.   四、配置開機自動啓動Nginx + PHP 
  558. vi /etc/rc.local 
  559.  
  560.   在末尾增加以下內容: 
  561. 引用 
  562. ulimit -SHn 65535 
  563. /usr/local/webserver/php/sbin/php-fpm start 
  564. /usr/local/webserver/nginx/sbin/nginx 
  565.  
  566.  
  567.   五、優化Linux內核參數 
  568. vi /etc/sysctl.conf 
  569.  
  570.   在末尾增加以下內容: 
  571. 引用 
  572. # Add 
  573. net.ipv4.tcp_max_syn_backlog = 65536 
  574. net.core.netdev_max_backlog =  32768 
  575. net.core.somaxconn = 32768 
  576.  
  577. net.core.wmem_default = 8388608 
  578. net.core.rmem_default = 8388608 
  579. net.core.rmem_max = 16777216 
  580. net.core.wmem_max = 16777216 
  581.  
  582. net.ipv4.tcp_timestamps = 0 
  583. net.ipv4.tcp_synack_retries = 2 
  584. net.ipv4.tcp_syn_retries = 2 
  585.  
  586. net.ipv4.tcp_tw_recycle = 1 
  587. #net.ipv4.tcp_tw_len = 1 
  588. net.ipv4.tcp_tw_reuse = 1 
  589.  
  590. net.ipv4.tcp_mem = 94500000 915000000 927000000 
  591. net.ipv4.tcp_max_orphans = 3276800 
  592.  
  593. #net.ipv4.tcp_fin_timeout = 30 
  594. #net.ipv4.tcp_keepalive_time = 120 
  595. net.ipv4.ip_local_port_range = 1024  65535 
  596.  
  597.  
  598.   使配置立即生效: 
  599. /sbin/sysctl -p 
  600.  
  601.  
  602.   六、在不停止Nginx服務的情況下平滑變更Nginx配置 
  603.   1、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件後,請執行以下命令檢查配置文件是否正確: 
  604. /usr/local/webserver/nginx/sbin/nginx -t 
  605.  
  606.   如果屏幕顯示以下兩行信息,說明配置文件正確: 
  607.   the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok 
  608.   the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully 
  609.  
  610.   2、平滑重啓: 
  611.   ①、對於Nginx 0.8.x版本,現在平滑重啓Nginx配置非常簡單,執行以下命令即可: 
  612. /usr/local/webserver/nginx/sbin/nginx -s reload 
  613.  
  614.  
  615.   ②、對於Nginx 0.8.x之前的版本,平滑重啓稍微麻煩一些,按照以下步驟進行即可。輸入以下命令查看Nginx主進程號: 
  616. ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' 
  617.  
  618.   屏幕顯示的即爲Nginx主進程號,例如: 
  619.   6302 
  620.   這時,執行以下命令即可使修改過的Nginx配置文件生效: 
  621. kill -HUP 6302 
  622.  
  623.   或者無需這麼麻煩,找到Nginx的Pid文件: 
  624. kill -HUP `cat /usr/local/webserver/nginx/nginx.pid` 
  625.  
  626.  
  627.   七、編寫每天定時切割Nginx日誌的腳本 
  628.   1、創建腳本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh 
  629. vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh 
  630.  
  631.   輸入以下內容: 
  632. 引用 
  633. #!/bin/bash 
  634. # This script run at 00:00 
  635.  
  636. # The Nginx logs path 
  637. logs_path="/usr/local/webserver/nginx/logs/" 
  638.  
  639. mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ 
  640. mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log 
  641. kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid` 
  642.  
  643.  
  644.   2、設置crontab,每天凌晨00:00切割nginx訪問日誌 
  645. crontab -e 
  646.  
  647.   輸入以下內容: 
  648. 引用 
  649. 00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh 

 

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