经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
shell脚本多实例部署nginx的详细教程
来源:jb51  时间:2021/10/25 19:07:47  对本文有异议

1. 创建一个目录,用来存放脚本和安装包

  1. [root@localhost nginx]# tree
  2. .
  3. ├── install.sh
  4. └── packages
  5. └── nginx-1.20.1.tar.gz
  6.  
  7. 1 directory, 2 files
  8. [root@localhost nginx]#
  9.  

2. 下载好对应的安装包

  1. [root@localhost packages]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
  2. [root@localhost packages]# ls
  3. nginx-1.20.1.tar.gz
  4. [root@localhost packages]#

3. 编写脚本

  1. [root@localhost nginx]# cat install.sh
  2. #!/bin/bash
  3.  
  4. log_dir=/var/log
  5. install_dir=/usr/local
  6.  
  7. id nginx &>/dev/null
  8. if [ $? -ne 0 ];then
  9. useradd -r -M -s /sbin/nologin nginx
  10. fi
  11.  
  12. yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
  13.  
  14. if [ ! -d $log_dir/nginx ];then
  15. mkdir -p $log_dir/nginx
  16. chown -R nginx.nginx $log_dir/nginx
  17. fi
  18.  
  19.  
  20. if [ ! -d $install_dir/nginx-1.20.1 ];then
  21. tar xf packages/nginx-1.20.1.tar.gz -C $install_dir
  22. fi
  23.  
  24. cd $install_dir/nginx-1.20.1
  25. if [ ! -d $install_dir/nginx ];then
  26. ./configure --prefix=$install_dir/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
  27. make && make install
  28. fi
  29.  
  30. echo "export PATH=$install_dir/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
  31.  
  32. cat > /usr/lib/systemd/system/nginx.service <<EOF
  33. [Unit]
  34. Description=Nginx server daemon
  35. After=network.target
  36.  
  37. [Service]
  38. Type=forking
  39. ExecStart=/usr/local/nginx/sbin/nginx
  40. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  41. ExecReload=/bin/kill -HUP \$MAINPID
  42.  
  43. [Install]
  44. WantedBy=multi-user.target
  45. EOF
  46.  
  47. systemctl daemon-reload
  48. systemctl enable --now nginx.service
  49.  
  50.  
  51. [root@localhost nginx]#
  52.  

4. 验证效果

  1. [root@localhost nginx]# bash -x install.sh
  2. + log_dir=/var/log
  3. + install_dir=/usr/local
  4. + id nginx
  5. + '[' 0 -ne 0 ']'
  6. + yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
  7. Updating Subscription Management repositories.
  8. Unable to read consumer identity
  9. This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
  10. 上次元数据过期检查:1:03:20 前,执行于 20211024 星期日 205726秒。
  11. 软件包 pcre-devel-8.42-4.el8.x86_64 已安装。
  12. 软件包 pcre-8.42-4.el8.x86_64 已安装。
  13. 软件包 gcc-8.4.1-1.el8.x86_64 已安装。
  14. 软件包 gcc-c++-8.4.1-1.el8.x86_64 已安装。
  15. 软件包 openssl-devel-1:1.1.1g-15.el8_3.x86_64 已安装。
  16. 软件包 zlib-1.2.11-17.el8.x86_64 已安装。
  17. 软件包 zlib-devel-1.2.11-17.el8.x86_64 已安装。
  18. 软件包 make-1:4.2.1-10.el8.x86_64 已安装。
  19. 软件包 vim-enhanced-2:8.0.1763-15.el8.x86_64 已安装。
  20. 软件包 wget-1.19.5-10.el8.x86_64 已安装。
  21. 软件包 openssl-1:1.1.1g-15.el8_3.x86_64 已安装。
  22. 软件包 gd-devel-2.2.5-7.el8.x86_64 已安装。
  23. 依赖关系解决。
  24. 无需任何处理。
  25. 完毕!
  26. + '[' '!' -d /var/log/nginx ']'
  27. + '[' '!' -d /usr/local/nginx-1.20.1 ']'
  28. + cd /usr/local/nginx-1.20.1
  29. + '[' '!' -d /usr/local/nginx ']'
  30. + echo 'export PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'
  31. + cat
  32. + systemctl daemon-reload
  33. + systemctl enable --now nginx.service
  34. [root@localhost nginx]#
  35. [root@localhost nginx]# ss -antl
  36. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  37. LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
  38. LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
  39. LISTEN 0 128 [::]:22 [::]:*
  40. [root@localhost nginx]#
  41.  

到此这篇关于shell脚本多实例部署nginx的详细教程的文章就介绍到这了,更多相关shell脚本部署nginx内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号