为了账号安全,请及时绑定邮箱和手机立即绑定

利用saltstack批量安装php

标签:
PHP

实验环境:

  • 环境规划

主机名: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身份去运行saltfile_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.slsinclude:  - 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.slsinclude:  - 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.phpconfignode2.enzhi.com:----------  ID: php5installedFunction: pkg.installedName: freetype-devel  Result: True Comment: Package freetype-devel is already installed. Started: 17:40:51.398777Duration: 866.709 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: freetds-devel  Result: True Comment: Package freetds-devel is already installed. Started: 17:40:52.265671Duration: 0.596 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: 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.266356Duration: 4081.753 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: gd  Result: True Comment: Package gd is already installed. Started: 17:40:56.348335Duration: 0.776 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: curl  Result: True Comment: Package curl is already installed. Started: 17:40:56.349210Duration: 0.423 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: bzip2  Result: True Comment: Package bzip2 is already installed. Started: 17:40:56.349721Duration: 0.429 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: bzip2-devel  Result: True Comment: Package bzip2-devel is already installed. Started: 17:40:56.350236Duration: 0.451 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: openssl-devel  Result: True Comment: Package openssl-devel is already installed. Started: 17:40:56.350774Duration: 0.423 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libpng  Result: True Comment: Package libpng is already installed. Started: 17:40:56.351277Duration: 0.422 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: freetds  Result: True Comment: Package freetds is already installed. Started: 17:40:56.351788Duration: 0.418 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libmcrypt-devel  Result: True Comment: Package libmcrypt-devel is already installed. Started: 17:40:56.352298Duration: 0.449 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: freetype  Result: True Comment: Package freetype is already installed. Started: 17:40:56.352828Duration: 0.629 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: gd-devel  Result: True Comment: Package gd-devel is already installed. Started: 17:40:56.353584Duration: 0.423 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libxslt-devel  Result: True Comment: Package libxslt-devel is already installed. Started: 17:40:56.354170Duration: 0.442 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: curl-devel  Result: False Comment: Package 'curl-devel' not found (possible matches: libcurl-devel, libcurl-devel.i686) Started: 17:40:56.354712Duration: 3452.342 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: zlib-devel  Result: True Comment: Package zlib-devel is already installed. Started: 17:40:59.807254Duration: 0.687 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libjpeg-devel  Result: False Comment: Package 'libjpeg-devel' not found (possible matches: libjpeg-turbo-devel, libjpeg-turbo-devel.i686) Started: 17:40:59.808032Duration: 816.712 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libjpeg  Result: False Comment: Package 'libjpeg' not found (possible matches: libjpeg-turbo, libjpeg-turbo.i686) Started: 17:41:00.624993Duration: 1262.685 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libxml2-devel  Result: True Comment: Package libxml2-devel is already installed. Started: 17:41:01.887927Duration: 0.662 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: zlib  Result: True Comment: Package zlib is already installed. Started: 17:41:01.888682Duration: 0.532 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: 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.889304Duration: 945.379 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libmcrypt  Result: True Comment: Package libmcrypt is already installed. Started: 17:41:02.834894Duration: 0.755 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: php-mssql  Result: True Comment: Package php-mssql is already installed. Started: 17:41:02.835757Duration: 0.437 ms Changes:   ----------  ID: php5installedFunction: pkg.installedName: libpng-devel  Result: True Comment: Package libpng-devel is already installed. Started: 17:41:02.836280Duration: 0.425 ms Changes:   ----------  ID: phpinstalledFunction: file.managedName: /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.839835Duration: 72.475 ms Changes:   ----------  ID: phpinstalledFunction: cmd.runName: 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.913239Duration: 7.166 ms Changes:   ----------  ID: phpiniFunction: file.managedName: /etc/php.ini  Result: True Comment: File /etc/php.ini updated Started: 17:41:02.921572Duration: 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: phpfpmconfFunction: file.managedName: /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.948910Duration: 3.481 ms Changes:   ----------  ID: php-fpmFunction: file.managedName: /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.952524Duration: 4.373 ms Changes:   ----------  ID: php-fpmFunction: cmd.runName: chkconfig --add php-fpm && chkconfig php-fpm on  Result: True Comment: unless execution succeeded Started: 17:41:02.957028Duration: 25.414 ms Changes:   ----------  ID: php-serviceFunction: cmd.runName: /etc/init.d/php-fpm restart  Result: True Comment: Command "/etc/init.d/php-fpm restart" run Started: 17:41:02.982731Duration: 1075.021 ms Changes:     ----------  pid:  73614  retcode:  0  stderr:  stdout:  Gracefully shutting down php-fpm . done   #可以看到重启php-fpm  Starting php-fpm  doneSummary-------------Succeeded: 26 (changed=2)Failed: 5-------------Total states run: 31#在minion端查看php-fpm端口是否在监听[root@node2 ~]# ss -tnlLISTEN      0      16384                            127.0.0.1:9000


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消