所需软件:

 

  1. eaccelerator-0.9.6.1.tar.bz2 
  2. mhash-0.9.9.9.tar.bz2 
  3. ImageMagick-6.6.1-10.tar.gz 
  4. mysql-5.0.56.tar.gz 
  5. imagick-2.3.0.tgz 
  6. nginx-0.8.46.tar.gz 
  7. libiconv-1.13.1.tar.gz 
  8. pcre-8.10.zip 
  9. libmcrypt-2.5.8.tar.gz 
  10. PDO_MYSQL-1.0.2.tgz 
  11. mcrypt-2.6.8.tar.gz 
  12. php-5.2.14-fpm-0.5.14.diff.gz 
  13. memcache-2.2.5.tgz 
  14. php-5.2.14.tar.gz 

安装PHP环境所需软件


  1. tar zxvf libiconv-1.13.1.tar.gz 
  2. cd libiconv-1.13.1/ 
  3. ./configure --prefix=/usr/local 
  4. make 
  5. make install 
  6. cd .. 
  7. tar zxvf libmcrypt-2.5.8.tar.gz 
  8. cd libmcrypt-2.5.8/ 
  9. ./configure 
  10. make 
  11. make install 
  12. /sbin/ldconfig 
  13. cd libltdl/ 
  14. ./configure --enable-ltdl-install 
  15. make 
  16. make install 
  17. cd .. 
  18. cd .. 
  19. tar jxvf mhash-0.9.9.9.tar.bz2 
  20. cd mhash-0.9.9.9/ 
  21. ./configure 
  22. make 
  23. make install 
  24. cd .. 
  25. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la 
  26. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so 
  27. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 
  28. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 
  29. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a 
  30. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la 
  31. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so 
  32. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 
  33. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 
  34. ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config 
  35. tar zxvf mcrypt-2.6.8.tar.gz 
  36. cd mcrypt-2.6.8/ 
  37. /sbin/ldconfig 
  38. LD_LIBRARY_PATH=/usr/local/lib ./configure 
  39. make 
  40. make install 
  41. cd .. 

MYSQL安装


  1. groupadd mysql 
  2. useradd -s /sbin/nologin -g mysql -M mysql 
  3. tar zxvf mysql-5.0.56.tar.gz 
  4. cd mysql-5.0.56 
  5. ./configure \ 
  6. --prefix=/usr/local/mysql \ 
  7. --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock \ 
  8. --localstatedir=/usr/local/mysql/data \ 
  9. --enable-assembler \ 
  10. --enable-thread-safe-client \ 
  11. --with-mysqld-user=mysql \ 
  12. --with-big-tables \ 
  13. --without-debug \ 
  14. --with-pthread \ 
  15. --enable-assembler \ 
  16. --with-extra-charsets=complex \ 
  17. --with-readline \ 
  18. --with-ssl \ 
  19. --with-embedded-server \ 
  20. --enable-local-infile \ 
  21. --with-plugins=partition,innobase \ 
  22. --with-plugin-PLUGIN \ 
  23. --with-mysqld-ldflags=-all-static \ 
  24. --with-client-ldflags=-all-static 
  25. make 
  26. make install 
  27. cp support-files/my-small.cnf /etc/my.cnf 
  28. mkdir -p /usr/local/mysql/data 
  29. /usr/local/mysql/bin/mysql_install_db --user=mysql 
  30. chgrp -R mysql /usr/local/mysql 
  31. /usr/local/mysql/bin/mysqld_safe --user=mysql& 
  32. ps -ef |grep mysql 
  33. echo "export PATH=$PATH:/usr/local/mysql/bin/">>/etc/profile       //设置环境变量 
  34. source /etc/profile 
  35. cp support-files/mysql.server /etc/init.d/mysqld                  //设置为service启动方式 
  36. chmod 755 /etc/init.d/mysqld 
  37. chkconfig --add mysqld                                     //添加为开机自启动 
  38. service mysqld restart 
  39. cd .. 
  40. mysqladmin -uroot password elain                      //设置密码 
  41. mysql -uroot -p 

安装PHP(FastCGI模式)


  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 
  3. cd php-5.2.14/ 
  4. ./configure \ 
  5. --prefix=/usr/local/php \ 
  6. --with-config-file-path=/usr/local/php/etc \ 
  7. --with-mysql=/usr/local/mysql \ 
  8. --with-mysqli=/usr/local/mysql/bin/mysql_config \ 
  9. --with-iconv-dir=/usr/local \ 
  10. --with-freetype-dir \ 
  11. --with-jpeg-dir \ 
  12. --with-png-dir \ 
  13. --with-zlib \ 
  14. --with-libxml-dir=/usr \ 
  15. --enable-xml \ 
  16. --disable-rpath \ 
  17. --enable-discard-path \ 
  18. --enable-safe-mode \ 
  19. --enable-bcmath \ 
  20. --enable-shmop \ 
  21. --enable-sysvsem \ 
  22. --enable-inline-optimization \ 
  23. --with-curl \ 
  24. --with-curlwrappers \ 
  25. --enable-mbregex \ 
  26. --enable-fastcgi \ 
  27. --enable-fpm \ 
  28. --enable-force-cgi-redirect \ 
  29. --enable-mbstring \ 
  30. --with-mcrypt \ 
  31. --with-gd \ 
  32. --enable-gd-native-ttf \ 
  33. --with-openssl \ 
  34. --with-mhash \ 
  35. --enable-pcntl \ 
  36. --enable-sockets \ 
  37. --with-ldap \ 
  38. --with-ldap-sasl \ 
  39. --with-xmlrpc \ 
  40. --enable-zip \ 
  41. --enable-soap 
  42. make ZEND_EXTRA_LIBS='-liconv' 
  43. make install 
  44. cp php.ini-dist /usr/local/php/etc/php.ini 
  45. cd ../ 

安装PHP扩展模块


  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 
  6. make install 
  7. cd ../ 
  8. tar jxvf eaccelerator-0.9.6.1.tar.bz2 
  9. cd eaccelerator-0.9.6.1/ 
  10. /usr/local/php/bin/phpize 
  11. ./configure \ 
  12. --enable-eaccelerator=shared \ 
  13. --with-php-config=/usr/local/php/bin/php-config 
  14. make 
  15. make install 
  16. cd ../ 
  17. tar zxvf PDO_MYSQL-1.0.2.tgz 
  18. cd PDO_MYSQL-1.0.2/ 
  19. /usr/local/php/bin/phpize 
  20. ./configure \ 
  21. --with-php-config=/usr/local/php/bin/php-config \ 
  22. --with-pdo-mysql=/usr/local/mysql 
  23. make 
  24. make install 
  25. cd ../ 
  26. tar zxvf ImageMagick-6.6.1-10.tar.gz 
  27. cd ImageMagick-6.6.1-10 
  28. ./configure 
  29. make 
  30. make install 
  31. cd ../ 
  32. tar zxvf imagick-2.3.0.tgz 
  33. cd imagick-2.3.0/ 
  34. /usr/local/php/bin/phpize 
  35. ./configure --with-php-config=/usr/local/php/bin/php-config 
  36. make 
  37. make install 
  38. cd ../ 

配置PHP
———————————-
修改php.ini文件
vi /usr/local/php/etc/php.ini
手工修改:
查找/usr/local/php/etc/php.ini中的
extension_dir = “./”    (约491行)
修改为


  1. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" 

并在此行后增加以下几行


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

再查找 output_buffering = Off    (约101行)
修改为 output_buffering = On
再查找 ;cgi.fix_pathinfo=0       (约523行)
修改为 cgi.fix_pathinfo=0,    //即取消注释,使生效,防止Nginx文件类型错误解析漏洞。
—————————-
配置eAccelerator加速PHP:


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

末尾添加以下配置信息:


  1. [eaccelerator] 
  2. zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" 
  3. eaccelerator.shm_size="64" 
  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="3600" 
  12. eaccelerator.shm_prune_period="3600" 
  13. eaccelerator.shm_only="0" 
  14. eaccelerator.compress="1" 
  15. eaccelerator.compress_level="9" 

创建www用户和组,以及供blog.cgzg.net使用的目录:


  1. /usr/sbin/groupadd www 
  2. /usr/sbin/useradd -g www www 
  3. mkdir -p /www/htdocs 
  4. chmod +w /www/htdocs 
  5. chown -R www:www /www 

修改php-fpm配置文件:
vi /usr/local/php/etc/php-fpm.conf
找到58-66行里注释去掉,或删除改为下面几行
<!–     –>    此为注释,需去掉
然后把里面的用户 nobody  改为 www
找113行,把<value name=”rlimit_files”>1024</value>里的数字改成 65535


  1. /usr/local/php/sbin/php-fpm start                  //启动PHP 
  2. ps -ef |grep php 

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署

注:默认启动6个进程,可根据自己服务器配置自行调整。
注:/usr/local/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。

安装Nginx


  1. unzip pcre-8.10.zip 
  2. cd pcre-8.10/ 
  3. ./configure 
  4. make &amp;&amp; make install 
  5. cd ../ 
  6.  
  7. tar zxvf nginx-0.8.46.tar.gz 
  8. cd nginx-0.8.46/ 
  9. ./configure --user=www --group=www \ 
  10. --prefix=/usr/local/nginx \ 
  11. --with-http_stub_status_module \ 
  12. --with-http_ssl_module 
  13. make &amp;&amp; make install 
  14. cd ../ 

 

创建Nginx日志目录


  1. mkdir -p /www/logs 
  2. chmod +w /www/logs 
  3. chown -R www:www /www/logs 

创建Nginx配置文件


  1. mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.ori   //备份原始配置文件 
  2. vi /usr/local/nginx/conf/nginx.conf 

输入以下内容:


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

创建fcgi.conf文件:


  1. vi /usr/local/nginx/conf/fcgi.conf 

输入以下内容:


  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; 

启动Nginx


  1. ulimit -SHn 65535 
  2. /usr/local/nginx/sbin/nginx 
  3. ps -ef |grep nginx     //检查启动情况 

注:这里默认开启8个进程,可根据自己服务器配置自行调整。

配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:


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

创建PHP测试文件

vi /www/htdocs/info.php


  1. <?php 
  2. phpinfo(); 
  3. ?> 

访问 http://10.0.0.84/info.php

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署
看到上图说明LNMP环境架设成功!!!

虚拟主机配置

vi /usr/local/nginx/conf/nginx.conf  在最后一个server前添加下面内容


  1. server 
  2. listen       80; 
  3. server_name  blog.cgzg.net; 
  4. index index.html index.htm index.php; 
  5. root  /www/htdocs/blog; 
  6. #limit_conn   crawler  20; 
  7. location ~ .*\.(php|php5)?$ 
  8. #fastcgi_pass  unix:/tmp/php-cgi.sock; 
  9. fastcgi_pass  127.0.0.1:9000; 
  10. fastcgi_index index.php; 
  11. include fcgi.conf; 
  12. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  13. expires      30d; 
  14. location ~ .*\.(js|css)?$ 
  15. expires      1h; 
  16. log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' 
  17. '$status $body_bytes_sent "$http_referer" ' 
  18. '"$http_user_agent" $http_x_forwarded_for'; 
  19. access_log  /www/logs/access.log  access; 
  20. server 
  21. listen       80; 
  22. server_name  wiki.cgzg.net; 
  23. index index.html index.htm index.php; 
  24. root  /www/htdocs/wiki; 
  25. location ~ .*\.(php|php5)?$ 
  26. #fastcgi_pass  unix:/tmp/php-cgi.sock; 
  27. fastcgi_pass  127.0.0.1:9000; 
  28. fastcgi_index index.php; 
  29. include fcgi.conf; 
  30. log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" ' 
  31. '$status $body_bytes_sent "$http_referer" ' 
  32. '"$http_user_agent" $http_x_forwarded_for'; 
  33. access_log  /www/logs/access.log  wwwlogs; 
  34. server 
  35. listen  80; 
  36. server_name  status.cgzg.net; 
  37. location / { 
  38. stub_status on; 
  39. access_log   off; 

  1. mkdir -p /www/htdocs/wiki 
  2. mkdir -p /www/htdocs/blog                       //建立网站程序目录 

检查是否正确:
/usr/local/nginx/sbin/nginx -t

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署
看到上图,表示配置成功!!
平滑重启:
/usr/local/nginx/sbin/nginx -s reload

 

编写每天定时切割Nginx日志的脚本
—————————————-
创建脚本cut_nginx_log.sh
vi /usr/local/nginx/sbin/cut_nginx_log.sh


  1. #!/bin/bash 
  2. # This script run at 00:00 
  3. # The Nginx logs path 
  4. logs_path="/www/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. kill -USR1 `cat /usr/local/nginx/nginx.pid` 

chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh        //赋予此脚本执行权限

设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
输入以下内容:

 


  1. 00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh 

安装Blog程序:
Blog 网站程序地址 http://cn.wordpress.org/wordpress-3.0.1-zh_CN.tar.gz
cd /www/htdocs/blog/
上传网站程序


  1. unzip wordpress-3.0.1.zip 
  2. cd wordpress 
  3. mv * ../ 
  4. cd .. 
  5. rm -Rf wordpress 
  6. cp wp-config-sample.php  wp-config.php 
  7. vi wp-config.php 

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署

根据上图填写


  1. chmod 777 wp-config.php 
  2. /usr/local/php/sbin/php-fpm restart 
  3. kill -HUP `cat /usr/local/nginxscp 10.0.10.84:/www/htdocs/blog.tar.gz  ./
    tar zxvf blog.tar.gz/nginx.pid` 

在DB2 3307数据库里为blog 创建一个数据库


  1. mysql -uroot -p -S /data/3307/mysql.sock             //登录mysql 输入密码elain 
  2. create database blog;          //建立数据库 
  3. grant all privileges on blog.* to 'elain'@'10.0.10.%' identified by 'elain'; 

//建立 blog 的帐号elain,密码elain,并授权管理 blog 这个库
注:’10.0.10.%’ 这里的“%”表示10.0.10这个网段都可访问
访问:http://blog.cgzg.net 安装

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署安装后效果访问:

[跟我学中小企业架构部署]之五:WEB服务器LNMP部署

到此LNMP安装完成!!!
接下来,在LAMP主机上打包wiki站点
在LNMP主机上打包blog站点
LAMP


  1. cd /application/apache2.2.15/htdocs/ 
  2. tar zcvf wiki.tar.gz  wiki/ 

LNMP


  1. cd /www/htdocs/ 
  2. tar zcvf blog.tar.gz  blog/ 
  3. rm -Rf wiki/ 
  4. scp 10.0.10.83:/application/apache2.2.15/htdocs/wiki.tar.gz  ./  //注意是 ./ 表示当前目录 
  5. tar zxvf wiki.tar.gz 

LAMP


  1. scp 10.0.10.84:/www/htdocs/blog.tar.gz  ./ 
  2. tar zxvf blog.tar.gz 

做下测试,首先把LNMP的WEB服务关闭,访问

http://wiki.cgzg.net

http://blog.cgzg.net

发现都可以访问,且可以写入数据
再把LAMP的WEB服务关闭,启动LNMP的WEB服务,访问

http://wiki.cgzg.net

http://blog.cgzg.net

发现都可以访问,且看到刚才在LAMP上写入的数据,这样就保障了两WEB服务器任意一台宕机,都不会影响网站的正常访问,当然还可以这层上扩展更多的WEB主机,来实现高可用,无单点服务。
到此为至,LAMP、LNMP服务器架设完毕。

转载请注明: 转载自http://www.elain.org

本文链接地址:[跟我学中小企业架构部署]之五:WEB服务器LNMP部署