首页 > 代码库 > nginx编译安装与apache动静分离共存设置及负载均衡设置

nginx编译安装与apache动静分离共存设置及负载均衡设置

nginx对于静态文件强大的响应能力一定程度上弥补了apache环境的不足,使用nginx做反向代理,一部分为了提高静态文件的相应能力,另外可以使用nginx做负载均衡来搭2至3台的apache服务器,这样在相当长的时间段内解决了初建团队的业务蓬勃发展所遇到的服务器资源问题.

1,安装依赖.
    yum -y install wget zip unzip zlib zlib-devel pcre pcre-devel  pcre* openssl openssl-devel perl perl-devel  perl-ExtUtils-Embed.
 
2,下载源码包
      wget http://nginx.org/download/nginx-1.6.1.tar.gz
 
3,  解压
     tar -zxvf nginx-1.6.1.tar.gz 
     cd nginx-1.6.1/
 
4,建立必须目录
  mkdir /var/tmp/nginx/ /var/log/nginx/ /var/run/nginx/
 
5,编译安装
  ./configure    --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf   --error-log-path=/var/log/nginx/error.log   --pid-path=/var/run/nginx.pid    --lock-path=/var/lock/nginx.lock     --http-log-path=/var/log/nginx/access.log   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --with-http_ssl_module   --with-http_flv_module   --with-http_gzip_static_module --with-http_realip_module  --with-http_stub_status_module   --with-http_addition_module --with-http_sub_module  --with-http_dav_module  --with-http_perl_module  --with-ld-opt="-Wl,-E"  --with-mail
  make &&  make install
 
6,为nginx建立用户
   /usr/sbin/useradd -c "Nginx user" -s /bin/false -r -d /var/lib/nginx nginx
 
7,建立启动脚本
  vi /etc/init.d/nginx
  输入以下内容:
  
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid    # Source function library. . /etc/rc.d/init.d/functions    # Source networking configuration. . /etc/sysconfig/network    # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0    nginx="/usr/sbin/nginx" prog=$(basename $nginx)    NGINX_CONF_FILE="/etc/nginx/nginx.conf"    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx    lockfile=/var/lock/subsys/nginx    make_dirs() {    # make required directories    user=`nginx -V 2>&1 | grep "configure arguments:" | sed s/[^*]*--user=([^ ]*).*/1/g -`    options=`$nginx -V 2>&1 | grep configure arguments:`    for opt in $options; do        if [ `echo $opt | grep .*-temp-path` ]; then            value=`echo $opt | cut -d "=" -f 2`            if [ ! -d "$value" ]; then                # echo "creating" $value                mkdir -p $value && chown -R $user $value            fi        fi    done }    start() {     [ -x $nginx ] || exit 5     [ -f $NGINX_CONF_FILE ] || exit 6     make_dirs     echo -n $"Starting $prog: "     daemon $nginx -c $NGINX_CONF_FILE     retval=$?     echo     [ $retval -eq 0 ] && touch $lockfile     return $retval }    stop() {     echo -n $"Stopping $prog: "     killproc $prog -QUIT     retval=$?     echo     [ $retval -eq 0 ] && rm -f $lockfile     return $retval }    restart() {     configtest || return $?     stop     sleep 1     start }    reload() {     configtest || return $?     echo -n $"Reloading $prog: "     killproc $nginx -HUP     RETVAL=$?     echo }    force_reload() {     restart }    configtest() {   $nginx -t -c $NGINX_CONF_FILE }    rh_status() {     status $prog }    rh_status_q() {     rh_status >/dev/null 2>&1 }    case "$1" in     start)         rh_status_q && exit 0         $1         ;;     stop)         rh_status_q || exit 0         $1         ;;     restart|configtest)         $1         ;;     reload)         rh_status_q || exit 7         $1         ;;     force-reload)         force_reload         ;;     status)         rh_status         ;;     condrestart|try-restart)         rh_status_q || exit 0             ;;     *)         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"         exit 2 esac

 

8,设置权限
  chmod 755 /etc/init.d/nginx
 
9,增加到系统服务
  chkconfig --add nginx
  以后的操作可以使用centos7的systemctl工具了
   systemctl start nginx
   systemctl stop nginx
   systemctl restart nginx
 
10,设置nginx和apache的动静分离
  编辑apache的配置文件,将Listen改为88端口
  vi /etc/nginx/nginx.conf 在第一个server配置中增加以下行
  
  location / {            root   /var/www/html;#apache的网站根目录            index  index.php index.html index.htm;        }          #将php文件请求分发给后端的apache        location ~ \.php$ {                 proxy_set_header Host $host;                proxy_set_header X-Real-IP $remote_addr;                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                proxy_pass http://127.0.0.1:88;        }          #将图像和静态文件由nginx处理        location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {                root /var/www/html;                expires 15d;        }           #将js文件由nginx处理         location ~ \.(js|css)$ {                expires 1h;        }

 

11,配置Nginx负载均衡

  vi /etc/nginx/nginx.conf在http节点里添加:

  

http {      include       mime.types;      default_type  application/octet-stream;      keepalive_timeout 120;      tcp_nodelay on;      upstream server1  {              server   192.168.1.2:80;              server   192.168.1.3:80;              server   192.168.1.4:80;              server   192.168.1.5:80;      }      upstream  server2  {              server   192.168.1.7:8080;              server   192.168.1.7:8081;              server   192.168.1.7:8082;      }      server      {              listen  80;              server_name server1;              location / {                       proxy_pass        http://www.zyan.cc;                       proxy_set_header   Host             $host;                       proxy_set_header   X-Real-IP        $remote_addr;                       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;              }      }      server      {              listen  80;              server_name server2;              location / {                       proxy_pass        http://blog.zyan.cc;                       proxy_set_header   Host             $host;                       proxy_set_header   X-Real-IP        $remote_addr;                       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;              }      }
 
 
另外:如果编译nginx有错误的时候
    
       the HTTP rewrite module requires the PCRE library.//安装pcre
                    the HTTP gzip module requires the zlib library//安装zlib
                    make[1]: *** [/usr/local/pcre/Makefile] Error 127//–with-pcre=DIR指向源码包,而不是安装后包,

nginx编译安装与apache动静分离共存设置及负载均衡设置