ansible搭建多台wordpress

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: ansible搭建多台wordpress

环境准备image.png

安装ansible

[root@ansible ~]# yum install -y epel-release
[root@ansible ~]# yum install -y ansible

配置ansible

配置免密登录

[root@ansible ~]# ssh-keygen
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:yoEbuPUA2JXWy6Sv/0XmVJFa2+kPDoeUBOHXlEuuX/M root@master
The key's randomart image is:
+---[RSA 2048]----+
| .o oo.... |
| o .o o . ++o |
|. o. + . .+o*.o |
| o..o .o+ = |
| . =.. S +. + |
| o *.o = + +..|
| . ..+ o = +o|
| . . o E|
| .... |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id 192.168.75.16
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.75.16 (192.168.75.16)' can't be established.
ECDSA key fingerprint is SHA256:zEsox3y0l5PHw5nPJwEx/6fDmULkhd0ad82TQQ/XkFU.
ECDSA key fingerprint is MD5:92:5b:3a:24:6f:0f:09:33:35:78:4c:de:93:74:bd:9b.
Are you sure you want to continue connecting (yes/no)? 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@192.168.75.16's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.75.16'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]# ssh-copy-id 192.168.75.17
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.75.17 (192.168.75.17)' can't be established.
ECDSA key fingerprint is SHA256:YP7fswNmzmBhCbu33ZfuJ1G7e+Cyi8dy+o48oWtmRqU.
ECDSA key fingerprint is MD5:b6:c3:2e:95:2f:c4:f5:1f:7c:e0:e7:ca:ca:4b:b7:c8.
Are you sure you want to continue connecting (yes/no)? 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@192.168.75.17's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.75.17'"
and check to make sure that only the key(s) you wanted were added.

配置ansible文件

[root@ansible ~]# vim /etc/ansible/hosts # 在结尾添加如下内容
[node1]
192.168.75.16
[node2]
192.168.75.17

搭建wordpress

创建相关目录

[root@ansible ~]# mkdir -pv
/etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,han
dlers}

ansible服务器配置

准备相关服务的配置文件、以及wordpress源码包

[root@ansible ~]# yum install -y httpd mariadb-server php php-mysql # 搭建lamp环
[root@ansible ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz #
下载wordpress

修改httpd文件

[root@ansible ~]# vim /etc/httpd/conf/httpd.conf # 找到如下内容,修改index.html为
index.php
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

拷贝对应的文件

[root@ansible ~]# cp /etc/httpd/conf/httpd.conf
/etc/ansible/lamp/roles/httpd/files/
[root@ansible ~]# cp wordpress-4.9.4-zh_CN.tar.gz
/etc/ansible/lamp/roles/httpd/files/

编写环境剧本

[root@ansible ~]# vim /etc/ansible/lamp/roles/prepare/tasks/main.yaml
- name: install wget
yum: name=wget state=present
- name: rm -rf *.repo
shell: rm -rf /etc/yum.repos.d/*
- name: wget huawei.repo
shell: wget -O /etc/yum.repos.d/CentOS-Base.repo
https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
- name: updaet yum
shell: yum repolist
- name: stop firewalld
service: name=firewalld state=stopped enabled=no
- name: stop iptables
shell: iptables -F

编写httpd剧本

[root@ansible ~]# vim /etc/ansible/lamp/roles/httpd/tasks/main.yaml
- name: install httpd
yum: name=httpd state=present
- name: copy wordpress
copy: src=wordpress-4.9.4-zh_CN.tar.gz dest=/opt
- name: copy httpd.conf
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: tar wordperss
shell: tar -xf /opt/wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html/
- name: start httpd
service: name=httpd state=started enabled=yes
# 编写触发条件
[root@ansible ~]# vim /etc/ansible/lamp/roles/httpd/handlers/main.yaml
- name: restart httpd
service: name=httpd state=restarted

编写mysql剧本

[root@ansible ~]# vim /etc/ansible/lamp/roles/mysql/tasks/main.yaml
- name: install mysql
yum: name=mariadb-server state=present
- name: start mariadb
service: name=mariadb state=started enabled=yes
- name: create database wordpress
shell: mysql -e "create database wordpress;"
- name: cretae wordpress user
shell: mysql -e "grant all privileges on wordpress.* to 'wordpress'@'localhost'
identified by '123456';"

构建PHP剧本

[root@ansible ~]# vim /etc/ansible/lamp/roles/php/tasks/main.yaml
- name: install php
yum: name=php state=present
- name: install php-mysql
yum: name=php-mysql state=present

构建总剧本

[root@ansible ~]# vim /etc/ansible/lamp/roles/all.yaml
- hosts: all
remote_user: root
roles:
- prepare
- httpd
- mysql
- php

查看目录结构

[root@ansible ~]# yum install tree -y
[root@ansible ~]# cd /etc/ansible/lamp/roles/
[root@ansible roles]# tree .
.
├── all.yaml
├── httpd
│ ├── files
│ │ ├── httpd.conf
│ │ └── wordpress-4.9.4-zh_CN.tar.gz
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── mysql
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── php
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
└── prepare
├── files
├── handlers
├── tasks
│ └── main.yaml
├── templates
└── vars
24 directories, 8 files

执行总剧本

[root@ansible roles]# ansible-playbook /etc/ansible/lamp/roles/all.yaml

image.png出现上面的图片,证明执行完成。

浏览器访问测试

http://192.168.75.16

连接数据库image.png


写入文件image.png

# 在IP地址的服务器按照要求完成,wp-config.php文件使用下面的命令创建
[root@node1 ~]# vim /var/www/html/wordpress/wp-config.php
<?php
/**
* WordPress基础配置文件。
*
* 这个文件被安装程序用于自动生成wp-config.php配置文件,
* 您可以不使用网站,您需要手动复制这个文件,
* 并重命名为“wp-config.php”,然后填入相关信息。
*
* 本文件包含以下配置选项:
*
* * MySQL设置
* * 密钥
* * 数据库表名前缀
* * ABSPATH
*
* @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php
*
* @package WordPress
*/
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wordpress');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', 'localhost');
/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8mb4');
/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
/**#@+
* 身份认证密钥与盐。
*
* 修改为任意独一无二的字串!
* 或者直接访问{@link https://api.wordpress.org/secret-key/1.1/salt/
* WordPress.org密钥生成服务}
* 任何修改都会导致所有cookies失效,所有用户将必须重新登录。
*
* @since 2.6.0
*/
define('AUTH_KEY', 'BQwZw%P{]*H=fEz_f-@;)/D|F9H!+&fy`s￾Ae~0fx<=]XU]z7.5IToi54Pvs#ZvQ');
define('SECURE_AUTH_KEY', 'xF%$T`0gGL>>s%<jnZGn7F+YA%D=
<V5~F,_;Lw//:z+>QCES0mX9ni0;eF8=Tw;g');
define('LOGGED_IN_KEY', 'h*Mm+<cqUY0emZJ+%4L6Rx.VVvz}$Pym8*YcN:lk7qF|j(,_%1
L=#=>^? [?4v%');
define('NONCE_KEY', '_ vKAo*e/2Tbo(#YwoPq>+yMEfh@9aWE)x
:v`,.B[b!F@oL0q<6@IrX7HYraX$6');
define('AUTH_SALT', 'yQp+Xyl^xp;0h2 RzxB@# Nk27Cp6*DMXobs|=Tl`5?-
hd$soAw*7*$,-feYkRHD');
define('SECURE_AUTH_SALT', 'OuExS2o9eUdud0kOl!)j1,0:[/UydLK*35n@y(,l .
<qb+OC>-/[*}ct:H,x.ueP');
define('LOGGED_IN_SALT', 'EpzF1tv;-UWQtbT}tElaeD<o&LIdaKSo3I2p$JfuMw!Z(npxmY?
+6l##BZNw~JBw');
define('NONCE_SALT', 'Ef3)$7ye.+!)b&7bn{~n+i!+XQ|iUKfVJuf)mO>F+uN4*{-
P<N&UOmau|v:]U Qa');
/**#@-*/
/**
* WordPress数据表前缀。
*
* 如果您有在同一数据库内安装多个WordPress的需求,请为每个WordPress设置
* 不同的数据表前缀。前缀名只能为数字、字母加下划线。
*/
$table_prefix = 'wp_';
/**
* 开发者专用:WordPress调试模式。
*
* 将这个值改为true,WordPress将显示所有用于开发的提示。
* 强烈建议插件开发者在开发环境中启用WP_DEBUG。
*
* 要获取其他能用于调试的信息,请访问Codex。
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', false);
/**
* zh_CN本地化设置:启用ICP备案号显示
*
* 可在设置→常规中修改。
* 如需禁用,请移除或注释掉本行。
*/
define('WP_ZH_CN_ICP_NUM', true);
/* 好了!请不要再继续编辑。请保存本文件。使用愉快! */
/** WordPress目录的绝对路径。 */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** 设置WordPress变量和包含文件。 */
require_once(ABSPATH . 'wp-settings.php');

填写信息image.png安装完成image.png

登录wordpressimage.png搭建完成image.png

查看数据是否写入数据库

[root@node1 ~]# mysql -uwordpress -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 40
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use wordpress; # 进入数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [wordpress]> show tables; # 查看表
+-----------------------+
| Tables_in_wordpress |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_termmeta |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
12 rows in set (0.00 sec)
MariaDB [wordpress]> select * from wp_users; # 查看是否与上方填写的w
+----+------------+------------------------------------+---------------+---------
----------+----------+---------------------+---------------------+-------------+-
-------------+
| ID | user_login | user_pass | user_nicename |
user_email | user_url | user_registered | user_activation_key |
user_status | display_name |
+----+------------+------------------------------------+---------------+---------
----------+----------+---------------------+---------------------+-------------+-
-------------+
| 1 | root | $P$BvWh7wNToYPOS11grHdFg1vC/Tersu. | root |
2924739965@qq.com | | 2022-11-17 05:33:07 | |
0 | root |
+----+------------+------------------------------------+---------------+---------
----------+----------+---------------------+---------------------+-------------+-
-------------+
1 row in set (0.00 sec)
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
关系型数据库 MySQL 网络安全
利用ansbile部署lamp并部署Discuz(非分布式)
利用ansbile部署lamp并部署Discuz(非分布式),实验准备,设置ansbile的hosts文件,在192.168.115.148上完成相关准备,编写roles,执行
149 0
|
2月前
|
弹性计算 自然语言处理 负载均衡
部署高可用WordPress网站
高可用服务是另外一个高频使用的场景,编写模板的流程和《部署单点WordPress网站》一样,但涉及的资源更多一些。本文以《部署高可用WordPress网站》为例,介绍高可用部署类的模板如何编写。
81141 7
|
3月前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
3月前
|
关系型数据库 MySQL 网络安全
VPS搭建WordPress
如果你想搭建的WordPress拥有一个较好的性能的话,那么你可以选择在VPS上搭建WordPress。本文将会带你从零开始,在VPS上一步一步敲代码来搭建WordPress。首先,你需要注册一台VPS,登录你的VPS先搭建好LAMP环境;接下来你需要将你的域名已经解析到你的VPS。在域名解析成功后,你需要创建MySQL数据库,然后安装PHP插件并配置Apache,接下来下载并配置WordPress,最后是安装SSL证书。
202 1
VPS搭建WordPress
|
负载均衡 大数据 应用服务中间件
10分钟搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡
10分钟搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡
|
9月前
|
弹性计算 关系型数据库 应用服务中间件
Wordpress 安装部署
简单几个步骤即可使用 ECS、RDS 进行 wordpress 安装,完成内容站点的部署。
146 0
|
Kubernetes 数据可视化 数据库
docker搭建图形化界面portainer1.24.2,快速搭建wordpress5.7.2站点
docker搭建图形化界面portainer1.24.2,快速搭建wordpress5.7.2站点
344 0
docker搭建图形化界面portainer1.24.2,快速搭建wordpress5.7.2站点
|
负载均衡 Ubuntu 固态存储
Linux服务器搭建gitlab需要什么配置?
Linux服务器搭建gitlab需要什么配置?
957 0
|
Kubernetes Ubuntu Linux
Linux服务器搭建K8S需要什么配置?
Linux服务器搭建K8S需要什么配置?
1974 0
|
安全 NoSQL MongoDB
一键安装脚本实现快速部署GrayLog Server 4.2.10单机版
一键安装脚本实现快速部署GrayLog Server 4.2.10单机版
418 0
一键安装脚本实现快速部署GrayLog Server 4.2.10单机版