使用saltstack编译安装LNMP环境
一,系统版本查看
二,安装salt-master和salt-minion
安装配置过程参考SaltStack概述及安装
三,修改配置文件
/etc/salt/master设置根的两个目录
四,设置Nginx安装文档
目录结构为
其中files文件夹下面放置的是源码包 install.sls是安装配置文件
install.sls
include: - pkg.make nginx: user.present: - uid: 1001 nginx-install: file.managed: - name: /root/tools/nginx-1.6.3.tar.gz - source: salt://nginx/files/nginx-1.6.3.tar.gz - mode: 755 - user: root - group: root cmd.run: - name: cd /root/tools/ && tar -xf nginx-1.6.3.tar.gz && cd nginx-1.6.3 && ./configure --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.3 && make && make install && ln -s /usr/local/nginx-1.6.3/ /usr/local/nginx - require: - pkg: make-pkg - file: nginx-install
其中user.present为用户模块,创建一个uid为1001的nginx用户 cmd.run执行安装命令 pkg文件夹下面的make.sls放置本次安装所需要的所有yum包 require依赖这两个
make.sls文件内容为
make-pkg: pkg.installed: - pkgs: - gcc - pcre - pcre-devel - openssl-devel - numactl - libaio - net-tools - freetype-devel - libjpeg-turbo-devel - libpng-devel - gd-devel - libcurl-devel - libxslt-devel
执行安装
salt 'web1.example.com' state.sls nginx.install saltenv=prod
默认没有加saltenv参数执行的是base环境下的配置,本次需要执行prod生产配置所以执行加参数saltenv=prod
五,设置MySQL安装文档
MySQL的install.sls文档内容为
include: - pkg.make mysql-install: file.managed: - name: /root/tools/mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar - source: salt://mysql/files/mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar - mode: 755 - user: root - group: root cmd.run: - name: cd /root/tools/ && yum remove mysql-libs -y && tar -xf mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar && rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm && rpm -ivh mysql-community-libs-5.7.20-1.el7.x86_64.rpm && rpm -ivh mysql-community-client-5.7.20-1.el7.x86_64.rpm && rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm - require: - pkg: make-pkg - file: mysql-install
执行安装
salt 'web1.example.com' state.sls mysql.install saltenv=prod
六,设置php安装文档
install.sls
include: - pkg.make php-install: file.managed: - name: /root/tools/php-7.0.27.tar.gz - source: salt://php/files/php-7.0.27.tar.gz - mode: 755 - user: root - group: root cmd.run: - name: cd /root/tools/ && tar -xf php-7.0.27.tar.gz && cd php-7.0.27 && ./configure --prefix=/usr/local/php7.0.27 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/ --enable-xml --disable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp --enable-xml && make && make test && make install && ln -s /usr/local/php7.0.27/ /usr/local/php && /usr/local/php/sbin/php-fpm - unless: test -L /usr/local/php - require: - pkg: make-pkg - file: php-install - file: php-config - file: php-fpm-config - file: php-www-config php-mysql-install: file.managed: - name: /root/tools/mysql-45881bd.tar.gz - source: salt://php/files/mysql-45881bd.tar.gz - mode: 755 - user: root - group: root cmd.run: - name: cd /root/tools/ && tar -xf mysql-45881bd.tar.gz && cd mysql-45881bd && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysql=mysqlnd && make && make install && pkill php-fpm && /usr/local/php/sbin/php-fpm - require: - pkg: make-pkg - file: php-mysql-install - file: php-config - file: php-fpm-config - file: php-www-config php-config: file.managed: - name: /usr/local/php/lib/php.ini - source: salt://php/files/php.ini - user: root - group: root - mode: 644 php-fpm-config: file.managed: - name: /usr/local/php/etc/php-fpm.conf - source: salt://php/files/php-fpm.conf - user: root - group: root - mode: 644 php-www-config: file.managed: - name: /usr/local/php/etc/php-fpm.d/www.conf - source: salt://php/files/www.conf - user: root - group: root - mode: 644 php-mysql-test: file.managed: - name: /usr/local/nginx/html/mysql.php - source: salt://php/files/mysql.php - user: root - group: root - mode: 644 php-test: file.managed: - name: /usr/local/nginx/html/test.php - source: salt://php/files/test.php - user: root - group: root - mode: 644
其中test.php的内容为
<?php phpinfo() ?
mysql.php的内容为
<?php $mysqli = new mysqli("localhost", "root", "Mysql123456!"); if(!$mysqli) { echo"database error"; }else{ echo"MySQL successful"; } $mysqli->close(); ?>
php-fpm.conf配置文档的源文件为/usr/local/php/etc/php-fpm.conf.default
www.conf配置文档的源文件为/usr/local/php/etc/php-fpm.d/www.conf.default
php.ini配置文件的源文件为/root/tools/php-7.0.27/php.ini-production
安装的源码包可以下载到本地电脑上再上传至对应的目录
执行安装(源码安装php时间较长)
salt 'web1.example.com' state.sls php.install saltenv=prod
测试