lnmp–nginx的负载,高可用
一、前期的架构搭建
基础架构搭建
环境准备:配置ip,关闭防火墙和selinux
ifdown ens33;ifup ens33
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
1、搭建nginx网站(192.168.1.10)
cd /etc/yum.repos.d/
vim nginx.repo
添加:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出
yum -y install nginx
systemctl restart nginx
systemctl enable nginx
2、搭建mysql服务器(192.168.1.20)
rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server -y
systemctl start mysqld
systemctl enable mysqld
创建需要的数据库,用户:
初始化密码:mysql_secure_installation
登录数据库:mysql -uroot -p123.com
创建数据库:create database blog;
创建远程管理用户:grant all on blog.* to lisi@'%' identified by '123.com';
3、搭建PHP服务器(192.168.1.30)
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php72w php72w-cli php72w-common php72w-devel \
php72w-embedded php72w-gd php72w-mbstring php72w-pdo \
php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache
systemctl start php-fpm
systemctl enable php-fpm
4、nginx 安装wordpress
(1)下载wordpress源码包
(2)复制wordpress安装包,到虚拟机/,解压并赋权
unzip wordpress-4.9.4-zh_CN.zip
chmod -R 777 /wordpress
(3)创建虚拟主机配置文件
vim /etc/nginx/conf.d/blog.conf
添加:
server {
listen 80;
server_name blog.benet.com;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass 192.168.1.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存退出
nginx -t
systemctl restart nginx
在nginx上复制wordpress目录到php的根目录
scp -rp /wordpress root@192.168.1.30:/
修改php服务器的配置文件
vim /etc/php-fpm.d/www.conf
定位并修改为:
listen = 192.168.1.30:9000 #PHP的ip地址
listen.allowed_clients = 192.168.1.10 #nginx的ip地址
保存退出
重启php:systemctl restart php-fpm
5、配置lb负载均衡及反向代理—lb1和lb2一样(lb1-1.40 lb2-1.50)
(1)安装nginx同上
(2)添加优化项
vim /etc/nginx/nginx_params
添加:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
保存退出
(3)创建lb配置文件,
vim /etc/nginx/conf.d/lb1.conf
添加:
upstream web_cluster {
server 192.168.1.10:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://web_cluster;
include nginx_params;
}
}
保存退出
检查配置:nginx -t
重启nginx:systemctl restart nginx
6、搭建nginx高可用–keepalived(1.40和1.50)
配置两台负载均衡服务器的高可用
(1)lb1和lb2安装keepalived
yum -y install keepalived
(2)配置keepalived
主服务器:lb1
vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
router_id lb1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
保存退出
启动服务:systemctl restart keepalived
备服务器:lb2
vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
router_id lb2 #路由id号,和主服务器必须不同
}
vrrp_instance VI_1 {
state BACKUP #状态:BACKUP备 MASTER主
interface ens33
virtual_router_id 51
priority 99 #优先级:备比主要小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254 #虚拟路由ip,公共ip
}
}
保存退出
启动服务:systemctl restart keepalived
(3)查看漂移地址
ip addr show dev ens33
开启一台linunx 在hosts文件中 添加解析
vim /etc/hosts
192.168.1.254 blog.benet.com
保存退出
浏览器访问:blog.benet.com 即可