目录
准备工作
!!!三台服务器全部关闭防火墙和selinux!!!
准备三台服务器:
服务器 | ip | 部署服务 |
ansible | 10.36.192.143 | ansible |
web-1 | 10.36.192.146 | nginx、php |
web-2 | 10.36.192.147 | 数据库(mariadb|mysql) |
添加域名解析
[root@ansible ~]# vim /etc/hosts
vim /etc/hosts
添加资源清单
[root@ansible ~]# vim /etc/ansible/hosts
vim /etc/ansible/hosts
web-1 ansible_ssh_root="root" ansible_ssh_pass="远程登录密码"
web-2 ansible_ssh_root="root" ansible_ssh_pass="远程登陆密码"
修改配置文件关闭检查ssh密钥
[root@ansible ~]# vim /etc/ansible/ansible.cfg
vim /etc/ansible/ansible.cfg
1、创建剧本文件
web-1服务器部署nginx、php
创建nginx配置文件(以便使用)
[root@ansible ~]# vim /root/nginx.conf
vim /root/nginx.conf
取消注释vim命令行模式:set paste
i插入
user nginx;
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server {
listen {{ port1 }};
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sendfile on;
#tcp_nopush on;
keepalive_timeout 0;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
下载wordpress包到ansible服务器
https://cn.wordpress.org/latest-zh_CN.zip
[root@ansible ~]# curl -O -o /root/latest-zh_CN.zip https://cn.wordpress.org/latest-zh_CN.zip
curl -O -o /root/latest-zh_CN.zip https://cn.wordpress.org/latest-zh_CN.zip
2、编译web-1服务器剧本
[root@ansible ~]# vim web-1.yml
vim web-1.yml
取消注释vim命令行模式:set paste
i插入
---
- hosts: web-1
remote_user: root
vars: #定义变量
- bag1: nginx
- bag2: php80-php-xsl,php80-php,php80-php-cli,php80-php-devel,php80-php-gd,php80-php-pdo,php80-php-mysql,php80-php-fpm
- service_1: php80-php-fpm
- port1: 80
- worker1: auto
tasks:
- name: 下载yum源
yum: name=http://rpms.remirepo.net/enterprise/remi-release-7.rpm state=present
- name: 下载阿里云基础源
get_url: url=https://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/Centos-7.repo
- name: 下载阿里云扩展源
get_url: url=https://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel-7.repo
- name: 下载解压工具
yum: name=unzip state=present
- name: 安装{{ bag1 }}
yum: name={{ bag1 }} state=present
- name: 修改{{ bag1 }}配置文件
template: src=/root/nginx.conf dest=/etc/nginx/
notify: reload
- name: 启动{{ bag1 }}
service: name={{ bag1 }} state=started enabled=true
- name: 下载{{ bag2 }}
yum: name={{ bag2 }} state=present
- name: 启动{{ bag2 }}
service: name={{ service_1 }} state=started enabled=yes
- name: 部署wordpress
unarchive: src=/root/latest-zh_CN.zip dest=/usr/share/nginx/html
# - name: 添加配置规则
# copy: src=/root/wp-config.php dest=/usr/share/nginx/html/wordpress/
# tags: wp-config #标签
handlers:
- name: reload
service: name={{ bag1 }} state=restarted
检查语法是否有误
[root@ansible ~]# ansible-playbook web-1.yml --syntax-check
ansible-playbook web-1.yml --syntax-check
执行剧本
[root@ansible ~]# ansible-playbook web-1.yml
ansible-playbook web-1.yml
3、编译web-2服务器剧本
[root@ansible ~]# vim web-2.yml
vim web-2.yml
取消注释vim命令行模式:set paste
i插入
---
- hosts: web-2
remote_user: root
vars: #定义变量
bag: mariadb-server,mariadb
server_1: mariadb
db_user: haitao #数据库用户
db_passwd: Haitao@123 #数据库密码
tasks:
- name: 下载阿里云基础源
get_url: url=https://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/Centos-7.repo
- name: 下载阿里云扩展源
get_url: url=https://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel-7.repo
- name: 安装数据库
yum: name={{ bag }} state=present
- name: 启动数据库
service: name={{ server_1 }} state=started
- name: 授权
shell: mysql -e "create database wordpress;grant all on wordpress.* to '{{ db_user }}'@'%' identified by '{{ db_passwd }}'"
检查语法
[root@ansible ~]# ansible-playbook web-2.yml --syntax-check
ansible-playbook web-2.yml --syntax-check
执行剧本
[root@ansible ~]# ansible-playbook web-2.yml
ansible-playbook web-2.yml
4、浏览器登录查看
http://10.36.192.146/wordpress
在ansible服务器通过剧本编写进去
[root@ansible ~]# vim /root/wp-config.php
vim /root/wp-config.php
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the installation.
* You don't have to use the web site, you can copy this file to "wp-config.php"
* and fill in the values.
*
* This file contains the following configurations:
*
* * Database settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://wordpress.org/documentation/article/editing-wp-config-php/
*
* @package WordPress
*/
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );
/** Database username */
define( 'DB_USER', 'haitao' );
/** Database password */
define( 'DB_PASSWORD', 'Haitao@123' );
/** Database hostname */
define( 'DB_HOST', '10.36.192.147' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**#@+
* Authentication unique keys and salts.
*
* Change these to different unique phrases! You can generate these using
* the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
*
* You can change these at any point in time to invalidate all existing cookies.
* This will force all users to have to log in again.
*
* @since 2.6.0
*/
define( 'AUTH_KEY', '9SArB,ANI6EznQ/yE)k*XBBl&We#WrIAtNodZ!ps5F-t|<w?;Z9|#0NR&*HEe3,0' );
define( 'SECURE_AUTH_KEY', 'J2v*[YxKco{R@PUoH@ibhV#lX/<in}5YR6EpsZe1%do`gNGaLHrCHUZ^8{Y))+IB' );
define( 'LOGGED_IN_KEY', ';8+7Lo6fsvd.^^j:e+5+pv1|u=#8MmEht%R9]Mgp{Wqd!bUI0W}Nmx9wlG=+DfU/' );
define( 'NONCE_KEY', 's|ZTocM_!zEkGg&|s,I7qIxAR-De%dOQQb]`3N9Iw.<Z5:jM)esc}L~LW;VL6Plz' );
define( 'AUTH_SALT', 'VR6?6012T]<&&m#&,FzP?0^+(1om3l%~;M@!S>H4lptYTHNeB}^*GvFW>JArQRAQ' );
define( 'SECURE_AUTH_SALT', '7(<b]cbjO9PZ5U&YV7A/_3jJH&c+ZNs78@M^>~m6(%z9rl+EzB4q1I,aUjMWI]GI' );
define( 'LOGGED_IN_SALT', '/gxMkzXv3tlNbif5A{VKYV=0UPpjkioK1GU)Q)gF*gzU+pm[Kxaz5&34X`.Y6oo#' );
define( 'NONCE_SALT', 'j:}5lw }%Wo^yd04PRdOKC]OWA^09TI>jBJ=e~(5P.sI-9zU*fzwq6EtM{qlG|h6' );
/**#@-*/
/**
* WordPress database table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the documentation.
*
* @link https://wordpress.org/documentation/article/debugging-in-wordpress/
*/
define( 'WP_DEBUG', false );
/* Add any custom values between this line and the "stop editing" line. */
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
[root@ansible ~]# vim web-1.yml
vim web-1.yml
打开添加配置规则的注释
单独允许标签剧本就可以
ansible-playbook -t [标签名] 剧本文件
[root@ansible ~]# ansible-playbook -t wp-config web-1.yml
ansible-playbook -t wp-config web-1.yml
回到页面运行安装程序
wordpress部署成功