精华内容
下载资源
问答
  • Ansible部署LAMP

    2021-07-25 17:21:11
    Ansible部署LAMP 文章目录Ansible部署LAMP环境说明准备工作1. 为ansible主机安装epel源2. 安装ansible3. 创建ansible清单4. 测试ansible主机与受管主机是否互通编写playbook编写index.php文件执行playbook测试 这里...

    Ansible部署LAMP


    这里我们采用循环变量的方式来部署LAMP架构

    环境说明

    IP主机名备注
    192.168.100.100node1ansible主机
    192.168.100.110node2受管主机

    准备工作

    1. 为ansible主机安装epel源

    [root@node1 ~]# yum -y install epel-release
    ......略
    Installed:
      epel-next-release-8-11.el8.noarch                                   epel-release-8-11.el8.noarch                                  
    
    Complete!
    

    2. 安装ansible

    [root@node1 ~]# yum -y install ansible
    ......略
    Installed:
      ansible-2.9.23-1.el8.noarch                          libsodium-1.0.18-2.el8.x86_64        python3-babel-2.5.1-6.el8.noarch       
      python3-bcrypt-3.1.6-2.el8.1.x86_64                  python3-cffi-1.11.5-5.el8.x86_64     python3-cryptography-3.2.1-5.el8.x86_64
      python3-jinja2-2.10.1-3.el8.noarch                   python3-jmespath-0.9.0-11.el8.noarch python3-markupsafe-0.23-19.el8.x86_64  
      python3-paramiko-2.4.3-1.el8.noarch                  python3-pip-9.0.3-20.el8.noarch      python3-ply-3.9-9.el8.noarch           
      python3-pyasn1-0.3.7-6.el8.noarch                    python3-pycparser-2.14-14.el8.noarch python3-pynacl-1.3.0-5.el8.x86_64      
      python3-pytz-2017.2-9.el8.noarch                     python3-pyyaml-3.12-12.el8.x86_64    python3-setuptools-39.2.0-6.el8.noarch 
      python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64 sshpass-1.06-9.el8.x86_64           
    
    Complete!
    

    3. 创建ansible清单

    [root@node1 ~]# mkdir /opt/httpd
    [root@node1 ~]# cd /opt/httpd
    [root@node1 httpd]# vim inventory
    [httpd]
    192.168.100.110
    

    4. 测试ansible主机与受管主机是否互通

    [root@node1 httpd]# ansible all -m ping
    192.168.100.110 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
    

    至此,准备工作已经完成,下面开始部署LAMP

    编写playbook

    [root@node1 lamp]# mkdir playbook
    [root@node1 lamp]# cd playbook/
    [root@node1 playbook]# vim install_lamp.yml
    ---
    - hosts: 192.168.100.110
      gather_facts: no
      vars: 
        packages: 
          - httpd
          - mariadb
          - mariadn-server
          - php*
        service: 
          - httpd
          - mariadb
          - php-fpm
      tasks: 
        - name: stop firewalld
          service: 
            name: firewalld
            state: stopped
            enabled: no
    
        - name: configure selinux
          lineinfile: 
            path: /etc/selinux/config
            regexp: '^SELINUX='
            line: SELINUX=disabled
    
        - name: install httpd,mysql,php
          yum: 
            name: "{{ item }}"
            state: present
          loop: 
            "{{ packages }}"
    
        - name: copy file
          copy: 
            src: /opt/lamp/file/index.php
            dest: /var/www/html
        
        - name: service restart
          service: 
            name: "{{ item }}"
            state: restarted
            enabled: yes
          loop: 
            "{{ service }}"      
    

    编写index.php文件

    [root@node1 ~]# vim /opt/lamp/file/index.php
    <?php
    phpinfo();
    ?>
    

    执行playbook

    [root@node1 playbook]# ansible-playbook install_lamp.yml 
    
    PLAY [192.168.100.110] **************************************************************************************************************
    
    TASK [stop firewalld] ***************************************************************************************************************
    ok: [192.168.100.110]
    
    TASK [configure selinux] ************************************************************************************************************
    ok: [192.168.100.110]
    
    TASK [install httpd,mysql,php] ******************************************************************************************************
    ok: [192.168.100.110] => (item=httpd)
    ok: [192.168.100.110] => (item=mariadb)
    changed: [192.168.100.110] => (item=mariadb-server)
    ok: [192.168.100.110] => (item=php*)
    
    TASK [copy file] ********************************************************************************************************************
    changed: [192.168.100.110]
    
    TASK [service restart] **************************************************************************************************************
    changed: [192.168.100.110] => (item=httpd)
    changed: [192.168.100.110] => (item=mariadb)
    changed: [192.168.100.110] => (item=php-fpm)
    
    PLAY RECAP **************************************************************************************************************************
    192.168.100.110            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    [root@node1 playbook]# 
    

    测试

    展开全文
  • ansible部署LAMP架构

    2021-07-21 09:18:00
    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。 ansible是基于模块工作的,本身...

    简介

    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
    ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
    (1)、连接插件connection plugins:负责和被监控端实现通信;
    (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
    (3)、各种模块核心模块、command模块、自定义模块;
    (4)、借助于插件完成记录日志邮件等功能;
    (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务

    Ansible的特点有:
    1、不需要在部署机器上安装客户端
    2、没有服务端,使用时直接执行命令即可
    3、基于模块工作,可以使用任意语言对模块进行开发
    4、使用yaml语言来定制编排剧本
    5、可实现多级指挥
    6、支持sudo
    7、支持邮件、日志等多种功能

    准备四台主机

    配置IP地址和hostname,关闭防火墙和selinux,服务器时间同步
    修改/etc/hosts配置文件进行hostname和IP地址映射

    hostnameip
    httpd192.168.153.130
    php192.168.153.131
    mysql192.168.153.132
    ansible192.168.153.129

    添加域名解析

    [root@ansible ~]# cat /etc/hosts 
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.153.130 httpd
    192.168.153.131 php
    192.168.153.132 mysql
    
    

    配合SSH免密登陆

    [root@ansible ~]# ssh-keygen -t rsa  #生成密钥,指定加密方式 ,下面默认一路回车即可
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Created directory '/root/.ssh'.
    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:m9UgEbkFLID4MnlRrsmn76K0ARRhb6NEScTM1etAfEM root@ansible
    The key's randomart image is:
    +---[RSA 3072]----+
    |*O=+=E .++       |
    |+==+ +. o..      |
    | =.=o o..o.      |
    |*.=+..  .. o     |
    |.=+ +   S . .    |
    |.  o .   +       |
    | o.     o        |
    |. +.             |
    |.o oo            |
    +----[SHA256]-----+
    
    [root@ansible ~]# ssh-copy-id root@httpd   #传递公钥
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
    The authenticity of host 'httpd (192.168.153.130)' can't be established.
    ECDSA key fingerprint is SHA256:+wH81RHiBmLpbkuk2OWGZxVRziiaNwJ9KAVjGtEM8zs.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    /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@httpd's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'root@httpd'"
    and check to make sure that only the key(s) you wanted were added.
    
    注意:php主机和mysql主机的传递公钥过程一样,因为太长在这里省略
    

    安装ansible

    [root@ansible ~]#yum install epel-release #安装epel源
    [root@ansible ~]#yum makecache
    [root@ansible ~]#yum install ansible
    [root@ansible ~]#systemctl start ansible
    [root@ansible ~]#systemctl enable ansible
    

    配置ansible

    配置主机组信息

    [root@ansible ]# vi /opt/inventory
    [webserver]
    httpd
    php
    mysql
    
    

    创建项目文件夹

    [root@ansible ~]# tree /opt/
    /opt/
    ├── ansible.cfg  #ansible配置文件,将inventory  改为当前目录下的inventory
    ├── inventory  #存放主机的清单文件
    └── playbook  
        ├── firewalld     
        │   └── firewalld.yaml  #关闭防火墙
        ├── httpd
        │   └── install
        │       ├── httpd.conf  #已修改好的配置文件,用于替换安装的配置文件,文章最下面有修改方式
        │       └── httpd-install.yaml #安装httpd服务,并重启
        ├── mariadb
        │   └── mariadb.yaml  #安装mariadb服务,并重启
        ├── php
        │   ├── index
        │   │   └── index.yaml  #创建index.php文件
        │   └── install
        │       ├── php-fpm-install.yaml #安装php服务,并重启
        │       └── www.conf  #已修改好的配置文件,用于替换安装的配置文件,文章最下面有修改方式
        ├── repo
        │   └── repo.yaml  #下载网络源
        └── site
            └── site.yaml  #一键部署lamp架构
    
    
    

    编写关闭防火墙firewalld.yaml文件

    
    [root@ansible ~]# vim /opt/playbook/firewalld/firewalld.yaml 
    ---
      - name: firewalld
        hosts: webserver
        tasks:
    
          - name: stop firewalld
            service:
              name: firewalld
              state: stopped
              enabled: no
    
          - name: stop selinux
            lineinfile:
              path: /etc/selinux/config
              regexp: "^SELINUX=enforcing"
              line: SELINUX=disabled
    
          - name: setenforce 0
            shell: "setenforce 0"
            failed_when: false
    
    

    编写下载网络源repo.yaml文件

    [root@ansible ~]# vim /opt/playbook/repo/repo.yaml
    ---
      - name: wget yum
        hosts: webserver
        tasks:
    
          - name:  test
            get_url:
              url: https://mirrors.aliyun.com/repo/Centos-8.repo
              dest: /etc/yum.repos.d/CentOS-Base.repo
    
    

    编写安装httpd服务httpd-install.yaml文件

    [root@ansible ~]# vi /opt/playbook/httpd/install/httpd-install.yaml
    ---
      - name: httpd-install
        hosts: httpd
        tasks:
          - name: install httpd service
            yum:
               name: httpd*
               state: present
    
          - name: copy httpd.conf
            copy:
              src: httpd.conf
              dest: /etc/httpd/conf/httpd.conf
    
          - name: start httpd
            service:
              name: httpd
              state: started
              enabled: yes
    
    
    

    编写安装php服务php-fpm-install.yaml文件

    [root@ansible ~]# vi /opt/playbook/php/install/php-fpm-install.yaml
    ---
      - name: php-install
        hosts: php
        tasks:
          - name: install php-fpm service
            yum:
              name: php*
              state: present
    
          - name: copy www.conf
            copy:
              src: www.conf
              dest: /etc/php-fpm.d/www.conf
    
          - name: start php-fpm service
            service:
              name: php-fpm
              state: started
              enabled: yes
    
    

    编写创建index.php文件index.yaml文件

    [root@ansible ~]# vi /opt/playbook/php/index/index.yaml
    ---
      - name: index
        hosts: php
        tasks:
          - name: Create directory /web/www/html
            file:
              path: /web/www/html
              state: directory
              mode: 0775
    
          - name:  Create a file if it does not exist
            file:
              path: /web/www/html/index.php
              state: touch
              mode: 0775
    
          - name: index.php
            lineinfile:
              path: /web/www/html/index.php
              line: |
                <?php
                  phpinfo();
                ?>
    
    
    

    编写安装数据库服务mariadb.yaml文件

    [root@ansible ~]# vi /opt/playbook/mariadb/mariadb.yaml 
    ---
      - name: install mariadb service
        hosts: mysql
        tasks:
          - name: install mariadb
            yum:
             name: mariadb*
             state: present
    
          - name: start mariadb
            service:
              name: mariadb
              state: started
              enabled: yes
    
    

    编写一键部署文件site.yaml文件

    [root@ansible ~]# vi /opt/playbook/site/site.yaml 
    ---
      - name: stop firewalld
        import_playbook: /opt/playbook/firewalld/firewalld.yaml
      - name: wget
        import_playbook: /opt/playbook/repo/repo.yaml
      - name: Install httpd service
        import_playbook: /opt/playbook/httpd/install/httpd-install.yaml
      - name: Install php-fpm service
        import_playbook: /opt/playbook/php/install/php-fpm-install.yaml
      - name: touch index.php
        import_playbook: /opt/playbook/php/index/index.yaml
      - name: Install mariadb service
        import_playbook: /opt/playbook/mariadb/mariadb.yaml
    
    

    执行playbook文件

    [root@ansible opt]# ansible-playbook playbook/site/site.yaml  #只能在/opt目录下才能执行此命令
    

    在浏览器用HTTP主机IP访问

    在这里插入图片描述

    修改httpd服务默认配置文件httpd.conf文件

    在默认配置文件最后加入下面的参数
    [root@ansible ]# vi /opt/playbook/httpd/install/httpd.conf
    <VirtualHost *:80>
    DocumentRoot "/web/www/html"  #php主机上的网站站点目录,用于后面存放index.php文件
    ServerName www.chenshunli.com  #域名 可自行更改
     ProxyRequests Off
     ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.153.131:9000/web/www/html/$1  #此行IP地址为php主机的IP地址
    <Directory "/web/www/html">
    Options none
    AllowOverride none
    Require all granted
    </Directory>
    </VirtualHost>
    
    注意:以上是在配置文件最后加入的内容,以下只有带有注释 ~~添加~~  的两行需要添加 
    
    
    搜索AddType,添加以下内容
        # If the AddEncoding directives above are commented-out, then you
        # 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 .php        #添加此行
        AddType application/x-httpd-php-source .phps        #添加此行
    
    

    修改php服务的默认配置文件www.conf文件

    编辑配置文件,过滤下面两行内容并修改,保存退出即可
    [root@ansible opt]# vi /opt/playbook/php/install/www.conf
    
    listen = 192.168.153.131:9000  #此处IP地址改为php主机本机的IP地址
    
    listen.allowed_clients = 192.168.153.130 #此处改为httpd主机的IP地址
    
    展开全文
  • docker-compose-ansible-lamp:关于如何使用docker-compose和ansible部署Lamp服务器的项目
  • Ansible部署lamp架构

    2021-07-18 12:09:13
    Ansible部署LAMP分离部署LAMP简介 LAMP简介 LAMP是指一组通常一起使用来运行动态网站或者服务器的开源软件名称首字母缩写: Linux Apache Mariadb或者MySQL PHP、Python或Perl

    LAMP简介

    LAMP是指一组通常一起使用来运行动态网站或者服务器的开源软件名称首字母缩写:

    • Linux
    • Apache
    • mariadb或者MySQL
    • PHP、Python或者Perl

    实验环境

    主机IP地址
    ansible192.168.172.167
    apache192.168.172.142
    mysql192.168.172.143
    php192.168.172.144

    四台机器关闭防火墙和selinux


    实验步骤

    安装

    1. 安装Ansible

    [root@ansible ~]# yum -y install https://dl.fedoraproj
    ect.org/pub/epel/epel-release-latest-8.noarch.rpm
    [root@ansible ~]# yum -y install ansible
    

    1.1 受控主机写入清单

    [root@ansible ansible]# vim inventory  
    [apache]
    192.168.172.142
    
    [mysql]
    192.168.172.143
    
    [php]
    192.168.172.144
    

    1.2 ssh免密登录

    [root@ansible ansible]# ssh-keygen -t rsa
    [root@ansible ansible]# ssh-copy-id root@192.168.172.142
    [root@ansible ansible]# ssh-copy-id root@192.168.172.143
    [root@ansible ansible]# ssh-copy-id root@192.168.172.144
    

    2. 安装apache

    [root@ansible ansible]# ansible http -m yum -a 'name=httpd state=present'
    192.168.172.142 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "msg": "Nothing to do",
        "rc": 0,
        "results": []
    }
    
    

    2.1开启Apache服务并设置开机启动

    [root@ansible ansible]# ansible http -m service -a 'name=httpd state=started enabled=yes'
    192.168.172.142 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "enabled": true,
        "name": "httpd",
        "state": "started",
        "status": {
    
    ......
    

    2.2 关闭防火墙及selinux

    [root@ansible ansible]# ansible http -m service -a 'name=firewalld state=stopped enabled=no'
    192.168.172.142 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "enabled": false,
        "name": "firewalld",
        "state": "stopped",
        "status": {
    ......
    

    2.3 访问

    在这里插入图片描述

    3. 安装MySQL

    //安装mariadb
    [root@ansible ansible]# ansible mysql -m yum -a 'name=mariadb state=present'
    192.168.172.143 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "msg": "",
        "rc": 0,
        "results": [
    
    ......
    
    
    //安装mariadb-server
    [root@ansible ansible]# ansible mysql -m yum -a 'name=mariadb-server state=present'
    192.168.172.143 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "msg": "",
        "rc": 0,
        "results": [
    
    ......
    

    3.1 启动MySQL并设置开机自启

    [root@ansible ansible]# ansible mysql -m service -a 'name=mariadb state=started enabled=yes'
    192.168.172.143 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "enabled": true,
        "name": "mariadb",
        "state": "started",
        "status": {
    
    ......
    

    4. 安装php

    //安装php
    [root@ansible ansible]# 
    [root@ansible ansible]# ansible php -m yum -a 'name=php state=present'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "msg": "",
        "rc": 0,
        "results": [
    
    //安装php组件
    [root@ansible ansible]# ansible php -m yum -a 'name=php-* state=present'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "msg": "",
        "rc": 0,
        "results": [
    
    //安装curl
    [root@ansible ansible]# ansible php -m yum -a 'name=curl state=present'
    192.168.172.144 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "msg": "Nothing to do",
        "rc": 0,
        "results": []
    }
    [root@ansible ansible]# ansible php -m yum -a 'name=curl-devel state=present'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "msg": "",
        "rc": 0,
        "results": [
            "Installed: libcurl-devel-7.61.1-12.el8.x86_64"
        ]
    }
    
    ......
    

    配置

    1. 配置httpd

    1.1 编写httpd配置文件

    先在ansible主机上将配置文件写好,再通过copy模块将配置文件传输到node2上

    [root@ansible ansible]# cat hello.conf 
    <VirtualHost 192.168.172.142:80>
        DocumentRoot "/etc/httpd/conf.d/"
        ServerName www.helloworld.com
        ProxyRequests off
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.172.144:9000/var/www/html/$1
        <Directory "/var/www/html/">
            Options none
            AllowOverride none
            Require all granted
        </Directory>
    </VirtualHost>
    
    
    
    
    [root@ansible ansible]# ansible http -m copy -a 'src=/etc/ansible/hello.conf dest=/etc/httpd/conf.d/ mode=0755'
    192.168.172.142 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "a2fdf4f938ee147eab9867325ee00ae9c1194df0",
        "dest": "/etc/httpd/conf.d/hello.conf",
        "gid": 0,
        "group": "root",
        "md5sum": "9c9e850994228cfd3d1f324781558895",
        "mode": "0755",
        "owner": "root",
        "secontext": "system_u:object_r:httpd_config_t:s0",
        "size": 344,
        "src": "/root/.ansible/tmp/ansible-tmp-1626891082.2830656-129763-142187681487216/source",
        "state": "file",
        "uid": 0
    }
    
    [root@ansible ansible]# ansible http -m  lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="^    AddType application/x-" line="    AddType application/x-httpd-php .php" '
    192.168.172.142 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "backup": "",
        "changed": true,
        "msg": "line added"
    }
    
    
    [root@ansible ansible]# ansible http -m  lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="^    AddType application/x-" line="    AddType application/x-httpd-php-source .phps" 
    '
    192.168.172.142 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "backup": "",
        "changed": true,
        "msg": "line added"
    }
    
    
    
    [root@ansible ansible]# ansible http -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^    DirectoryIndex" line="    DirectoryIndex index.html index.php"'
    192.168.172.142 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "backup": "",
        "changed": true,
        "msg": "line replaced"
    }
    
    

    2. 配置php

    2.1 编写php测试页

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

    2.2 编写修改php配置

    [root@ansible ansible]# ansible php -m file -a  'path=/var/www/html mode=0777
    > '
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "gid": 0,
        "group": "root",
        "mode": "0777",
        "owner": "root",
        "path": "/var/www/html",
        "secontext": "system_u:object_r:httpd_sys_content_t:s0",
        "size": 6,
        "state": "directory",
        "uid": 0
    }
    
    [root@ansible ansible]# ansible php -m copy -a 'src=/etc/ansible/index.php dest=/var/www/html mode=0777'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "26af88945e23289d15e128606a29932b3d78787c",
        "dest": "/var/www/html/index.php",
        "gid": 0,
        "group": "root",
        "md5sum": "62210a938d0199092c2d3976a45bf86d",
        "mode": "0777",
        "owner": "root",
        "secontext": "system_u:object_r:httpd_sys_content_t:s0",
        "size": 22,
        "src": "/root/.ansible/tmp/ansible-tmp-1626891809.6933286-152862-250210448981018/source",
        "state": "file",
        "uid": 0
    }
    
    
    [root@ansible ansible]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen =" line="listen = 192.168.172.144:9000" '
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "backup": "",
        "changed": true,
        "msg": "line replaced"
    }
    
    [root@ansible ansible]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen.allowed_clients =" line="listen.allowed_clients = 192.168.172.142" '
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "backup": "",
        "changed": true,
        "msg": "line replaced"
    }
    

    2.3 关闭防火墙及selinux

    [root@ansible ansible]# ansible php -m  service -a 'name=firewalld state=stopped enabled=no'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "enabled": false,
        "name": "firewalld",
        "state": "stopped",
        "status": {
    ......
    
    
    [root@ansible ansible]# ansible php -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "backup": "",
        "changed": true,
        "msg": "line replaced"
    }
    

    重启服务

    //重启http
    [root@ansible ansible]# ansible http -m service -a 'name=httpd state=restarted'
    192.168.172.142 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "name": "httpd",
        "state": "started",
        "status": {
    ......
    
    
    //重启php
    [root@ansible ansible]# ansible php -m service -a 'name=php-fpm  state=restarted'
    192.168.172.144 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "name": "php-fpm",
        "state": "started",
        "status": {
    

    测试

    在这里插入图片描述

    展开全文
  • Ansible部署LAMP.pdf

    2021-09-29 10:32:28
    搭建环境
  • Ansible部署lamp

    2021-07-19 18:32:28
    Ansible部署lamp 安装环境 主机 ip ansible 192.168.136.224 apache 192.168.136.219 mysql 192.168.136.221 php 192.168.136.223 添加受控主机 [root@ansible ansible]# vim inventory [root@...

    Ansible部署lamp

    安装环境

    主机ip
    ansible192.168.136.224
    apache192.168.136.219
    mysql192.168.136.221
    php192.168.136.223

    添加受控主机

    [root@ansible ansible]# vim inventory 
    [root@ansible ansible]# cat inventory 
    [mysql]
    192.168.136.221
    [apache]
    192.168.136.219
    [php]
    192.168.136.223
    

    测试连接状态

    [root@ansible ansible]# ansible all -m ping
    192.168.136.219 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    192.168.136.221 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    192.168.136.223 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    
    

    下载httpd、php、mysql服务

    [root@ansible ansible]# ansible apache -m yum -a 'name=httpd* state=present'
    
    [root@ansible ansible]# ansible mysql -m yum -a 'name=mariadb* state=present'
     
    [root@ansible ansible]# ansible php -m yum -a 'name=php* state=present'
    
    

    配置httpd、php

    配置httpd
    [root@ansible ansible]# ansible apache -m shell -a "sed -i '/proxy_module/s/#//g' /etc/httpd/conf.modules.d/00-proxy.conf"
    
    [root@ansible ansible]# ansible apache -m shell -a "sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd/conf.modules.d/00-proxy.conf"
    
    [root@ansible ansible]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^#Listen 80" lie="Listen 80"'
    
    [root@ansible ansible]# ansible apache -m shell -a "sed -i '/www.example/s/#//g' /etc/httpd/conf/httpd.conf"
    
    [root@ansible ansible]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType" line="AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps"'
    
    [root@ansible ansible]# ansible apache -m shell -a "sed -i '/index.html/s/index.html/index.php index.html/g' /etc/httpd/conf/httpd.conf"
    
    [root@ansible ansible]# ansible apache -m shell -a "find / -name *vhosts.conf"
    
    [root@ansible ansible]# ansible apache -m shell -a "mv /usr/share/doc/httpd/httpd-vhosts.conf /etc/httpd/conf.d/"
    
    [root@ansible ansible]# ansible apache -m shell -a ">  /etc/httpd/conf.d/httpd-vhosts.conf"
    
    [root@ansible ansible]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf.d/httpd-vhosts.conf line="<VirtualHost *:80>\n    DocumentRoot "/var/www/html"\n    ServerName 192.168.136.223.com\n    ProxyRequests Off\n    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.136.223:9000/var/bin/php/$1\n    <Directory "/var/bin/php/">\n      Options none\n      AllowOverride none\n      Require all granted\n    </Directory>\n</VirtualHost>"'
    [root@apache html]# cd /etc/httpd/conf/
    [root@apache html]#  vim httpd.conf 
        # If the AddEncoding directives above are commented-out, then you
        # 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.136.223:9000/var/www/html/$1
    
    php配置
    [root@ansible ansible]# ansible php -m lineinfile -a "path=/var/www/html/index.php line=<?php\n    phpinfo();\n?> create=yes"
    
    [root@ansible ansible]# ansible php -m shell -a "sed -i '/www.sock/s/listen/;listen/g' /etc/php-fpm.d/www.conf"
    [root@ansible ansible]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="listen.allowed_clients = 127.0.0.1" line="listen.allowed_clients = 0.0.0.0:9000"'
    
    
    
    

    关闭防火墙

    [root@ansible ansible]# ansible all -m shell -a 'systemctl stop firewalld && systemctl disable firewalld '
    

    启动服务

    [root@ansible ~]# ansible apache -m shell -a "systemctl start httpd-service"
    [root@ansible ~]# ansible php -m shell -a "systemctl start php-fpm-service"
    

    效果测试

    在这里插入图片描述

    展开全文
  • https://github.com/ansible/ansible-examples/tree/master/lamp_simple 目录结构: [root@maple-c8-n1 lamp_simple]# tree . ├── group_vars │ ├── all │ └── dbservers ├── hosts ├── LICENSE.md...
  • ansible 中利用playbook部署lamp环境

    千次阅读 2018-09-18 20:08:11
    基本文件目录如下: httpd所需软件:httpd mariadb mariadb-server php php-mysql gd ... [root@ansible role]# ls httpd site_nginx.retry site.retry mariadb php-apache site_apache.yaml  [root@ansib...
  • Ansible部署LAMP架构

    2021-07-19 14:53:35
    Ansible实现LAMP的分离部署1.配置主控机和受控主机2.安装httpd服务3.安装MySQL数据库4.安装php及常见组件 1.配置主控机和受控主机 环境说明: 主机名 作用 IP master Ansible控制主机 192.168.237.160 ...
  • Ansible部署LAMP分离

    2021-07-19 13:58:13
    环境需求 四台主机 系统 IP 主机名 服务 Redhat8 192.168.47.157 marter ansible Redhat8 192.168.47.159 ...1.在主机192.168.47.157上配置ansible自动化工具 [root@marter ~]# yum -y install e

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,164
精华内容 465
关键字:

ansible部署lamp