对于通过saltstack自动化安装LNMP环境,请先参阅
Saltstack自动化源码编译安装Nginx
Saltstack自动化源码编译安装mysql
本文将讲述如何通过saltstack来实现PHP的自动化源码安装,以及将LNMP中的各部分整合起来通过saltstack一键安装配置
主机环境:
master 192.168.199.130 rhel7.2
minion 192.168.199.66 rhel6.5
所有配置均在master端进行
本次实验PHP源码包下载http://br2.php.net/get/php-5.6.31.tar.gz/from/this/mirror
[root@lockey151 ~]# cat /srv/salt/pkg/lnmp-pre.sls
pkg-init:
pkg.installed:
- pkgs:
- gcc-c++
- zlib-devel
- libxml2-devel
- openssl-devel
- pcre-devel
- ncurses-devel
- bison
[root@lockey151 ~]# cat /srv/salt/php/install.sls
include:
- pkg.lnmp-pre
php-source-install:
file.managed:
- name: /mnt/php-5.6.31.tar.gz
- source: salt://php/files/php-5.6.31.tar.gz
cmd.run:
- name: cd /mnt && tar zxf php-5.6.31.tar.gz && cd php-5.6.31 && ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fpm && make && make install && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && /usr/local/php/sbin/php-fpm
- creates: /usr/local/php
[root@lockey151 php]# salt rhel6-vm2 state.sls php.install test=true
[root@lockey151 php]# salt rhel6-vm2 state.sls php.install
在minion端查看安装进程:
注意为了使得Nginx支持PHP,需要取消/usr/local/nginx/conf/nginx.conf中的以下注释行
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
到这里Nginx、MySQL、PHP的单独自动化编译安装完成,接下来进行整合:
新建一个lnmp的目录,目录结构如下:
[root@lockey151 lnmp]# tree
.
├── cmake.sls
├── files
│ ├── cmake-2.8.10.2.tar.gz
│ ├── my.cnf
│ ├── mysql-boost-5.7.19.tar.gz
│ ├── mysqld
│ ├── nginx
│ ├── nginx-1.12.1.tar.gz
│ ├── nginx.conf
│ └── php-5.6.31.tar.gz
├── mysqlinstall.sls
├── nginxinstall.sls
├── phpinstall.sls
└── service.sls
1 directory, 13 files
[root@lockey151 lnmp]# pwd
/srv/salt/lnmp
各配置文件内容如下:
[root@lockey151 lnmp]# cat cmake.sls
cmake-install:
file.managed:
- name: /mnt/cmake-2.8.10.2.tar.gz
- source: salt://lnmp/files/cmake-2.8.10.2.tar.gz
cmd.run:
- name: cd /mnt && tar -xzvf cmake-2.8.10.2.tar.gz && cd /mnt/cmake-2.8.10.2 && ./configure && make && make install
- creates: /mnt/cmake-2.8.10.2
[root@lockey151 lnmp]# cat mysqlinstall.sls
include:
- lnmp.cmake
mysql-source-install:
file.managed:
- name: /mnt/mysql-boost-5.7.19.tar.gz
- source: salt://lnmp/files/mysql-boost-5.7.19.tar.gz
cmd.run:
- name: cd /mnt && tar zxf mysql-boost-5.7.19.tar.gz && cd mysql-5.7.19 && /usr/local/bin/cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=./boost/boost_1_59_0 -DSYSCONFDIR=/etc -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci && make && make install && cd /usr/local/mysql && mkdir data && chown -R mysql:mysql .
- creates: /usr/local/mysql
[root@lockey151 lnmp]# cat nginxinstall.sls
include:
- pkg.lnmp-pre
nginx-source-install:
file.managed:
- name: /mnt/nginx-1.12.1.tar.gz
- source: salt://lnmp/files/nginx-1.12.1.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-1.12.1.tar.gz && cd nginx-1.12.1 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
- creates: /usr/local/nginx
[root@lockey151 lnmp]# cat phpinstall.sls
php-source-install:
file.managed:
- name: /mnt/php-5.6.31.tar.gz
- source: salt://lnmp/files/php-5.6.31.tar.gz
cmd.run:
- name: cd /mnt && tar zxf php-5.6.31.tar.gz && cd php-5.6.31 && ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fpm && make && make install
- creates: /usr/local/php
最终需要执行的整体安装文件如下
[root@lockey151 lnmp]# cat service.sls
include:
- user.users
- lnmp.nginxinstall
- lnmp.mysqlinstall
- lnmp.phpinstall
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://lnmp/files/nginx.conf
- mode: 644
/etc/init.d/nginx:
file.managed:
- source: salt://lnmp/files/nginx
- mode: 755
nginx-service:
service.running:
- name: nginx
- enable: true
- reload: true
- require:
- file: /etc/init.d/nginx
- watch:
- file: /usr/local/nginx/conf/nginx.conf
/etc/my.cnf:
file.managed:
- source: salt://lnmp/files/my.cnf
- mode: 644
/etc/init.d/mysqld :
file.managed:
- source: salt://lnmp/files/mysqld
- mode: 755
mysql-service:
service.running:
- name: mysql
- enable: true
- reload: true
- require:
- file: /etc/init.d/mysqld
- watch:
- file: /etc/my.cnf
[root@lockey151 lnmp]# salt rhel6-vm2 state.sls lnmp.service test=true
rhel6-vm2:
----------
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: False
Comment: The named service nginx is not available
Started: 20:09:10.570342
Duration: 11.46 ms
Changes:
Summary for rhel6-vm2
-------------
Succeeded: 17 (unchanged=11, changed=2)
Failed: 1
-------------
Total states run: 18
Total run time: 1.190 s
ERROR: Minions returned with non-zero exit code
根据测试的返回结果,nginx服务部分无法启动,这个是因为启动脚本还没有发送过去导致的,整体来说测试是成功的,接下来可以直接进行推送安装了:
[root@lockey151 lnmp]# salt rhel6-vm2 state.sls lnmp.service
如果安装过程中有错误可以对应解决