目录
LAMP分布式部署是将数据,PHP(python,perl,这里主要说PHP)和apache分开部署在不同主机上的web架构。PHP以fpm独立守护进程的方式监听另一台apache服务器以fcgi模式发送来的请求的工作模式。
共需要四台虚拟机
ip | 需要安装的服务 |
192.168.75.142 | ansible |
192.168.75.143 | mysql |
192.168.75.150 | apache |
192.168.75.128 | php |
1.测试ansible
//测试ansible ping通三台受管主机
[root@ansible opt]# ansible all -m ping
192.168.75.143 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.75.150 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.75.128 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
2.安装apache
1.安装apache并启动
//用yum模块在主机75.150上安装apache
[root@ansible opt]# ansible 192.168.75.150 -m yum -a 'name=httpd state=present'
192.168.75.150 | 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-2.4.37-39.module_el8.4.0+778+c970deab.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: centos-logos-httpd-85.8-1.el8.noarch",
"Installed: httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch",
"Installed: httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64",
"Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64"
]
}
//使用service模块开启apache服务
[root@ansible opt]# ansible 192.168.75.150 -m service -a 'name=httpd state=started'
192.168.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
......
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"UtmpMode": "init",
"Wants": "httpd-init.service",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
[root@ansible opt]#
//设置apache服务开机自启
[root@ansible opt]# ansible 192.168.75.150 -m service -a 'name=httpd enabled=yes'
192.168.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"status": {
......
2.运用filewalld模块设定防火墙规则,开放httpd
[root@ansible opt]# ansible 192.168.75.150 -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.75.0/24 service name=http accept" permanent=yes state=enabled immediate=yes'
192.168.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed rich_rule rule family=ipv4 source address=192.168.75.0/24 service name=http accept to enabled"
}
3.测试结果
2.安装php
//安装php
[root@ansible opt]# ansible 192.168.75.128 -m yum -a 'name=php state=present'
192.168.75.128 | 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-2.4.37-39.module_el8.4.0+778+c970deab.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: centos-logos-httpd-85.8-1.el8.noarch",
"Installed: httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch",
"Installed: httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64",
"Installed: nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch",
"Installed: php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-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: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: php-cli-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64"
]
}
2.安装php相关依赖包
[root@ansible opt]# ansible 192.168.75.128 -m yum -a 'name=php-* state=present'
192.168.75.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
......
[root@ansible opt]# ansible 192.168.75.128 -m yum -a 'name=curl state=present'
192.168.75.128 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": []
}
[root@ansible opt]# ansible 192.168.75.128 -m yum -a 'name=curl-devel state=present'
192.168.75.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: libcurl-devel-7.61.1-18.el8.x86_64",
"Installed: openssl-libs-1:1.1.1g-15.el8_3.x86_64",
"Installed: libssh-0.9.4-2.el8.x86_64",
"Installed: libssh-config-0.9.4-2.el8.noarch",
"Installed: openssl-1:1.1.1g-15.el8_3.x86_64",
"Installed: libcurl-7.61.1-18.el8.x86_64",
"Removed: libcurl-7.61.1-12.el8.x86_64",
"Removed: openssl-1:1.1.1c-15.el8.x86_64",
"Removed: openssl-libs-1:1.1.1c-15.el8.x86_64",
"Removed: libssh-0.9.0-4.el8.x86_64",
"Removed: libssh-config-0.9.0-4.el8.noarch"
]
}
3.安装mysql
1.安装mariadb和mariadb-server
//安装mariadb
[root@ansible opt]# ansible 192.168.75.143 -m yum -a 'name=mariadb state=present'
192.168.75.143 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mariadb-connector-c-config-3.1.11-2.el8_3.noarch",
"Installed: mariadb-common-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-connector-c-3.1.11-2.el8_3.x86_64"
]
}
//安装mariadb-server
[root@ansible opt]# ansible 192.168.75.143 -m yum -a 'name=mariadb-server state=present'
192.168.75.143 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mariadb-server-utils-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-errmsg-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: perl-DBD-MySQL-4.046-3.module_el8.1.0+203+e45423dc.x86_64",
"Installed: mariadb-gssapi-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-backup-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64"
]
}
2.启动mysql并设置开机自启
[root@ansible opt]# ansible 192.168.75.143 -m service -a 'name=mariadb state=started enabled=yes'
192.168.75.143 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "mariadb",
"state": "started",
"status": {
4.配置apache和php
1.先修改apache配置文件
//运用linefire模块修改httpd的配置文件,使其可以访问php
[root@ansible opt]# ansible 192.168.75.150 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf line="<VirtualHost 192.168.75.150:80>\nDocumentRoot "/var/www/html/www1"\nServerName www.192.168.72.132.com\nProxyRequests off\nProxyPassMatch ^/(.*\.php)$ fcgi://192.168.72.135: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.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@ansible opt]# ansible 192.168.75.150 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php .php"'
192.168.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@ansible opt]# ansible 192.168.75.150 -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.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible opt]# ansible 192.168.75.150 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^DirectoryIndex" line="DirectoryIndex index.html index.php"'
192.168.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
2.修改php配置文件
//使其可以监听9000端口
[root@ansible opt]# ansible 192.168.75.128 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen =" line="listen = 192.168.75.128:9000"'
192.168.75.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible opt]# ansible 192.168.75.128 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen.allowed_clients =" line="listen.allowed_clients = 192.168.75.128"'
192.168.75.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
5.重启httpd和php服务,使其重新读取配置文件
//重启httpd服务
[root@ansible opt]# ansible 192.168.75.150 -m service -a 'name=httpd state=restarted'
192.168.75.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
......
//重启php服务
[root@ansible opt]# ansible 192.168.75.128 -m service -a 'name=php-fpm state=restarted'
192.168.75.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "php-fpm",
"state": "started",
"status": {