LNMP部署状态文件编写

该博客详细介绍了如何使用Saltstack自动化部署LNMP(Linux、Nginx、MySQL、PHP)环境。通过配置Saltstack的状态文件(sls),包括用户管理、软件包安装、服务配置等步骤,实现Nginx、MySQL和PHP的完整安装和配置。内容涵盖了从源码下载、依赖安装到服务启动的全过程,并且展示了如何通过Saltstack的include指令将各个组件的安装过程整合在一起。
摘要由CSDN通过智能技术生成

Saltstack部署zabbix

目录树

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

[root@master prod]# tree modules/
modules/
├── app
│   └── php
│       ├── files
│       │   ├── install.sh
│       │   ├── oniguruma-devel-6.8.2-2.el8.x86_64.rpm
│       │   ├── php-7.4.24.tar.xz
│       │   ├── 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
│       │   └── mysql.server
│       └── install.sls
└── web
    ├── apache
    │   ├── files
    │   │   ├── apr-1.7.0.tar.gz
    │   │   ├── apr-util-1.6.1.tar.gz
    │   │   ├── httpd-2.4.48.tar.gz
    │   │   ├── httpd.conf
    │   │   ├── httpd.service
    │   │   ├── index.php
    │   │   └── install.sh
    │   └── install.sls
    └── nginx
        ├── files
        │   ├── install.sh
        │   ├── nginx-1.20.1.tar.gz
        │   └── nginx.service
        └── install.sls

nginx安装

[root@master nginx]# cat install.sls 
usradd.nginx:
  user.present:
    - name: nginx
    - shell: /sbin/nologin
    - system: true
    - createhome: false

'Development Tools':
  pkg.group_installed

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

copy_nginx:
  file.managed:
    - names:
      - /usr/src/nginx-1.20.1.tar.gz:
        - source: salt://modules/web/nginx/files/nginx-1.20.1.tar.gz
      - /usr/lib/systemd/system/nginx.service:
        - source: salt://modules/web/nginx/files/nginx.service 
        - template: jinja       
 
install-nginx:
  cmd.script:
    - name: salt://modules/web/nginx/files/install.sh {{ pillar['install_dir'] }}
    - unless: test -d {{ pillar['install_dir'] }}

mysql安装

[root@master mysql]# cat install.sls 
mysql:
  user.present:
    - shell: /sbin/nologin
    - system: true
    - createhome: false

ncurses-compat-libs:
  pkg.installed

/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


/opt/data:
  file.directory:
    - user: mysql
    - group: mysql
    - mode: '0755'

install-mysql:
  cmd.script:
    - name: salt://modules/database/mysql/files/install.sh
    - unless: test -d /usr/local/mysql

copy-mysql-soft:
  file.managed:
    - names:
      - /usr/lib/systemd/system/mysqld.service:
        - source: salt://modules/database/mysql/files/mysqld.service
        - user: root
        - group: root
        - mode: '0644'
      - {{ pillar['mysql_dir'] }}/support-files/mysql.server:
        - source: salt://modules/database/mysql/files/mysql.server
        - user: mysql
        - group: mysql
        - mode: '0755'
        - template: jinja
    - require:
      - cmd: install-mysql

php安装

[root@master php]# cat install.sls 
php-dep-packages:
  pkg.installed:
    - pkgs:
      - 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
      - libmcrypt
      - libmcrypt-devel
      - readline
      - readline-devel
      - libxslt
      - libxslt-devel
      - mhash
      - mhash-devel
      - php-mysqlnd
      - libsqlite3x-devel
      - libzip-devel

/usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm:
  file.managed:
    - source: salt://modules/app/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

/usr/src/php-7.4.24.tar.xz:
  file.managed:
    - source: salt://modules/app/php/files/php-7.4.24.tar.xz

install-php:
  cmd.script:
    - name: salt://modules/app/php/files/install.sh {{ pillar['php_dir'] }}
    - unless: test -d /usr/local/php

copy-php-soft:
  file.managed:
    - names:
      - /etc/init.d/php-fpm:
        - source: salt://modules/app/php/files/php-fpm
        - user: root
        - group: root
        - mode: '0755'
      - {{ pillar['php_dir'] }}/etc/php-fpm.conf:
        - source: salt://modules/app/php/files/php-fpm.conf
      - {{ pillar['php_dir'] }}/etc/php-fpm.d/www.conf:
        - source: salt://modules/app/php/files/www.conf
      - /usr/lib/systemd/system/php-fpm.service:
        - source: salt://modules/app/php/files/php-fpm.service
    - require:
      - cmd: install-php


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

lnmp文件配置

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

copy_nginx_files:
  file.managed:
    - names:
      - {{ pillar['install_dir'] }}/conf/nginx.conf:
        - source: salt://lnmp/files/nginx.conf
        - user: root
        - group: root
        - mode: 644
      - {{ pillar['install_dir'] }}/html/index.php:
        - source: salt://lnmp/files/index.php
    - require:
      - cmd: install-nginx

nginx:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: install-nginx
    - watch:
      - file: copy_nginx_files
[root@master lnmp]# cat mysql.sls 
include:
  - modules.database.mysql.install

copy-lnmp-mysql-file:
  file.managed:
    - user: root
    - group: root
    - mode: 0644
    - names: 
      - /etc/my.cnf:
        - source: salt://lnmp/files/my.cnf
    - require:
      - cmd: install-mysql

mysqld:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: install-mysql
      - file: copy-mysql-soft
    - watch:
      - file: copy-lnmp-mysql-file


set-mysql-passwd:
  cmd.run:
    - name: {{ pillar['mysql_dir'] }}/bin/mysql -e "set password=password('123456');"
    - require: 
      - service: mysqld
    - unless: {{ pillar['mysql_dir'] }}/bin/mysql -uroot -p123456 -e "exit"
[root@master lnmp]# cat install.sls 
include:
  - lnmp.nginx
  - lnmp.mysql
  - modules.app.php.install
[root@node1 ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process                                                     
LISTEN 0      128        127.0.0.1:9000        0.0.0.0:*                                                                
LISTEN 0      128          0.0.0.0:80          0.0.0.0:*                                                                
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*                                                                
LISTEN 0      80                 *:3306              *:*                                                                
LISTEN 0      128             [::]:22             [::]:* 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值