web架构之nginx反代+LAP负载均衡+mysql主从(一)
此架构需要五台Linux虚拟机,由于电脑性能有限,本次实验使用三台虚拟机,web和mysql共同部署,通过停止服务模拟服务器宕机。后期会增加继续优化,加入mysql主从自动切换、MHA高可用;Nginx高可用,nginx+Apache动静分离、rsync网站数据同步等等,持续更新中!
mysql-master:192.168.29.132 mysql-slave:192.168.29.131
Web1:192.168.29.132 web2:192.168.29.131
Nginx:192.168.29.133
一、数据库服务器和web服务器一起安装,为了快速部署使用yum安装。
yum install -y mariadb mariadb-devel mariadb-server httpd httpd-devel php php-devel php-mysql
二、Mysql主从配置
Mysql-Master 和mysql-slave安装相同版本的mysql
- 132master端配置my.cnf 添加以下内容
vim /etc/my.cnf
log-bin=mysql-bin-master #启用二进制日志
server-id=1 #本机数据库ID 标示
binlog-do-db=wordpress #可以被从服务器复制的库, 二进制需要同步的数据库名
binlog-do-db=discuz #可以被从服务器复制的库, 二进制需要同步的数据库名
binlog-ignore-db=mysql #不可以被从服务器复制的库
在Master数据库服务器上设置权限,执行如下命令:
grant replication slave on *.* to 'tongbu'@'%' identified by '123.com';
flush privileges;
show master status;
- 131Slave 端配置my.cnf
vim /etc/my.cnf 添加以下内容
server-id=2 #本机数据库ID 标示,不能与master ID相同;
- 重载数据库,测试连接master:
mysql -utongbu -p123.com -h 192.168.29.132
进入数据库;
> grant replication slave on *.* to 'tongbu'@'%' identified by '123.com';
> flush privileges;
>stop slave; #停止slave
>change master to master_host='192.168.29.132',master_user='tongbu',master_password='123.com',master_log_file='mysql-bin-master.000001',master_log_pos=697;
>start slave; #开启slave
>show slave status\G # 查看slave状态
- master创建web数据库并授权;
>create database discuz charset=utf8;
>create database wordpress charset=utf8;
>grant all on wordpress.* to 'wordpress'@'%' identified by '123.com';
>grant all on discuz.* to 'discuz'@'%' identified by '123.com';
>flush privileges;
查看授权结果;
MariaDB [(none)]> select user,host,password from mysql.user;
- 131slave查看是否同步
三、配置web 服务
- 创建web目录,下载网站解压到对应目录。
cd /var/www/html
[root@localhost html]# mkdir discuz wordpress
- 配置Apache
echo "Include conf/vhosts.conf" >> /etc/httpd/conf/httpd.conf
- 创建Apache虚拟主机配置文件vhosts.conf
vim /etc/httpd/conf/vhosts.conf
<VirtualHost *:80>
ServerAdmin 461699721@qq.com
DocumentRoot "/var/www/html/discuz"
ServerName www.dz.com
ErrorLog "logs/www.dz.com_error_log"
CustomLog "logs/www.dz.com_access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 461699721@qq.com
DocumentRoot "/var/www/html/wordpress"
ServerName www.wp.com
ErrorLog "logs/www.wp.com_error_log"
CustomLog "logs/www.wp.com_access_log" common
</VirtualHost>
测试Apache配置是否正确
httpd -t
有提示ServerName
echo "ServerName localhost" >> /etc/httpd/conf/httpd.conf
131配置Linux hosts文件,以域名方式访问数据库。Windows hosts以域名方式访问mysql master
192.168.129.132配置www.dz.com
授写入权限
[root@localhost discuz]# chmod o+w config/ data/ uc_* -R
Windows 浏览器访问www.dz.com
配置www.wp.com
网站目录授权:
chmod o+w /var/www/html/wordpress/ -R
Web2从web1复制网站目录和Apache配置文件
验证Apache配置是否正确
httpd -t
- 192.168.29.131网站目录授权
chmod o+w /var/www/html/* -R
192.168.29.131配置hosts文件,添加数据库域名,该服务器上的web需要连接mysql主库。
重启Apache mariadb
systemctl restart httpd mariadb
四、配置133nginx反代
源码安装nginx,安装脚本
#!/bin/bash
#2019年6月1日16:05:45
#this shell is install nginx
#by authors xbz
#http://nginx.org/download/nginx-1.16.0.tar.gz
nginx_ver="1.16.0"
nginx_soft="nginx-${nginx_ver}.tar.gz"
nginx_url="http://nginx.org/download/${nginx_soft}"
nginx_prefix="/usr/local/nginx/"
nginx_yilai="gcc gcc-c++ expat-devel openssl-devel pcre pcre-devel zlib-devel"
nginx_module="--user=www \
--group=www \
--prefix=$ninix_prefix \
--with-http_stub_status_module \
--wtih-http_ssl_module "
yum install -y $nginx_yilai
useradd www
cd /root && wget -c $nginx_url && tar -zxvf $nginx_soft && cd nginx-$nginx_ver ; ./configure --prefix=${nginx_prefix}
if [ $? -eq 0 ];then
make && make install
cp $nginx_prefix/sbin/nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
echo -e "\033[32mThe nginx server install ok!\033[0m"
else
echo -e "\033[32mThe nginx server install failed!\033[0m"
fi
/usr/local/nginx/sbin/nginx -t
启动nginx
/usr/local/nginx/sbin/nginx 回车
修改nginx配置文件 红色为需要添加的内容
vim /usr/local/nginx/conf/nginx.conf
改:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
为:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
Include xbz_web/*;
}
133创建nginx虚拟主机配置网站
mkdir /usr/local/nginx/conf/xbz_web
vim /usr/local/nginx/conf/xbz_web/www.dz.com
upstream web_wp {
server 192.168.29.131:80 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.29.132:80 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.wp.com;
location / {
root html;
index index.html index.htm index.php;
proxy_set_header Host $host;
proxy_pass http://web_wp;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
vim /usr/local/nginx/conf/xbz_web/www.dz.com
upstream web_dz {
server 192.168.29.131:80 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.29.132:80 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.dz.com;
location / {
root html;
index index.html index.htm index.php;
proxy_set_header Host $host;
proxy_pass http://web_dz;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重载nginx,更改Windows hosts文件
/usr/local/nginx/sbin/nginx -s reload
浏览器测试网站
测试web负载
分别停止web:192.168.29.131 和web:192.168.29.132
更新网站内容,切换web服务器测试数据是否同步。
网站若能正常访问数据同步,侧web架构OK!
下一篇博文:web架构之nginx反代负载均衡+LAP负载均衡+mysql主从(二)添加nginx高可用,引入VIP,通过脚本实现VIP无缝漂移;