saltstack部署lnmp()
变量目录
[root@master ~]# cd /srv
[root@master srv]# ls
pillar salt
[root@master srv]# tree pillar/
pillar/
├── base
│ ├── apache.sls
│ └── top.sls
└── prod
├── mysql.sls
├── nginx.sls
├── php.sls
└── top.sls
2 directories, 6 files
[root@master srv]#
变量查看
[root@master srv]# cat pillar/prod/mysql.sls
mysql_installdir: /usr/local
mysql_password: 123456
[root@master srv]# cat pillar/prod/nginx.sls
nginx_installdir: /usr/local
[root@master srv]# cat pillar/prod/php.sls
php_installdir: /usr/local
[root@master srv]# cat pillar/prod/top.sls
prod:
'minion':
- nginx
- mysql
- php
[root@master srv]#
文件目录
[root@master prod]# tree modules/
modules/
├── application
│ └── php
│ ├── files
│ │ ├── index.php
│ │ ├── install.sh
│ │ ├── oniguruma-devel-6.8.2-2.el8.x86_64.rpm
│ │ ├── php-8.0.10.tar.gz
│ │ ├── php-fpm
│ │ ├── php-fpm.conf
│ │ ├── php-fpm.service
│ │ └── www.conf
│ └── install.sls
├── database
│ └── mysql
│ ├── files
│ │ ├── install.sh
│ │ ├── mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
│ │ ├── mysqld.service.j2
│ │ └── mysql.server
│ └── install.sls
└── web
├── httpd //忽略HTTP,因为lnmp只需要nginx
│ ├── files
│ │ ├── apr-1.7.0.tar.gz
│ │ ├── apr-util-1.6.1.tar.gz
│ │ ├── httpd-2.4.49.tar.gz
│ │ ├── httpd.conf
│ │ ├── httpd.service
│ │ └── install.sh
│ └── install.sls
└── nginx
├── files
│ ├── install.sh
│ ├── nginx-1.20.1.tar.gz
│ ├── nginx.conf
│ └── nginx.service.j2
└── install.sls
11 directories, 26 files
[root@master prod]#
nginx
[root@master prod]# cd modules/web/nginx/
[root@master nginx]# ls
files install.sls
[root@master nginx]# cat install.sls
nginx-dep-package:
pkg.installed:
- pkgs:
- pcre-devel
- openssl
- openssl-devel
- gd-devel
- gcc
- gcc-c++
- wget
- make
- ncurses-compat-libs
nginx:
user.present:
- shell: /sbin/nologin
- createhome: false
- system: true
/usr/src/nginx-1.20.1.tar.gz:
file.managed:
- source: salt://modules/web/nginx/files/nginx-1.20.1.tar.gz
nginx-installsh:
cmd.script:
- name: salt://modules/web/nginx/files/install.sh {{ pillar['nginx_installdir'] }}
- unless: test -d {{ pillar['nginx_installdir'] }}/nginx/
/usr/lib/systemd/system/nginx.service:
file.managed:
- source: salt://modules/web/nginx/files/nginx.service.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://modules/web/nginx/files/nginx.conf
systemctl daemon-reload:
cmd.run
[root@master nginx]#
进入files查看里面得文件内容
[root@master nginx]# cd files/
[root@master files]# ls
install.sh nginx-1.20.1.tar.gz nginx.conf nginx.service.j2
[root@master files]# cat install.sh
#!/bin/bash
cd /usr/src/
tar xf nginx-1.20.1.tar.gz
cd /usr/src/nginx-1.20.1/
./configure \
--prefix=$1/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log && \
make && make install
[root@master files]#
[root@master files]# cat nginx.service.j2
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart={{ pillar['nginx_installdir'] }}/nginx/sbin/nginx
ExecStop={{ pillar['nginx_installdir'] }}/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@master files]#
[root@lnmp ~]# cat nginx.conf //在配置文件里面修改一下得配置
location ~ \.php$ { //取消掉这里所有得注释
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //这里改成 $document_root
include fastcgi_params;
}
location / {
root html;
index index.php index.html index.htm; //这里加上index.php
}
mysql
查看状态文件
[root@master prod]# cd modules/database/mysql/
[root@master mysql]# ls
files install.sls
[root@master mysql]# cat install.sls
ncurses-compat-libs:
pkg.installed
mysql:
user.present:
- shell: /sbin/nologin
- createhome: false
- system: true
create-data:
file.directory:
- name: /opt/data
- user: mysql
- group: mysql
- mode: '0755'
- makedirs: true
/usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz:
file.managed:
- source: salt://modules/database/mysql/files/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
- user: root
- group: root
- mode: '0644'
mysql-installsh:
cmd.script:
- name: salt://modules/database/mysql/files/install.sh {{ pillar['mysql_installdir'] }}
- unless: test -d {{ pillar['mysql_installdir'] }}/mysql
trasfer-files:
file.managed:
- names:
- {{ pillar['mysql_installdir'] }}/mysql/support-files/mysql.server:
- source: salt://modules/database/mysql/files/mysql.server
- /usr/lib/systemd/system/mysqld.service:
- source: salt://modules/database/mysql/files/mysqld.service.j2
[root@master mysql]#
进入files里面去查看脚本和配置文件
[root@master mysql]# cd files/
[root@master files]#
[root@master files]# ls
install.sh mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz mysqld.service.j2 mysql.server
[root@master files]# cat install.sh
#!/bin/bash
cd /usr/src
rm -rf mysql-5.7.34-linux-glibc2.12-x86_64
tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local
ln -s /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64 $1/mysql
chown -R mysql.mysql /usr/local/mysql*
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
[root@master files]# cat mysqld.service.j2
[Unit]
Description=mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
Execstop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@master files]#
[root@minion ~]# cd /usr/local/mysql/support-files/
[root@minion support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@minion support-files]#
注意:mysql.server这个配置文件我们可以直接在上面这个目录下面用scp复制过去,不需要做任何更改
php
[root@master prod]# cd modules/application/php/
[root@master php]# ls
files install.sls
[root@master php]# cat install.sls
/usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm:
file.managed:
- source: salt://modules/application/php/files/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- user: root
- group: root
- mode: '0644'
cmd.run:
- name: yum -y install /usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- unless: yum -y provides oniguruma-devel
yum -y install epel-release:
cmd.run
php-dep-package:
pkg.installed:
- pkgs:
- sqlite-devel
- libzip-devel
- libxml2
- libxml2-devel
- openssl
- openssl-devel
- bzip2
- bzip2-devel
- libcurl
- libcurl-devel
- libicu-devel
- libjpeg-turbo
- libjpeg-turbo-devel
- libpng
- libpng-devel
- openldap-devel
- pcre-devel
- freetype
- freetype-devel
- gmp
- gmp-devel
- readline
- readline-devel
- libxslt
- libxslt-devel
- libzip-devel
- libsqlite3x-devel
/usr/src/php-8.0.10.tar.gz:
file.managed:
- source: salt://modules/application/php/files/php-8.0.10.tar.gz
- user: root
- group: root
- mode: '0644'
php-install.sh:
cmd.script:
- name: salt://modules/application/php/files/install.sh {{ pillar['php_installdir'] }}
- unless: test -d {{ pillar['php_installdir'] }}/php8
cp-files:
file.managed:
- names:
- /etc/init.d/php-fpm:
- source: salt://modules/application/php/files/php-fpm
- user: root
- group: root
- mode: '0755'
- /usr/local/php8/etc/php-fpm.conf:
- source: salt://modules/application/php/files/php-fpm.conf
- /usr/local/php8/etc/php-fpm.d/www.conf:
- source: salt://modules/application/php/files/www.conf
- /usr/lib/systemd/system/php-fpm.service:
- source: salt://modules/application/php/files/php-fpm.service
- /usr/local/nginx/html/index.php:
- srouce: salt://modules/application/php/files/index.php
#/usr/lib/systemd/system/php-fpm.service:
# file.managed:
# - source: salt://modules/application/php/files/php-fpm.service
# - user: root
# - group: root
# - mode: '0644'
# - template: jinja
php-fpm.service:
service.running:
- enable: true
- reload: true
- require:
- cmd: php-install.sh
- file: cp-files
- watch:
- file: cp-files
[root@master php]#
进入files里面去
[root@master files]# ls
index.php oniguruma-devel-6.8.2-2.el8.x86_64.rpm php-fpm php-fpm.service
install.sh php-8.0.10.tar.gz php-fpm.conf www.conf
[root@master files]# cat index.php
<?php
phpinfo();
?>
[root@master files]# cat install.sh
#!/bin/bash
cd /usr/src
rm -rf php-8.0.10
tar xf php-8.0.10.tar.gz
cd php-8.0.10
./configure --prefix=$1/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix && \
make && make install
[root@master files]#
[root@master files]# cat php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
Execstop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@master files]#
[root@master files]# ls //剩余得配置文件在执行主机上解压好得目录里面有,只需要scp
index.php oniguruma-devel-6.8.2-2.el8.x86_64.rpm php-fpm php-fpm.service
install.sh php-8.0.10.tar.gz php-fpm.conf www.conf
编写执行文件
[root@master prod]# ls
modules zabbix
[root@master prod]# tree zabbix/
zabbix/
├── apache.sls
├── files
│ ├── index.php
│ ├── install.sh
│ ├── my.cnf
│ ├── mysql.conf
│ ├── vhosts.conf
│ └── zabbix-5.4.4.tar.gz //忽略
├── lnmp.sls
├── mysql.sls
├── nginx.sls
└── zabbix.sls
1 directory, 11 files
[root@master prod]#
apache
[root@master prod]# ls
modules zabbix
[root@master prod]# tree zabbix/
zabbix/
├── apache.sls //忽略
├── files
│ ├── index.php
│ ├── install.sh
│ ├── my.cnf
│ ├── mysql.conf
│ ├── vhosts.conf
│ └── zabbix-5.4.4.tar.gz
├── lnmp.sls
├── mysql.sls
├── nginx.sls
└── zabbix.sls
1 directory, 11 files
[root@master prod]#
[root@master zabbix]# cat mysql.sls
include:
- modules.database.mysql.install
lamp-dep-packages:
pkg.installed:
- pkgs:
- ncurses-devel
- openssl-devel
- openssl
- cmake
- mariadb-devel
provides-mysql-file:
file.managed:
- user: root
- group: root
- mode: '0644'
- names:
- /etc/my.cnf:
- source: salt://zabbix/files/my.cnf
- /etc/ld.so.conf.d/mysql.conf:
- source: salt://zabbix/files/mysql.conf
/usr/local/include/mysql:
file.symlink:
- target: /usr/local/mysql/include
mysqld.service:
service.running:
- enable: true
[root@master zabbix]#
[root@master zabbix]# cat nginx.sls
"Development Tools":
pkg.group_installed
include:
- modules.web.nginx.install
nginx-service:
service.running:
- name: nginx
- enable: true
[root@master zabbix]#
因为php没有漏掉得步骤,所以在这里不需要弄php得
[root@master files]# ls
index.php install.sh my.cnf mysql.conf vhosts.conf zabbix-5.4.4.tar.gz
[root@master files]# cat my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@master files]# cat mysql.conf
/usr/local/mysql/lib
包含所有一键部署
[root@master zabbix]# cat lnmp.sls
include:
- zabbix.nginx
- zabbix.mysql
- modules.application.php.install
[root@master zabbix]#
网站测试
|