源码包下载: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错误日志
源码安装nginx
1. 前提环境
[root@localhost ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
2. 解压Nginx包
1. [root@localhost ~]# useradd -s /sbin/nologin nginx 2. [root@localhost ~]# tar zxf /media/nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz -C /usr/src 3. [root@localhost ~]# tar zxf /media/ngx_cache_purge-2.3.tar.gz -C /usr/src 4. [root@localhost ~]# tar zxf /media/nginx-1.12.0.tar.gz -C /usr/src
3. 编译安装nginx
1. [root@localhost ~]# cd /usr/src/nginx-1.12.0/ 2. [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \ 3. --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module \ 4. --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client \ 5. --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ 6. --with-pcre --add-module=../ngx_cache_purge-2.3 --with-http_flv_module \ 7. --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42 8. [root@localhost nginx-1.12.0]# make && make install
4. 添加系统服务并启动
1. [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 2. [root@localhost ~]# mkdir -p /var/tmp/nginx/client 3. [root@localhost ~]# chown -R nginx:nginx /var/tmp/nginx 4. [root@localhost ~]# vim /etc/init.d/nginx 5. #!/bin/bash 6. # chkconfig: 2345 99 20 7. # description: Nginx Service Control Script 8. PROG="/usr/local/nginx/sbin/nginx" 9. PIDF="/usr/local/nginx/logs/nginx.pid" 10. case "$1" in 11. start) 12. netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null 13. if [ $? -eq 0 ] 14. then 15. echo "Nginx service already running." 16. else 17. $PROG -t &> /dev/null 18. if [ $? -eq 0 ] ; then 19. $PROG 20. echo "Nginx service start success." 21. else 22. $PROG -t 23. fi 24. fi 25. ;; 26. stop) 27. netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null 28. if [ $? -eq 0 ] 29. then 30. kill -s QUIT $(cat $PIDF) 31. echo "Nginx service stop success." 32. else 33. echo "Nginx service already stop" 34. fi 35. ;; 36. restart) 37. $0 stop 38. $0 start 39. ;; 40. status) 41. netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null 42. if [ $? -eq 0 ] 43. then 44. echo "Nginx service is running." 45. else 46. echo "Nginx is stop." 47. fi 48. ;; 49. reload) 50. netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null 51. if [ $? -eq 0 ] 52. then 53. $PROG -t &> /dev/null 54. if [ $? -eq 0 ] ; then 55. kill -s HUP $(cat $PIDF) 56. echo "reload Nginx config success." 57. else 58. $PROG -t 59. fi 60. else 61. echo "Nginx service is not run." 62. fi 63. ;; 64. *) 65. echo "Usage: $0 {start|stop|restart|reload}" 66. exit 1 67. esac 68. [root@localhost ~]# chmod +x /etc/init.d/nginx 69. [root@localhost ~]# chkconfig --add nginx 70. [root@localhost ~]# chkconfig nginx on 71. [root@localhost ~]# service nginx start 72. Nginx service start success.
5. 验证
查看版本信息:nginx -v
查看模块信息:nginx -V
通过访问Nginx服务器,可以看到Nginx默认网页(如下图)。
rpm安装nginx
调度器配置
1. [root@localhost ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force //rpm安装nginx 2. 3. [root@localhost ~]# cd /etc/nginx/conf.d/ 4. 5. [root@localhost conf.d]# ls 6. 7. default.conf 8. 9. [root@localhost conf.d]# rm -rf * //删除默认扩展配置文件 10. 11. [root@localhost conf.d]# vim lb.conf //添加扩展配置文件,指定两个节点IP 12. 13. upstream web { 14. 15. server 192.168.1.5:80; 16. 17. server 192.168.1.6:80; 18. 19. } 20. 21. server { 22. 23. listen 80; 24. 25. server_name localhost; 26. 27. location / { 28. 29. root html; 30. 31. index index.html index.htm; 32. 33. proxy_pass http://web; 34. 35. } 36. 37. } 38. 39. [root@localhost conf.d]# nginx -t //检查语法 40. 41. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 42. 43. nginx: configuration file /etc/nginx/nginx.conf test is successful 44. 45. [root@localhost conf.d]# systemctl start nginx
节点服务器配置
两台服务器配置相同,但网页内容需要做出区分,以方便测试。
1. [root@node1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force 2. 3. [root@node1 ~]# echo "192.168.1.5" > /usr/share/nginx/html/index.html 4. 5. [root@node1 ~]# systemctl start nginx
分别访问node1和node2可以看到网页内容分别不同。
此时可以访问调度器IP,刷新两次网址,可以看到两个节点的网站内容即调度完成。