ansible分离部署LNMP架构

ansible分离部署LNMP

环境说明:

系统 主机名 IP 服务
centos8 ansible 192.168.111.141 ansible主控机
centos8 nginx 192.168.111.142 nginx受控机
centos8 mysql 192.168.111.143 mysql受控机
centos8 php 192.168.111.144 php受控机

1.准备工作

修改默认清单文件位置,构建清单

[root@ansible ~]# vim /etc/ansible/ansible.cfg 
inventory = /etc/ansible/inventory
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# touch inventory
[root@ansible ansible]# vim inventory 
[lnmp]
nginx ansible_user=root ansible_password=123456
mysql ansible_user=root ansible_password=123456
php ansible_user=root ansible_password=123456

[root@ansible ~]# vim /etc/hosts 
192.168.111.142 nginx
192.168.111.143 mysql
192.168.111.144 php

//列出主机
[root@ansible ~]# ansible lnmp --list-hosts
  hosts (3):
    nginx
    mysql
    php

//设置密钥连接
[root@ansible ~]# ssh nginx
[root@nginx ~]# exit
logout
[root@ansible ~]# ssh mysql
[root@mysql ~]# exit
logout
[root@ansible ~]# ssh php
[root@php ~]# exit
logout
[root@ansible ~]# 

//测试连通性
[root@ansible ~]# ansible lnmp -m ping
nginx | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
php | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
mysql | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

2.部署nginx

//关闭selinux和防火墙
[root@ansible ~]# ansible nginx -m service -a 'name=firewalld state=stopped enabled=no'
[root@ansible ~]# ansible nginx -a 'setenforce 0'
[root@ansible ~]# ansible nginx -a "sed -ri 's/^(SELINUX=).*/\1disabled/g'/etc/selinux/config"

//创建用户
[root@ansible ~]# ansible nginx -m user -a 'name=nginx system=yes create_home=no shell=/sbin/nologin state=present'

//安装依赖包
[root@ansible ~]# ansible nginx -m yum -a 'name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++,make state=present'

//下载软件包并解压
[root@ansible ~]# ansible nginx -a 'wget http://nginx.org/download/nginx-1.20.2.tar.gz'
[root@ansible ~]# ansible nginx -a 'tar -xf nginx-1.20.2.tar.gz'

//进入目录编译安装
[root@ansible ~]# mkdir -p /etc/ansible/scripts/
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim configure.sh
#!/bin/bash

cd nginx-1.20.2

./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 && \

make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

root@ansible scripts]# ll
total 4
-rw-r--r-- 1 root root 470 Oct 23 22:04 configure.sh
[root@ansible scripts]# ansible nginx -m script -a '/etc/ansible/scripts/configure.sh'

//安装完成
[root@ansible ~]# ansible nginx -a 'ls /usr/local/nginx'
nginx | CHANGED | rc=0 >>
conf
html
logs
sbin

//配置环境变量
[root@ansible ~]# ansible nginx -m shell -a 'echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh'
[root@ansible ~]# ansible nginx -a 'which nginx'
nginx | CHANGED | rc=0 >>
/usr/local/nginx/sbin/nginx

//启动服务
[root@ansible ~]# vim /etc/ansible/scripts/nginx_service.sh 
#!/bin/bash

cat > /usr/lib/systemd/system/nginx.service << EOF
[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
EOF

systemctl daemon-reload
systemctl enable --now nginx

[root@ansible ~]# ansible nginx -m script -a '/etc/ansible/scripts/nginx_service.sh'
[root@ansible ~]# ansible nginx -a 'ss -antl'
nginx | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
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      128             [::]:22           [::]:*          

3.部署mysql

//关闭防火墙和selinux
[root@ansible ~]# ansible mysql -m service -a 'name=firewalld state=stopped enabled=no'
[root@ansible ~]# ansible mysql -a 'setenforce 0'
[root@ansible ~]# ansible mysql -a "sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config"

//创建用户
[root@ansible ~]# ansible mysql -m user -a 'name=mysql system=yes create_home=no shell=/sbin/nologin state=present'

//安装依赖包
[root@ansible ~]# ansible mysql -m yum -a 'name=ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel,ncurses-compat-libs state=present'

//下载软件包解压重命名
[root@ansible ~]# ansible mysql -a 'wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
[root@ansible ~]# ansible mysql -a 'tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/'
[root@ansible ~]# ansible mysql -a 'mv /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64 /usr/local/mysql'

//修改属主属组
[root@ansible ~]# ansible mysql -a 'chown -R mysql.mysql /usr/local/mysql'

//配置环境
[root@ansible ~]# ansible mysql -a 'ln -s /usr/local/mysql/include /usr/include/mysql'
[root@ansible ~]# ansible mysql -m shell -a "echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf"
[root@ansible ~]# ansible mysql -a "sed -i '22a MANDATORY_MANPATH    /usr/local/mysql/man' /etc/man_db.conf"
[root@ansible ~]# ansible mysql -m shell -a "echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh"
[root@ansible ~]# ansible mysql -a 'which mysql'
[root@ansible ~]# ansible mysql -a 'which mysql'
mysql | CHANGED | rc=0 >>
/usr/local/mysql/bin/mysql

//建立数据存放目录
[root@ansible ~]# ansible mysql -a 'mkdir /opt/data'
[root@ansible ~]# ansible mysql -a 'chown -R mysql.mysql /opt/data'

//初始化数据库
[root@ansible ~]# ansible mysql -a 'mysqld --initialize --user mysql --datadir /opt/data'
mysql | CHANGED | rc=0 >>
2022-10-23T14:24:07.127784Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-23T14:24:07.286100Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-23T14:24:07.314541Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-23T14:24:07.383098Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5a8e11ea-52de-11ed-b270-000c29c34b3e.
2022-10-23T14:24:07.383794Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-23T14:24:07.600947Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T14:24:07.600960Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T14:24:07.601238Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-23T14:24:07.640229Z 1 [Note] A temporary password is generated for root@localhost: y*rou<U9Om.c
[root@ansible ~]# ansible mysql -m shell -a "echo 'y*rou<U9Om.c' > pass"

//生成配置文件启动服务
[root@ansible ~]# vim /etc/ansible/scripts/mysql_service.sh 
#!/bin/bash

cat >> /etc/my.cnf <<EOF
[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
EOF

cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysqld server daemon
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now mysqld

[root@ansible ~]# ansible mysql -m script -a '/etc/ansible/scripts/mysql_service.sh'
[root@ansible ~]# ansible mysql -a 'ss -antl'
mysql | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      80                 *:3306            *:*          
LISTEN 0      128             [::]:22           [::]:* 

4.部署php

//安装依赖包
[root@ansible ~]# ansible php -m yum -a 'name=epel-release state=present'
[root@ansible ~]# ansible php -m yum -a 'name=libxml2,libxml2-devel,openssl,openssl-devel,bzip2,bzip2-devel,libcurl,libcurl-devel,libicu-devel,libjpeg,libjpeg-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,wget,gcc,gcc-c++,make state=present'
[root@ansible ~]# ansible php -a 'yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm'

//下载PHP并解压
[root@ansible ~]# ansible php -a 'wget https://www.php.net/distributions/php-8.1.11.tar.gz'
[root@ansible ~]# ansible php -a 'tar xf php-8.1.11.tar.gz -C /usr/src'

//编译安装php
[root@ansible ~]# vim /etc/ansible/scripts/php.sh
#!/bin/bash

cd /usr/src/php-8.1.11/
./configure --prefix=/usr/local/php \
--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

[root@ansible ~]# ansible php -m script -a '/etc/ansible/scripts/php.sh'
[root@ansible ~]# ansible php -m shell -a 'cd /usr/src/php-8.1.11/ && make && make install'

//配置文件
[root@ansible ~]# ansible php -a 'cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf'
[root@ansible ~]# ansible php -a 'cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf'

//头文件
[root@ansible ~]# ansible php -a 'ln -sv /usr/local/php /usr/include/php'

//编写service文件
[root@ansible ~]# vim /etc/ansible/scripts/php_service.sh
#!/bin/bash

cat > /usr/lib/systemd/system/php.service << EOF
[Unit]
Description=php server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
[root@ansible ~]# ansible php -m script -a '/etc/ansible/scripts/php_service.sh'
[root@ansible ~]# ansible php -a 'systemctl daemon-reload'

//开启服务并开机自启
[root@ansible ~]# ansible php -m service -a 'name=php state=started enabled=yes'
[root@ansible ~]# ansible php -a 'ss -antl'
php | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*          
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*  

5.配置LNMP界面

//修改nginx配置文件
[root@ansible ~]# vim /etc/ansible/scripts/nginxconf.sh
#!/bin/bash

sed -i "45c                   index  index.php index.html index.htm;" /usr/local/nginx/conf/nginx.conf
sed -i "65c     location ~ \.php$ {" /usr/local/nginx/conf/nginx.conf
sed -i "66c     root      /var/www/html;" /usr/local/nginx/conf/nginx.conf
sed -i "67c     fastcgi_pass   192.168.111.144:9000;" /usr/local/nginx/conf/nginx.conf
sed -i "68c     fastcgi_index  index.php;" /usr/local/nginx/conf/nginx.conf
sed -i "69c     fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;" /usr/local/nginx/conf/nginx.conf
sed -i "70c      include        fastcgi_params;" /usr/local/nginx/conf/nginx.conf
sed -i "71c      }" /usr/local/nginx/conf/nginx.conf

[root@ansible ~]# ansible nginx -m script -a '/etc/ansible/scripts/nginxconf.sh'
[root@ansible ~]# ansible nginx -a 'touch /usr/local/nginx/html/index.php'

//在php端上配置网站
[root@ansible ~]# vim /etc/ansible/scripts/phpindex.sh
#!/bin/bash

mkdir -p /var/www/html
cat > /var/www/html/index.php << EOF
<?php
    phpinfo();
?>
EOF
[root@ansible ~]# ansible php -m script -a '/etc/ansible/scripts/phpindex.sh'

//修改php配置文件
[root@ansible ~]# ansible php -a 'sed -i "36c listen = 192.168.111.144:9000" /usr/local/php/etc/php-fpm.d/www.conf'
[root@ansible ~]# ansible php -a 'sed -i "63c listen.allowed_clients = 192.168.111.142" /usr/local/php/etc/php-fpm.d/www.conf'


//重启nginx服务和php服务
[root@ansible ~]# ansible nginx -m service -a 'name=nginx state=restarted'
[root@ansible ~]# ansible php -m service -a 'name=php state=restarted'

image

原文链接:https://www.cnblogs.com/Their-own/p/17341574.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:ansible分离部署LNMP架构 - Python技术站

(0)
上一篇 2023年4月22日
下一篇 2023年4月22日

相关文章

  • rhel6.3如何使用yum安装Apache

    以下是“RHEL6.3如何使用yum安装Apache”的完整使用攻略,包含两个示例说明。 步骤 打开终端。在RHEL6.3中,您可以通过单击“应用程序”菜单中的“终端”图标来打开终端。 确认您的系统已更新。在终端中输入以下命令: sudo yum update 安装Apache。终端中输入以下命令: sudo yum install httpd 启动Apac…

    Linux 2023年5月13日
    00
  • 用rsync从Linux到Windows远程备份

    rsync是Linux系统下的数据镜像备份工具,从软件的命名上就可以看出来 了——remote sync。rsync支持大多数的类Unix系统,无论是Linux、Solaris还是BSD上都经过了良好的测试。rsync的最新版本可以从 http://rsync.samba.org/rsync/获得。 它的特性如下:   rsync是Linux系统下的数据镜像…

    Linux 2023年4月12日
    00
  • Linux Shell 数学运算

    1、执行数学运算   另一个对任何编程语言都很重要的特性是操作数字的能力。遗憾的是,对shell脚本来说,这个处理过程会比较麻烦。在shell脚本中有两种途径来进行数学运算。 1.1 expr 命令   最开始,Bourne shell提供了一个特别的命令用来处理数学表达式。expr命令允许在命令行上处理数学表达式,但是特别笨拙。 1 $ expr 1 + …

    Linux 2023年4月11日
    00
  • linux系统中的删除操作

    #rm [-fir] 文件或者目录 参数: -f:就是force的意思,忽略不存在的文件,不会出现警告信息; -i:互动模式,在删除前会询问用户是否操作; -r:递归删除。最常用的在目录删除。这是一个非常危险的参数!!!   范例一:删除bashrc #rm -i bashrc rm: remove regular file `bashrc’? y #如果加…

    Linux 2023年4月11日
    00
  • linux 基础(7)账号和群组的管理

    了解账号和群组的基本信息 账号使用 如何查看 linux 计算机上有哪些账号呢?账号的信息储存在/etc/passwd中,打开就可以看到: less /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/s…

    Linux 2023年4月10日
    00
  • Centos 7系统虚拟机桥接模式详解

    Centos 7系统虚拟机桥接模式详解 在Centos 7系统中,虚拟机可以通过桥接模式连接到主机网络中,让虚拟机与主机在同一个局域网中。本文将详细介绍Centos 7系统虚拟机桥接模式的配置过程和注意事项。 配置桥接模式 要使用虚拟机桥接模式连接到主机网络中,需要进行以下步骤: 确保宿主机网络是可用的,可以正常连接到局域网或互联网,可以使用命令ping w…

    Linux 2023年5月24日
    00
  • Apache服务的主要目录和配置文件详解

    以下是“Apache服务的主要目录和配置文件详解”的完整使用攻略,包含两个示例说明。 Apache服务主要目录 Apache服务的主要目录包括以下几个: bin:包含Apache服务器的执行文件。 conf:包含Apache服务器配置文件。 htdocs:包含Apache服务器的网站文件。 logs:包Apache服务器的日志。 Apache服务的主要配置文…

    Linux 2023年5月13日
    00
  • CentOS系统环境精简优化详解

    CentOS系统环境精简优化详解 CentOS系统环境精简优化是提高服务器稳定性和性能的关键步骤之一。本攻略将详细讲解如何进行CentOS系统环境精简优化,包括以下内容: 禁用不必要的服务 移除不必要的软件包 使用优化内核 调整文件系统参数 禁用不必要的服务 CentOS系统默认启动了许多不必要的系统服务,这些服务可能会占用大量系统资源。我们可以使用syst…

    Linux 2023年5月14日
    00
合作推广
合作推广
分享本页
返回顶部