一、整个实验拓扑图
二、搭建实验环境
- nginx1: 192.168.30.102 和 10.128.0.0
- nginx2: 192.168.30.103 和 10.128.0.7
- websrv1(动态资源服务器): 192.168.30.100
- websrv2(静态资源服务器): 192.168.30.104
- mariadb: 192.168.30.107
- imagesrv: 192.168.30.105
三、配置各服务器
1. websrv1的配置
- 安装软件包
yum -y install httpd php php-mysql php-mbstring
systemctl start httpd
- 解压缩wordpress和配置
tar xf wordpress-4.9.4-zh_CN.tar.gz
cd wordpress
cp -a * /var/www/html/
cd /var/www/html/
chown -R apache.apache *
cp wp-config-sample.php wp-config.php
# 配置MySQL连接
vim wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'test');
define('DB_PASSWORD', 'centos');
define('DB_HOST', '192.168.30.107');
这里关于vim中文编码的设置,将下面内容保存到.vimrc文件中
set fileencodings=utf-8
set termencoding=utf-8
set encoding=utf-8
- 本地测试
firefox http://192.168.30.100
2. websrv2配置
- 安装软件包
yum -y install httpd
systemctl start httpd
- 准备测试数据
echo "This is websrv1" > /var/www/html/index.html
- 本地测试
curl http://192.168.30.104
- 为了让wordpress的图片等静态资源显示正常,也需要拷贝一下wordpress的程序包
tar xf wordpress-4.9.4-zh_CN.tar.gz
cd wordpress
cp -a * /var/www/html/
chown -R apache.apache /var/www/html/*
3.mariadg的配置
- 安装软件包
yum -y install mariadb-server
systemctl start mariadb
- 设置权限
# 新增用户权限
grant all on wordpress.* to test@'192.168.30.%' identified by 'centos';
# 删除用户及相应的权限
drop user test@'192.168.30.%';
# 查看用户的权限
show grants for test@'192.168.30.%';
- 测试
mysql -utest -pcentos -h192.168.30.107
> use wordpress
4. nginx1和nginx2的配置
- 安装软件包
yum -y install nginx
- 配置nginx(默认的其他配置省略了)
vim /etc/nginx.conf
http {
upstream websrvs {
server 192.168.30.104:80;
}
upstream phpsrvs {
server 192.168.30.100:80;
}
# 还需要将默认主机删除掉
}
vim /etc/nginx/conf.d/web.conf
server {
server_name www.ilinux.io;
listen 80;
location / {
index index.php;
proxy_pass http://phpsrvs;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header host $host;
}
location ~* \.(jpg|jpeg|css|js|png|gif)$ {
proxy_pass http://websrvs;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header host $host;
}
}
- 测试
curl http://192.168.30.102
firefox http://192.168.30.102/index.php
5. imagesrv的配置
- 安装软件包
# 使用NFS给WEB服务器提供图片共享服务
yum -y install nfs-utils
- nfs服务端配置(192.168.30.105)
mkdir /data/upload
chown apache.apche /data/upload
# 配置nfs
vim /etc/exports
/data/upload 192.168.30.0/24(rw,no_root_squash)
systemctl start nfs
- nfs客服端配置(192.168.30.102 和 192.168.30.103)
yum -y install nfs-utils
mkdir /var/www/html/wp-content/uploads
chown apache.apache /var/www/html/wp-content/uploads
mount.nfs 192.168.30.105:/data/upload /var/www/html/wp-content/uploads
- 测试
在 wordpress上使用图片附件发一篇文章,查看显示是否正常。
6.在两个nginx服务器上配置keepalive服务
- 安装软件包
yum -y install keepalived
- 配置服务(注意:{前必须要有空格,查看日志使用 journalctl -u keepalived)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1 #主从不同的地方
vrrp_mcast_group4 224.0.100.10
}
vrrp_instance VI_1 {
state BACKUP #主从不同的地方
interface ens37
virtual_router_id 20
priority 98 #主从不同的地方
advert_int 1
authentication {
auth_type PASS
auth_pass ed93f89c
}
virtual_ipaddress {
10.1.0.100/8 dev ens37
}
}
- wordpress上的设置
修改URL
在客户机上修改 /etc/hosts 文件做上面的映射
10.1.0.100 www.ilinux.io
- 测试
curl -I http://www.ilinux.io
# 人为停止主的keepalive查看VIP是否转移
ip a
四、进行压力测试
- 无varnish
ab -c 200 -n 2000 http://10.1.0.100/
Requests per second: 4103.34 [#/sec] (mean)
- 有varnish
首先安装和配置varnish服务
# 安装软件包,需要启用epel源
yum -y install varnish
# 修改nginx的监听端口
vim /etc/nginx/conf.d/web.conf
server {
server_name www.ilinux.io;
listen 8080;
location / {
index index.php;
proxy_pass http://phpsrvs;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header host $host;
}
location ~* \.(jpg|jpeg|css|js|png|gif)$ {
proxy_pass http://websrvs;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header host $host;
}
}
# 配置,注意修改主机和监听端口号
cd /etc/varnish
vim default.vcl
...
backend default {
.host = "127.0.0.1";
.port = "8080";
}
...
vim varnish.params
...
VARNISH_LISTEN_PORT=80
...
VARNISH_STORAGE="malloc,256M"
...
# 启动服务
systemctl start varnish
使用测试工具测试,结果很意外竟然比没有varnish的情况要差
ab -c 200 -n 2000 http://www.ilinux.io/
Requests per second: 3327.47 [#/sec] (mean)