SaltStack自动化部署LNMP

1 Nginx状态文件

[root@master web]# tree nginx/
nginx/
├── files
│   ├── install.sh
│   ├── my.cnf
│   ├── mysql.conf
│   ├── nginx-1.20.1.tar.gz
│   ├── nginx.conf
│   └── nginx.service
└── install.sls

[root@master web]# cat nginx/install.sls 
install-epel:
  cmd.run:
    - name: yum -y install epel-release
    - unless: rpm -q epel-release-8-11.el8.noarch

"Development Tools":
  pkg.group_installed

nginx-dep-package:
  pkg.installed:
    - pkgs:
      - pcre-devel 
      - openssl 
      - openssl-devel 
      - gd-devel 
      - gcc 
      - gcc-c++ 
      - make

nginx-user:
  user.present:
    - name: nginx
    - shell: /sbin/nologin
    - createhome: false
    - system: true

/var/log/nginx:
  file.directory:
    - user: root
    - group: root
    - mkdor: '0644'
    - makedirs: true

/usr/src/nginx-1.20.1.tar.gz:
  file.managed:
    - source: salt://modules/web/nginx/files/nginx-1.20.1.tar.gz
    - user: root
    - group: root
    - mode: '0644'

compile-nginx:
  cmd.script:
    - name: install.sh
    - source: salt://modules/web/nginx/files/install.sh
    - unless: test -d /usr/local/nginx    
    - require:
      - file: /usr/src/nginx-1.20.1.tar.gz

/usr/lib/systemd/system/nginx.service:
  file.managed:
    - source: salt://modules/web/nginx/files/nginx.service
    - user: root
    - group: root
    - mode: '0644'

nginx-service:
  service.running:
    - name: nginx
    - require:
      - cmd: compile-nginx

[root@master web]# cat nginx/files/install.sh 
#! /bin/bash

cd /usr/src
rm -rf nginx-1.20.1
tar xf nginx-1.20.1.tar.gz
cd nginx-1.20.1
  ./configure \
	  --prefix=/usr/local/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

2 mysql状态文件

[root@master database]# tree mysql/
mysql/
├── files
│   ├── install.sh
│   ├── mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
│   ├── mysqld.service
│   └── mysql.server
└── install.sls


[root@master database]# cat mysql/files/install.sh 
#! /bin/bash

cd /usr/src
tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local
ln -s /usr/local/mysql-5.7.35-linux-glibc2.12-x86_64 /usr/local/mysql

chown -R mysql.mysql /usr/local/mysql*
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/

echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server


3 php状态文件

[root@master prod]# cat lnmp/php.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: rpm -q oniguruma-devel-6.8.2-2.el8.x86_64.rpm

dep-package-install:
  pkg.installed:
    - pkgs:
      - libxml2 
      - libxml2-devel 
      - openssl 
      - openssl-devel 
      - bzip2 
      - bzip2-devel 
      - libcurl 
      - libcurl-devel 
      - libicu-devel 
      - libicu-devel
      - libjpeg-turbo 
      - libjpeg-turbo-devel 
      - libpng 
      - libpng-devel 
      - openldap-devel  
      - pcre-devel 
      - freetype 
      - freetype-devel 
      - gmp 
      - gmp-devel 
      - libmcrypt 
      - libmcrypt-devel 
      - readline 
      - readline-devel 
      - libxslt 
      - libxslt-devel 
      - mhash 
      - mhash-devel
      - php-mysqlnd
      - libzip-devel
      - libsqlite3x-devel

/usr/src/php-7.4.24.tar.xz:
  file.managed:
    - source: salt://modules/application/php/files/php-7.4.24.tar.xz
    - user: root
    - group: root
    - mode: '0644'

php-install:
  cmd.script:
    - name: salt://modules/application/php/files/install.sh
    - unless: test -d /usr/local/php8
    - require:
      - file: /usr/src/php-7.4.24.tar.xz

copy-php:
  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
    - require:
      - cmd: php-install    

php-fpm.service:
  service.running:
    - enable: true  
    - reload: true
    - require:
      - cmd: php-install
      - file: copy-php
    - watch:
      - file: copy-php

4 部署Lnmp的状态文件

[root@master prod]# tree lnmp/
lnmp/
├── files
│   ├── index.php
│   ├── my.cnf
│   ├── mysql.conf
│   └── nginx.conf
├── main.sls
├── mysql.sls
├── nginx.sls
└── php.sls

[root@master prod]# cat lnmp/main.sls 
include:
  - lnmp.nginx
  - lnmp.mysql
  - lnmp.php



[root@master prod]# cat lnmp/nginx.sls 
include:   
  - modules.web.nginx.install

/usr/local/nginx/html/index.php:
  file.managed:
    - source: salt://lnmp/files/index.php
    - user: root
    - group: root
    - mode: '0644'
    - require:
      - cmd: compile-nginx

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://lnmp/files/nginx.conf
    - user: root
    - group: root
    - mode: '0644'
    - require:
      - cmd: compile-nginx

extend:
  nginx-service:
    service.running:
      - enable: true
      - reload: true
      - require:
        - file: /usr/local/nginx/conf/nginx.conf
      - watch:
        - file: /usr/local/nginx/conf/nginx.conf


[root@master prod]# cat lnmp/mysql.sls 
lamp-dep-package:
  pkg.installed:
    - pkgs:
      - ncurses-devel 
      - openssl-devel 
      - openssl 
      - cmake 
      - mariadb-devel

include:
  - modules.database.mysql.install

provides-mysql-file:
  file.managed:
    - user: root
    - group: root
    - mode: '0644'
    - names:
      - /etc/my.cnf:
        - source: salt://lnmp/files/my.cnf
      - /etc/ld.so.conf.d/mysql.conf:
        - source: salt://lnmp/files/mysql.conf
    - require:
      - cmd: mysql-install

/usr/local/include/mysql:
  file.symlink:
    - target: /usr/local/mysql/include/

mysqld.service:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: mysql-install
      - file: provides-mysql-file
    - watch:
      - file: provides-mysql-file

mysqld-set-password:
  cmd.run:
    - name: /usr/local/mysql/bin/mysql -e "set password = password('123');"
    - unless: /usr/local/mysql/bin/mysql -uroot -p123 -e "exit"
    - require:
      - service: mysqld.service

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值