西数超哥博客
运维经验教程分享

[原创]Saltstack 配置管理学习笔记:配置nginx+fastcgi 环境以及设置文件同步

学了这么久的Saltstack,需要做一个实战操作练练手了,本次文章就是写了一个关于通过使用saltstack批量部署服务器nginx+fastcgi环境。说明下,其中的prce、nginx以及php都是通过官网下载的,需要对应版本的朋友可以自己到官网下载需要的版本。
1,写状态脚本sls。
1.1安装依赖包。由于安装这个环境,需要很多的依赖包,我们这里先通过单独设置一个声明ID去安装这些依赖模块。具体init下的依赖模块sls文件(里边的pkg对应centos 是执行的yum)如下:

pkg-init:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - glibc
      - make
      - autoconf
      - libjpeg-turbo
      - libjpeg-turbo-devel
      - libpng
      - libpng-devel
      - freetype
      - freetype-devel
      - libxml2
      - libxml2-devel
      - zlib
      - zlib-devel
      - libcurl
      - libcurl-devel
      - openssl
      - openssl-devel
      - swig
      - mysql
      - mysql-devel

1.2安装nginx。由于安装nginx,我们需要pcre安装,其他采用默认。传统的yum install pcre* 安装sls脚本执行出现问题,没法,采用源码安装搞一次。具体pcre的sls脚本如下:

include:
  - init.install
pcre-source-install:
  file.managed:
    - name: /usr/local/src/pcre-8.36.tar.gz
    - source: salt://pcre/files/pcre-8.36.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src  && tar zxf pcre-8.36.tar.gz &&  cd pcre-8.36 && ./configure --prefix=/usr/local/pcre && make && make install
    - unless: test -d /usr/local/pcre
    - require:
      - file:  pcre-source-install
      - pkg: pkg-init

注意这里加了一个unless,是只有test -d /usr/local/pcre这个目录不存在的时候我才去执行这个命令cmd.run命令,如果已经存在则不执行。如下是nginx.sls:

include:
  - init.install
  - pcre.install
nginx-source-install:
  file.managed:
    - name: /usr/local/src/nginx-1.9.12.tar.gz
    - source: salt://nginx/files/nginx-1.9.12.tar.gz
    - user: root
    - group: root
    - mode: 644
  cmd.run:
    - name:  cd /usr/local/src && tar zxf nginx-1.9.12.tar.gz  && cd nginx-1.9.12 && ./configure --prefix=/usr/local/nginx  --user=www --group=www --with-http_ssl_module  --with-pcre=/usr/local/src/pcre-8.36  && make &&; make install
    - unless: test -d  /usr/local/nginx
    - require:
      - pkg: pkg-init
      - cmd: pcre-source-install
nginx-init:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx-init
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: chkconfig --add nginx
    - unless: chkconfig  --list | grep nginx
    - require:
      - file: nginx-init
nginx-conf:
  file.managed:
    - name: /usr/local/nginx/conf/nginx.conf
    - source: salt://nginx/files/nginx.conf
    - user: root
    - group: root
    - mode: 644
nginx-service:
  file.directory:
    - name: /usr/local/nginx/conf/vhost
    - require:
      - cmd: nginx-source-install
  service.running:
    - name: nginx
    - enable: True
    - reload: True
    - require:
      - cmd: nginx-source-install
      - cmd: nginx-init
    - watch:
      - file: nginx-conf

1.3 编写安装fastcgi,其中sls脚本如下,注意编译之前,对编译安装的依赖、数据同步、编译安装、配置文件、服务脚本以及服务启动,每个环节都要考虑周到,不然以后容易出很多问题,可以实现编译执行的时候执行test测试,也可以salt单独执行这个sls执行测试通过后进行集成。。当然这里的扩展并不一定是最优的,具体依赖自己的参数进行优化即可。

php-source-install:
  file.managed:
    - name: /usr/local/src/php-5.6.19.tar.gz
    - source: salt://php/files/php-5.6.19.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar zxf php-5.6.19.tar.gz && cd php-5.6.19 &&  ./configure --prefix=/usr/local/php-fastcgi --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-libxml-dir --with-curl --enable-bcmath --enable-shmop --enable-sysvsem  --enable-inline-optimization --enable-mbregex --with-openssl --enable-mbstring --with-gd --enable-gd-native-ttf --with-freetype-dir  --with-gettext  --enable-sockets --with-xmlrpc --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --with-config-file-path=/usr/local/php-fastcgi/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --disable-fileinfo  && make && make install
    - require:
      - file: php-source-install
    - unless: test -d /usr/local/php-fastcgi

pdo-plugin:
  cmd.run:
    - name: cd /usr/local/src/php-5.6.19/ext/pdo_mysql/ && /usr/local/php-fastcgi/bin/phpize && ./configure --with-php-config=/usr/local/php-fastcgi/bin/php-config &&  make && make install
    - unless: test -f /usr/local/php-fastcgi/lib/php/extensions/*/pdo_mysql.so
    - require:
      - cmd: php-source-install
php-ini:
  file.managed:
    - name: /usr/local/php-fastcgi/etc/php.ini
    - source: salt://php/files/php.ini-production
    - user: root
    - group: root
    - mode: 644
    - require:
      - cmd: php-source-install
php-fpm:
  file.managed:
    - name: /usr/local/php-fastcgi/etc/php-fpm.conf
    - source: salt://php/files/php-fpm.conf.default
    - user: root
    - group: root
    - mode: 644
    - require:
      - cmd: php-source-install

php-fastcgi-service:
  file.managed:
    - name: /etc/init.d/php-fpm
    - source: salt://php/files/init.d.php-fpm
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: chkconfig --add php-fpm
    - unless: chkconfig  --list | grep php-fpm
    - require:
      - file: php-fastcgi-service
  service.running:
    - name: php-fpm
    - enable: True
    - require:
      - cmd: php-fastcgi-service
    - watch:
      - file: php-ini

2,Master选中minion执行。 选中要操作的minion主机执行这个脚本。注意,由于这个脚本是写到了/srv/salt/test环境下的,需要执行的时候要加env=test这个参数方可。
命令为:salt-ssh  ‘HK-VPN’ state.highstate  env=test
等待执行。由于涉及一些数据同步、编译,时间可能比较久,需要耐心等待。我这个虚拟机执行这个操作,花了一个小时。
 

转载请注明:西数超哥博客www.ysidc.top» [原创]Saltstack 配置管理学习笔记:配置nginx+fastcgi 环境以及设置文件同步

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0)
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥博客 » [原创]Saltstack 配置管理学习笔记:配置nginx+fastcgi 环境以及设置文件同步