ansible分离部署lamp

本文档详细记录了使用Ansible自动化工具在四台CentOS8服务器上部署LAMP(Linux、Apache、MySQL、PHP)环境的过程。首先在控制机上配置主机清单并验证连接,然后分别在各服务器上安装Apache、MySQL、PHP及其相关组件,配置Apache服务,调整PHP-FPM设置,并最终重启服务以完成部署。整个过程体现了自动化运维的强大效率。
摘要由CSDN通过智能技术生成

lamp分离部署

首先是主机分配(4台)

服务ip
ansible(控制机)192.168.100.146(centos8-1)
nginx192.168.100.147(centos8-2)
mysql192.168.100.148(centos8-3)
php192.168.100.149(centos8-4)

1.在控制机上将2、3、4主机的ip写入清单中,并且ping通

[root@centos8-1 ansible]# cat shen
[webservers]
192.168.100.147   ansible_user=root  ansible_passwd=1 

[nginx]
192.168.100.147   ansible_user=root  ansible_passwd=1

[mysql]
192.168.100.148   ansible_user=root  ansible_passwd=1

[php]
192.168.100.149   ansible_user=root  ansible_passwd=1
[root@centos8-1 ansible]# 

测试能否ping通

[root@centos8-1 ansible]# ansible nginx -m ping     //nginx可以ping通
192.168.100.147 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@centos8-1 ansible]# 

[root@centos8-1 ~]# ansible mysql -m ping      //mysql可以ping通
192.168.100.148 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@centos8-1 ~]# 


[root@centos8-1 ~]# ansible php -m ping      //php能ping通
192.168.100.149 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@centos8-1 ~]# 

注意:我们既然使用ansible远程管理搭建,那我们就要用模块去远程搭建。

2.在2、上安装httpd

在控制机上用ansible模块命令执行在2主机上安装apache服务。

[root@centos8-1 ~]# ansible nginx -m yum -a "name=httpd state=present"     //安装成功
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
        "Installed: centos-logos-httpd-85.8-1.el8.noarch",
        "Installed: mailcap-2.1.48-3.el8.noarch",
        "Installed: httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
        "Installed: apr-1.6.3-11.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-40.module_el8.5.0+852+0aafc63b.noarch",
        "Installed: apr-util-1.6.1-6.el8.x86_64"
    ]
}
[root@centos8-1 ~]# 

设置apache开机自启 ,并且启动服务

[root@centos8-1 ~]# ansible nginx -m service -a "name=httpd state=started enabled=yes"
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd", 
    "state": "started",    //此处内容省略。。。。。。

在防火墙中放行我们http服务

[root@centos8-1 ~]# ansible nginx -m firewalld -a 'service=http zone=public permanent=yes state=enabled'     //在防火墙中放行我们的http服务成功
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Permanent operation, Changed service http to enabled, (offline operation: only on-disk configs were altered)"
}
[root@centos8-1 ~]# 

上面配置完成之后我们在浏览器中访问一下apache是否正常

在这里插入图片描述

在这里插入图片描述

3.在3、主机上安装mysql数据库

在控制机中对mysql主机进行数据库安装

[root@centos8-1 ~]# ansible mysql -m yum -a "name=mariadb state=present"    //yum下载mariadb成功
192.168.100.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: perl-Term-Cap-1.17-395.el8.noarch",
此处省略。。。。。。

[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible mysql -m yum -a 'name=mariadb-server state=present'    //安装mariadb-server成功
192.168.100.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: perl-Math-Complex-1.59-420.el8.noarch",
        "Installed: mariadb-server-utils-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
        "Installed: perl-DBD-MySQL-4.046-3.module_el8.3.0+419+c2dec72b.x86_64",
 此处省略。。。。。。

设置mariadb开机自启,并启动服务。

[root@centos8-1 ~]# ansible mysql -m service -a "name=mariadb enabled=yes"    //成功启动mariadb服务
192.168.100.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "mariadb",
    "status": {
        "ActiveState": "inactive",
 此处省略。。。。。。

4.安装php及一些常见的组件

[root@centos8-1 ~]# ansible php -m yum -a 'name=php state=present'   //成功安装php服务。
192.168.100.149 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
        "Installed: httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
 此处省略。。。。。。

安装php相关的组件

[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible php -m yum -a "name=php-* state=present"   //php组件安装成功
192.168.100.149 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: php-pecl-apcu-devel-5.1.12-2.module_el8.2.0+313+b04d0a66.x86_64",
此处省略。。。。。。
[root@centos8-1 ~]# ansible php -m yum -a "name=curl state=present"
192.168.100.149 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible php -m yum -a "name=curl-devel state=present"
192.168.100.149 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: libcurl-7.61.1-18.el8.x86_64",
        "Installed: libcurl-devel-7.61.1-18.el8.x86_64",
        "Removed: libcurl-7.61.1-17.el8.x86_64"
    ]
}
[root@centos8-1 ~]# 

5.配置apache服务

首先是httpd服务器配置

[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf line="<VirtualHost 192.168.100.147:80>\nDocumentRoot "/var/www/html/www1"\nServerName www.node2.com\nProxyRequests off\nProxyPassMatch ^/(.*\.php)$ fcgi://192.168.100.149:9000/var/www/html/www1/$1\n<Directory "/var/www/html/www1">\nOptions None\nAllowOverride None\nOrder allow,deny\nAllow from all\n</Directory>\n</VirtualHost>"'
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php .php"'
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php-source .phps"'
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible nginx -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^DirectoryIndex" line="DirectoryIndex index.html index.php"'
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@centos8-1 ~]# 

6.配置php文件

[root@centos8-1 ~]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen =" line="listen = 192.168.100.149:9000"'
192.168.100.149 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen.allowed_clients =" line="listen.allowed_clients = 192.168.100.147"'
192.168.100.149 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible nginx -a 'mkdir /var/www/html/www1'   //创建跟目录成功
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.  If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False'
in ansible.cfg to get rid of this message.
192.168.100.147 | CHANGED | rc=0 >>

[root@centos8-1 ~]# 
[root@centos8-1 ~]# ansible nginx -a 'cp /var/www/html/index.php /var/www/html/www1/'
192.168.100.147 | CHANGED | rc=0 >>

[root@centos8-1 ~]# 

7.重启php服务和apache服务

[root@centos8-1 ~]# ansible nginx -m service -a 'name=httpd state=restarted'  //重启apache服务
192.168.100.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "httpd",
    "state": "started",
    "status": {
此处省略。。。。。。

[root@centos8-1 ~]# ansible php -m service -a 'name=php-fpm state=restarted'  //重启php服务
192.168.100.149 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "php-fpm",
    "state": "started",
    "status": {
此处省略。。。。。。

访问ip地址,看看是否能通
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lfei5120

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

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

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

打赏作者

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

抵扣说明:

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

余额充值