手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

单一Nginx负载均衡+LNMP分布式架构

[日期:2012-08-13] 来源:Linux社区  作者:ant595 [字体:大 中 小]
二,安装配置SVR3
1.安装基本开发库,及依赖软件包
  1. #yum groupinstall "Development Libraries"  "Development Tools" 
  2. #yum -y install pcre-devel 

2.安装配置nginx
2.1 编译安装nginx

  1. #useradd -s /sbin/nologin nginx 
  2. #cd /usr/src 
  3. #tar xzvf nginx-1.2.2.tar.gz 
  4. #cd nginx-1.2.2 
  5. #./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid \ 
  6. --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module \ 
  7. --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module \ 
  8. --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy \ 
  9. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  10. --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre 
  11. #make && make install 

2.2 为nginx提供服务启动脚本

  1. #vim /etc/rc.d/init.d/nginx  内容如下 
  2. #!/bin/sh 
  3. # 
  4. # nginx - this script starts and stops the nginx daemon 
  5. # 
  6. # chkconfig:   - 85 15  
  7. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  8. #               proxy and IMAP/POP3 proxy server 
  9. # processname: nginx 
  10. # config:      /etc/nginx/nginx.conf 
  11. # config:      /etc/sysconfig/nginx 
  12. # pidfile:     /var/run/nginx.pid 
  13.   
  14. # Source function library. 
  15. . /etc/rc.d/init.d/functions 
  16.   
  17. # Source networking configuration. 
  18. . /etc/sysconfig/network 
  19.   
  20. # Check that networking is up. 
  21. [ "$NETWORKING" = "no" ] && exit 0 
  22.   
  23. nginx="/usr/local/nginx/sbin/nginx" 
  24. prog=$(basename $nginx) 
  25.   
  26. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
  27.   
  28. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  29.   
  30. lockfile=/var/lock/subsys/nginx 
  31.   
  32. make_dirs() { 
  33.    # make required directories 
  34.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  35.    options=`$nginx -V 2>&1 | grep 'configure arguments:'` 
  36.    for opt in $options; do 
  37.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  38.            value=`echo $opt | cut -d "=" -f 2` 
  39.            if [ ! -d "$value" ]; then 
  40.                # echo "creating" $value 
  41.                mkdir -p $value && chown -R $user $value 
  42.            fi 
  43.        fi 
  44.    done 
  45. } 
  46.   
  47. start() { 
  48.     [ -x $nginx ] || exit 5 
  49.     [ -f $NGINX_CONF_FILE ] || exit 6 
  50.     make_dirs 
  51.     echo -n $"Starting $prog: " 
  52.     daemon $nginx -c $NGINX_CONF_FILE 
  53.     retval=$? 
  54.     echo 
  55.     [ $retval -eq 0 ] && touch $lockfile 
  56.     return $retval 
  57. } 
  58.   
  59. stop() { 
  60.     echo -n $"Stopping $prog: " 
  61.     killproc $prog -QUIT 
  62.     retval=$? 
  63.     echo 
  64.     [ $retval -eq 0 ] && rm -f $lockfile 
  65.     return $retval 
  66. } 
  67.   
  68. restart() { 
  69.     configtest || return $? 
  70.     stop 
  71.     sleep 1 
  72.     start 
  73. } 
  74.   
  75. reload() { 
  76.     configtest || return $? 
  77.     echo -n $"Reloading $prog: " 
  78.     killproc $nginx -HUP 
  79.     RETVAL=$? 
  80.     echo 
  81. } 
  82.   
  83. force_reload() { 
  84.     restart 
  85. } 
  86.   
  87. configtest() { 
  88.   $nginx -t -c $NGINX_CONF_FILE 
  89. } 
  90.   
  91. rh_status() { 
  92.     status $prog 
  93. } 
  94.   
  95. rh_status_q() { 
  96.     rh_status >/dev/null 2>&1 
  97. } 
  98.   
  99. case "$1" in 
  100.     start) 
  101.         rh_status_q && exit 0 
  102.         $1 
  103.         ;; 
  104.     stop) 
  105.         rh_status_q || exit 0 
  106.         $1 
  107.         ;; 
  108.     restart|configtest) 
  109.         $1 
  110.         ;; 
  111.     reload) 
  112.         rh_status_q || exit 7 
  113.         $1 
  114.         ;; 
  115.     force-reload) 
  116.         force_reload 
  117.         ;; 
  118.     status) 
  119.         rh_status 
  120.         ;; 
  121.     condrestart|try-restart) 
  122.         rh_status_q || exit 0 
  123.             ;; 
  124.     *) 
  125.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  126.         exit 2 
  127. esac 
  128.  
  129.  
  130. #chmod a+x /etc/init.d/nginx 
  131. #chkconfig --add nginx 
  132. #chkconfig nginx on 

2.3 编辑nginx的主配置文件

  1. #vim /usr/local/nginx/conf/nginx.conf  内容如下 
  2. user  nginx; 
  3. worker_processes  10; 
  4. error_log  logs/error.log crit; 
  5. pid        logs/nginx.pid; 
  6. events 
  7. { 
  8.   use epoll; 
  9.   worker_connections 51000; 
  10. } 
  11. http { 
  12.     include       mime.types; 
  13.     default_type  application/octet-stream; 
  14.         client_header_buffer_size 32k; 
  15.         large_client_header_buffers 4 32k; 
  16.         client_max_body_size 10m; 
  17.     sendfile        on; 
  18.     tcp_nopush     on; 
  19.     keepalive_timeout  60; 
  20.     tcp_nodelay on; 
  21.         fastcgi_connect_timeout 300; 
  22.         fastcgi_send_timeout 300; 
  23.         fastcgi_read_timeout 300; 
  24.         fastcgi_buffer_size 64k; 
  25.         fastcgi_buffers 4 64k; 
  26.         fastcgi_busy_buffers_size 128k; 
  27.         fastcgi_temp_file_write_size 128k; 
  28.         gzip on; 
  29.         gzip_min_length 1k; 
  30.         gzip_buffers    4 16k; 
  31.         gzip_http_version 1.0; 
  32.         gzip_comp_level 2; 
  33.         gzip_types      text/plain application/x-Javascript text/ccs application/xml; 
  34.         gzip_vary on; 
  35.         server { 
  36.                 listen       80; 
  37.                 server_name  172.16.0.3; 
  38.                 index index.html index.htm index.php; 
  39.                 root /web/bbs; 
  40.                 location ~ .*\.(php|php5)?$ 
  41.                 { 
  42.                 fastcgi_pass    172.16.0.4:9000; 
  43.                 fastcgi_index index.php; 
  44.                 fastcgi_param SCRIPT_FILENAME /web/bbs$fastcgi_script_name; 
  45.                 include fastcgi.conf; 
  46.                 } 
  47.                 access_log      logs/bbs.log; 
  48.  
  49.         } 
  50. } 

2.4 创建网站根目录以及测试文件

  1. #mkdir -pv /web/bbs 
  2. #vim /web/bbs/index.php 
  3. <h1>test</h1> 
  4. <?php 
  5. phpinfo(); 
  6. ?> 

2.5 启动nginx服务

  1. #service nginx restart 


3,配置NFS

  1. #vim /etc/exports  
  2. /web 172.16.0.*(rw,no_root_squash,sync) 
  3.  
  4. #chkconfig portmap on 
  5. #chkconfig nfs on 
  6. #service portmap restart 
  7. #service nfs restart 


三,安装配置SVR2

1.安装基本开发库,及依赖软件包

  1. #yum groupinstall "Development Libraries"  "Development Tools" 
  2. #yum -y install pcre-devel 

2.安装配置nginx
2.1 编译安装nginx

  1. #useradd -s /sbin/nologin nginx 
  2. #cd /usr/src 
  3. #tar xzvf nginx-1.2.2.tar.gz 
  4. #cd nginx-1.2.2 
  5. #./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid \ 
  6. --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module \ 
  7. --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module \ 
  8. --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy \ 
  9. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  10. --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre 
  11. #make && make install 

2.2 为nginx提供服务启动脚本

  1. 见 二.2.2 的配置内容

2.3 编辑nginx的主配置文件

  1. #vim /usr/local/nginx/conf/nginx.conf  内容如下 
  2. user  nginx; 
  3. worker_processes  10; 
  4. error_log  logs/error.log crit; 
  5. pid        logs/nginx.pid; 
  6. events 
  7. { 
  8.   use epoll; 
  9.   worker_connections 51000; 
  10. } 
  11. http { 
  12.     include       mime.types; 
  13.     default_type  application/octet-stream; 
  14.         client_header_buffer_size 32k; 
  15.         large_client_header_buffers 4 32k; 
  16.         client_max_body_size 10m; 
  17.     sendfile        on; 
  18.     tcp_nopush     on; 
  19.     keepalive_timeout  60; 
  20.     tcp_nodelay on; 
  21.         fastcgi_connect_timeout 300; 
  22.         fastcgi_send_timeout 300; 
  23.         fastcgi_read_timeout 300; 
  24.         fastcgi_buffer_size 64k; 
  25.         fastcgi_buffers 4 64k; 
  26.         fastcgi_busy_buffers_size 128k; 
  27.         fastcgi_temp_file_write_size 128k; 
  28.         gzip on; 
  29.         gzip_min_length 1k; 
  30.         gzip_buffers    4 16k; 
  31.         gzip_http_version 1.0; 
  32.         gzip_comp_level 2; 
  33.         gzip_types      text/plain application/x-javascript text/ccs application/xml; 
  34.         gzip_vary on; 
  35.         server { 
  36.                 listen       80; 
  37.                 server_name  172.16.0.2; 
  38.                 index index.html index.htm index.php; 
  39.                 root /web/bbs; 
  40.                 location ~ .*\.(php|php5)?$ 
  41.                 { 
  42.                 fastcgi_pass    172.16.0.4:9000; 
  43.                 fastcgi_index index.php; 
  44.                 fastcgi_param SCRIPT_FILENAME /web/bbs$fastcgi_script_name; 
  45.                 include fastcgi.conf; 
  46.                 } 
  47.                 access_log      logs/bbs.log; 
  48.  
  49.         } 
  50. } 
  51.  

3.重启nginx服务

  1. #service nginx restart 


4.使用NFS 共享的网站目录,以达到共享存储的目的

  1. #mkdir /web
  2. #mount -t nfs 172.16.0.3:/web /web 
  3. #echo "mount -t nfs 172.16.0.3:/web /web " >> /etc/rc.local
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款