day01项目实战目标单机安装基于LNMP结构的WordPress网站基本环境准备配置nginx配置数据库服务部署wordpressweb与数据库服务分离准备数据库服务器迁移数据库配置额外的web服务器
主机名 | IP地址 |
---|---|
client01 | 192.168.88.10/24 |
web1 | 192.168.88.11/24 |
web2 | 192.168.88.12/24 |
web3 | 192.168.88.13/24 |
database | 192.168.88.21/24 |
nfs | 192.168.88.31/24 |
haproxy01 | 192.168.88.5 |
haproxy02 | 192.168.88.6 |
[root@myhost ~]# vm clone web1 # 克隆一台虚拟机
[root@myhost ~]# vm setip web1 192.168.88.11 # 设置ip地址
# 1. 创建工作目录
[root@pubserver ~]# mkdir -p project01/files
[root@pubserver ~]# cd project01/
# 2. 创建主配置文件、主机清单文件、yum配置文件
[root@pubserver project01]# vim ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false
[root@pubserver project01]# vim inventory
[webservers]
web1 ansible_host=192.168.88.11
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
[root@pubserver project01]# vim files/local88.repo
[BaseOS]
name = BaseOS
baseurl = ftp://192.168.88.240/dvd/BaseOS
enabled = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = ftp://192.168.88.240/dvd/AppStream
enabled = 1
gpgcheck = 0
[rpms]
name = rpms
baseurl = ftp://192.168.88.240/rpms
enabled = 1
gpgcheck = 0
[root@pubserver project01]# vim 01-upload-repo.yml
---
- name: config repos.d
hosts: all
tasks:
- name: delete repos.d
file:
path: /etc/yum.repos.d
state: absent
- name: create repos.d
file:
path: /etc/yum.repos.d
state: directory
mode: '0755'
- name: upload local88
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml
[root@pubserver project01]# vim 02-config-web1.yml
---
- name: config web1
hosts: webservers
tasks:
- name: install pkgs # 安装软件包
yum:
name:
- nginx
- mysql-server
- php-mysqlnd
- php-fpm
- php-json
state: present
- name: start service # 循环启动多个服务
service:
name: "{{item}}"
state: started
enabled: yes
loop:
- nginx
- php-fpm
- mysqld
[root@pubserver project01]# ansible-playbook 02-config-web1.yml
[root@web1 ~]# vim /usr/share/nginx/html/index.php
phpinfo();?>
[root@web1 ~]# rm -f /usr/share/nginx/html/index.php
# 1. 编写用于创建数据库和用户的脚本
[root@pubserver project01]# vim files/config_mysql.sh
#!/bin/bash
mysql -e "create database wordpress character set utf8mb4"
mysql -e "create user wpuser01@localhost identified by 'wordpress'"
mysql -e "grant all privileges on wordpress.* to wpuser01@localhost"
# 2. 通过ansible的script模块执行脚本
[root@pubserver project01]# vim 03-config-mysql.yml
---
- name: config mysql
hosts: web1
tasks:
- name: create database
script: files/config_mysql.sh
[root@pubserver project01]# ansible-playbook 03-config-mysql.yml
# 3. 测试账号,如果可以成功登陆mysql,则数据库和用户创建正确
[root@web1 ~]# mysql -uwpuser01 -pwordpress -hlocalhost wordpress
# 1. 拷贝wordpress到web1
[root@myhost ~]# scp /linux-soft/s2/zzg/project01_soft/wordpress-6.1.1-zh_CN.tar.gz 192.168.88.11:/root/
# 2. 解压并复制文件到nginx文档目录
[root@web1 ~]# tar xf wordpress-6.1.1-zh_CN.tar.gz
[root@web1 ~]# cp -r wordpress/* /usr/share/nginx/html/
# 3. php程序是由php-fpm处理的,php-fpm以apache身份运行
[root@web1 ~]# ps aux | grep php-fpm
root 5655 0.0 0.4 395620 19056 ? Ss 12:13 0:00 php-fpm: master process (/etc/php-fpm.conf)
apache 5670 0.0 0.3 412108 13812 ? S 12:13 0:00 php-fpm: pool www
# 4. 为了让php-fpm程序能对html目录进行读写操作,需要为他授予权限
[root@web1 ~]# chown -R apache:apache /usr/share/nginx/html/
注意:注销登陆后,如果再次登陆,需访问http://192.168.88.11/wp-login.php
# 1. 创建虚拟机并登陆
[root@myhost ~]# vm clone database
[root@myhost ~]# vm setip database 192.168.88.21
[root@pubserver project01]# vim inventory
[webservers]
web1 ansible_host=192.168.88.11
[dbs]
database ansible_host=192.168.88.21
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
# 1. 修改yum配置
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml
# 2. 安装数据库服务,并创建数据库及用户
[root@pubserver project01]# vim files/config_mysql2.sh
#!/bin/bash
mysql -e "create database wordpress character set utf8mb4"
mysql -e "create user wpuser01@'%' identified by 'wordpress'"
mysql -e "grant all privileges on wordpress.* to wpuser01@'%'"
[root@pubserver project01]# vim 04-config-database.yml
---
- name: config database
hosts: dbs
tasks:
- name: install mysql # 安装数据库服务
yum:
name: mysql-server
state: present
- name: start service # 启动数据库服务
service:
name: mysqld
state: started
enabled: yes
- name: create database
script: files/config_mysql2.sh
[root@pubserver project01]# ansible-playbook 04-config-database.yml
# 1. 在源服务器上备份数据库中的数据。备份数据库wordpress中的数据到wordpress.sql文件
[root@web1 ~]# mysqldump wordpress > wordpress.sql
# 2. 将备份文件拷贝到新数据库服务器
[root@web1 ~]# scp wordpress.sql 192.168.88.21:/root/
# 3. 在新数据库服务器上,导入数据。将wordpress.sql中的数据导入到wordpress数据库中
[root@database ~]# mysql wordpress < wordpress.sql
# 4. 修改php网站,将数据库服务器地址,指向新数据库服务器
[root@web1 ~]# vim /usr/share/nginx/html/wp-config.php
...略...
31 /** Database hostname */
32 define( 'DB_HOST', '192.168.88.21' );
...略...
# 5. 停止web1上的数据库服务,wordpress网站仍然可以访问
[root@web1 ~]# systemctl stop mysqld
[root@web1 ~]# yum remove -y mysql-server
# 6. 停止database上的数据库服务,wordpress将不能访问
[root@database ~]# mysql # 打开mysql命令行
mysql> show databases; # 查看有哪些数据库
mysql> use wordpress; # 切换到wordpress数据库
mysql> show tables; # 查看wordpress库中有哪些表
mysql> select * from wp_posts\G # 查看wp_posts表中的内容
[root@myhost ~]# vm clone web{2..3}
[root@myhost ~]# vm setip web2 192.168.88.12
[root@myhost ~]# vm setip web3 192.168.88.13
[root@pubserver project01]# vim inventory
[webservers]
web1 ansible_host=192.168.88.11
web2 ansible_host=192.168.88.12
web3 ansible_host=192.168.88.13
[dbs]
database ansible_host=192.168.88.21
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
# 1. 配置yum
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml
# 2. 配置web服务器
[root@pubserver project01]# vim 05-config-webservers.yml
---
- name: config webservers
hosts: webservers
tasks:
- name: install pkgs # 安装软件包
yum:
name:
- nginx
- php-mysqlnd
- php-fpm
- php-json
state: present
- name: start service # 循环启动多个服务
service:
name: "{{item}}"
state: started
enabled: yes
loop:
- nginx
- php-fpm
[root@pubserver project01]# ansible-playbook 05-config-webservers.yml
[root@pubserver project01]# vim 06-fetch-web1.yml
---
- name: copy web
hosts: web1
tasks:
- name: compress html # 压缩html目录到/root下
archive:
path: /usr/share/nginx/html
dest: /root/html.tar.gz
format: gz
- name: download html # 下载压缩文件
fetch:
src: /root/html.tar.gz
dest: files/
flat: yes
[root@pubserver project01]# ansible-playbook 06-copy-web.yml
[root@pubserver project01]# vim 07-deploy-web23.yml
---
- name: deploy web2 and web3
hosts: web2,web3
tasks:
- name: unarchive to web # 解压文件到指定位置
unarchive:
src: files/html.tar.gz
dest: /usr/share/nginx/
[root@pubserver project01]# ansible-playbook 07-deploy-web23.yml