此脚本可实现执行一次全自动无从守护搭建lnmp环境,执行完成后,PHP、nginx、MYSQL、以及一些库文件均安装在/usr/local/目录下。如果第一次搭建lnmp环境,最好还是手动装一遍。

  此脚本参考张宴的Nginx 0.7.x + PHP 5.2.8(FastCGI)搭建胜过Apache十倍的Web服务器,地址: http://blog.s135.com/post/366/ ,可以前往了解,对更好配置lnmp环境也有好处。

  把所以需要的软件下载好(可以到官网下载,也可以到张宴博客下载)与shell脚本配置文件放到一个名为lnmp的文件夹内。

  新建一个名为install.sh文件,加上可执行权限,代码如下

 

 
  
  1. #!/bin/bash 
  2. #升级所需要的库文件,编译工具…… 
  3. 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 
  4.  
  5. #安装PHP 5.2.8所需要的库 
  6. tar zxvf libiconv-1.13.tar.gz 
  7. cd ./libiconv-1.13/ 
  8. ./configure --prefix=/usr/local 
  9. make && make install 
  10. cd ../ 
  11.  
  12. tar zxvf libmcrypt-2.5.7.tar.gz 
  13. cd ./libmcrypt-2.5.7/ 
  14. ./configure 
  15. make && make install 
  16. /sbin/ldconfig 
  17. cd libltdl/ 
  18. ./configure --enable-ltdl-install 
  19. make && make install 
  20. cd ../../ 
  21.  
  22. tar zxvf mhash-0.9.9.9.tar.gz 
  23. cd mhash-0.9.9.9/ 
  24. ./configure 
  25. make && make install 
  26. cd ../ 
  27.  
  28. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la 
  29. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so 
  30. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 
  31. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 
  32. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a 
  33. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la 
  34. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so 
  35. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 
  36. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 
  37.  
  38. tar zxvf mcrypt-2.6.8.tar.gz 
  39. cd mcrypt-2.6.8/ 
  40. /sbin/ldconfig 
  41. ./configure 
  42. make && make install 
  43. cd ../ 
  44.  
  45. #安装MySQL 5.1.33 
  46. groupadd mysql 
  47. useradd -g mysql mysql 
  48. tar zxvf mysql-5.0.87.tar.gz 
  49. cd mysql-5.0.87 
  50. ./configure --prefix=/usr/local/mysql --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readine --with-readine --with-readline  --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase 
  51. make && make install 
  52. chmod +w /usr/local/mysql 
  53. chown -R mysql:mysql /usr/local/mysql 
  54. cp support-files/my-medium.cnf /usr/local/mysql/my.cnf 
  55. cd ../ 
  56. /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql 
  57.  
  58. #编译安装PHP(FastCGI模式) 
  59. tar zxvf php-5.2.14.tar.gz 
  60. gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1 
  61. cd php-5.2.14 
  62. ./configure --prefix=/usr/local/php --with-config-file-path=/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-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 --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl 
  63. make ZEND_EXTRA_LIBS='-liconv' 
  64. make install 
  65. cp php.ini-dist /usr/local/php/etc/php.ini 
  66. cd ../ 
  67.  
  68. #安装PHP5扩展模块 
  69. tar zxvf memcache-2.2.5.tgz 
  70. cd memcache-2.2.5/ 
  71. /usr/local/php/bin/phpize 
  72. ./configure --with-php-config=/usr/local/php/bin/php-config 
  73. make && make install 
  74. cd .. 
  75.  
  76. tar jxvf eaccelerator-0.9.5.3.tar.bz2 
  77. cd eaccelerator-0.9.5.3/ 
  78. /usr/local/php/bin/phpize 
  79. ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config 
  80. make && make install 
  81. cd ../ 
  82.  
  83. tar zxvf PDO_MYSQL-1.0.2.tgz 
  84. cd PDO_MYSQL-1.0.2 
  85. /usr/local/php/bin/phpize 
  86. ./configure -with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql 
  87. make && make install 
  88. cd ../ 
  89.  
  90. tar zxvf ImageMagick.tar.gz 
  91. cd ImageMagick-6.6.9-9/ 
  92. ./configure 
  93. make && make install 
  94. cd ../ 
  95.  
  96. tar zxvf imagick-2.3.0.tgz 
  97. cd imagick-2.3.0/ 
  98. /usr/local/php/bin/phpize 
  99. ./configure --with-php-config=/usr/local/php/bin/php-config 
  100. make && make install 
  101. cd ../ 
  102.  
  103. #用sed修改php.ini文件配置 
  104. sed -i 's!extension_dir = "./"!extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\n!' /usr/local/php/etc/php.ini 
  105. sed -i 's!output_buffering = Off!output_buffering = On!' /usr/local/php/etc/php.ini 
  106.  
  107. mkdir -p /usr/local/eaccelerator_cache 
  108. sed -i '$r ./eaccelerator' /usr/local/php/etc/php.ini 
  109.  
  110. sed -i '$a kernel.shmmax = 134217728' /etc/sysctl.conf 
  111. /sbin/sysctl -p 
  112.  
  113. #增加运行nginx的用户用户组 
  114. groupadd www 
  115. useradd -g www www 
  116. mkdir -p /home/www/phpmyadmin 
  117. chmod +w /home/www/phpmyadmin 
  118. chown -R www:www /home/www/phpmyadmin 
  119.  
  120. rm -f /usr/local/php/etc/php-fpm.conf 
  121. cp ./php-fpm.conf //usr/local/php/etc/php-fpm.conf 
  122.  
  123. #安装Nginx 需要的pcre库 
  124. tar zxvf pcre-8.01.tar.gz 
  125. cd  pcre-8.01/ 
  126. ./configure 
  127. make && make install 
  128. cd .. 
  129.  
  130. #安装nginx 
  131. tar zxvf nginx-0.8.50.tar.gz 
  132. cd nginx-0.8.50/ 
  133. ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 
  134. make && make install 
  135. cd .. 
  136.  
  137. #创建nginx日志目录。 
  138. mkdir -p /var/www/nginx_log 
  139. chmod +w /var/www/nginx_log 
  140. chown -R www:www /var/www/nginx_log 
  141.  
  142. rm -f /usr/local/nginx/conf/nginx.conf 
  143. cp ./nginx.conf /usr/local/nginx/conf 
  144. cp ./fcgi.conf /usr/local/nginx/conf/ 
  145.  
  146. #修改内核参数 
  147. sed -i '$r ./sysctl' /etc/sysctl.conf 
  148. sysctl -p 

  以上代码经本人测试可以无错误地在centOS5.5、centOS5.6环境下安装,如果安装到其它环境或者用新版软件,最好在每一步安装一个软件后写上如下代码,以免出错提示,如第6、7行,如果安装正解,系统参数$?应该为0,否则为安装出错。

 
  
  1. tar zxvf memcache-2.2.5.tgz  
  2. cd memcache-2.2.5/  
  3. /usr/local/php/bin/phpize  
  4. ./configure --with-php-config=/usr/local/php/bin/php-config  
  5. make && make install 
  6. if [ $? ne 0 ];then 
  7.   echo "install  memcache error" 
  8. cd ..  

  脚本中所用到的配置文件按顺序如下

1、eaccelerator

 
  
  1. [eaccelerator] 
  2. zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" 
  3. eaccelerator.shm_size="128" 
  4. eaccelerator.cache_dir="/usr/local/eaccelerator_cache" 
  5. eaccelerator.enable="1" 
  6. eaccelerator.optimizer="1" 
  7. eaccelerator.check_mtime="1" 
  8. eaccelerator.debug="0" 
  9. eaccelerator.filter="" 
  10. eaccelerator.shm_max="0" 
  11. eaccelerator.shm_ttl="300" 
  12. eaccelerator.shm_prune_period="120" 
  13. eaccelerator.shm_only="0" 
  14. eaccelerator.compress="1" 
  15. eaccelerator.compress_level="9" 

 

2、php-fpm.conf

 
  
  1. <?xml version="1.0" ?> 
  2. <configuration> 
  3.  
  4.   All relative paths in this config are relative to php's install prefix 
  5.  
  6.   <section name="global_options"> 
  7.  
  8.     Pid file 
  9.     <value name="pid_file">/usr/local/php/logs/php-fpm.pid</value> 
  10.  
  11.     Error log file 
  12.     <value name="error_log">/usr/local/php/logs/php-fpm.log</value> 
  13.  
  14.     Log level 
  15.     <value name="log_level">notice</value> 
  16.  
  17.     When this amount of php processes exited with SIGSEGV or SIGBUS ... 
  18.     <value name="emergency_restart_threshold">10</value> 
  19.  
  20.     ... in a less than this interval of time, a graceful restart will be initiated. 
  21.     Useful to work around accidental curruptions in accelerator's shared memory. 
  22.     <value name="emergency_restart_interval">1m</value> 
  23.  
  24.     Time limit on waiting child's reaction on signals from master 
  25.     <value name="process_control_timeout">5s</value> 
  26.  
  27.     Set to 'no' to debug fpm 
  28.     <value name="daemonize">yes</value> 
  29.  
  30.   </section> 
  31.  
  32.   <workers> 
  33.  
  34.     <section name="pool"> 
  35.  
  36.       Name of pool. Used in logs and stats. 
  37.       <value name="name">default</value> 
  38.  
  39.       Address to accept fastcgi requests on. 
  40.       Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket' 
  41.       <value name="listen_address">127.0.0.1:9000</value> 
  42.  
  43.       <value name="listen_options"> 
  44.  
  45.         Set listen(2) backlog 
  46.         <value name="backlog">-1</value> 
  47.  
  48.         Set permissions for unix socket, if one used. 
  49.         In Linux read/write permissions must be set in order to allow connections from web server. 
  50.         Many BSD-derrived systems allow connections regardless of permissions. 
  51.         <value name="owner"></value> 
  52.         <value name="group"></value> 
  53.         <value name="mode">0666</value> 
  54.       </value> 
  55.  
  56.       Additional php.ini defines, specific to this pool of workers. 
  57.       <value name="php_defines"> 
  58.         <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> 
  59.         <value name="display_errors">1</value> 
  60.       </value> 
  61.  
  62.       Unix user of processes 
  63.         <value name="user">www</value> 
  64.  
  65.       Unix group of processes 
  66.         <value name="group">www</value> 
  67.  
  68.       Process manager settings 
  69.       <value name="pm"> 
  70.  
  71.         Sets style of controling worker process count. 
  72.         Valid values are 'static' and 'apache-like' 
  73.         <value name="style">static</value> 
  74.  
  75.         Sets the limit on the number of simultaneous requests that will be served. 
  76.         Equivalent to Apache MaxClients directive. 
  77.         Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi 
  78.         Used with any pm_style. 
  79.         <value name="max_children">128</value> 
  80.  
  81.         Settings group for 'apache-like' pm style 
  82.         <value name="apache_like"> 
  83.  
  84.           Sets the number of server processes created on startup. 
  85.           Used only when 'apache-like' pm_style is selected 
  86.           <value name="StartServers">20</value> 
  87.  
  88.           Sets the desired minimum number of idle server processes. 
  89.           Used only when 'apache-like' pm_style is selected 
  90.           <value name="MinSpareServers">5</value> 
  91.  
  92.           Sets the desired maximum number of idle server processes. 
  93.           Used only when 'apache-like' pm_style is selected 
  94.           <value name="MaxSpareServers">35</value> 
  95.  
  96.         </value> 
  97.  
  98.       </value> 
  99.  
  100.       The timeout (in seconds) for serving a single request after which the worker process will be terminated 
  101.       Should be used when 'max_execution_time' ini option does not stop script execution for some reason 
  102.       '0s' means 'off' 
  103.       <value name="request_terminate_timeout">0s</value> 
  104.  
  105.       The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file 
  106.       '0s' means 'off' 
  107.       <value name="request_slowlog_timeout">0s</value> 
  108.  
  109.       The log file for slow requests 
  110.       <value name="slowlog">logs/slow.log</value> 
  111.  
  112.       Set open file desc rlimit 
  113.       <value name="rlimit_files">51200</value> 
  114.  
  115.       Set max core size rlimit 
  116.       <value name="rlimit_core">0</value> 
  117.  
  118.       Chroot to this directory at the start, absolute path 
  119.       <value name="chroot"></value> 
  120.  
  121.       Chdir to this directory at the start, absolute path 
  122.       <value name="chdir"></value> 
  123.  
  124.       Redirect workers' stdout and stderr into main error log. 
  125.       If not set, they will be redirected to /dev/null, according to FastCGI specs 
  126.       <value name="catch_workers_output">yes</value> 
  127.  
  128.       How much requests each process should execute before respawn. 
  129.       Useful to work around memory leaks in 3rd party libraries. 
  130.       For endless request processing please specify 0 
  131.       Equivalent to PHP_FCGI_MAX_REQUESTS 
  132.       <value name="max_requests">500</value> 
  133.  
  134.       Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect. 
  135.       Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+) 
  136.       Makes sense only with AF_INET listening socket. 
  137.       <value name="allowed_clients">127.0.0.1</value> 
  138.  
  139.       Pass environment variables like LD_LIBRARY_PATH 
  140.       All $VARIABLEs are taken from current environment 
  141.       <value name="environment"> 
  142.         <value name="HOSTNAME">$HOSTNAME</value> 
  143.         <value name="PATH">/usr/local/bin:/usr/bin:/bin</value> 
  144.         <value name="TMP">/tmp</value> 
  145.         <value name="TMPDIR">/tmp</value> 
  146.         <value name="TEMP">/tmp</value> 
  147.         <value name="OSTYPE">$OSTYPE</value> 
  148.         <value name="MACHTYPE">$MACHTYPE</value> 
  149.         <value name="MALLOC_CHECK_">2</value> 
  150.       </value> 
  151.  
  152.     </section> 
  153.  
  154.   </workers> 
  155.  
  156. </configuration> 

 

3、nginx.conf(nginx配置文件)

 
  
  1. user  www www; 
  2.  
  3. worker_processes 8; 
  4.  
  5. error_log  /var/www/nginx_log/nginx_error.log  crit; 
  6.  
  7. pid        /usr/local/nginx/nginx.pid; 
  8.  
  9. #Specifies the value for maximum file descriptors that can be opened by this process. 
  10. worker_rlimit_nofile 51200; 
  11.  
  12. events 
  13.   use epoll; 
  14.   worker_connections 51200; 
  15.  
  16. http 
  17.   include       mime.types; 
  18.   default_type  application/octet-stream; 
  19.  
  20.   #charset  gb2312; 
  21.  
  22.   server_names_hash_bucket_size 128; 
  23.   client_header_buffer_size 32k; 
  24.   large_client_header_buffers 4 32k; 
  25.   client_max_body_size 8m; 
  26.  
  27.   sendfile on; 
  28.   tcp_nopush     on; 
  29.  
  30.   keepalive_timeout 60; 
  31.  
  32.   tcp_nodelay on; 
  33.  
  34.   fastcgi_connect_timeout 300; 
  35.   fastcgi_send_timeout 300; 
  36.   fastcgi_read_timeout 300; 
  37.   fastcgi_buffer_size 64k; 
  38.   fastcgi_buffers 4 64k; 
  39.   fastcgi_busy_buffers_size 128k; 
  40.   fastcgi_temp_file_write_size 128k; 
  41.  
  42.   gzip on; 
  43.   gzip_min_length  1k; 
  44.   gzip_buffers     4 16k; 
  45.   gzip_http_version 1.0; 
  46.   gzip_comp_level 2; 
  47.   gzip_types       text/plain application/x-javascript text/css application/xml; 
  48.   gzip_vary on; 
  49.  
  50.   #limit_zone  crawler  $binary_remote_addr  10m; 
  51.  
  52. server 
  53.  { 
  54.   listen  80; 
  55.   server_name phpmyadmin.ke.cn; 
  56.   index index.html index.htm index.php; 
  57.   root /home/www/phpmyadmin; 
  58.  
  59.   location ~ .*\.(php|php5)?$ 
  60.   { 
  61.     #fastcgi_pass unix:/tmp/php-cgi.sock; 
  62.     fastcgi_pass 127.0.0.1:9000; 
  63.     fastcgi_index index.php; 
  64.     include fcgi.conf; 
  65.   } 
  66.  
  67.   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  68.   { 
  69.     expires 1h; 
  70.   } 
  71.  
  72.   log_format access '$remote_addr - $remote_user [$time_local]"$request"' 
  73.         '$status $body_bytes_sent "$http_referer"' 
  74.         '$http_user_agent" $http_x_forwarded_for'; 
  75.   access_log /var/www/nginx_log/access.log access; 
  76.  } 

 

4、fcgi.conf (phpFsatCGI配置文件)

 
  
  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; 

 

5、sysctl(修改内核文件)

 
  
  1. net.ipv4.tcp_fin_timeout = 30 
  2. net.ipv4.tcp_keepalive_time = 300 
  3. net.ipv4.tcp_syncookies = 1 
  4. net.ipv4.tcp_tw_reuse = 1 
  5. net.ipv4.tcp_tw_recycle = 1 
  6. net.ipv4.ip_local_port_range = 5000    65000