2023年5月

---
- hosts: 192.168.26.21
  gather_facts: True

  tasks:
    # common tasks
    - name: Install epel-release
      yum: 
        name: epel-release
        state: present 

    - name: Temporary Disable SELinux
      command: setenforce 0

    - name: Disable SELinux
      shell: sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

    # Install apache server
    - name: Install httpd
      yum:
        name: httpd
        state: present
      
    - name: Start httpd
      service:
        name: httpd
        state: started
        enabled: True

    - name: Permit 80/tcp port
      firewalld:
        service: http
        permanent: True
        immediate: True
        state: enabled

    #Install php82
    - name: Install epel-release-latest-7
      yum:
        name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
        state: present
    
    - name: Install remirepo
      yum:
        name: https://rpms.remirepo.net/enterprise/remi-release-7.rpm
        state: present
      
    - name: Enable remi-php82 repo
      shell: yum-config-manager --enable remi-php82

    - name: Install php82
      yum:
        name:
          - php82-php
          - php82-php-mbstring
          - php82-php-mysqlnd
          - php82-php-gd
          - php82-php-xml
        state: present

    - name: Install mariadb-server
      yum:
        name: mariadb-server
        state: present
      
    - name: Start mariadb-server
      service:
        name: mariadb
        state: started
        enabled: True
    
    - name: Install pip
      yum:
        name: python2-pip
        state: present

    - name: Install Python Library
      pip:
        name: PyMySQL==0.10.0
        state: present

    - name: Create Database
      mysql_db:
        name: wordpressdb
        state: present

    - name: Create database user
      mysql_user:
        name: wordpress
        password: wordpress
        priv: 'wordpressdb.*:ALL'
        state: present

    # Install wordpress
    - name: Install wordpress
      get_url:
        url: https://wordpress.org/wordpress-6.2.tar.gz
        dest: /tmp/wordpress-6.2.tar.gz
        checksum: md5:34f279efe623025641bc11a69e3c02fa

    - name: Extract wordpress-6.X.tar.gz into /var/www/wordpress
      unarchive:
        src: /tmp/wordpress-6.2.tar.gz
        dest: /var/www/
        remote_src: True

    - name: Recursive Modify file permission
      file:
        path: /var/www/wordpress
        owner: apache
        group: apache
        mode: '0766'
        recurse: True
        state: directory
      
    - name: Add apache virtual host config
      copy:
        src: wordpress.conf
        dest: /etc/httpd/conf.d/wordpress.conf

    - name: Restart httpd
      service:
        name: httpd
        state: restarted

    - name: Add wordpress config
      copy:
        src: wp-config.php
        dest: /var/www/wordpress/wp-config.php
        owner: apache
        group: apache
        mode: '0744'

一、课前准备

二、环境简述

  • node20, 192.168.26.20/24, gw: 192.168.26.2, ansible管理端
  • node21, 192.168.26.21/24, gw: 192.168.26.2,被管理端
  • node22, 192.168.26.22/24, gw: 192.168.26.2,被管理端
  • node23, 192.168.26.23/24, gw: 192.168.26.2, 被管理端

三、搭建流程

  1. 最小化安装1台CentOS,安装后关机,给虚拟机创建一个快照;
  2. 从快照克隆新虚拟机,使用链接克隆方式,克隆出4台虚拟机;
  3. 打开这4台虚拟机,配置静态ip地址、主机名,完成后开机;
  4. 在node20上安装Ansible,使用ssh-keygen生成服务端公钥对;
  5. 在node20上使用copy-ssh-id命令,与node21/22/23配置无密码访问;
  6. 完成后关机,对node21/22/23创建虚拟机快照;

四、本地配置

  • 下载安装git客户端msi安装包, https://www.git-scm.com/
  • 下载安装VSCode,https://code.visualstudio.com/
  • 在VSCode插件库里安装Remote-SSH插件
  • git客户端安装后桌面右键Git bash here,使用ssh-keygen生成密钥对,使用ssh-copy-id连接node20配置无密码登录
  • 打开VScode,在remote-ssh插件配置文件中填写如下内容,配置文件位置:C:\Users\lixm.ssh\config

    Host 192.168.26.20
    HostName 192.168.26.20
    User root
    Port 22
    IdentityFile "C:\Users\lixm\.ssh\id_rsa" 
  • 完成后即可在本地Windows远程编辑Linux主机中的文件

五、 LAMP环境搭建步骤

1. Install Base Environment

  • epel-release
  • turn off selinux

2. Install Apache Server

  • Install httpd
  • Set httpd service enable and start service
  • Open http 80/tcp port

3. Install PHP

  • Install remi-repo
  • Install php82 packages
  • Restart httpd

4. Install Mariadb

  • Install mariadb-server
  • Set mariadb service enable and start service
  • Install Python Library PyMySQL
  • Create database wordpressdb
  • Create user wordpress@localhost and grant permission to access wordpressdb

5. Install Wordpress

  • Download wordpress package
  • Unarchive package to target directory
  • Recursive modify files permission
  • Upload httpd virtualhost config file
  • Restart httpd service
  • Upload wordpress config file

六、other

pip install PyMySQL==0.10.0 -i https://mirrors.aliyun.com/pypi/simple/