前言
一个优秀的状态文件不是一次就能写出来的,而是经过不断的修改优化,但是我们写的时候要有,解耦,模块,幂等性的概念,这样以后可以用到其他的项目上。
有许多文件都是手动部署拷贝过去的,推荐一起进行,状态文件里面用到的都是要用的
参考文章: 手动搭建lamp架构
**其一: 先把效果实现,暂时不考虑优化 **
项目总结构
[root@master base]# tree lamp/
lamp/
|-- application
| `-- php
| |-- files
| | |-- init.d.php-fpm
| | |-- install.sh
| | |-- oniguruma-devel-6.8.2-2.el8.x86_64.rpm
| | |-- php-7.4.25.tar.gz
| | |-- php-8.0.12.tar.gz
| | |-- php-fpm.conf.default
| | |-- php-fpm.service
| | |-- php.ini-production
| | `-- www.conf.default
| `-- install.sls
|-- database
| `-- mysql
| |-- files
| | |-- my.conf
| | |-- mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
| | |-- mysql.server
| | |-- mysqld.service
| | `-- mysqld.sh
| `-- install.sls
|-- main
| |-- files
| | |-- httpd.conf
| | `-- index.php
| `-- main.sls
`-- web
`-- apache
|-- file
| |-- apr-1.7.0.tar.gz
| |-- apr-util-1.6.1.tar.gz
| |-- httpd-2.4.48.tar.gz
| |-- httpd.conf
| |-- httpd.service
| `-- install.sh
`-- install.sls
11 directories, 26 files
apache安装
结构
//在base环境下
[root@master base]# tree lamp
lamp
`-- web
`-- apache
|-- file //各种包自己提前去各大网站下载好
| |-- apr-1.7.0.tar.gz
| |-- apr-util-1.6.1.tar.gz
| |-- httpd-2.4.48.tar.gz
| |-- httpd.conf //这个文件是空的,留着之后用
| |-- httpd.service //与平时源码安装写的一样
| `-- install.sh //安装脚本
`-- install.sls //状态文件
//service文件
[root@master base]# cat lamp/web/apache/file/httpd.service
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
//安装脚本
[root@master base]# cat lamp/web/apache/file/install.sh
#!/bin/bash
cd /usr/src
rm -rf apr-1.7.0 apr-util-1.6 httpd-2.4.48
tar xf apr-1.7.0.tar.gz
tar xf apr-util-1.6.1.tar.gz
tar xf httpd-2.4.48.tar.gz
cd apr-1.7.0/
sed -i 's/$RM "$cfgfile"/ # $RM "$cfgfile"/g' configure
./configure --prefix=/usr/local/apr && make && make install
cd ../apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
cd ../httpd-2.4.48
./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install
//主状态文件
[root@master base]# cat lamp/web/apache/install.sls
"Development Tools":
pkg.group_installed //下载包组
httpd-dep-package: //下载依赖包等文件
pkg.installed:
- pkgs:
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- make
create-apache-user: //创建系统用户
user.present:
- name: apache
- createhome: false
- system: true
- shell: /sbin/nologin
download-apache: //把包传过去
file.managed:
- names:
- /usr/src/apr-1.7.0.tar.gz:
- source: salt://lamp/web/apache/file/apr-1.7.0.tar.gz
- /usr/src/apr-util-1.6.1.tar.gz:
- source: salt://lamp/web/apache/file/apr-util-1.6.1.tar.gz
- /usr/src/httpd-2.4.48.tar.gz:
- source: salt://lamp/web/apache/file/httpd-2.4.48.tar.gz
salt://lamp/web/apache/file/install.sh: //执行安装脚本
cmd.script
/usr/lib/systemd/system/httpd.service: //传递配置文件
file.managed:
- source: salt://lamp/web/apache/file/httpd.service
httpd.service: //开启服务
service.running:
- enable: true
//最后跑一遍测试
[root@master base]# salt '*' state.sls lamp.web.apache.install
mysql安装
//结构
[root@master base]# tree lamp/
lamp/
|-- database
| `-- mysql
| |-- files
| | |-- my.conf //只安装暂时不用
| | |-- mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
| | |-- mysql.server //修改好的配置文件,可以查看往期文章 mysql的二进制安装
| | |-- mysqld.service //服务控制文件
| | `-- mysqld.sh //环境变量
| `-- install.sls //状态文件
//状态文件
[root@master base]# cat lamp/database/mysql/install.sls
ncurses-compat-libs:
pkg.installed
mysql:
user.present:
- system: true
- createhome: false
- shell: /sbin/nologin
/usr/local:
archive.extracted:
- source: salt://lamp/database/mysql/files/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
file.symlink:
- name: /usr/local/mysql
- target: /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64
/usr/local/mysql:
file.directory:
- user: mysql
- group: mysql
- mode: '0755'
- recurse:
- user
- group
/opt/data:
file.directory:
- user: mysql
- group: mysql
- mode: '0755'
- makedirs: true
- recurse:
- user
- group
/etc/profile.d/mysqld.sh: //传递环境变量 这个要在目标主机手动读取才能生效
file.managed:
- source: salt://lamp/database/mysql/files/mysqld.sh
- user: root
- group: root
- mode: '0644'
/usr/local/mysql/support-files/mysql.server:
file.managed:
- source: salt://lamp/database/mysql/files/mysql.server
- user: mysql
- group: mysql
- mode: '0755'
/usr/lib/systemd/system/mysqld.service:
file.managed:
- source: salt://lamp/database/mysql/files/mysqld.service
- user: root
- group: root
- mode: '0644'
mysql-initialize:
cmd.run:
- name: '/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/'
mysqld:
service.running:
- enable: true
//服务控制文件
[root@master base]# cat lamp/database/mysql/files/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 base]# cat lamp/database/mysql/files/mysqld.sh
export PATH=/usr/local/mysql/bin:$PATH
//执行状态文件
[root@master base]# salt '*' state.sls lamp.database.mysql.install
//目标主机查看
[root@slave1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@slave1 ~]# source /etc/profile.d/mysqld.sh //读取环境变量
[root@slave1 ~]# which mysql
/usr/local/mysql/bin/mysql
[root@slave1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
php安装
参考文章 php安装报错汇总
//结构
lamp/
|-- application
| `-- php
| |-- files
| | |-- init.d.php-fpm
| | |-- install.sh
| | |-- oniguruma-devel-6.8.2-2.el8.x86_64.rpm
| | |-- php-7.4.25.tar.gz
| | |-- php-8.0.12.tar.gz
| | |-- php-fpm.conf.default
| | |-- php-fpm.service
| | |-- php.ini-production
| | `-- www.conf.default
| `-- install.sls
oniguruma-devel是编译缺少的包,其他都是手动部署拷贝来的文件
//主状态文件
[root@master base]# cat lamp/application/php/install.sls
/tmp/oniguruma-devel-6.8.2-2.el8.x86_64.rpm: //这里是传包过去,然后yum装这个包和epel源
file.managed:
- source: salt://lamp/application/php/files/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- user: root
- group: root
- move: '0644'
cmd.run:
- name: yum -y install /tmp/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- name: yum -y install epel-release
php-dep-package: //下载一些包
pkg.installed:
- pkgs:
- sqlite-devel
- libzip-devel
- libsqlite3x-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
- readline
- readline-devel
- libxslt
- libxslt-devel
- make
/usr/src/: //解压包
archive.extracted:
- source: salt://lamp/application/php/files/php-7.4.25.tar.gz
salt://lamp/application/php/files/install.sh: //执行安装脚本
cmd.script
copy-file-php7: //传递配置文件
file.managed:
- names:
- /usr/local/php7/etc/php-fpm.conf:
- source: salt://lamp/application/php/files/php-fpm.conf.default
- /usr/local/php7/etc/php-fpm.d/www.conf:
- source: salt://lamp/application/php/files/www.conf.default
- /etc/php.ini:
- source: salt://lamp/application/php/files/php.ini-production
- /etc/init.d/php-fpm:
- source: salt://lamp/application/php/files/init.d.php-fpm
- user: root
- group: root
- mode: '0755'
- /usr/lib/systemd/system/php-fpm.service:
- source: salt://lamp/application/php/files/php-fpm.service
php-fpm.service: //启动服务
service.running:
- enable: true
//service文件
[root@master base]# cat lamp/application/php/files/php-fpm.service
[Unit]
Description=php 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 base]# cat lamp/application/php/files/install.sh
#!/bin/bash
cd /usr/src/php-7.4.25
./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--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-json \
--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 \
--enable-inline-optimization && \
make && make install
至此安装完成
配置apache
前面的模块仅安装,这里的模块在安装后实现lamp
//结构
|-- main
| |-- files
| | |-- httpd.conf //改好的配置文件
| | `-- index.php //php测试页面
| `-- main.sls //状态文件
[root@master base]# cat lamp/main/main.sls
/usr/local/apache/htdocs: //改网站目录的属主,属组
file.directory:
- user: root
- group: root
- recurse:
- user
- group
/usr/local/apache/htdocs/index.php: //传递测试页面
file.managed:
- source: salt://lamp/main/files/index.php
lamp-set-apache: //更改配置文件
file.managed:
- name: /usr/local/apache/conf/httpd.conf
- source: salt://lamp/main/files/httpd.conf
restart-apache: //重启时服务生效
service.running:
- name: httpd
- reload: True
- watch: //如果下面的文件发生变化就reload
- file: /usr/local/apache/conf/httpd.conf
//测试页面
[root@master base]# cat lamp/main/files/index.php
<?php
phpinfo();
?>
依次执行apache,mysql,php的安装文件后最后执行main即可得到测试页面
如果均无报错,可以手动重启一下httpd