ansible分离部署LNMP架构

这篇具有很好参考价值的文章主要介绍了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'

ansible分离部署LNMP架构文章来源地址https://www.toymoban.com/news/detail-420629.html

到了这里,关于ansible分离部署LNMP架构的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • lnmp架构部署Discuz论坛并配置重定向转发

    主机名称 IP地址 所需服务/架构 系统版本 lnmp 192.168.195.133 nginx-1.24.0 mysql-5.7 php-8.2.11(lnmp架构) centos 8 需要首先部署好lnmp架构。详细步骤请阅读: 源码编译安装部署lnmp 下载Discuz论坛系统代码包,官网地址如下: Disucz! 下载_免费搭建网站_开源建站系统下载_Discuz!官方_为您提

    2024年02月08日
    浏览(50)
  • LNMP环境下综合部署动态网站

    目录 LNMP部署--nginx 搭建mysql数据库 安装mysql的过程: 部署PHP: ​编辑​编辑php的配置文件在哪 wordpress程序安装 纯净--联网状态 环境变量中没有nginx 安装形式的选择: yum安装:自动下载安装包及其依赖,自动化安装,省时省力 都是默认的安装路径,以及版本不容易指定,自

    2024年01月17日
    浏览(36)
  • Ansible详解(架构,模块)及部署示例

    目录 Ansible概述 Ansible作用 Ansible特点 Ansible架构 工作流程 ansible 环境安装部署 环境准备 安装Ansible服务 Ansible 命令行模块 模块详解 ansible-doc command模块 shell模块 cron 模块 user模块 group模块 copy 模块 file 模块 hostname 模块 ping 模块 yum 模块 service/systemd 模块 script 模块 setup 模块 Y

    2024年01月21日
    浏览(39)
  • Zabbix 6.0 图文安装部署讲解---LNMP环境

    Zabbix 主要有以下几个组件组成: Zabbix Server :Zabbix 服务端,是 Zabbix 的核心组件。它负责接收监控数据并触发告警,还负责将监控数据持久化到数据库中。 Zabbix Agent :Zabbix 客户端,部署在被监控设备上,负责采集监控数据,采集后的数据发送给 Zabbix Server 处理。Zabbix Agen

    2023年04月15日
    浏览(46)
  • ansible安装lnmp(集中式)

    2024年02月14日
    浏览(57)
  • ansible-playbook roles编写lnmp剧本

    目录 集中式编写lnmp剧本 执行 分布式编写lnmp剧本 一定要设置ssh免交互  nginx mysql php  执行

    2024年02月14日
    浏览(41)
  • 在 CentOS 7.4 上使用 Docker极速部署 LNMP (Linux, Nginx, MySQL, PHP) 环境和 WordPress

    前置CentOS7条件: 关闭防护墙: setenforce = 0 systemctl stop firewalld 1. 安装 Docker* 如果你的系统中还没有安装 Docker,可以使用以下命令进行安装:   sudo yum install docker 启动 Docker 服务并设置开机自启: sudo systemctl start docker sudo systemctl enable docker 2. 安装 Docker Compose Docker Compose 可以让

    2024年01月17日
    浏览(48)
  • 基于Jenkins自动打包并部署docker、PHP环境,ansible部署-------从小白到大神之路之学习运维第86天

    第四阶段提升 时  间:2023年8月23日 参加人:全班人员 内  容: 基于Jenkins部署docker、PHP环境 目录 一、环境部署 (一)实验环境,服务器设置 (二)所有主机关闭防火墙和selinux,修改主机名 (三)配置git主机 (四)配置jenkins主机 (五)Jenkins配置 二、基于Jenkins自动打包

    2024年02月11日
    浏览(63)
  • 超详细实战Docker+Jenkins部署生产环境前后分离项目

    环境: 本文的所有服务都部署在阿里云ECS服务器(2H4G)上面, 前后端分离项目使用gitee上面的开源项目若依(ruoyi-vue)为例, 从零开始教学 1、先停止服务器, 再重装系统, 用最干净的系统从头教学, 保证大家环境一样 2、系统选择Centos最新版本, 然后开始安装, 等待安装完成 3、安装完

    2024年01月17日
    浏览(39)
  • Ruoyi安装部署(linux环境、前后端不分离版本)

    目录 简介 1 新建目录 2 安装jdk 2.1 jdk下载 2.2 解压并移动文件夹到/data/service目录 2.3 配置环境变量 3 安装maven 3.1 进入官网下载最新的maven 3.2 解压并移动文件夹到/data//service目录 3.3 配置环境变量 3.4 配置本地仓库地址与阿里云镜像 4 安装git 4.1 进入官网下载git 4.2 解压编译安

    2024年02月11日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包