目录
源码包下载:http://nginx.org/en/download.html (mainline:开发版 stable:稳定版)
rpm包的yum源:http://nginx.org/packages/centos/7/x86_64/
本次通过安装另两个方式安装nginx实现调度功能,配置功能相同。源码安装方式只讲解如何安装,rpm安装将修改配置文件,只需要找到配置文件位置添加配置文件即可。
源码安装nginx配置主要目录
/usr/local/nginx/ 安装目录
/usr/local/nginx/conf/nginx.conf 主配置文件
/usr/share/nginx/html 网页根目录
/var/log/nginx/ 存放日志的位置,access.log访问日志,error.log错误日志
rpm安装nginx配置文件路径
/etc/nginx/nginx.conf 主配置文件,通过include /etc/nginx/conf.d/*.conf;指定扩展配置文件的路径
/etc/nginx/conf.d/ 扩展配置文件的路径,配置文件名称自定义
/usr/share/nginx/html 网页根目录
/var/log/nginx/ 存放日志的位置,access.log访问日志,error.log错误日志
1. 前提环境
[root@localhost ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
2. 解压Nginx包
- [root@localhost ~]# useradd -s /sbin/nologin nginx
- [root@localhost ~]# tar zxf /media/nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz -C /usr/src
- [root@localhost ~]# tar zxf /media/ngx_cache_purge-2.3.tar.gz -C /usr/src
- [root@localhost ~]# tar zxf /media/nginx-1.12.0.tar.gz -C /usr/src
3. 编译安装nginx
- [root@localhost ~]# cd /usr/src/nginx-1.12.0/
- [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
- --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module \
- --with-http_gzip_static_module --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-pcre --add-module=../ngx_cache_purge-2.3 --with-http_flv_module \
- --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42
- [root@localhost nginx-1.12.0]# make && make install
4. 添加系统服务并启动
- [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
- [root@localhost ~]# mkdir -p /var/tmp/nginx/client
- [root@localhost ~]# chown -R nginx:nginx /var/tmp/nginx
- [root@localhost ~]# vim /etc/init.d/nginx
- #!/bin/bash
- # chkconfig: 2345 99 20
- # description: Nginx Service Control Script
- PROG="/usr/local/nginx/sbin/nginx"
- PIDF="/usr/local/nginx/logs/nginx.pid"
- case "$1" in
- start)
- netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
- if [ $? -eq 0 ]
- then
- echo "Nginx service already running."
- else
- $PROG -t &> /dev/null
- if [ $? -eq 0 ] ; then
- $PROG
- echo "Nginx service start success."
- else
- $PROG -t
- fi
- fi
- ;;
- stop)
- netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
- if [ $? -eq 0 ]
- then
- kill -s QUIT $(cat $PIDF)
- echo "Nginx service stop success."
- else
- echo "Nginx service already stop"
- fi
- ;;
- restart)
- $0 stop
- $0 start
- ;;
- status)
- netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
- if [ $? -eq 0 ]
- then
- echo "Nginx service is running."
- else
- echo "Nginx is stop."
- fi
- ;;
- reload)
- netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
- if [ $? -eq 0 ]
- then
- $PROG -t &> /dev/null
- if [ $? -eq 0 ] ; then
- kill -s HUP $(cat $PIDF)
- echo "reload Nginx config success."
- else
- $PROG -t
- fi
- else
- echo "Nginx service is not run."
- fi
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|reload}"
- exit 1
- esac
- [root@localhost ~]# chmod +x /etc/init.d/nginx
- [root@localhost ~]# chkconfig --add nginx
- [root@localhost ~]# chkconfig nginx on
- [root@localhost ~]# service nginx start
- Nginx service start success.
5. 验证
查看版本信息:nginx -v
查看模块信息:nginx -V
通过访问Nginx服务器,可以看到Nginx默认网页(如下图)。

- [root@localhost ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force //rpm安装nginx
-
- [root@localhost ~]# cd /etc/nginx/conf.d/
-
- [root@localhost conf.d]# ls
-
- default.conf
-
- [root@localhost conf.d]# rm -rf * //删除默认扩展配置文件
-
- [root@localhost conf.d]# vim lb.conf //添加扩展配置文件,指定两个节点IP
-
- upstream web {
-
- server 192.168.1.5:80;
-
- server 192.168.1.6:80;
-
- }
-
- server {
-
- listen 80;
-
- server_name localhost;
-
- location / {
-
- root html;
-
- index index.html index.htm;
-
- proxy_pass http://web;
-
- }
-
- }
-
- [root@localhost conf.d]# nginx -t //检查语法
-
- nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
-
- nginx: configuration file /etc/nginx/nginx.conf test is successful
-
- [root@localhost conf.d]# systemctl start nginx
两台服务器配置相同,但网页内容需要做出区分,以方便测试。
- [root@node1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force
-
- [root@node1 ~]# echo "192.168.1.5" > /usr/share/nginx/html/index.html
-
- [root@node1 ~]# systemctl start nginx
分别访问node1和node2可以看到网页内容分别不同。

此时可以访问调度器IP,刷新两次网址,可以看到两个节点的网站内容即调度完成。
