千家信息网

saltstack部署nginx

发表于:2025-01-24 作者:千家信息网编辑
千家信息网最后更新 2025年01月24日,1、前期规划准备nginx安装准备:1)安装必要依赖2)准备pcre安装包2)源码安装pcre3)准备nginx安装包4)源码安装nginxnginx配置:1)拷贝nginx.conf配置文件2)拷贝
千家信息网最后更新 2025年01月24日saltstack部署nginx

1、前期规划准备

nginx安装准备:

1)安装必要依赖

2)准备pcre安装包

2)源码安装pcre

3)准备nginx安装包

4)源码安装nginx

nginx配置:

1)拷贝nginx.conf配置文件

2)拷贝启停脚本

3)添加系统服务并设置开机启动

2、部署过程

1)准备安装包

[root@salt-master base]# cd /srv/salt/base[root@salt-master base]# mkdir -p pcre/files[root@salt-master base]# mkdir -p nginx/files[root@salt-master base]# cd /srv/salt/base/pcre/files[root@salt-master base]# wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz[root@salt-master base]# cd /srv/salt/base/nginx/files[root@salt-master base]# wget http://nginx.org/download/nginx-1.6.3.tar.gz

2)编写sls文件

[root@salt-master base]# cd nginx/[root@salt-master nginx]# vi install.sls [root@salt-master nginx]# vi service.sls[root@salt-master nginx]# cd ../pcre/[root@salt-master pcre]# vi install.sls

3)测试sls

[root@salt-master base]# salt 'salt-minion01.contoso.com' state.sls nginx.service test=Truesalt-minion01.contoso.com:----------          ID: pcre-source-install    Function: pkg.installed        Name: zlib-devel      Result: None     Comment: The following packages are set to be installed/updated: zlib-devel     Started: 14:41:07.540210    Duration: 18297.782 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: glibc-devel      Result: True     Comment: Package glibc-devel is already installed.     Started: 14:41:25.838187    Duration: 0.536 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: gcc      Result: True     Comment: Package gcc is already installed.     Started: 14:41:25.838783    Duration: 0.294 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: cmake      Result: True     Comment: Package cmake is already installed.     Started: 14:41:25.839133    Duration: 0.241 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: glibc      Result: True     Comment: Package glibc is already installed.     Started: 14:41:25.839432    Duration: 0.241 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: zlib      Result: True     Comment: Package zlib is already installed.     Started: 14:41:25.839727    Duration: 0.305 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: autoconf      Result: None     Comment: The following packages are set to be installed/updated: autoconf     Started: 14:41:25.840087    Duration: 0.281 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: gcc-c++      Result: True     Comment: Package gcc-c++ is already installed.     Started: 14:41:25.840422    Duration: 0.239 ms     Changes:   ----------          ID: pcre-source-install    Function: pkg.installed        Name: make      Result: True     Comment: Package make is already installed.     Started: 14:41:25.840709    Duration: 0.282 ms     Changes:   ----------          ID: pcre-source-install    Function: file.managed        Name: /opt/tools/pcre-8.37.tar.gz      Result: None     Comment: The file /opt/tools/pcre-8.37.tar.gz is set to be changed     Started: 14:41:25.888753    Duration: 17.582 ms     Changes:                 ----------              newfile:                  /opt/tools/pcre-8.37.tar.gz----------          ID: pcre-source-install    Function: cmd.run        Name: cd /opt/tools/ && tar -zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install      Result: None     Comment: Command "cd /opt/tools/ && tar -zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install" would have been executed     Started: 14:41:25.910768    Duration: 10.909 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: zlib-devel      Result: None     Comment: The following packages are set to be installed/updated: zlib-devel     Started: 14:41:25.922254    Duration: 2.483 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: gcc      Result: True     Comment: Package gcc is already installed.     Started: 14:41:25.925010    Duration: 0.926 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: make      Result: True     Comment: Package make is already installed.     Started: 14:41:25.926183    Duration: 0.975 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: zlib      Result: True     Comment: Package zlib is already installed.     Started: 14:41:25.927407    Duration: 1.276 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: openssl      Result: True     Comment: Package openssl is already installed.     Started: 14:41:25.929287    Duration: 1.331 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: openssl-devel      Result: None     Comment: The following packages are set to be installed/updated: openssl-devel     Started: 14:41:25.930830    Duration: 1.027 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: automake      Result: None     Comment: The following packages are set to be installed/updated: automake     Started: 14:41:25.931943    Duration: 0.392 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: autoconf      Result: None     Comment: The following packages are set to be installed/updated: autoconf     Started: 14:41:25.932402    Duration: 0.385 ms     Changes:   ----------          ID: nginx-source-install    Function: pkg.installed        Name: gcc-c++      Result: True     Comment: Package gcc-c++ is already installed.     Started: 14:41:25.932851    Duration: 0.33 ms     Changes:   ----------          ID: nginx-source-install    Function: group.present        Name: nginx      Result: None     Comment: The following group attributes are set to be changed:              gid: 601     Started: 14:41:25.934673    Duration: 0.425 ms     Changes:   ----------          ID: nginx-source-install    Function: user.present        Name: nginx      Result: None     Comment: The following user attributes are set to be changed:              homeDoesNotExist: /home/nginx              gid: 601              fullname: nginx              uid: 601     Started: 14:41:25.943129    Duration: 10.523 ms     Changes:   ----------          ID: nginx-source-install    Function: file.managed        Name: /opt/tools/nginx-1.6.3.tar.gz      Result: None     Comment: The file /opt/tools/nginx-1.6.3.tar.gz is set to be changed     Started: 14:41:25.953761    Duration: 3.461 ms     Changes:                 ----------              newfile:                  /opt/tools/nginx-1.6.3.tar.gz----------          ID: nginx-source-install    Function: cmd.run        Name: cd /opt/tools/ && tar -zxf nginx-1.6.3.tar.gz && mkdir -p /usr/local/nginx/tmp/{client,proxy,fcgi} && cd nginx-1.6.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/opt/tools/pcre-8.37 && make && make install && chown -R nginx:nginx /usr/local/nginx/      Result: None     Comment: Command "cd /opt/tools/ && tar -zxf nginx-1.6.3.tar.gz && mkdir -p /usr/local/nginx/tmp/{client,proxy,fcgi} && cd nginx-1.6.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/opt/tools/pcre-8.37 && make && make install && chown -R nginx:nginx /usr/local/nginx/" would have been executed     Started: 14:41:25.958142    Duration: 4.665 ms     Changes:   ----------          ID: nginx-init    Function: file.managed        Name: /etc/init.d/nginx      Result: None     Comment: The file /etc/init.d/nginx is set to be changed     Started: 14:41:25.963238    Duration: 3.005 ms     Changes:                 ----------              newfile:                  /etc/init.d/nginx----------          ID: nginx-init    Function: cmd.run        Name: chkconfig --add nginx      Result: None     Comment: Command "chkconfig --add nginx" would have been executed     Started: 14:41:25.966786    Duration: 293.959 ms     Changes:   ----------          ID: /usr/local/nginx/conf/nginx.conf    Function: file.managed      Result: None     Comment: The file /usr/local/nginx/conf/nginx.conf is set to be changed     Started: 14:41:26.261344    Duration: 2.984 ms     Changes:                 ----------              newfile:                  /usr/local/nginx/conf/nginx.conf----------          ID: nginx-service    Function: file.directory        Name: /usr/local/nginx/conf.d      Result: None     Comment: The following files will be changed:              /usr/local/nginx/conf.d: directory - new     Started: 14:41:26.264670    Duration: 0.371 ms     Changes:   ----------          ID: nginx-service    Function: service.running        Name: nginx      Result: None     Comment: Service is set to be started     Started: 14:41:26.286912    Duration: 53.579 ms     Changes:   Summary-------------Succeeded: 29 (unchanged=17, changed=4)Failed:     0-------------Total states run:     29

4)正式部署

[root@salt-master base]# salt 'salt-minion01.contoso.com' state.sls nginx.service

5)sls代码

目录结构如下:

[root@salt-master salt]# tree /srv/salt/base//srv/salt/base/├── nginx│   ├── files│   │   ├── nginx│   │   ├── nginx-1.6.3.tar.gz│   │   └── nginx.conf│   ├── install.sls│   └── service.sls└── pcre    ├── files    │   └── pcre-8.37.tar.gz    └── install.sls4 directories, 7 files

pcre install.sls代码:

[root@salt-master base]# cat pcre/install.sls pcre-source-install:  pkg.installed:    - names:      - gcc      - gcc-c++      - autoconf      - zlib      - zlib-devel      - glibc      - glibc-devel      - make      - cmake  file.managed:    - source: salt://pcre/files/pcre-8.37.tar.gz    - name: /opt/tools/pcre-8.37.tar.gz    - user: root    - group: root    - mode: 755    - makedirs: True    - dir_mode: 644  cmd.run:    - name: cd /opt/tools/ && tar -zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install     - unless: test -d /usr/local/pcre    - require:      - file: pcre-source-install

nginx install.sls代码:

[root@salt-master base]# cat nginx/install.sls include:  - pcre.installnginx-source-install:  pkg.installed:    - names:      - gcc      - gcc-c++      - autoconf      - automake      - zlib      - zlib-devel      - make      - openssl      - openssl-devel  group.present:    - name: nginx    - gid: 601  user.present:    - name: nginx    - fullname: nginx    - shell: /sbin/nologin    - uid: 601    - gid: 601  file.managed:    - source: salt://nginx/files/nginx-1.6.3.tar.gz    - name: /opt/tools/nginx-1.6.3.tar.gz    - user: root    - group: root    - mode: 755  cmd.run:    - name: cd /opt/tools/ && tar -zxf nginx-1.6.3.tar.gz && mkdir -p /usr/local/nginx/tmp/{client,proxy,fcgi} && cd nginx-1.6.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/opt/tools/pcre-8.37 && make && make install && chown -R nginx:nginx /usr/local/nginx/    - unless: test -e /usr/local/nginx/sbin/nginx    - require:      - file: nginx-source-install      - cmd: pcre-source-install

nginx service.sls代码:

[root@salt-master base]# cat nginx/service.sls include:  - nginx.installnginx-init:  file.managed:    - name: /etc/init.d/nginx    - source: salt://nginx/files/nginx    - user: root    - group: root    - mode: 755  cmd.run:    - name: chkconfig --add nginx    - unless: chkconfig --list|grep nginx    - require:       - file: nginx-init/usr/local/nginx/conf/nginx.conf:  file.managed:    - source: salt://nginx/files/nginx.conf    - user: nginx    - group: nginx    - mode: 644nginx-service:  file.directory:    - name: /usr/local/nginx/conf.d    - require:      - cmd: nginx-source-install  service.running:    - name: nginx    - enable: True    - reload: True    - require:      - cmd: nginx-init    - watch:      - file: /usr/local/nginx/conf/nginx.conf

3、部署结果验收

[root@salt-minion01 ~]# service nginx statusnginx (pid 11406 11405 11404 11403 11400) is running...[root@salt-minion01 ~]# ps -ef|grep nginxroot      11494      1  0 14:22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnginx     11496  11494  0 14:22 ?        00:00:00 nginx: worker process                                          nginx     11497  11494  0 14:22 ?        00:00:00 nginx: worker process                                          nginx     11498  11494  0 14:22 ?        00:00:00 nginx: worker process                                          nginx     11499  11494  0 14:22 ?        00:00:00 nginx: worker process                                          root      11503   1049  0 14:22 pts/0    00:00:00 grep nginx[root@salt-minion01 ~]# chkconfig --list|grep nginxnginx          0:off1:off2:on3:on4:on5:on6:off[root@salt-minion01 ~]# service nginx checkChecking config file: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@salt-minion01 ~]# service nginx reloadReloading nginx: [  OK  ][root@salt-minion01 ~]# service nginx restartStopping nginx: [  OK  ]Starting nginx: [  OK  ]


0