LNMP详细安装
LNMP准备环境:
nginx1:192.168.1.8
nginx: 192.168.1.9
mysql: 192.168.1.11
php:192.168.1.12
lb1:192.168.1.13
lb2:192.168.1.17
nfs:192.168.1.18
1.nginx1服务器配置
-
关闭防火墙:
systemctl stop firewalld (关闭防火墙)
systemctl disable firewalld (开机自动关闭防火墙) -
关闭selilnux
setenforce 0 -
配置nginx官方源:
vim /etc/yum.repo.d/nginx.repo
#添加
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出 -
yum安装nginx
yum -y install nginx -
启动nginx
systemctl start nginx -
开机自动启动nginx
systemctl enable nginx
2.nginx2服务器配置
-
关闭防火墙:
systemctl stop firewalld (关闭防火墙)
systemctl disable firewalld (开机自动关闭防火墙) -
关闭selilnux
setenforce 0 -
配置nginx官方源:
vim /etc/yum.repo.d/nginx.repo
#添加
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出 -
yum安装nginx
yum -y install nginx -
启动nginx
systemctl start nginx -
开机自动启动nginx
systemctl enable nginx
3.mysql的配置
-
下载mysql包到mysql5.6.rpm文件夹
mysql包下载 -
进入到mysql5.6.rpm文件夹,安装mysql
cd mysql5.6.rpm/
yum -y localinstall *.rpm -
启动mysql
systemctl start mysqld -
开机自动启动mysql
systemctl enable mysqld
4.PHP配置:
- 下载PHP包到php文件夹中
php包下载. - 进入到php文件夹中,安装php
cd php/
yum -y localinstall *.rpm - 启动php
systemctl start php-fpm - 开机自动启动php
systemctl enable php-fpm
5.nginx1中安装wordpress
- 下载wordpress源码包
wordpress源码包 - 复制wordpress安装包,到/下,解压,并赋权
unzip wordpress-4.9.4-zh_CN.zip
chmod -R 777 /wordpress - 创建虚拟主机配置文件
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.12:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- 重启nginx
systemctl restart nginx - 在mysql服务器上创建blog数据库和用户
登录数据库:mysql -u root
创建数据库:create database blog;
创建远程管理用户: grant all on blog.* to lisi@"%" identified by ‘123.com’;
注意:这里的引号是英文下的引号,因此要修改一下 - 在nginx1复制wordpress目录到php的根目录
scp -rp /wordpress root@192.168.1.12:/ - 修改php服务器的配置文件
vim /etc/php-fpm.d/www.conf
定位20行和48行并修改为:
listen = 192.168.1.12:9000
listen.allowed_clients = 192.168.1.8,192.168.1.9
保存退出 - 重启php:
systemctl restart php-fpm
6.nginx1下安装zh
- 下载wecenter源码包
Wecenter源码包 - 复制wecenter安装包,到虚拟机/zh目录下,赋权
mkdir /zh
cd /zh
unzip Wecenter_3-3-4.zip
chmod -R 777 /zh - 创建虚拟主机配置文件
vim /etc/nginx/conf.d/zh.conf
#添加:
server {
listen 80;
server_name zh.benet.com;
root /zh;
index index.php index.html;
location ~ \.php$ {
root /zh;
fastcgi_pass 192.168.1.12:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- . 重启nginx
systemctl restart nginx - 在mysql服务器上创建zh数据库和管理用户
登录数据库:mysql -u root
创建数据库:create database zh;
设置管理用户及密码:grant all on zh.* to wangwu@’%’ identified by ‘123.com’; - 在nginx1复制wordpress目录到php的根目录
scp -rp /zh root@192.168.1.12:/zh - 在客户端上配置域名解析
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.9 blog.benet.com zh.benet.com
- 客户端通过域名访问zh,安装并配置
blog.benet.com
zh.benet.com
- 配置需要注意的:
blog.benet.com
数据库名称:blog;
用户名:lisi
密码:123.com
数据库主机:192.168.1.11
zh.benet
数据库主机:192.168.1.11
数据库账号:wangwu
数据库密码:123.com
数据库名称:zh
7. nginx2服务器配置:
-
复制nginx1下的/etc/nginx/到nginx2中/etc/nginx下
scp -rp root@192.168.1.8:/etc/nginx/ /etc/nginx/ -
复制nginx1下的/wordpress/到nginx2/wordpress下
scp -rp root@192.168.1.8:/wordpress/ /wordpress -
复制nginx1下的/zh/到nginx2/zh下
scp -rp root@192.168.1.8:/zh/ /zh -
重启nginx
systemctl restart nginx
####### 6.配置负载均衡服务器lb1: -
关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 -
添加nginx官方源
vim /etc/yum.repos.d/nginx.repo
#添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
- 安装naginx
yum -y install nginx - 启动nginx,并开机自启
systemctl start nginx
systemctl enable nginx
5.优化nginx
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;
- 创建lb配置文件:
vim /etc/nginx/conf.d/lb1.conf
#添加:
upstream web_cluster {
server 192.168.1.8:80;
server 192.168.1.9:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://web_cluster;
include nginx_params;
}
}
server {
listen 80;
server_name zh.benet.com;
location / {
proxy_pass http://web_cluster;
include nginx_params;
}
}
- 重启nginx:
systemctl restart nginx
8.lb2负载均衡服务器配置:
关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
- 添加nginx官方源
vim /etc/yum.repos.d/nginx.repo
#添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
- 安装naginx
yum -y install nginx - 启动nginx,并开机自启
systemctl start nginx
systemctl enable nginx - 复制b1服务器/etc/nginx/复制到lb2中
__scp -rp root@192.168.1.13:/etc/nginx/ /etc/nginx/__ - 修改/etc/nginx/conf.d/lb1.conf修改为lb2.conf
__mv /etc/nginx/conf.d/lb1.conf /etc/nginx/conf.d/lb2.conf __ - 修改客户端的域名解析配置:
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.13 blog.benet.com zh.benet.com
- 再次访问blog.benet.com, zh.benet.com
9.配置lb1负载均衡服务器高可用
- 安装keepalived
yum -y install keepalived - 修改keepalived的配置文件
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
}
}
- 启动keepalived
systemctl start keepalived
10.配置lb2负载均衡服务器高可用
-
- 安装keepalived
yum -y install keepalived
- 安装keepalived
- 修改keepalived的配置文件
vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
router_id lb2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
- 启动keepalived
systemctl start keepalived - 在lb1上查看漂移地址
ip a show dev ens33 - 修改客户端的域名解析配置:
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.254 blog.benet.com zh.benet.com
11.lb1配置解决nginx故障造成群集无法工作
- 编辑nginx监控脚本
vim /sh/check_nginx_proxy.sh
#!/bin/bash
killall -0 nginx
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
- 添加脚本追踪模块到keepalived配置文件
vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb1
}
#增加以下字段
vrrp_script check_nginx_proxy {
script "/sh/check_nginx_proxy.sh"
interval 2
weight 5
}
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
}
#增加以下字段
track_script {
check_nginx_proxy
}
}
- 重启keepalived
systemctl restart keepalived - lb2配置:
vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb2
}
#增加以下字段
vrrp_script check_nginx_proxy {
script "/sh/check_nginx_proxy.sh"
interval 2
weight 5
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
#增加以下字段:
track_script {
check_nginx_proxy
}
}
- 重启keepalived
systmctl restart keepalived
13.配置nfs共享
- 安装nfs-utils、rpcbind:
yum -y install nfs-utils rpcbind - 创建挂载点
mkdir -p /nfs/{blog,zh} - 发布共享目录
vim /etc/exports
添加:
/nfs/blog 192.168.1.0/24(rw,sync,no_root_squash)
/nfs/zh 192.168.1.0/24(rw,sync,no_root_squash)
- 启动nfs服务
systemctl start rpcbind
systemctl start nfs - 在nginx1服务器上查看nfs共享目录
showmount -e 192.168.1.18 - 把wordpress的内容目录挂载到nfs
cd /wordpress
cp -rp wp-content/ wp-contentbak
mount -t nfs 192.168.1.116:/nfs/blog wp-content
cp -rp wp-contentbak/* wp-content/ - 设置永久挂载
vim /etc/fstab
#末尾添加:
192.168.1.18:/nfs/log /wordpress/wp-content nfs defaults 0 0
14.验证:
- 在nfs上验证
cd /nfs/blog/ 进入到nfs/blog下
ls 查看 - 关闭lb1上的keepalived看是否可以访问到论坛
10.关闭lb1上的nginx看是否可以访问到论坛: