lamp分离部署

lamp分离部署

一、准备工作

服务地址
ansible192.168.142.100
mysql192.168.142.97
apache192.168.142.98
php192.168.142.99

二、工作思路

  • 安装apache
  • 安装mysql
  • 安装php
  • 连接
  • 提供测试页

一、搭建源代码ansible进行配置并更改主机名(192.168.142.195)

[root@ansible ~]# yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:05:29 前,执行于 20210719日 星期一 022324秒。
epel-release-latest-8.noarch.rpm      14 kB/s |  23 kB     00:01    
依赖关系解决。
=====================================================================
 软件包            架构        版本          仓库               大小
=====================================================================
安装:
 epel-release      noarch      8-11.el8      @commandline       23 k

事务概要
=====================================================================
安装  1 软件包

总计:23 k
安装大小:35 k
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                      1/1 
  安装    : epel-release-8-11.el8.noarch                         1/1 
  运行脚本: epel-release-8-11.el8.noarch                         1/1 
  验证    : epel-release-8-11.el8.noarch                         1/1 
Installed products updated.

已安装:
  epel-release-8-11.el8.noarch                                       

完毕!
[root@localhost ~]# yum install epel-release
[root@localhost ~]# yum list all|grep ansible	#查看是否下载成功
ansible.noarch                              2.9.23-1.el7               epel     
ansible-doc.noarch                          2.9.23-1.el7               epel     
ansible-inventory-grapher.noarch            2.4.4-1.el7                epel     
ansible-lint.noarch                         3.5.1-1.el7                epel     
ansible-openstack-modules.noarch            0-20140902git79d751a.el7   epel     
ansible-python3.noarch                      2.9.23-1.el7               epel     
ansible-review.noarch                       0.13.4-1.el7               epel     
ansible-test.noarch                         2.9.23-1.el7               epel     
[root@localhost ~]# yum -y install ansible
[root@localhost ~]# ansible --version  #显示版本信息
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
[root@localhost ~]# hostnamectl set-hostname ansible
[root@localhost ~]# bash
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# vim ansible.cfg 
[root@ansible ansible]# touch inventory
[root@ansible ansible]# ls
ansible.cfg  hosts  inventory  roles
[root@ansible ansible]# vim inventory 
[root@ansible ansible]# cat inventory 
[web1]
192.168.142.22
[web2]
192.168.142.174
[web3]
192.168.142.189

二、免密登录后用ping来查看是否连接

[root@ansible ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:AbIOJ/qmgy7rA5lVzLyvzqgXFBZbk+eifxermyCA56c root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|  .=+..          |
|  oo*+..         |
| .+oo+  .        |
|..o=o .  .       |
|+=...o  S        |
|+=o   . .        |
|o =oo.   o       |
|++.*o...o        |
|BBE.o.++         |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id root@192.168.142.22
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.142.192's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.142.22'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]# ansible all -m ping
192.168.142.99 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.142.98 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.142.97 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

三、把yum源发到三台主机上

[root@ansible ~]# ansible all -m copy -a 'src=/etc/yum.repos.d/  dest=/etc/yum.repos.d/'
192.168.142.99 | CHANGED => {
    "changed": true,
    "dest": "/etc/yum.repos.d/",
    "src": "/etc/yum.repos.d/"
}
192.168.142.98 | CHANGED => {
    "changed": true,
    "dest": "/etc/yum.repos.d/",
    "src": "/etc/yum.repos.d/"
}
192.168.142.97 | CHANGED => {
    "changed": true,
    "dest": "/etc/yum.repos.d/",
    "src": "/etc/yum.repos.d/"
}

四、使用shell模块和lineinfile模块为所有主机关闭防火墙和selinux

[root@ansible ~]# ansible all -m shell -a 'systemctl stop firewalld && systemctl disable firewalld '
192.168.142.99 | CHANGED | rc=0 >>
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
192.168.142.98 | CHANGED | rc=0 >>
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
192.168.142.97 | CHANGED | rc=0 >>

[root@ansible ~]# ansible all -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
192.168.142.97 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}

五、使用yum模块为web2安装apache

[root@ansible ansible]# ansible web2 -m yum -a 'name=httpd'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-1.6.3-9.el8.x86_64",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch",
        "Installed: httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
        "Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Installed: redhat-logos-httpd-81.1-1.el8.noarch",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
    ]
}

六、使用yum模块为web3主机安装mysql服务

[root@ansible ansible]# ansible web3 -m yum -a 'name=mariadb*'
192.168.142.97 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: perl-Env-1.04-395.el8.noarch",
        "Installed: libcom_err-devel-1.45.4-3.el8.x86_64",
        "Installed: pcre2-devel-10.32-1.el8.x86_64",
        "Installed: pcre2-utf16-10.32-1.el8.x86_64",
        "Installed: pcre2-utf32-10.32-1.el8.x86_64",
        "Installed: copy-jdk-configs-3.7-1.el8.noarch",
        "Installed: perl-Test-Simple-1:1.302135-1.el8.noarch",
        "Installed: keyutils-libs-devel-1.5.10-6.el8.x86_64",
        "Installed: zlib-devel-1.2.11-13.el8.x86_64",
        "Installed: libverto-devel-0.3.0-5.el8.x86_64",
        "Installed: lksctp-tools-1.0.18-3.el8.x86_64",
        "Installed: tzdata-java-2019c-1.el8.noarch",
        "Installed: mariadb-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-backup-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: Judy-1.0.5-18.module+el8+2765+cfa4f87b.x86_64",
        "Installed: mariadb-common-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-connector-c-3.0.7-1.el8.x86_64",
        "Installed: mariadb-connector-c-config-3.0.7-1.el8.noarch",
        "Installed: perl-Time-HiRes-1.9758-1.el8.x86_64",
        "Installed: java-1.8.0-openjdk-headless-1:1.8.0.242.b08-4.el8.x86_64",
        "Installed: mariadb-connector-c-devel-3.0.7-1.el8.x86_64",
        "Installed: galera-25.3.26-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-connector-odbc-3.0.7-1.el8.x86_64",
        "Installed: mariadb-devel-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-embedded-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-gssapi-server-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-embedded-devel-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-oqgraph-engine-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-java-client-2.2.5-2.el8.noarch",
        "Installed: mariadb-errmsg-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-server-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-server-galera-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: javapackages-filesystem-5.3.0-1.module+el8+2447+6f56d9a6.noarch",
        "Installed: mariadb-server-utils-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: mariadb-test-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
        "Installed: unixODBC-2.3.7-1.el8.x86_64",
        "Installed: perl-DBD-MySQL-4.046-3.module+el8.1.0+2938+301254e2.x86_64",
        "Installed: krb5-devel-1.17-18.el8.x86_64",
        "Installed: libaio-devel-0.3.112-1.el8.x86_64",
        "Installed: libselinux-devel-2.9-3.el8.x86_64",
        "Installed: libsepol-devel-2.9-1.el8.x86_64",
        "Installed: perl-Memoize-1.03-416.el8.noarch",
        "Installed: openssl-devel-1:1.1.1c-15.el8.x86_64",
        "Installed: libkadm5-1.17-18.el8.x86_64"
    ]
}


七、使用yum模块为web1主机安装php服务

[root@ansible ansible]# ansible web1 -m yum -a "name=php*"
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: m4-1.4.18-7.el8.x86_64",
        "Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
        "Installed: httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch",
        "Installed: httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
        "Installed: pcre-cpp-8.42-4.el8.x86_64",
        "Installed: pcre-devel-8.42-4.el8.x86_64",
        "Installed: automake-1.16.1-6.el8.noarch",
        "Installed: pcre-utf16-8.42-4.el8.x86_64",
        "Installed: pcre-utf32-8.42-4.el8.x86_64",
        "Installed: net-snmp-1:5.8-14.el8.x86_64",
        "Installed: php-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: net-snmp-agent-libs-1:5.8-14.el8.x86_64",
        "Installed: php-bcmath-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-cli-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-common-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-dba-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-dbg-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-devel-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-embedded-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-enchant-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: cpp-8.3.1-5.el8.x86_64",
        "Installed: php-fpm-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-gd-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Installed: nginx-filesystem-1:1.14.1-9.module+el8.0.0+4108+af250afe.noarch",
        "Installed: php-gmp-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-intl-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-json-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: enchant-1:1.6.0-21.el8.x86_64",
        "Installed: isl-0.16.1-6.el8.x86_64",
        "Installed: php-ldap-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-mbstring-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-mysqlnd-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: tokyocabinet-1.4.48-10.el8.x86_64",
        "Installed: php-odbc-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: kernel-headers-4.18.0-193.el8.x86_64",
        "Installed: php-opcache-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-pdo-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-pear-1:1.10.5-9.module+el8.1.0+3202+af5476b9.noarch",
        "Installed: php-pecl-apcu-5.1.12-2.module+el8.1.0+3202+af5476b9.x86_64",
        "Installed: php-pecl-apcu-devel-5.1.12-2.module+el8.1.0+3202+af5476b9.x86_64",
        "Installed: apr-1.6.3-9.el8.x86_64",
        "Installed: php-pecl-zip-1.15.3-1.module+el8.1.0+3186+20164e6f.x86_64",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: php-pgsql-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: php-process-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: perl-Thread-Queue-3.13-1.el8.noarch",
        "Installed: php-recode-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: mariadb-connector-c-3.0.7-1.el8.x86_64",
        "Installed: mariadb-connector-c-config-3.0.7-1.el8.noarch",
        "Installed: php-snmp-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: php-soap-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: php-xml-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: recode-3.6-47.el8.x86_64",
        "Installed: php-xmlrpc-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
        "Installed: libxcrypt-devel-4.1.1-4.el8.x86_64",
        "Installed: gcc-8.3.1-5.el8.x86_64",
        "Installed: gcc-c++-8.3.1-5.el8.x86_64",
        "Installed: unixODBC-2.3.7-1.el8.x86_64",
        "Installed: libstdc++-devel-8.3.1-5.el8.x86_64",
        "Installed: lm_sensors-libs-3.4.0-21.20180522git70f7e08.el8.x86_64",
        "Installed: glibc-devel-2.28-101.el8.x86_64",
        "Installed: redhat-logos-httpd-81.1-1.el8.noarch",
        "Installed: glibc-headers-2.28-101.el8.x86_64",
        "Installed: libtool-2.4.6-25.el8.x86_64",
        "Installed: autoconf-2.69-27.el8.noarch",
        "Installed: libzip-1.5.1-2.module+el8.1.0+3202+af5476b9.x86_64",
        "Installed: libpq-12.1-3.el8.x86_64"
    ]
}

八、使用script模块为所有受管主机添加host解析

[root@ansible ansible]# vim host.sh
[root@ansible ansible]# cat host.sh 
#!/bin/bash

echo 192.168.142.22 mysql >> /etc/hosts
echo 192.168.142.174 nginx >> /etc/hosts
echo 192.168.142.189 php >> /etc/hosts
[root@ansible ansible]# ansible all -m script -a '/etc/ansible/host.sh'
192.168.142.98 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.142.98 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.142.98 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
192.168.142.97 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.142.97 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.142.97 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
192.168.142.99 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.142.99 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.142.99 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

九、使用service模块启用apache、mysql、php服务,并设置开机自启

[root@ansible ansible]# ansible web1 -m service -a "name=httpd state=started enabled=yes"
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",
        "After": "nss-lookup.target httpd-init.service systemd-journald.socket remote-fs.target systemd-tmpfiles-setup.service tmp.mount system.slice -.mount sysinit.target basic.target network.target",
        "AllowIsolate": "no",
        "AllowedCPUs": "",
        "AllowedMemoryNodes": "",
        "AmbientCapabilities": "",
        "AssertResult": "no",
        "AssertTimestampMonotonic": "0",
        "Before": "shutdown.target",
        "BlockIOAccounting": "no",
        "BlockIOWeight": "[not set]",
        "CPUAccounting": "no",
        "CPUAffinity": "",
        "CPUQuotaPerSecUSec": "infinity",
        "CPUSchedulingPolicy": "0",
        "CPUSchedulingPriority": "0",
        "CPUSchedulingResetOnFork": "no",
        "CPUShares": "[not set]",
        "CPUUsageNSec": "[not set]",
        "CPUWeight": "[not set]",
        "CacheDirectoryMode": "0755",
        "CanIsolate": "no",
        "CanReload": "yes",
        "CanStart": "yes",
        "CanStop": "yes",
...
[root@ansible ansible]# ansible 192.168.142.98 -m service -a "name=php-fpm state=started enabled=yes"
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "php-fpm",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",
        "After": "network.target systemd-journald.socket -.mount tmp.mount sysinit.target system.slice systemd-tmpfiles-setup.service syslog.target basic.target",
        "AllowIsolate": "no",
        "AllowedCPUs": "",
        "AllowedMemoryNodes": "",
        "AmbientCapabilities": "",
        "AssertResult": "no",
        "AssertTimestampMonotonic": "0",
        "Before": "shutdown.target",
        "BlockIOAccounting": "no",
        "BlockIOWeight": "[not set]",
        "CPUAccounting": "no",
        "CPUAffinity": "",
        "CPUQuotaPerSecUSec": "infinity",
        "CPUSchedulingPolicy": "0",
        "CPUSchedulingPriority": "0",
        "CPUSchedulingResetOnFork": "no",
        "CPUShares": "[not set]",
        "CPUUsageNSec": "[not set]",
        "CPUWeight": "[not set]",
        "CacheDirectoryMode": "0755",
        "CanIsolate": "no",
        "CanReload": "yes",
        "CanStart": "yes",
        "CanStop": "yes",
...
[root@ansible ansible]# ansible web3 -m service -a "name=mariadb state=started enabled=yes"
192.168.142.97 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "mariadb",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",
        "After": "-.mount tmp.mount sysinit.target network.target systemd-journald.socket basic.target systemd-tmpfiles-setup.service system.slice",
        "AllowIsolate": "no",
        "AllowedCPUs": "",
        "AllowedMemoryNodes": "",
        "AmbientCapabilities": "",
        "AssertResult": "no",
        "AssertTimestampMonotonic": "0",
        "Before": "shutdown.target",
        "BlockIOAccounting": "no",
        "BlockIOWeight": "[not set]",
        "CPUAccounting": "no",
        "CPUAffinity": "",
        "CPUQuotaPerSecUSec": "infinity",
        "CPUSchedulingPolicy": "0",
        "CPUSchedulingPriority": "0",
        "CPUSchedulingResetOnFork": "no",
        "CPUShares": "[not set]",
        "CPUUsageNSec": "[not set]",
        "CPUWeight": "[not set]",
        "CacheDirectoryMode": "0755",
        "CanIsolate": "no",
        "CanReload": "no",
        "CanStart": "yes",
        "CanStop": "yes",
        ...

使用shell、lineinfile、copy模块来修改apache的配置

[root@ansible ansible]# ansible web2 -m shell -a 'sed -i "/DirectoryIndex/s/index.html/index.php index.html/g" /etc/httpd/conf/httpd.conf'
192.168.142.99 | CHANGED | rc=0 >>
[root@ansible ansible]# ansible web2 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="AddType application/x-gzip.gz.tgz" line="AddType application/x-httpd-php .php"'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible ansible]# ansible web2 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="AddType application/x-gzip .gz.tgz" line="AddType application/x-httpd-php-source.phps"'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}


[root@ansible ~]# ansible web2 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="# LoadModule foo_module modules/mod_foo.so" line="LoadModule proxy_module modules/mod_proxy.so"'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible ~]# ansible web2 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="# LoadModule foo_module modules/mod_foo.so" line="LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so"'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}

把虚拟站点文件传输到apache服务器上

[root@ansible ansible]# vim /root/httpd.conf
[root@ansible ansible]# cat /root/httpd.conf 
<VirtualHost *:80>
    DocumentRoot "/var/www/html/"
    ServerName www.ll.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.142.99:9000/www/html/$1
</VirtualHost>


[root@ansible ansible]# ansible web2 -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf.d/'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "164d64c4d5a318e5f53b862be7f5f64757fa22e7",
    "dest": "/etc/httpd/conf.d/httpd.conf",
    "gid": 0,
    "group": "root",
    "md5sum": "8ba496e98726df0f1d5d6bdaf47ccaf6",
    "mode": "0644",
    "owner": "root",
    "size": 184,
    "src": "/root/.ansible/tmp/ansible-tmp-1626634125.713699-1366458-122833635380879/source",
    "state": "file",
    "uid": 0
}
#修改apache主机配置文件
[root@apache html]# vim /etc/httpd/conf/httpd.conf 

 58 LoadModule proxy_module modules/mod_proxy.so
 59 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
 60 
 61 #
 62 Include conf.modules.d/*.conf

使用lineinfile模块来修改php的配置文件

[root@ansible ansible]# ansible web1 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="listen = /usr" line="listen = 0.0.0.0:9000"'
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}

[root@ansible ansible]# ansible web1 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="listen.allowed_clients = 127.0.0.1" line="listen.allowed_clients = 192.168.142.99"'
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}

把php测试文件index.php放到php服务器上

[root@ansible ~]# cat index.php 
<?php
phpinfo();
?>

[root@ansible ~]# ansible web1 -m copy -a 'src=index.php dest=/www/html/'
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "b3297c8ddc22e97b622dace5e84c4e990bac811b",
    "dest": "/www/html/index.php",
    "gid": 0,
    "group": "root",
    "md5sum": "24f1084a4efde84a9884c917c92e80da",
    "mode": "0644",
    "owner": "root",
    "size": 682,
    "src": "/root/.ansible/tmp/ansible-tmp-1626639490.1302476-1569650-63636623964637/source",
    "state": "file",
    "uid": 0
}

网站源码要放在php服务端的/var/www/html目录
httpd服务配置添加远程调用php服务

[root@apache html]# vim /etc/httpd/conf/httpd.conf
 # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php
    Proxyrequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.142.99:9000/var/www/html/$1
                                                           注:此目录是php服务端站点的存放位置
    #

修改php服务监听方式

[root@php ~]# vim /etc/php-fpm.d/www.conf
 
 37 ; Note: This value is mandatory.
 38 ;listen = /run/php-fpm/www.sock
 39 listen = 9000
 40 
 41 ; Set listen(2) backlog.
 42 ; Default Value: 511
 43 ;listen.backlog = 511

使用service模块重启服务

[root@ansible ~]#  ansible web2 -m service -a 'name=httpd state=restarted'
192.168.142.98 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "httpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestamp": "Sun 2021-07-18 14:18:14 EDT",
        "ActiveEnterTimestampMonotonic": "1687750076",
        "ActiveExitTimestamp": "Sun 2021-07-18 14:56:07 EDT",
        "ActiveExitTimestampMonotonic": "3960529872",
        "ActiveState": "failed",
        "After": "nss-lookup.target tmp.mount -.mount system.slice basic.target systemd-tmpfiles-setup.service sysinit.target network.target systemd-journald.socket remote-fs.target httpd-init.service",
        "AllowIsolate": "no",

[root@ansible ansible]# ansible web1 -m service -a 'name=php-fpm state=restarted'
192.168.142.99 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "php-fpm",
    "state": "started",
    "status": {
        "ActiveEnterTimestamp": "Sun 2021-07-18 14:20:26 EDT",
        "ActiveEnterTimestampMonotonic": "1818573356",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",
        "After": "-.mount network.target syslog.target systemd-tmpfiles-setup.service tmp.mount system.slice systemd-journald.socket basic.target sysinit.target",
        "AllowIsolate": "no",
        "AllowedCPUs": "",
        "AllowedMemoryNodes": "",

[root@ansible ansible]# ansible web3 -m service -a 'name=mariadb state=restarted'
192.168.142.97 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "mariadb",
    "state": "started",
    "status": {
        "ActiveEnterTimestamp": "Sun 2021-07-18 14:21:44 EDT",
        "ActiveEnterTimestampMonotonic": "1896786251",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",
        "After": "systemd-tmpfiles-setup.service -.mount sysinit.target tmp.mount network.target systemd-journald.socket basic.target system.slice",
        "AllowIsolate": "no",

访问ip验证
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值