LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Nginx较为稳定、功能丰富、安装配置简单、低系统资源
Nginx既可以在内部直接支持Rails和PHP,也可以支持作为HTTP代理服务器对外进行服务。Nginx用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal好得多
————————————————
基于6台服务器实现LNMP分离部署,A服务器部署nginx,BC服务器部署php,DEF服务器部署MySQL一主二从 。
实战环境
A服务器(nginx):192.168.161.92
B服务器(php1):192.168.161.90
C服务器(php2):192.168.161.91
D服务器(master):192.168.161.82
E服务器(slave1):192.168.161.83
F服务器(slave2):192.168.161.84
安装配置nginx、php、MariaDB
nginx
yum install epel* -y #源库没有nginx,需要配置epel来提供nginx
yum install nginx -y
php
yum install php php-fpm php-devel php-mysql php-xml -y
# centos7.6安装的php版本是5.4,在部署lnmp时会有问题,我们要进行升级,至少PHP5.6
# php升级(5.4~5.6)
[root@localhost ~]# cd /etc/yum.repos.d/
yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm -y
# 开启要升级的remi.repo,选择php5.6
enabled=1
# 重新安装
yum install php php-devel php-fpm php-mysql -y
mysql
# 主库和从库均执行以下命令:
yum install mariadb mariadb-server mariadb-devel -y
# 配置MySQL互为主从:
[root@node5 ~]# egrep "log|server" /etc/my.cnf
log-bin=node5-bin
server-id=1
[root@node6 ~]# egrep "log|server" /etc/my.cnf
server-id=2
[root@node6 ~]# egrep "log|server" /etc/my.cnf
server-id=3
# 启动主库和从库:
systemctl start mariadb
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
mysql
# 在node5上授权:
MariaDB [(none)]> grant replication slave on *.* to "tongbu"@"192.168.161.83" identified by "123456";
MariaDB [(none)]> grant replication slave on *.* to "tongbu"@"192.168.161.84" identified by "123456";
MariaDB [(none)]> flush privileges;
# node6进行同步:
change master to master_host="192.168.161.82", master_user="tongbu", master_password="123456", master_log_file="node5-bin.000001", master_log_pos=477;
#在node7进行同步:
change master to master_host="192.168.161.82", master_user="tongbu", master_password="123456", master_log_file="node5-bin.000001", master_log_pos=477;
# 在node6和node7上均开启slave进程,然后执行show slave status\G,能看到以下状态表示主从搭建成功!
slave start;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
# 创建数据库:
# MariaDB [(none)]> create database discuz charset utf8;
# 授权php连接
# MariaDB [(none)]> grant all on discuz.* to "discuz"@"192.168.161.90" identified by "123456";
# MariaDB [(none)]> grant all on discuz.* to "discuz"@"192.168.161.91" identified by "123456";
# MariaDB [(none)]> flush privileges;
部署流程
解压Discuz网站包到nginx网站发布目录
unzip Discuz_X3.1_SC_UTF8.zip -d /data/webapps
chown nginx. -R /data/webapps/upload/
修改配置文件-nginx.conf
vim nginx.conf
# 删除主配文件nginx.conf中log_format段的注释
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 指定应用的虚拟主机目录(主配置文件http指令块下添加):
include vhost/*.conf;
# 创建Discuz虚拟主机配置文件
# 切换到nginx的vhost目录:
# 创建并编辑bbs.discuz.com.conf
# 这里配置php均衡池,将php请求转到后端php1和php2处理:
vim bbs.discuz.com.conf
upstream cgidiscuz {
server 192.168.161.90:9000;
server 192.168.161.91:9000;
}
server {
listen 80;
server_name bbs.discuz.com;
charset utf-8;
access_log logs/discuz.access.log main;
location / {
root /data/webapps/upload/;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /data/webapps/upload/;
fastcgi_pass cgidiscuz;
fastcgi_next_upstream error http_500;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 记得重启nginx服务
配置php-fpm
# 修改php1中的/etc/php-fpm.d/www.conf
listen = 192.168.161.90:9000
listen.allowed_clients = 192.168.161.92 #nginx
user = nginx
group = nginx
# 修改php2中的/etc/php-fpm.d/www.conf
listen = 192.168.161.91:9000
listen.allowed_clients = 192.168.161.92 #nginx
user = nginx
group = nginx
# 重启php1和php2
# 分别查看php1和php2的监听端口
[root@localhost ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.161.90:9000 0.0.0.0:* LISTEN 112965/php-fpm: mas
[root@localhost php-7.4.6]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.161.91:9000 0.0.0.0:* LISTEN 112138/php-fpm: mas
暂时关闭php2的php-fpm服务
# 关闭php2服务,防止安装过程中数据写入不一致的问题,方便后续数据同步
systemctl stop php-fpm
同步Discuz网站代码
# 先在php1和php2上创建发布目录
mkdir -p /data/webapps/
# 在nginx上操作,同步网站代码至php1发布目录
rsync -av /data/webapps/upload 192.168.161.90:/data/webapps/
创建数据库及授权用户(直接对网段进行授权)
# 登录MariaDB Master,并操作
create database discuz charset utf8;
grant all on discuz.* to "discuz"@"192.168.161.%" identified by "123456";
flush privileges;
添加host解析,并访问http://bbs.discuz.com
安装完访问后报错
加载某些静态资源报错:404资源未找到
原因:安装过程中写入到php1发布目录的静态资源,在nginx的发布目录中未找到
解决办法
# 在php1中操作rsync,将安装过程中生成的静态文件同步到nginx
rsync -av /data/webapps/upload 192.168.161.92:/data/webapps/
再次刷新页面
所有资源加载正常
同步php1发布目录中的资源到php2发布目录
# 在php1中操作rsync,将发布目录中的所有资源同步至php2发布目录
rsync -av /data/webapps/upload 192.168.161.91:/data/webapps/
开启php2的php-fpm服务
# 在php2上操作
systemctl start php-fpm
故障测试
关闭php1的php-fpm服务
# 在php1上操作
systemctl stop php-fpm
再次访问页面
网站正常访问,表示php1和php2负载均衡正常。
心得体会:使用分布式部署,资源发布还是用nfs挂载比较方便,要不然后期维护时的资源同步问题很麻烦……
方案改进
增加一台nfs服务器,把nginx、php1和php2的发布目录指定到nfs上
安装nfs组件
# 分别在nfs、nginx、php1和php2上安装nfs组件
yum -y install nfs-utils
配置nfs服务端-192.168.161.89
# 创建/data/nfs/webapps目录,用于共享网站资源
mkdir -p /data/nfs/webapps
# 编辑/etc/exports文件,指定共享目录
/data/nfs 192.168.161.0/24(rw,sync)
## 格式:
## /data/jfedu 要共享的目录,需要存在
## 192.168.161.0/24 谁能挂载使用,可以是网段,也可以指定具体ip
## /data/jfedu *(rw,sync,no_root_squash)表示任意客户端可连接
## (rw,sync) 挂载的一些参数,rw表示挂载为可读可写,sync表示同步
# 重启rpc和nfs服务并导出(广播)编辑的exports文件
systemctl restart rpcbind
systemctl restart nfs
exportfs -r
解压Discuz网站包到nfs共享目录
unzip Discuz_X3.1_SC_UTF8.zip -d /data/nfs/webapps
chown nginx. -R /data/nfs/webapps/upload/
分别在nginx, php1, php2上挂载nfs共享目录
# 可用showmount搜索服务端可用的共享目录
showmount -e 192.168.161.89
创建目录,用于挂载
mkdir /mnt/nfs
# 在nginx,php1,php2上执行挂载
mount -t nfs 192.168.161.89:/data/nfs /mnt/nfs
修改nginx虚拟主机配置文件
# 编辑/usr/local/nginx/conf/vhost/blog.wordpress.com.con 修改location段和location ~ \.php$ 段中的root目录
location / {
root /mnt/nfs/webapps/upload/;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /mnt/nfs/webapps/upload/;
fastcgi_pass cgidiscuz;
fastcgi_next_upstream error http_500;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
关闭php2的php-fpm服务
systemctl stop php-fpm
# 多次测试发现:安装Discuz过程中如果开启多台php均衡,会导致安装后访问静态资源失效!
访问网站,开始Discuz安装 – http://bbs.discuz.com/
再次开启php2的php-fpm服务
systemctl start php-fpm
网站访问测试