0%

使用 Ansible Roles 快速搭建 Nginx 代理后端的 LAMP 模型

关于 Ansible 系列其他文章的传送门

  1. 《 Ansible 介绍和基础操作 》

  2. 《 Ansible Playbook 以及 Roles 的简单使用方法 》

  3. 《 使用 Ansible Playbook 通过 yum 部署 LNMP 环境 》

  4. 《 使用 Ansible Roles 快速搭建 Nginx 代理后端的 LAMP 模型 》         您当前所在位置

  5. 《 使用 Ansible Playbook 自动完成硬盘的分区、格式化、挂载 》



head

一、准备环境

记录使用 Ansible Roles,部署 Nginx 反向代理 LNMP 的方法

系统:三台 CentOS 7.6.1810

功能 Hostname IP
proxy centos_1 192.168.50.11
LAMP centos_2 192.168.50.12
LAMP centos_3 192.168.50.13
  • Ansible 版本:ansible 2.8.0
  • 全部关闭 selinux
  • 全部安装好 epel 源

需要通过剧本来安装的程序有:

  • nginx 安装在第一台,用来把请求反向代理至后端节点
    (以下四个安装在其他两台,组成 LAMP 来处理请求)
  • httpd
  • php-fpm
  • php-mysql
  • mariadb-server

二、准备配置文件

先创建好目录结构:

1
mkdir -pv /etc/ansible/roles/{nginx,httpd}/{files,templates,tasks,handlers,vars}

所有 ansible 文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
roles

├── httpd # http 角色目录
├── files
├── handlers
└── main.yml # handlers 主文件
├── tasks
└── main.yml # tasks 主文件
├── templates
└── index.j2 # httpd 主页文件
└── vars
└── nginx
├── files
└── proxy.conf # nginx 代理配置文件
├── handlers
├── tasks
└── main.yml # tasks 主文件
├── templates
└── vars

1. nginx 端的配置文件如下

nginx 的 tasks 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@centos_1 ~   # cat /etc/ansible/roles/nginx/tasks/main.yml

- name: stop firewalld packages # 关闭防火墙
service: name=firewalld state=stopped enabled=no

- name: install nginx # yum 安装 nginx
yum: name=nginx

- name: make dirctory # 创建 nginx 配置文件目录
file: path=/etc/nginx/default.d state=directory

- name: copy proxy conf # 拷贝 proxy 的配置文件到上边的目录
copy: src=proxy.conf dest=/etc/nginx/default.d/proxy.conf

- name: restart nginx # 启动或者重启 nginx,生产环境慎用,看具体情况
service: name=nginx state=restarted

proxy 配置文件

将 centos_2 和_3 的请求分别反代至 50.12 和 50.13

1
2
3
4
5
6
7
8
root@centos_1 ~# cat /etc/ansible/roles/nginx/files/proxy.conf

location /centos_2/ {
proxy_pass http://192.168.50.12/;
}
location /centos_3/ {
proxy_pass http://192.168.50.13/;
}

2. httpd 端的配置文件如下

httpd 的 tasks 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
root@centos_1 ~# cat /etc/ansible/roles/httpd/tasks/main.yml

- name: stop iptables firewalld packages # 关闭防火墙
service: name=firewalld state=stopped enabled=no

- name: install packages # 安装软件
yum: name={{ item }} state=installed
loop:
- httpd
- php
- php-mysql

- name: install mysql # 安装数据库后,通知 handlers 设置 mysql 权限
yum: name=mariadb-server state=installed
notify:
- set mysql-1
- set mysql-2
- set mysql-3

- name: install php index # 通过模板设置 index.php
template: src=index.j2 dest=/var/www/html/index.php

- name: restart services # 服务走起
service: name={{ item }} state=restarted
loop:
- mariadb
- httpd

index 模板文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@centos_1 ~# cat /etc/ansible/roles/httpd/templates/index.j2

<?php
$servername = "localhost";
$username = "root";
$password = "123qwe";

// 创建连接
$conn = new mysqli($servername, $username, $password);

// 检测连接
if ($conn->connect_error) {
die("{{ ansible_hostname }}连接失败: " . $conn->connect_error);
}
echo "{{ ansible_hostname }} 连接成功";
?>

handlers 配置文件

设定 mysql 的相关权限

1
2
3
4
5
6
7
root@centos_1 ~# cat /etc/ansible/roles/httpd/handlers/main.yml
- name: set mysql-1
command: mysql --execute="UPDATE mysql.user SET Password=PASSWORD('123qwe') WHERE User='root';"
- name: set mysql-2
command: mysql --execute="UPDATE mysql.user SET host='%' WHERE host='localhost';"
- name: set mysql-3
command: mysql --execute="FLUSH PRIVILEGES"

三、设定 Roles 并执行 Playbook

1. 剧本

超级简单,定义好 hosts 和 roles 就可以跑了

1
2
3
4
5
6
7
8
9
10
11
root@centos_1 ~# cat /root/lnmp/roles/proxy_lnmp.yaml

- hosts: proxy
remote_user: root
roles:
- nginx

- hosts: web
remote_user: root
roles:
- httpd

2. 执行剧本

接下来执行命令,“一键” 安装 nginx 代理 lamp 环境
可以先用 ↓ 测试语法

1
ansible-playbook --syntax-check

↓ 干跑

1
ansible-playbook -check

↓ 正式运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
root@centos_1 ~# ansible-playbook lnmp/roles/proxy_lnmp.yaml

PLAY [proxy] ******************************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [192.168.50.11]

TASK [nginx : stop firewalld packages] ****************************************************
ok: [192.168.50.11]

TASK [nginx : install nginx] **************************************************************
changed: [192.168.50.11]

TASK [nginx : make dirctory] **************************************************************
changed: [192.168.50.11]

TASK [nginx : copy proxy conf] ************************************************************
changed: [192.168.50.11]

TASK [nginx : start nginx] ****************************************************************
changed: [192.168.50.11]

PLAY [web] ********************************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [192.168.50.12]
ok: [192.168.50.13]

TASK [httpd : stop iptables firewalld packages] *******************************************
ok: [192.168.50.12]
ok: [192.168.50.13]

TASK [httpd : install packages] ***********************************************************
changed: [192.168.50.13] => (item=httpd)
changed: [192.168.50.12] => (item=httpd)
changed: [192.168.50.13] => (item=php)
changed: [192.168.50.12] => (item=php)
changed: [192.168.50.13] => (item=php-mysql)
changed: [192.168.50.12] => (item=php-mysql)

TASK [httpd : install mysql] **************************************************************
changed: [192.168.50.13]
changed: [192.168.50.12]

TASK [httpd : install php index] **********************************************************
changed: [192.168.50.13]
changed: [192.168.50.12]

TASK [httpd : start services] *************************************************************
changed: [192.168.50.13] => (item=mariadb)
changed: [192.168.50.12] => (item=mariadb)
changed: [192.168.50.12] => (item=httpd)
changed: [192.168.50.13] => (item=httpd)

RUNNING HANDLER [httpd : set mysql-1] *****************************************************
changed: [192.168.50.12]
changed: [192.168.50.13]

RUNNING HANDLER [httpd : set mysql-2] *****************************************************
changed: [192.168.50.13]
changed: [192.168.50.12]

RUNNING HANDLER [httpd : set mysql-3] *****************************************************
changed: [192.168.50.13]
changed: [192.168.50.12]

PLAY RECAP ********************************************************************************
192.168.50.11 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.50.12 : ok=9 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.50.13 : ok=9 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

3. 检查效果

运行完成后,打开本地浏览器,分别输入 ip 地址,验证 php 和 mysql 连通情况

index_1

index_2

至此,通过 Ansible Roles 部署 Nginx 代理 LAMP 大功告成!!!完结撒花 \(^o^)/