使用Ansible分离部署LAMP

lamp简介

​ 所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。

web服务器的工作流程
web服务器的资源分为两种,静态资源和动态资源

  • 静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源

  • 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端

    那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求

    在这里插入图片描述

阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行

阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。

通过上面的图说明一下web的工作流程:

1.客户端通过http协议请求web服务器资源
2.web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
若是静态资源则直接从本地文件系统取之返回给客户端。
否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。

cgi与fastcgi
CGI(Common Gateway Interface,通用网关接口),CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。

FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通过启用一个解释器进程来处理每个请求,耗时且耗资源,而FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求,这样就避免了重复的生成和杀死进程带来的频繁cpu上下文切换而导致耗时

httpd与php结合的方式
httpd与php结合的方式有以下三种:

  • modules:php将以httpd的扩展模块形式存在,需要加载动态资源时,httpd可以直接通过php模块来加工资源并返回给客户端
    httpd prefork:libphp5.so(多进程模型的php)
    httpd event or worker:libphp5-zts.so(线程模型的php)

  • CGI:httpd需要加载动态资源时,通过CGI与php解释器联系,获得php执行的结果,此时httpd负责与php连接的建立和断开等

  • FastCGI:利用php-fpm机制,启动为服务进程,php自行运行为一个服务,https通过socket与php通信
    较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源。

使用ansible分离部署lamp

服务IP系统
ansible(控制机)192.168.220.7CentOS8
httpd192.168.220.8CentOS8
mysql192.168.220.9CentOS8
php192.168.220.10CentOS8

在ansible主机上使用service模块关闭另外三台主机的firewalld和selinux

[root@localhost ansible]# ansible all -m service -a ‘name=firewalld state=stopped’
192.168.220.10 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“name”: “firewalld”,
“state”: “stopped”,
“status”: {

[root@localhost ansible]# ansible all -m service -a ‘name=selinux state=stopped’
192.168.220.8 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: false,
“name”: “selinux”,
“state”: “stopped”,
“status”: {
“ActiveState”: “inactive”,

1.安装httpd服务

//在ansible主机上使用命令安装httpd服务

[root@localhost ansible]# ansible 192.168.220.8 -m yum -a ‘name=httpd state=present’
192.168.220.8 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [
“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: mailcap-2.1.48-3.el8.noarch”,
“Installed: apr-util-1.6.1-6.el8.x86_64”,
“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”
]
}

//设置httpd服务启动并开机自启

[root@localhost ansible]# ansible 192.168.220.8 -m service -a ‘name=httpd state=started enabled=yes’
192.168.220.8 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: false,
“enabled”: true,
“name”: “httpd”,
“state”: “started”,
“status”: {
“ActiveState”: “active”,

//浏览器访问一下httpd
在这里插入图片描述

//修改httpd的配置文件

//在ansible主机上将 192.168.220.8 的httpd的配置文件/etc/httpd/conf/httpd.conf cp到ansible主机上,进行修改,再传回去

[root@localhost ansible]# scp 192.168.220.8:/etc/httpd/conf/httpd.conf . //复制到ansible主机上,进行修改
httpd.conf 100% 12KB 9.5MB/s 00:00
[root@localhost ansible]# ls
ansible.cfg hosts httpd.conf inventory roles
[root@localhost ansible]# vim httpd.conf

166
167 DirectoryIndex index.php index.html //在这一行添加index.php
168

285 #
286 AddType application/x-compress .Z
287 AddType application/x-gzip .gz .tgz
288 AddType application/x-httpd-php .php // 添加这一行
289 AddType application/x-httpd-php-source .phps //添加这一行

//把这段加到最后面(不能直接复制,上传后显示有问题)
358 IncludeOptional conf.d/*.conf
359 <VirtualHost *:80>
360 DocumentRoot “/usr/local/apache/htdocs”
361 ServerName www.scl.com
362 ProxyRequests Off
363 ProxyPassMatch ^/(. .php)$ fcgi://192.168.220.10:9000/www/abc/index.php/$1
364 <Directory “/www/abc/”>
365 Options none
366 AllowOverride none
367 Require all granted
368
369
在这里插入图片描述

//改完后从ansible主机上回传到httpd主机上/etc/httpd/conf/httpd.conf,会自动覆盖之前的文件内容

[root@localhost ansible]# ansible 192.168.220.8 -m copy -a ‘src="/etc/ansible/httpd.conf" dest="/etc/httpd/conf/httpd.conf"’
192.168.220.8 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“checksum”: “6dc148b8f238843ef7bb330fbd1daf1f8916adc3”,
“dest”: “/etc/httpd/conf/httpd.conf”,
“gid”: 0,
“group”: “root”,
“md5sum”: “45ddfbdecf90303a6c6f98c08ba39d76”,
“mode”: “0644”,
“owner”: “root”,
“secontext”: “system_u:object_r:httpd_config_t:s0”,
“size”: 12337,
“src”: “/root/.ansible/tmp/ansible-tmp-1626667766.6328678-397460-205425196918576/source”,
“state”: “file”,
“uid”: 0
}

2.安装mysql服务

//在ansible主机上使用yum模块安装

[root@localhost ansible]# ansible 192.168.220.9 -m yum -a ‘name=mariadb state=present’
192.168.220.9 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [

//安装mariadb-server

[root@localhost ansible]# ansible 192.168.220.9 -m yum -a ‘name=mariadb-server state=present’
192.168.220.9 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [

//启动mariadb,设置开机自启

[root@localhost ansible]# ansible 192.168.220.9 -m service -a ‘name=mariadb state=started enabled=yes’
192.168.220.9 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“enabled”: true,
“name”: “mariadb”,
“state”: “started”,
“status”: {

3.安装php服务

//在ansible主机上使用yum模块给主机192.168.220.10安装php服务

[root@localhost ansible]# ansible 192.168.220.10 -m yum -a ‘name=php state=present’
192.168.220.10 | 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”,
“Installed: php-cli-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64”,
“Installed: php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.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: mailcap-2.1.48-3.el8.noarch”,
“Installed: apr-util-1.6.1-6.el8.x86_64”,
“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: php-7.2.24-1.module_el8.2.0+313+b04d0a66.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: nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch”
]
}

//安装php依赖

[root@localhost ansible]# ansible 192.168.220.10 -m yum -a ‘name=php-* state=present’
192.168.220.10 | 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”,

//复制php服务主机上的php文件到ansible主机上,进行修改,再传回php服务主机

[root@localhost ansible]# scp 192.168.220.10:/etc/php-fpm.d/www.conf .

37 ; Note: This value is mandatory.
38 listen = 192.168.220.10:9000 //修改为192.168.220.10:9000 ,IP是安装php服务的主机

63 ; Default Value: any
64 listen.allowed_clients = 192.168.220.8 //IP是httpd服务的主机

//改完后从ansible主机上传回php服务主机,会自动覆盖之前的内容

[root@localhost ansible]# ansible 192.168.220.10 -m copy -a ‘src="/etc/ansible/www.conf" dest="/etc/php-fpm.d/www.conf"’
192.168.220.10 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“checksum”: “65def268e4dc1bb3da0592df208462b9416b14ba”,
“dest”: “/etc/php-fpm.d/www.conf”,
“gid”: 0,
“group”: “root”,
“md5sum”: “6032ee094cb602831fc4a8668ee97563”,
“mode”: “0644”,
“owner”: “root”,
“secontext”: “system_u:object_r:etc_t:s0”,
“size”: 19417,
“src”: “/root/.ansible/tmp/ansible-tmp-1626675295.1828792-488775-165384675583500/source”,
“state”: “file”,
“uid”: 0
}

//在ansible主机上给php服务主机创建目录 www/abc

[root@localhost ansible]# ansible 192.168.220.10 -m file -a 'path=/root/www/abc state=directory ’
192.168.220.10 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“gid”: 0,
“group”: “root”,
“mode”: “0755”,
“owner”: “root”,
“path”: “/root/xxx/x”,
“secontext”: “unconfined_u:object_r:admin_home_t:s0”,
“size”: 6,
“state”: “directory”,
“uid”: 0
}

//在ansible主机上创建文件index.php 写好配置传到php服务主机的/www/abc/目录下

[root@localhost ansible]# touch index.php
[root@localhost ansible]# ls
ansible.cfg hosts httpd.conf index.php inventory php.conf roles www.conf

[root@localhost ansible]# vim index.php
[root@localhost ansible]# cat index.php

<?php phpinfo(); ?>

//传到php服务主机的/www/abc/目录下

[root@localhost ansible]# ansible 192.168.220.10 -m copy -a ‘src="/etc/ansible/index.php" dest=“www/abc/”’
192.168.220.10 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“checksum”: “7210ed0a963b6123f207c61fa37bb2cc6bd2a405”,
“dest”: “www/abc/index.php”,
“gid”: 0,
“group”: “root”,
“md5sum”: “17b4604c33f37433d90d289ebc7649d8”,
“mode”: “0644”,
“owner”: “root”,
“secontext”: “unconfined_u:object_r:admin_home_t:s0”,
“size”: 23,
“src”: “/root/.ansible/tmp/ansible-tmp-1626675770.502575-503395-171214268822188/source”,
“state”: “file”,
“uid”: 0
}

重启httpd服务

[root@localhost ansible]# ansible 192.168.220.10 -m service -a ‘name=httpd state=restarted’
192.168.220.10 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“name”: “httpd”,
“state”: “started”,
“status”: {

重启php-fpm服务

[root@localhost ansible]# ansible 192.168.220.10 -m service -a ‘name=php-fpm state=restarted’
192.168.220.10 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“name”: “php-fpm”,
“state”: “started”,
“status”: {

4.测试访问

地址栏输入安装httpd服务的地址
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值