saltstack部署lnmp

该博客详细记录了使用SaltStack自动化部署Linux、Nginx、MySQL和PHP的过程。首先展示了LNMP目录结构,然后逐步介绍了安装Nginx、MySQL和PHP的配置文件及脚本,最后通过`main.sls`文件整合所有步骤,实现一键部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 目录结构

[root@master ~]# cd /srv/salt/prod/
[root@master prod]# ls
lnmp  modules
[root@master prod]# tree
.
├── lnmp
│   ├── files
│   │   ├── index.php
│   │   ├── my.cnf
│   │   ├── mysql.conf
│   │   └── nginx.conf
│   ├── install.sls
│   ├── main.sls
│   └── mysql.sls
└── modules
    ├── application
    │   └── php7
    │       ├── files
    │       │   ├── install.sh
    │       │   ├── oniguruma-devel-6.8.2-2.el8.x86_64.rpm
    │       │   ├── php-7.4.24.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
    │       │   └── mysql.server
    │       └── install.sls
    └── web
        ├── apache
        │   ├── 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.service
            └── install.sls

14 directories, 31 files
[root@master prod]# 

2. 安装nginx

[root@master lnmp]# cd ..
[root@master prod]# ls
lnmp  modules  
[root@master prod]# cd modules/web/nginx/
[root@master nginx]# ls
files  install.sls
[root@master nginx]# cat install.sls 
'Development Tools':
  pkg.group_installed


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

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

create-nginxdir:
  file.directory:
    - name: /var/log/nginx
    - user: mysql
    - group: mysql
    - mode: '0755'
    - makedirs: true

tarxf-nginx:
  archive.extracted:
    - names: 
      - /usr/src:
        - source: salt://modules/web/nginx/files/nginx-1.20.1.tar.gz

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

nginx-service:
  file.managed:
    - names:
      - /usr/lib/systemd/system/nginx.service:
        - source: salt://modules/web/nginx/files/nginx.service
    - require:
      - cmd: nginx-install
[root@master nginx]# cd files/
[root@master files]# ls
install.sh  nginx-1.20.1.tar.gz  nginx.service
[root@master files]# cat install.sh 
#!/bin/bash

cd /usr/src/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 -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

[root@master files]# cat nginx.service 
[Unit]
Description=Nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx 
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target

[root@master files]# 

//配置nginx
[root@master prod]# ls
lnmp  modules  
[root@master prod]# cd lnmp/
[root@master lnmp]# ls
files  install.sls  main.sls  mysql.sls
[root@master lnmp]# cat install.sls 
include:
  - modules.web.nginx.install

config-nginx-files:
  file.managed:
    - names:
      - /usr/local/nginx/conf/nginx.conf:
        - source: salt://lnmp/files/nginx.conf
        - user: root
        - group: root
        - mode: 644
      - /usr/local/nginx/html/index.php:
        - source: salt://lnmp/files/index.php
    - require:
      - cmd: nginx-install

nginx:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: nginx-install
    - watch:
      - file: config-nginx-files

[root@master lnmp]# 
[root@master lnmp]# cd files/
[root@master files]# ls
index.php  my.cnf  mysql.conf  nginx.conf
[root@master files]# cat index.php 
<?php
   phpinfo();
?>
[root@master files]# 
[root@master files]# vim nginx.conf 

 45             index  index.php index.html index.htm;   //修改这一行

//修改下面几行
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  $Document_Root$fastcgi_script_name;
 70             include        fastcgi_params;
 71         }


3. 安装mysql

[root@master prod]# ls
lnmp  modules  
[root@master prod]# cd modules/database/
[root@master database]# ls
mysql
[root@master database]# cd mysql/
[root@master mysql]# ls
files  install.sls
[root@master mysql]# cat install.sls 
ncurses-compat-libs:
  pkg.installed
create-mysql-user:
  user.present:
    - name: mysql
    - system: true
    - createhome: false
    - shell: /sbin/nologin

create-datadir:
  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-install:
  cmd.script:
    - name: salt://modules/database/mysql/files/install.sh
    - unless: test -d /usr/local/mysql

trasfer-files:
  file.managed:
    - names:
      - /usr/local/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
    - require:
      - cmd: mysql-install 
[root@master mysql]# 
[root@master mysql]# cd files/
[root@master files]# ls
install.sh  mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz  mysqld.service  mysql.server
[root@master files]# cat install.sh 
#!/bin/bash

cd /usr/src
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 /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/mysqld.sh
[root@master files]# cat mysqld.service 
[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]# vim mysql.server 

 46 basedir=/usr/local/mysql   //修改这一行
 47 datadir=/opt/data          //修改这一行

//配置mysql
[root@master prod]# cd lnmp/
[root@master lnmp]# ls
files  install.sls  main.sls  mysql.sls
[root@master lnmp]# cat 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

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

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

mysqld-set-password:
  cmd.run:
    - name: /usr/local/mysql/bin/mysql -e "set password = password('1');"
    - require:
      - service: mysqld.service
    - unless: /usr/local/mysql/bin/mysql -uroot -p1 -e "exit"
[root@master lnmp]# 
[root@master lnmp]# cd files/
[root@master files]# ls
index.php  my.cnf  mysql.conf  nginx.conf
[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 files]# 

4. 安装php

[root@master prod]# ls
lnmp  modules  
[root@master prod]# cd modules/application/php7/
[root@master php7]# ls
files  install.sls
[root@master php7]# 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 
      - libsqlite3x-devel 
      - php-mysqlnd 
      - libzip-devel
      - gcc
      - gcc-c++
      - make

/usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm:
  file.managed:
    - source: salt://modules/application/php7/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:
  archive.extracted:
    - source: salt://modules/application/php7/files/php-7.4.24.tar.gz

php-install:
  cmd.script:
    - name: salt://modules/application/php7/files/install.sh
    - unless: test -d /usr/local/php7

copy-php:
  file.managed:
    - names:
      - /etc/init.d/php-fpm:
        - source: salt://modules/application/php7/files/php-fpm
        - user: root
        - group: root
        - mode: '0755'
      - /usr/local/php7/etc/php-fpm.conf:
        - source: salt://modules/application/php7/files/php-fpm.conf
      - /usr/local/php7/etc/php-fpm.d/www.conf:
        - source: salt://modules/application/php7/files/www.conf
      - /usr/lib/systemd/system/php-fpm.service:
        - source: salt://modules/application/php7/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
[root@master php7]# cd files/
[root@master files]# ls
install.sh                              php-7.4.24.tar.gz  php-fpm.conf     www.conf
oniguruma-devel-6.8.2-2.el8.x86_64.rpm  php-fpm            php-fpm.service
[root@master files]# cat install.sh 
#!/bin/bash

cd /usr/src/php-7.4.24/

./configure --prefix=/usr/local/php7 \--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 -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@master files]# 

5. 验证效果

[root@master lnmp]# cat main.sls 
include:
  - lnmp.install
  - lnmp.mysql
  - modules.application.php7.install
[root@master lnmp]# 
[root@master ~]# salt node1 state.sls lnmp.main saltenv=prod

浏览器输入node1ip进行访问lnmp
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值