ansible用playbook进行lamp分离部署

这个博客展示了如何使用Ansible自动化配置Apache服务器,包括设置虚拟主机、安装和配置HTTPD服务。同时,它还涵盖了安装和启动MariaDB数据库服务的步骤。此外,还演示了如何通过Ansible安装PHP,配置PHP-FPM服务,创建测试页面并确保服务正常运行。
摘要由CSDN通过智能技术生成

编辑apache主机playbook文件

[root@ansible playbook]# cat httpd.conf     ##需要复制过去的虚拟站点文件
<VirtualHost *:80>
    DocumentRoot "/var/www/html/"
    ServerName web.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.10.203:9000/www/html/$1
</VirtualHost>
[root@ansible playbook]#
[root@ansible playbook]# cat apache.yml 
---
- name: apache install and config
  hosts: apache
  tasks:
    - name: stop and disable firewalld
      service: 
        name: firewalld
        state: stopped
        enabled: no

    - name: disable selinux
      lineinfile:
        path: /etc/selinux/config
        regexp: "^SELINUX="
        line: "SELINUX=disabled"
        state: present
          
    - name: install httpd
      yum:
        name: httpd
        state: latest

    - name: config
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        insertafter: "    AddType application/x-gzip .gz .tgz"
        line: |
          AddType application/x-httpd-php .php
          AddType application/x-httpd-php-source .phps

    - name: config2
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        insertafter: "# LoadModule foo_module modules/mod_foo.so"
        line: |
          LoadModule proxy_module modules/mod_proxy.so
          LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

    - name: config3
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^    DirectoryIndex"
        line: "    DirectoryIndex index.php index.html"

    - name: virtualhost
      copy:
        src: httpd.conf
        dest: /etc/httpd/conf.d/

    - name: serviec start and enable
      service:
        name: httpd
        state: started
        enabled: yes
[root@ansible playbook]#

编辑mysql的playbook文件

[root@ansible playbook]# cat mysql.yml 
---
- name: mysql node
  hosts: mysql
  tasks:
    - name: stop and disable firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no

    - name: disable selinux
      lineinfile:
        path: /etc/selinux/config
        regexp: "^SELINUX="
        line: "SELINUX=disabled"
        state: present

    - name: install mariadb-server
      yum:
        name: mariadb-server
        state: latest

    - name: install mariadb
      yum:
        name: mariadb
        state: latest

    - name: mysql start and enabled
      service:
        name: mariadb
        state: started
        enabled: yes
[root@ansible playbook]#

编辑php的playbook文件

[root@ansible playbook]# cat index.php   ##需要复制过去的测试页文件
<?php
phpinfo();
?>
[root@ansible playbook]#
[root@ansible playbook]# cat php.yml 
---
- name: php node
  hosts: php
  tasks:
    - name: stop and disabled firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no

    - name: disable selinux
      lineinfile:
        path: /etc/selinux/config
        regexp: "^SELINUX="
        line: "SELINUX=disabled"
        state: present

    - name: install php 
      yum:
        name: php*
        state: latest

    - name: config 
      lineinfile:
        path: /etc/php-fpm.d/www.conf
        regexp: "^listen = "
        line: "listen = 0.0.0.0:9000"

    - name: config2
      lineinfile:
        path: /etc/php-fpm.d/www.conf
        regexp: "listen.allowed_clients = 127.0.0.1"
        line: "listen.allowed_clients = 192.168.10.201"

    - name: create directory
      file:
        path: /www/html/
        state: directory

    - name: file index.php 
      copy:
        src: index.php
        dest: /www/html

    - name: restart and enabled service
      service:
        name: php-fpm
        state: started
        enabled: yes

[root@ansible playbook]#

测试结果:

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值