`

在生产环境下配置LNMP环境

阅读更多

1.安装前的准备工作

(1)保证ntp时间同步,命令如下:

  1. yum -y install vixie-cron 

 

我们可以每天ntp对时一次,即编辑/etc/crontab文件,添加代码如下:
  1. */1****root /usr/sbin/ntpdate ntp.api.bz >>/dev/null 2>&1 

 

让Centos5.5的crond开机即启动,命令如下:
  1. service crond start| chkconfig crond start 

 

(2)由于在安装前还需要安装软件库,我在内网配置了一台yum服务器(过程略),嫌麻烦的朋友可直接采用Centos5.5的源来安装以下软件库,命令如下:
  1. 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 

(3)下载LNMP环境所需要的源码包,如下所示:

  1. mkdir-p/usr/local/src 

 

cd/usr/local/src下载架构环境所需的源码包,其文件列表清单list.txt文件如下:
  1. http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz  
  2. http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz  
  3. http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz  
  4. http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz  
  5. http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz  
  6. http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz  
  7. http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz  
  8. http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz  
  9. http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz  
  10. http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz  
  11. http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2  
  12. http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz  
  13. http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz  
  14. http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz 

 

开始下载,可以用如下命令来实现:
  1. wget -i /usr/local/src/list.txt 

2.安装php-5.2.14所需要的支持库

以下过程我们可以写一个SHELL脚本,让它全自动完成,免得一个个地手动输入命令,SHELL脚本如下:

 

  1. #!/bin/bash  
  2. tar zxvf libiconv-1.13.1.tar.gz  
  3. cd libiconv-1.13.1/  
  4. ./configure--prefix=/usr/local  
  5. make  
  6. make install  
  7. cd ../  
  8.  
  9. tar zxvf libmcrypt-2.5.8.tar.gz  
  10. cd libmcrypt-2.5.8/  
  11. ./configure  
  12. make  
  13. make install  
  14. /sbin/ldconfig  
  15. cd libltdl/  
  16. ./configure--enable-ltdl-install  
  17. make  
  18. make install  
  19. cd ../../  
  20.  
  21. tar zxvf mhash-0.9.9.9.tar.gz  
  22. cd mhash-0.9.9.9/  
  23. ./configure  
  24. make  
  25. 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. ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config  
  38.  
  39. tar zxvf mcrypt-2.6.8.tar.gz  
  40. cd mcrypt-2.6.8/  
  41. /sbin/ldconfig  
  42. ./configure  
  43. make  
  44. make install  
  45. cd ../ 

 



3.源码编译安装MySQL5.5.3-m3

(1)建立运行MySQL数据库的用户组及用户mysql,完成下面的安装过程,命令如下:

  1. groupadd mysql  
  2. useradd-g mysql mysql  
  3. tar zxvf mysql-5.5.3-m3.tar.gz  
  4. cd mysql-5.5.3-m3/ 

 

MySQL5.5.3-m3的编译参数如下:
  1. ./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--with-
    plugins
    =partition,innobase,myisammrg  
  2. make && make install 

给mysql运行所在的目录以执行权限,将其所有权给予mysql:mysql,命令如下:

  1. chmod +w/usr/local/webserver/mysql  
  2. chown-R mysql:mysql/usr/local/webserver/mysql  
  3. cd ../ 

 

(2)创建MySQL数据库存放的目录,这里建立reallog目录用于存放mysql-relay-bin.xxx文件,它可以存放slave端的I/O线程从master端所读取的binary log信息,然后由slave端的SQL线程从该relay log中读取并解析相应的日志信息,转化成master所执行的query语句,接着在slave端应用,/data/mysql/3306/data用于存放MySQL数据,/data/mysql/3306/binlog用于此MySQL运行时所产生的二进制文件,如下所示:
  1. mkdir -p /data/mysql/3306/data/  
  2. mkdir -p /data/mysql/3306/binlog/  
  3. mkdir -p /data/mysql/3306/relaylog/  
  4. chown -R mysql:mysql/data/mysql/ 

 

(3)以mysql用户账号的身份建立数据表,命令如下:
  1. /usr/local/webserver/mysql/bin/mysql_install_db  
  2. --basedir=/usr/local/webserver/mysql  
  3. --datadir=/data/mysql/3306/data--user=mysql 

 

成功后应该有如下显示:
  1. /usr/local/webserver/mysql/bin/mysqladmin-u root password 'new-password'  
  2. /usr/local/webserver/mysql/bin/mysqladmin-u root-h server.
    cn7788.com password 'new-password'  
  3. Alternatively you can run:  
  4. /usr/local/webserver/mysql/bin/mysql_secure_installation  
  5. which will also give you the option of removing the test  
  6. databases and anonymous user created by default. This is  
  7. strongly recommended for production servers.  
  8. See the manual for more instructions.  
  9. You can start the MySQL daemon with:  
  10. cd/usr/local/webserver/mysql ;/usr/local/webserver/mysql/bin/mysqld_safe &  
  11. You can test the MySQL daemon with mysql-test-run.pl  
  12. cd/usr/local/webserver/mysql/mysql-test ; perl mysql-test-run.pl  
  13. Please report any problems with the/usr/local/
    webserver/mysql/scripts/mysqlbug script! 

(4)创建my.cnf配置文件,我将其放在/data/mysql/3306下,即/data/mysql/3306文件。文件内容如下所示:

  1. [client]  
  2. character-set-server = utf8 
  3. port = 3306 
  4. socket = /tmp/mysql.sock  
  5.  
  6. [mysqld]  
  7. character-set-server = utf8 
  8. replicate-ignore-db = mysql 
  9. replicate-ignore-db = test 
  10. replicate-ignore-db = information_schema 
  11. user = mysql 
  12. port = 3306 
  13. socket =/tmp/mysql.sock  
  14. basedir =/usr/local/webserver/mysql  
  15. datadir =/data/mysql/3306/data  
  16. log-error =/data/mysql/3306/mysql_error.log  
  17. pid-file =/data/mysql/3306/mysql.pid  
  18. open_files_limit= 10240  
  19. back_log = 600 
  20. max_connections = 5000 
  21. max_connect_errors = 6000 
  22. table_cache = 614 
  23. external-locking = FALSE 
  24. max_allowed_packet = 32M 
  25. sort_buffer_size = 1M 
  26. join_buffer_size = 1M 
  27. thread_cache_size = 300 
  28. #thread_concurrency = 8 
  29. query_cache_size = 512M 
  30. query_cache_limit = 2M 
  31. query_cache_min_res_unit = 2k 
  32. default-storage-engine = MyISAM 
  33. thread_stack = 192K 
  34. transaction_isolation = READ-COMMITTED  
  35. tmp_table_size = 246M 
  36. max_heap_table_size = 246M 
  37. long_query_time = 3 
  38. log-slave-updates  
  39. log-bin =/data/mysql/3306/binlog/binlog  
  40. binlog_cache_size = 4M 
  41. binlog_format = MIXED 
  42. max_binlog_cache_size = 8M 
  43. max_binlog_size = 1G 
  44. relay-log-index =/data/mysql/3306/relaylog/relaylog  
  45. relay-log-info-file =/data/mysql/3306/relaylog/relaylog  
  46. relay-log =/data/mysql/3306/relaylog/relaylog  
  47. expire_logs_days = 30 
  48. key_buffer_size = 256M 
  49. read_buffer_size = 1M 
  50. read_rnd_buffer_size = 16M 
  51. bulk_insert_buffer_size = 64M 
  52. myisam_sort_buffer_size = 128M 
  53. myisam_max_sort_file_size = 10G 
  54. myisam_repair_threads = 1 
  55. myisam_recover  
  56.  
  57. interactive_timeout = 120 
  58. wait_timeout = 120 
  59.  
  60. skip-name-resolve  
  61. slave-skip-errors = 1032,1062,126,1114,1146,1048,1396  
  62.  
  63. server-id = 1 
  64.  
  65. innodb_additional_mem_pool_size = 16M 
  66. innodb_buffer_pool_size = 512M 
  67. innodb_data_file_path = ibdata1:256M:autoextend  
  68. innodb_file_io_threads = 4 
  69. innodb_thread_concurrency = 8 
  70. innodb_flush_log_at_trx_commit = 2 
  71. innodb_log_buffer_size = 16M 
  72. innodb_log_file_size = 128M 
  73. innodb_log_files_in_group = 3 
  74. innodb_max_dirty_pages_pct = 90 
  75. innodb_lock_wait_timeout = 120 
  76. innodb_file_per_table = 0 
  77.  
  78. log-slow-queries =/data/mysql/3306/slow.log  
  79. long_query_time = 10 
  80.  
  81. [mysqldump]  
  82. quick  
  83. max_allowed_packet = 32M 

(5)用命令启动MySQL数据库,无论是内部开发环境还是生产环境,我们的MySQL数据库启动后就不会关闭了,没有特殊情况也不会重启。所以也可以将以下启动MySQL的命令置于/etc/rc.local文件内,命令如下:

  1. /usr/local/webserver/mysql/bin/mysqld_safe  
  2. --defaults-file=/data/mysql/3306/my.cnf 

 

 


这样我们只要重启服务器,就可以启动MySQL数据库了。我们还可以用命令验证,命令如下:

  1. lsof-i:3306  
  2. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME  
  3. mysqld 18286 mysql 14u IPv6 92763 TCP*:mysql (LISTEN) 

4.编译安装php-5.2.14

我们进入/usr/local/src目录后,就可以编译安装php-5.2.14,如下所示:

  1. tar zxvf php-5.2.14.tar.gz  
  2. gzip-cd php-5.2.14-fpm-0.5.14.diff.gz| patch-d php-5.2.14-p1 

 

不打这个补丁的话,会产生无sbin目录的错误。cd php-5.2.14/php-5.2.14的编译参数如下:
  1. ./configure--prefix=/usr/local/webserver/php  
  2. --with-config-file-path=/usr/local/webserver/php/etc  
  3. --with-mysql=/usr/local/webserver/mysql  
  4. --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config  
  5. --with-iconv-dir=/usr/local--with-freetype-dir  
  6. --with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml  
  7. --disable-rpath--enable-discard-path--enable-safe-mode  
  8. --enable-bcmath--enable-shmop--enable-sysvsem  
  9. --enable-inline-optimization--with-curl--with-curlwrappers  
  10. --enable-mbregex--enable-fastcgi--enable-fpm  
  11. --enable-force-cgi-redirect--enable-mbstring--with-mcrypt  
  12. --with-gd--enable-gd-native-ttf--with-openssl--with-mhash  
  13. --enable-pcntl--enable-sockets--with-ldap--with-ldap-sasl  
  14. --with-xmlrpc--enable-zip--enable-soap  
  15. make ZEND_EXTRA_LIBS='-liconv' 

 

为免得发生liconv报错,所以带上ZEND参数,这个问题要是经常编译PHP的朋友非常熟悉的。make install源码编译安装完成后,我们为php创建php.ini文件,如下所示:
  1. cp php.ini-dist /usr/local/webserver/php/etc/php.ini  
  2. cd ../ 

5.安装php5扩展模块

以下过程也可以写成脚本形式,免得手动一个个输入,内容如下:

  1. #!/bin/bash  
  2. tar zxvf memcache-2.2.5.tgz  
  3. cd memcache-2.2.5/  
  4. /usr/local/webserver/php/bin/phpize  
  5. ./configure--with-php-config=/usr/local/webserver/php/bin/php-config  
  6. make  
  7. make install  
  8. cd ../  
  9.  
  10. tar jxvf eaccelerator-0.9.6.1.tar.bz2  
  11. cd eaccelerator-0.9.6.1/  
  12. /usr/local/webserver/php/bin/phpize  
  13. ./configure --enable-eaccelerator=shared--with-php
    -config
    =/usr/local/webserver/php/bin/php-config  
  14. make  
  15. make install  
  16. cd ../  
  17.  
  18. tar zxvf PDO_MYSQL-1.0.2.tgz  
  19. cd PDO_MYSQL-1.0.2/  
  20. /usr/local/webserver/php/bin/phpize  
  21. ./configure--with-php-config=/usr/local/webserver/php/
    bin/
    php-config--with-pdo-mysql=/usr/local/webserver/mysql  
  22. make  
  23. make install  
  24. cd ../  
  25.  
  26. tar zxvf ImageMagick.tar.gz  
  27. cd ImageMagick-6.5.1-2/  
  28. ./configure  
  29. make  
  30. make install  
  31. cd ../  
  32.  
  33. tar zxvf imagick-2.3.0.tgz  
  34. cd imagick-2.3.0/  
  35. /usr/local/webserver/php/bin/phpize  
  36. ./configure--with-php-config=/usr/local/webserver/php/bin/php-config  
  37. make  
  38. make install  
  39. cd ../ 

修改php.ini文件,加载动态模块,让PHP5.2.14能顺利启动。

手动修改,查找:

  1. /usr/local/webserver/php/etc/php.ini中的extension_dir = "./" 

 

将其修改如下:
  1. extension_dir =  
  2.  "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/" 

 

 

并在此行后增加以下几行内容,然后保存:

  1. extension = "memcache.so" 
  2. extension = "imagick.so" 

 

再查找以下内容:
  1. output_buffering = Off 

 

然后将其修改如下:
  1. output_buffering = On 

 

再查找以下内容:
  1. cgi.fix_pathinfo=0 

然后将其修改如下:

cgi.fix_pathinfo=0此处代码的作用是防止Nginx文件类型错误解析漏洞。

配置eAccelerator加速PHP,命令如下:

  1. mkdir-p/usr/local/webserver/eaccelerator_cache  
  2. vim /usr/local/webserver/php/etc/php.ini 

 

在文件末尾加上以下配置信息:
  1. [eaccelerator]  
  2. zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" 
  3. eaccelerator.shm_size="64" 
  4. eaccelerator.cache_dir="/usr/local/webserver/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="3600" 
  12. eaccelerator.shm_prune_period="3600" 
  13. eaccelerator.shm_only="0" 
  14. eaccelerator.compress="1" 

6.安装Nginx

关于Nginx的虚拟用户是一个重难点,不止一个朋友与我交流这个问题。其实其配置方法跟Apache一样,下面以我线上服务器的配置文件向大家演示一下,大家自然就明白了。

(1)创建www用户和www用户组,也为后面的Nginx安装做准备。/data/logs是Nginx相关日志文件存放的目录,如下所示:

  1. /usr/sbin/groupadd www  
  2. /usr/sbin/useradd-g www www  
  3. mkdir-p/data/htdocs/www 

 

创建根路径,如下所示:
  1. mkdir-p /data/logs 

 

创建Nginx日志路径。分别为我的Nginx创建虚拟用户目录,如下所示:
  1. cd /data/htdocs/www  
  2. mkdir adongweb baobei ImageVue very365 

 

分别将其目录权限给予www:www,命令如下:
  1. chown-R www:www/data/logs  
  2. chown-R www:www/data/htdocs/www 

 

(2)创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi)。在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件,命令如下:
  1. rm-f/usr/local/webserver/php/etc/php-fpm.conf 

 


我们用rm命令删除原有的php-fpm.conf文件后,创建一个新的/usr/local/webserver/php/etc/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/webserver/php/logs/php-fpm.pid</value> 
  10.  
  11. Error log file  
  12.  <value name="error_log">/usr/local/webserver/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. InLinux 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">0</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.  
  79. Used with any pm_style.  
  80. <value name="max_children">300</value> 
  81.  
  82. Settings group for 'apache-like' pm style  
  83. <value name="apache_like"> 
  84.  
  85.  Sets the number of server processes created on startup.  
  86.  Used only when 'apache-like' pm_style is selected  
  87.  <value name="StartServers">20</value> 
  88.  
  89.  Sets the desired minimum number of idle server processes.  
  90.  Used only when 'apache-like' pm_style is selected  
  91.  <value name="MinSpareServers">5</value> 
  92.  
  93.  Sets the desired maximum number of idle server processes.  
  94.  Used only when 'apache-like' pm_style is selected  
  95.  <value name="MaxSpareServers">35</value> 
  96.  
  97. </value> 
  98.  
  99.  </value> 
  100.  
  101.  The timeout (in seconds)for serving a single request after
    which the worker process will be terminated  
  102.  Should be used when 'max_execution_time' ini option does 
    not stop script execution for some reason  
  103.  '0s' means 'off'  
  104.  <value name="request_terminate_timeout">0s</value> 
  105.  
  106.  The timeout (in seconds)for serving of single request 
    after which a php backtrace will be dumped to slow.log file  
  107.  '0s' means 'off'  
  108.  <value name="request_slowlog_timeout">0s</value> 
  109.  
  110.  The log file for slow requests  
  111.  <value name="slowlog">logs/slow.log</value> 
  112.  
  113.  Set open file desc rlimit  
  114.  <value name="rlimit_files">65535</value> 
  115.  
  116.  Set max core size rlimit  
  117.  <value name="rlimit_core">0</value> 
  118.  
  119.  Chroot to this directory at the start, absolute path  
  120.  <value name="chroot"></value> 
  121.  
  122.  Chdir to this directory at the start, absolute path  
  123.  <value name="chdir"></value> 
  124.  
  125.  Redirect workers' stdout and stderr into main error log.  
  126.  If not set, they will be redirected to/dev/null, according to FastCGI specs  
  127.  <value name="catch_workers_output">yes</value> 
  128.  
  129.  How much requests each process should execute before respawn.  
  130.  Useful to work around memory leaks in 3rd party libraries.  
  131.  For endless request processing please specify 0  
  132.  Equivalent to PHP_FCGI_MAX_REQUESTS  
  133.  <value name="max_requests">1024</value> 
  134.  
  135.  Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.  
  136.  Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)  
  137.  Makes sense only with AF_INET listening socket.  
  138.  <value name="allowed_clients">127..0.0.1</value> 
  139.  
  140.  Pass environment variables like LD_LIBRARY_PATH  
  141.  All $VARIABLEs are taken from current environment  
  142.  <value name="environment"> 
  143. <value name="HOSTNAME">$HOSTNAME</value> 
  144. <value name="PATH">/usr/local/bin:/usr/bin:/bin</value> 
  145. <value name="TMP">/tmp</value> 
  146. <value name="TMPDIR">/tmp</value> 
  147. <value name="TEMP">/tmp</value> 
  148. <value name="OSTYPE">$OSTYPE</value> 
  149. <value name="MACHTYPE">$MACHTYPE</value> 
  150. <value name="MALLOC_CHECK_">2</value> 
  151.  </value> 
  152.  
  153. </section> 
  154.  
  155.  </workers> 
  156.  
  157. </configuration> 

 

 

(3)启动php-cgi进程,监听127.0.0.1的9000端口,进程数为300(生产环境下的服务器为300~500个均可),用户为www,如下所示:

  1. ulimit-SHn 65535  
  2. /usr/local/webserver/php/sbin/php-fpm start 

注意 usr/local/webserver/php/sbin/php-fpm还有其他参数,包括:

startstopquitrestartreloadlogrotate。修改php.ini后不重启php-cgi,重新加载配置文件使用reload。

(4)安装并配置Nginx0.8.46。

首先安装PCRE,让Nginx0.8.46能支持正则表达式,命令如下:

  1. tar zxvf pcre-8.46.tar.gz  
  2. cd pcre-8.10/  
  3. ./configure  
  4. make && make install  
  5. cd ../ 

 

再接着安装Nginx0.8.46,命令如下:
  1. tar zxvf nginx-0.8.46.tar.gz  
  2. cd nginx-0.8.46/  
  3. ./configure --user=www --group=www 
  4. --prefix=/usr/local/webserver/nginx --with-http_stub_status_module  
  5. --with-http_ssl_module  
  6. make && make install  
  7. cd ../ 

 

(5)配置Nginx的conf文件,我们可以用Vim修改/usr/local/webserver/nginx/conf/nginx.conf文件,内容如下:
  1. user www www;  
  2. worker_processes 8;  
  3. error_log /data/logs/nginx_error.log crit;  
  4. pid/usr/local/webserver/nginx/nginx.pid;  
  5. #Specifies the value for maximum file descriptors that can be opened by this process.  
  6. worker_rlimit_nofile 65535;  
  7.  
  8. events  
  9. {  
  10.  use epoll;  
  11.  worker_connections 65535;  
  12. }  
  13.  
  14. http  
  15. {  
  16.  include mime.types;  
  17.  default_type application/octet-stream;  
  18.  
  19.  #charset gb2312;  
  20.  server_names_hash_bucket_size 128;  
  21.  client_header_buffer_size 32k;  
  22.  large_client_header_buffers 4 32k;  
  23.  client_max_body_size 8m;  
  24.  sendfile on;  
  25.  tcp_nopushon;  
  26.  
  27.  keepalive_timeout 60;  
  28.  
  29.  tcp_nodelay on;  
  30.  
  31.  fastcgi_connect_timeout 300;  
  32.  fastcgi_send_timeout 300;  
  33.  fastcgi_read_timeout 300;  
  34.  fastcgi_buffer_size 64k;  
  35.  fastcgi_buffers 4 64k;  
  36.  fastcgi_busy_buffers_size 128k;  
  37.  fastcgi_temp_file_write_size 128k;  
  38.  
  39.  gzip on;  
  40.  gzip_min_length 1k;  
  41.  gzip_buffers4 16k;  
  42.  gzip_http_version 1.0;  
  43.  gzip_comp_level 2;  
  44.  gzip_types text/plain application/x-javascript text/css application/xml;  
  45.  gzip_vary on;  
  46.  
  47.  #limit_zone crawler $binary_remote_addr 10m;  
  48.  
  49.  server  
  50.  {  
  51. listen 80 default;  
  52. server_name _;  
  53. index index.html index.htm index.php;  
  54. root /data/htdocs/www;  
  55. #server_name_in_redirect off;  
  56.  
  57.  location~.*\.(php| php5)?$  
  58. {  
  59.  #fastcgi_pass unix:/tmp/php-cgi.sock;  
  60.  fastcgi_pass 127.0.0.1:9000;  
  61.  fastcgi_index index.php;  
  62.  include fcgi.conf;  
  63. }  
  64.  
  65. location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  66. {  
  67.  expires 30d;  
  68. }  
  69.  
  70. location~.*\.(js| css)?$  
  71. {  
  72.  expires 1h;  
  73. }  
  74.  
  75. }  
  76.  
  77.  server  
  78.  {  
  79. listen 80;  
  80. server_name www.adongstudio.com;  
  81. index index.html index.htm index.php;  
  82. root /data/htdocs/www/adongweb;  
  83.  
  84. #limit_conn crawler 20;  
  85.  
  86.  location~.*\.(php| php5)?$  
  87.  {  
  88. #fastcgi_pass unix:/tmp/php-cgi.sock;  
  89. fastcgi_pass 127.0.0.1:9000;  
  90. fastcgi_index index.php;  
  91. include fcgi.conf;  
  92.  }  
  93.  
  94.  location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  95.  {  
  96. expires 30d;  
  97.  }  
  98.  
  99.  location~.*\.(js| css)?$  
  100.  {  
  101. expires 1h;  
  102.  }  
  103.  
  104.  log_format access '$remote_addr-$remote_user[$time_local] "$request" '  
  105.  '$status $body_bytes_sent "$http_referer" '  
  106.  '"$http_user_agent" $http_x_forwarded_for';  
  107.  access_log /data/logs/access.log access;  
  108. }  
  109.  server  
  110.  {  
  111. listen 80;  
  112. server_name www.longfeistudio.com;  
  113. index index.html index.htm index.php;  
  114. root /data/htdocs/www/ImageVue;  
  115.  
  116. #limit_conn crawler 20;  
  117.  
  118. location~.*\.(php| php5)?$  
  119. {  
  120.  #fastcgi_pass unix:/tmp/php-cgi.sock;  
  121.  fastcgi_pass 127.0.0.1:9000;  
  122.  fastcgi_index index.php;  
  123.  include fcgi.conf;  
  124. }  
  125.  
  126. location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  127. {  
  128.  expires 30d;  
  129. }  
  130.  
  131. location~.*\.(js| css)?$  
  132. {  
  133.  expires 1h;  
  134. }  
  135.  
  136. access_log off;  
  137.  
  138. }  
  139. server  
  140.  {  
  141. listen 80;  
  142. server_name www.hongyanbike.com;  
  143. index index.html index.htm index.php;  
  144. root /data/htdocs/www/xhui/hybike;  
  145.  
  146. location~.*\.(php| php5)?$  
  147. {  
  148.  #fastcgi_pass unix:/tmp/php-cgi.sock;  
  149.  fastcgi_pass 127.0.0.1:9000;  
  150.  fastcgi_index index.php;  
  151.  include fcgi.conf;  
  152. }  
  153.  
  154. location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  155. {  
  156.  expires 30d;  
  157. }  
  158.  
  159. location~.*\.(js| css)?$  
  160. {  
  161.  expires 1h;  
  162. }  
  163.  
  164. access_log off;  
  165.  }  
  166. server  
  167.  {  
  168. listen 80;  
  169. server_name www.very365.com mm.very365.com very365.com;  
  170. index index.html index.htm index.php;  
  171. root /data/htdocs/www/very365;  
  172.  location/  
  173.  {  
  174.  rewrite ^/(.*)/product/([0-9]+)/$/seoproduct\.php\?spell=$1&productid=$2;  
  175.  rewrite ^/brand/(.*)/page/([0-9]+)/$/seobrand\.php\?spell=$1&page=$2;  
  176.  rewrite ^/brand/(.*)/$/seobrand\.php\?spell=$1;  
  177.  
  178. }  
  179. location~.*\.(php| php5)?$  
  180. {  
  181.  #fastcgi_pass unix:/tmp/php-cgi.sock;  
  182.  fastcgi_pass 127.0.0.1:9000;  
  183.  fastcgi_index index.php;  
  184.  include fcgi.conf;  
  185.  fastcgi_param SCRIPT_FILENAME /data/htdocs/www/very365$fastcgi_script_name;  
  186.  fastcgi_param SCRIPT_NAME /data/htdocs/www/very365 $fastcgi_script_name;  
  187. }  
  188.  
  189. location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  190. {  
  191.  expires 30d;  
  192. }  
  193.  
  194. location~.*\.(js| css)$  
  195. {  
  196.  expires 1h;  
  197. }  
  198. access_log off;  
  199.  }  
  200. server  
  201.  {  
  202. listen 80;  
  203. server_name www.wqueen.cn wqueen.cn;  
  204.  
  205. index index.html index.htm index.php;  
  206. root /data/htdocs/www/wqueen/bbs;  
  207.  
  208. location~.*\.(php| php5)?$  
  209. {  
  210.  #fastcgi_pass unix:/tmp/php-cgi.sock;  
  211.  fastcgi_pass 127.0.0.1:9000;  
  212.  fastcgi_index index.php;  
  213.  include fcgi.conf;  
  214. }  
  215.  
  216. location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  217. {  
  218.  expires 30d;  
  219. }  
  220.  
  221. location~.*\.(js| css)?$  
  222. {  
  223.  expires 1h;  
  224. }  
  225.  
  226. access_log off;  
  227. }  
  228.  
  229. server  
  230.  {  
  231. listen 80;  
  232. server_name baobei.wqueen.cn;  
  233. index index.html index.htm index.php;  
  234. root /data/htdocs/www/baobei;  
  235.  
  236. location~.*\.(php| php5)?$  
  237. {  
  238.  #fastcgi_pass unix:/tmp/php-cgi.sock;  
  239.  fastcgi_pass 127.0.0.1:9000;  
  240.  fastcgi_index index.php;  
  241.  include fcgi.conf;  
  242. }  
  243.  
  244. location~.*\.(gif| jpg| jpeg| png| bmp| swf)$  
  245. {  
  246.  expires 30d;  
  247. }  
  248.  
  249. location~.*\.(js| css)?$  
  250. {  
  251.  expires 1h;  
  252. }  
  253.  
  254. access_log off;  
  255.  }  

 

 

其实Nginx的虚拟目录跟Apache的配置过程差不多,每一个server{}即对应一个虚拟主机,server_name是此虚拟主机的域名,root是此虚拟主机的根目录。当然,我们预先就应该在DNS做好相应的解析对应关系。另外,由于此LNMP主要用于博客和论坛,后面的虚拟主机我就没有配置相应的日志了,有兴趣的朋友可以根据我的配置文件自行修改设置,这里就不细述了。

(6)在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件。/usr/local/webserver/nginx/conf/fcgi.conf文件的内容如下:

  1. fastcgi_param GATEWAY_INTERFACE CGI/1.1;  
  2. fastcgi_param SERVER_SOFTWAREnginx;  
  3. fastcgi_param QUERY_STRING$query_string;  
  4. fastcgi_param REQUEST_METHOD$request_method;  
  5. fastcgi_param CONTENT_TYPE$content_type;  
  6. fastcgi_param CONTENT_LENGTH$content_length;  
  7. fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;  
  8. fastcgi_param SCRIPT_NAME$fastcgi_script_name;  
  9. fastcgi_param REQUEST_URI$request_uri;  
  10. fastcgi_param DOCUMENT_URI$document_uri;  
  11. fastcgi_param DOCUMENT_ROOT$document_root;  
  12. fastcgi_param SERVER_PROTOCOL$server_protocol;  
  13. fastcgi_param REMOTE_ADDR$remote_addr;  
  14. fastcgi_param REMOTE_PORT$remote_port;  
  15. fastcgi_param SERVER_ADDR$server_addr;  
  16. fastcgi_param SERVER_PORT$server_port;  
  17. fastcgi_param SERVER_NAME$server_name;  
  18. fastcgi_param REDIRECT_STATUS200; 

 

我们启动Nginx时,经常会遇到no input file specified的问题,其实这是由于我们的nginx.conf文件没有指定$document_root变量,所以我们将下面这行内容改动一下(另外一种方法就是正确指定此变量):
  1. fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name; 

 

将其修改如下:
  1. fastcgi_param SCRIPT_FILENAME/data/htdocs/www$fastcgi_script_name; 

 

(7)启动Nginx,命令如下:
  1. /usr/local/webserver/nginx/sbin/nginx 

 

以后每次更改Nginx配置文件想重新启动Nginx时,都可以平滑地启动Nginx,这也是Nginx0.8.x增加的新特征,命令如下:
  1. /usr/local/webserver/nginx/sbin/nginx-s reload 

 

(8)编写每天定时切割Nginx日志的脚本,创建脚本文件/usr/local/webserver/nginx/sbin/cut_nginx_log.sh,文件内容如下:
  1. #!/bin/bash  
  2. # This script run at 00:00  
  3. # The Nginx logs path  
  4. logs_path="/data/logs" 
  5. mkdir-p ${logs_path}$(date-d "yesterday" +"%Y")/$(date-d "yesterday" +"%m")/  
  6. mv ${logs_path}access.log ${logs_path}$(date-d "yesterday" +"
    %Y")/$(date-d "yesterday" +"%m")/access_$(date-d "yesterday" +"%Y%m%d").log  
  7. /usr/local/webserver/nginx/sbin/nginx-s reload 

 

设置Crontab,每天零点切割Nginx访问日志。/etc/crontab文件新增的内容如下:
  1. 00 00***/bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh 

7.优化Linux内核

我们可以编辑/etc/sysctl.conf,新增内容如下:

  1. net.ipv4.tcp_max_syn_backlog = 65536 
  2. net.core.netdev_max_backlog = 32768  
  3. net.core.somaxconn = 32768 
  4. net.core.wmem_default = 8388608 
  5. net.core.rmem_default = 8388608 
  6. net.core.rmem_max = 16777216 
  7. net.core.wmem_max = 16777216 
  8. net.ipv4.tcp_timestamps = 0 
  9. net.ipv4.tcp_synack_retries = 2 
  10. net.ipv4.tcp_syn_retries = 2 
  11. net.ipv4.tcp_tw_recycle = 1 
  12. #net.ipv4.tcp_tw_len = 1 
  13. net.ipv4.tcp_tw_reuse = 1 
  14. net.ipv4.tcp_mem = 94500000 915000000 927000000  
  15. net.ipv4.tcp_max_orphans = 3276800 
  16. net.ipv4.ip_local_port_range = 1024 65535 

 

使配置立即生效,命令如下:
  1. /sbin/sysctl-p 

8.将相关软件都配置成自启动模式

我们将一些内容添加进/etc/rc.local文件中,如下所示:

  1. ulimit-SHn 65535  
  2. /usr/local/webserver/php/sbin/php-fpm start  
  3. /usr/local/webserver/nginx/sbin/nginx  
  4. /usr/local/webserver/mysql/bin/mysqld_safe 
    --defaults-file=/data/mysql/3306/my.cnf 

 

基本的安装过程到这里就结束了。通过对线上项目相当长时间的观察,我们也发现,Nginx作为Web服务器,在高并发的情况下还是非常稳定的。我也推荐大家将其应用于高并发的Web环境中,尤其是那种日PV百万级以上的Web网站。

分享到:
评论

相关推荐

    lnmp环境配置

    一个用Linux Shell编写的可以为CentOS/RHEL/Fedora/Aliyun/Amazon、Debian/Ubuntu/Raspbian/Deepin/Mint Linux VPS或独立主机安装LNMP(Nginx/MySQL/PHP)、LNMPA(Nginx/MySQL/PHP/Apache)、LAMP(Apache/MySQL/PHP)...

    在Debian系统下配置LNMP的教程

    LNMP环境的配置是需要让我们的主机支持Nginx、MySQL、PHP、phpMyAdmin,这样配置好之后就可以直接使用此环境,并在上面运行网站了,下面我来配置方法。 我们先来看官方说明 LNMP一键安装包是一个用Linux Shell编写的...

    Ubuntu Debian(NGINX/PHP/MYSQL)快速配置工具LNMP云安装

    Debian6系统下使用之前发布的“DebianLNMP”服务器软件套件,DebianLNMP一键安装包是一个快捷的服务器套件,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。现在我们已经在Debian/...

    lnmp懒人包

    只需要输入nginx 、 php 、 mysql 的版本号并指定安装路径,你就可以其实配置lnmp生产环境。

    LNMP 一键安装包.zip

    你可以自由 下载 并使用它在VPS(VDS)或独立服务器上,做为真正的生产环境或测试环境。 它安装那些软件 Nginx MySQL PHP PHPMyAdmin Apache Zend Optimizer eAccelerator ionCube PureFTPd ...

    LNMP一键安装包 0.5 完整版/下载版.gz

     编译安装需要输入大量的命令,如果是配置生产环境需要耗费大量的时间。  不会Linux的站长或Linux新手想使用Linux作为生产环境……  系统需求:  需要2 GB硬盘剩余空间  128M以上内存  安装步骤:  1、...

    使用LNMP一键安装包配置CentOS服务器环境教程

    作为使用linux配置生产环境的新手,无疑是最不想看到的。下面介绍如何在CentOS安装LNMP。 什么是LNMP LNMP指的是Linux系统下Nginx+MySQL+PHP这种网站服务器架构。这种网站服务器架构使用更少的资源,支持更多的并发...

    一个简洁的全自动安装LNMP服务器环境的Shell脚本分享

    此脚本在生产服务器上使用了一年多,本脚本崇尚简单唯美,只需要一个脚本就可以在任何一台有网络的服务器上自动配置LNMP。 本脚本会在脚本执行目录下,建packages目录用于存放LNMP所需要的软件。大家安装完可以删除...

    DebianLNMP加强版一键安装包Debian6配置Nginx+PHP5.3+MySQL最快最新

    DebianLNMP是一个用Linux Shell编写的可以为Debian/Ubuntu VPS(VDS)或独立主机安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)生产环境的Shell程序。DebianLNMP 是以 Debian + Linux + Nginx + MySQL + PHP 的安装顺序命名...

    DebianLNMP加强版一键安装包Debian6配置Nginx PHP5.3 MySQL最快最新

    DebianLNMP一键安装包是很不错的安装脚本,用在低配置VPS上最合适不过啦,安装方便快捷,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。 DebianLNMP加强版? 延续上面全部功能...新增...

    DebianLNMP一键安装包Debian6系统下的Nginx PHP5.3 MySQL最快最新

    DebianLNMP一键安装包是很不错的安装脚本,用在低配置VPS上最合适不过啦,安装方便快捷,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。 DebianLNMP特点: •独特的DebianLNMP...

    DebianLNMP一键安装包Debian6系统下的Nginx/PHP5.3/MySQL最快最新

    DebianLNMP一键安装包是很不错的安装脚本,用在低配置VPS上最合适不过啦,安装方便快捷,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。 DebianLNMP特点: •独特的DebianLNMP...

    两条命令Debian6一键安装包DebianLNMP最快.最新.最省!

    DebianLNMP一键安装包是很不错的安装脚本,用在低配置VPS上最合适不过啦,安装方便快捷,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。 DebianLNMP特点: •独特的DebianLNMP...

    DebianLNMP虚拟主机管理工具Nginx+PHP5.3+MySQL管理软件

    Debian6系统下使用之前发布的“DebianLNMP”服务器软件套件,DebianLNMP一键安装包是一个快捷的服务器套件,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。现在我们已经在Debian/...

    使用docker搭建一套开发环境全家桶的详细教程

    是适用于多端的一款docker开发与生产环境的PHP环境全家桶。 DNMP项目特点: 100%开源 100%遵循Docker标准 支持多版本PHP共存,可任意切换(PHP5.4、PHP5.6、PHP7.1、PHP7.2、PHP7.3) 支持绑定任意多个域名 支持...

    DebianLNMP虚拟主机管理工具Nginx PHP5.3 MySQL管理软件

    Debian6系统下使用之前发布的“DebianLNMP”服务器软件套件,DebianLNMP一键安装包是一个快捷的服务器套件,轻轻松松全自动安装LNMP(Nginx、MySQL、PHP、phpMyAdmin)可直接用再生产环境。现在我们已经在Debian/...

    LTMP:LTMP(CentOSTengineMySQLPHP)

    现在很多朋友都了解或者已经在使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL/PHP),LNMPA(Nginx/MySQL/PHP/Apache),LAMP(Apache/MySQL/PHP)等类似的...

    CentOS 7系统管理与运维实战 part2

    本书从实际应用出发,以 CentOS 7作为操作系统... 本书从实际生产应用环境出发,并注重安全与运维思路教学,既适合于有一定计算机基础的学习 Linux的初学者,又适合于有一定 Linux基础,需要学习运维知识的人员阅读。

    CentOS 7系统管理与运维实战 part3

    本书从实际应用出发,以 CentOS 7作为操作系统... 本书从实际生产应用环境出发,并注重安全与运维思路教学,既适合于有一定计算机基础的学习 Linux的初学者,又适合于有一定 Linux基础,需要学习运维知识的人员阅读。

Global site tag (gtag.js) - Google Analytics