成都网站建设设计

将想法与焦点和您一起共享

利用saltstack批量安装php

saltstack编译安装php

作者信息:http://www.codegreen.cn

创新互联公司主要从事网站设计、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务永定,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

作者博客http://www.codegreen.cn

实验环境:

  • 环境规划

主机名:node1.enzhi.com IP地址:192.168.2.159 角色:salt-master

主机名:node2.enzhi.com IP地址:192.168.2.198 角色:salt-minion

  • 配置好时间同步及hosts文件实现主机名方式解析地址

安装salt-master:
[root@node1 ~]# yum -y install salt-master
# 配置salt-master
[root@node1 ~]# cd /etc/salt/
[root@node1 salt]# vim master
# The address of the interface to bind to:
interface: 0.0.0.0  #取消此行注释,监听在任意地址
user: root  #取消此行注释,以root身份去运行salt
file_roots: #找到file_roots配置salt.state模块的文件位置
  base:
    - /etc/salt/states
#保存退出
#启动salt-master
[root@node1 salt]# /etc/init.d/salt-master start
安装salt-minion:
[root@node2 ~]# yum -y install salt-minion
[root@node2 ~]# cd /etc/salt/
[root@node2 salt]# vim minion
# Set the location of the salt master server. If the master server cannot be
# resolved, then the minion will fail to start.
master: 192.168.2.159   #将salt改为salt-master端的IP地址
#保存退出
#启动salt-minion
[root@node2 salt]# /etc/init.d/salt-minion start
salt-master创建states目录:
[root@node1 salt]# mkdir /etc/salt/states
创建phppkg.sls安装php所依赖的软件包
[root@node1 salt]# cd /etc/salt/states/
[root@node1 states]# mkdir init
[root@node1 states]# cd init/
[root@node1 init]# vi phppkg.sls 
php5installed:  #自定义一个ID名称
  pkg.installed:    #使用pkg模块的installed方法,开头两个空格
    - names:        #-names声明有多个软件包每个软件包的名称写在下面,开头四个空格
      - openssl-devel   #开头6个空格下面其它的一样
      - libmcrypt
      - libmcrypt-devel 
      - bzip2 
      - bzip2-devel
      - php-mssql
      - zlib
      - libxml
      - libjpeg
      - freetype
      - libpng
      - gd
      - curl
      - libiconv
      - zlib-devel
      - libxml2-devel
      - libjpeg-devel
      - freetype-devel
      - libpng-devel
      - gd-devel
      - curl-devel
      - libxslt-devel
      - freetds
      - freetds-devel
创建phpinstall.sls安装php并提供源码包
[root@node1 init]# cd ../
[root@node1 states]# mkdir php5
[root@node1 states]# cd php5/
[root@node1 php5]# vim phpinstall.sls
include:
  - init.phppkg #使用include方法将phppkg.sls包含进来,作用就是先执行init下的phppkg.sls将依赖包安装上
#注意:minion端/home/wangenzhi/tools这个目录要事先存在否则复制不过去
phpinstalled:   #自定义一个ID
  file.managed: #使用file模块的managed方法
    - name: /home/wangenzhi/tools/php-5.6.21.tar.xz #指定salt-minion端要被管理的文件,如果文件不存在就执行下面source的方法将文件复制过去
    - source: salt://files/php-5.6.21.tar.xz
    - user: root        #指定文件复制过去后的属主
    - group: root
    - mode: 644
  cmd.run:  #使用cmd模块的run方法。可以执行任何命令
    - name: cd /home/wangenzhi/tools/ && tar xf php-5.6.21.tar.xz && cd php-5.6.21/ && ./configure --prefix=/usr/local/php --with-MySQL=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-zip --enable-soap --enable-short-tags --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization  --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --with-gd --enable-gd-native-ttf --enable-maintainer-zts && make && make install
    - unless: test -d /usr/local/php/ 
    # unless: 结果为True则不执行-name后面的命令,为false则执行
创建phpconfig.sls为php提供配置文件并启动服务
[root@node1 php5]# cd /etc/salt/states/php5/
[root@node1 php5]# vim phpconfig.sls 
#如果phpinstall执行成功则执行phpconfig.sls
include:
  - php5.phpinstall
#为minion端提供php.ini配置文件
phpini:
  file.managed:
    - name: /etc/php.ini
    - source: salt://files/php.ini
    - user: root
    - group: root
    - mode: 644
#为minion端提供php-fpm.conf配置文件
phpfpmconf:
  file.managed:
    - name: /usr/local/php/etc/php-fpm.conf
    - source: salt://files/php-fpm.conf
    - user: root
    - group: root
    - mode: 644
#为minion端提供php-fpm启动脚本
php-fpm:
  file.managed:
    - name: /etc/rc.d/init.d/php-fpm
    - source: salt://files/php-fpm
    - user: root
    - group: root
    - mode: 755
  cmd.run:  #并添加为系统服务
    - name: chkconfig --add php-fpm && chkconfig php-fpm on
    - unless: chkconfig --list | grep php-fpm
#启动php-fpm服务
php-service:
  cmd.run:
    - name: /etc/init.d/php-fpm restart
    - request:
       phpini
# - request 意思是如果上面的phpini执行成功了则执行- name后面的命令去重启php-fpm
执行测试:
#注意:执行过程中可能会出现某个依赖包yum找不到的情况
[root@node1 php5]# salt 'node2.enzhi.com' state.sls php5.phpconfig
node2.enzhi.com:
----------
  ID: php5installed
Function: pkg.installed
Name: freetype-devel
  Result: True
 Comment: Package freetype-devel is already installed.
 Started: 17:40:51.398777
Duration: 866.709 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: freetds-devel
  Result: True
 Comment: Package freetds-devel is already installed.
 Started: 17:40:52.265671
Duration: 0.596 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libxml
  Result: False
 Comment: The following package(s) were not found, and no possible matches were found in the package db: libxml
 Started: 17:40:52.266356
Duration: 4081.753 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: gd
  Result: True
 Comment: Package gd is already installed.
 Started: 17:40:56.348335
Duration: 0.776 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: curl
  Result: True
 Comment: Package curl is already installed.
 Started: 17:40:56.349210
Duration: 0.423 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: bzip2
  Result: True
 Comment: Package bzip2 is already installed.
 Started: 17:40:56.349721
Duration: 0.429 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: bzip2-devel
  Result: True
 Comment: Package bzip2-devel is already installed.
 Started: 17:40:56.350236
Duration: 0.451 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: openssl-devel
  Result: True
 Comment: Package openssl-devel is already installed.
 Started: 17:40:56.350774
Duration: 0.423 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libpng
  Result: True
 Comment: Package libpng is already installed.
 Started: 17:40:56.351277
Duration: 0.422 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: freetds
  Result: True
 Comment: Package freetds is already installed.
 Started: 17:40:56.351788
Duration: 0.418 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libmcrypt-devel
  Result: True
 Comment: Package libmcrypt-devel is already installed.
 Started: 17:40:56.352298
Duration: 0.449 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: freetype
  Result: True
 Comment: Package freetype is already installed.
 Started: 17:40:56.352828
Duration: 0.629 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: gd-devel
  Result: True
 Comment: Package gd-devel is already installed.
 Started: 17:40:56.353584
Duration: 0.423 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libxslt-devel
  Result: True
 Comment: Package libxslt-devel is already installed.
 Started: 17:40:56.354170
Duration: 0.442 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: curl-devel
  Result: False
 Comment: Package 'curl-devel' not found (possible matches: libcurl-devel, libcurl-devel.i686)
 Started: 17:40:56.354712
Duration: 3452.342 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: zlib-devel
  Result: True
 Comment: Package zlib-devel is already installed.
 Started: 17:40:59.807254
Duration: 0.687 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libjpeg-devel
  Result: False
 Comment: Package 'libjpeg-devel' not found (possible matches: libjpeg-turbo-devel, libjpeg-turbo-devel.i686)
 Started: 17:40:59.808032
Duration: 816.712 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libjpeg
  Result: False
 Comment: Package 'libjpeg' not found (possible matches: libjpeg-turbo, libjpeg-turbo.i686)
 Started: 17:41:00.624993
Duration: 1262.685 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libxml2-devel
  Result: True
 Comment: Package libxml2-devel is already installed.
 Started: 17:41:01.887927
Duration: 0.662 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: zlib
  Result: True
 Comment: Package zlib is already installed.
 Started: 17:41:01.888682
Duration: 0.532 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libiconv
  Result: False
 Comment: The following package(s) were not found, and no possible matches were found in the package db: libiconv
 Started: 17:41:01.889304
Duration: 945.379 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libmcrypt
  Result: True
 Comment: Package libmcrypt is already installed.
 Started: 17:41:02.834894
Duration: 0.755 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: php-mssql
  Result: True
 Comment: Package php-mssql is already installed.
 Started: 17:41:02.835757
Duration: 0.437 ms
 Changes:   
----------
  ID: php5installed
Function: pkg.installed
Name: libpng-devel
  Result: True
 Comment: Package libpng-devel is already installed.
 Started: 17:41:02.836280
Duration: 0.425 ms
 Changes:   
----------
  ID: phpinstalled
Function: file.managed
Name: /home/wangenzhi/tools/php-5.6.21.tar.xz
  Result: True
 Comment: File /home/wangenzhi/tools/php-5.6.21.tar.xz is in the correct state
 Started: 17:41:02.839835
Duration: 72.475 ms
 Changes:   
----------
  ID: phpinstalled
Function: cmd.run
Name: cd /home/wangenzhi/tools/ && tar xf php-5.6.21.tar.xz && cd php-5.6.21/ && ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-zip --enable-soap --enable-short-tags --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization  --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --with-gd --enable-gd-native-ttf --enable-maintainer-zts && make && make install
  Result: True
 Comment: unless execution succeeded
 Started: 17:41:02.913239
Duration: 7.166 ms
 Changes:   
----------
  ID: phpini
Function: file.managed
Name: /etc/php.ini
  Result: True
 Comment: File /etc/php.ini updated
 Started: 17:41:02.921572
Duration: 27.204 ms
 Changes:   
  ----------
  diff:
  ---  
  +++  
  @@ -5,10 +5,6 @@
   ;;;;;;;;;;;;;;;;;;;
   ; PHP's initialization file, generally called php.ini, is responsible for
   ; configuring many of the aspects of PHP's behavior.
  -; hehe
  -; hehe
  -; haha
  -; hehe

   ; PHP attempts to find and load this configuration from a number of locations.
   ; The following is a summary of its search order:
----------
  ID: phpfpmconf
Function: file.managed
Name: /usr/local/php/etc/php-fpm.conf
  Result: True
 Comment: File /usr/local/php/etc/php-fpm.conf is in the correct state
 Started: 17:41:02.948910
Duration: 3.481 ms
 Changes:   
----------
  ID: php-fpm
Function: file.managed
Name: /etc/rc.d/init.d/php-fpm
  Result: True
 Comment: File /etc/rc.d/init.d/php-fpm is in the correct state
 Started: 17:41:02.952524
Duration: 4.373 ms
 Changes:   
----------
  ID: php-fpm
Function: cmd.run
Name: chkconfig --add php-fpm && chkconfig php-fpm on
  Result: True
 Comment: unless execution succeeded
 Started: 17:41:02.957028
Duration: 25.414 ms
 Changes:   
----------
  ID: php-service
Function: cmd.run
Name: /etc/init.d/php-fpm restart
  Result: True
 Comment: Command "/etc/init.d/php-fpm restart" run
 Started: 17:41:02.982731
Duration: 1075.021 ms
 Changes:   
  ----------
  pid:
  73614
  retcode:
  0
  stderr:
  stdout:
  Gracefully shutting down php-fpm . done   #可以看到重启php-fpm
  Starting php-fpm  done

Summary
-------------
Succeeded: 26 (changed=2)
Failed: 5
-------------
Total states run: 31
#在minion端查看php-fpm端口是否在监听
[root@node2 ~]# ss -tnl
LISTEN      0      16384                            127.0.0.1:9000

本文标题:利用saltstack批量安装php
分享地址:http://chengdu.cdxwcx.cn/article/jcopch.html