LNMP网站架构分离
环境:
- LNMP:192.168.1.11
- MySQL:192.168.1.10
- PHP:192.168.1.12
- Nginx-web-1 :192.168.1.100
- Nginx-web-2:192.168.1.102
- lb(nginx负载均衡):192.168.1.101
- NFS:192.168.1.103
一、实现MySQl数据库迁移
(1)另起一台centos7,安装mysql,并且修改mysql的密码
[root@MySQL ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm
[root@MySQL ~]# yum install mysql-community-server -y
[root@MySQL ~]# systemctl start mysqld
[root@MySQL ~]# systemctl enable mysqld
[root@MySQL ~]# mysql_secure_installation //设置root密码为123.com
(2)把原LNMP服务器上的数据库文件导出并发送到新安装的myql服务器
[root@lnmp ~]# mysqldump -uroot -p --all-databases > `date +%F%H`-mysql-all.sql
Enter password: //lnmp服务器mysql的密码
[root@lnmp ~]# ls
2020-06-2216-mysql-all.sql
LNMP服务器
[root@lnmp ~]# scp -rp 2020-06-2216-mysql-all.sql root@192.168.1.10:/
The authenticity of host '192.168.1.10 (192.168.1.10)' can't be established.
ECDSA key fingerprint is SHA256:sJkYoY2zxGP2sh3mmb/cpDkn3SwjpxqJ3On1SsXvcvs.
ECDSA key fingerprint is MD5:96:1d:bc:3a:86:49:0d:9f:69:56:01:83:14:f5:90:ba.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.10' (ECDSA) to the list of known hosts.
root@192.168.1.10's password:
Permission denied, please try again.
root@192.168.1.10's password:
2020-06-2216-mysql-all.sql 100% 1231KB 23.3MB/s 00:00
mysql服务器
[root@MySQL ~]# cd /
[root@MySQL /]# ls
2020-06-2216-mysql-all.sql boot etc lib media mysql proc run srv tmp var
bin dev home lib64 mnt opt root sbin sys usr
(3)在新的mysql服务器上导入数据库文件
[root@MySQL /]# mysql -u root -p < 2020-06-2216-mysql-all.sql
Enter password:
[root@MySQL /]# mysql -u root -p
Enter password:
mysql> show databases; //数据导入成功
+--------------------+
| Database |
+--------------------+
| information_schema | |
| mysql |
| performance_schema |
| zh |
+--------------------+
5 rows in set (0.00 sec)
(4)在新mysql服务器上创建同名管理用户和密码
[root@MySQL /]# mysql -u root -p
Enter password:
mysql> grant all on blog.* to wangwu@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
(5)在LNMP服务器上修改知乎(zh)的配置文件,重新指定数据库服务器ip
[root@lnmp ~]# cd /zh/ //切换到blog网页根目录
[root@lnmp zh]# grep -R 123.com //搜索保存密码的配置文件位置
system/config/database.php: 'password' => '123.com',
[root@lnmp zh]# vim system/config/database.php
<?php
$config['charset'] = 'utf8mb4';
$config['prefix'] = 'aws_';
$config['driver'] = 'MySQLi';
$config['master'] = array (
'charset' => 'utf8mb4',
'host' => '192.168.1.10', //新MySQL主机ip
'username' => 'wangwu', //MySQL数据库用户名
'password' => '123.com', //MySQL数据库密码
'dbname' => 'zh', //数据库的名称
);
$config['slave'] = false;
(6)把LNMP上的MySQL服务关闭,仍可以访问到页面,表示mysql分离成功
[root@LNMP ~]# systemctl stop mysqld
二、实现PHP拆分
(1)启动一台centos7,安装php
[root@PHP ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
warning: /var/tmp/rpm-tmp.vn0VNk: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:epel-release-7-12 ################################# [100%]
[root@PHP ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
warning: /var/tmp/rpm-tmp.8A5yj5: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:webtatic-release-7-3 ################################# [100%]
[root@PHP ~]# 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
[root@PHP ~]# systemctl start php-fpm.service
(2)修改LNMP服务器上的配置文件,重新指向新的php服务器
[root@LNMP ~]# 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; //新PHP的IP
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@LNMP ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@LNMP ~]# systemctl restart nginx
(3)修改php服务器的配置文件
[root@PHP ~]# vim /etc/php-fpm.d/www.conf
定位并修改为:
listen = 192.168.1.12:9000
listen.allowed_clients = 192.168.1.11
[root@PHP ~]# systemctl restart php-fpm.service
(4)从LNMP服务器复制知乎(zh)的安装目录到php服务器
[root@LNMP ~]# scp -rp /zh/ root@192.168.1.12:/
[root@PHP /]# ls
bin dev home lib64 mnt php root sbin sys usr zh
boot etc lib media opt proc run srv tmp var
(5)LNMP服务器关闭php,客户端验证访问
[root@LNMP ~]# systemctl stop php-fpm.service
三、实现nginx负载均衡
(1)web-1 和web-2安装Nginx(步骤一样)
[root@web-1 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@web-1 ~]# yum -y install nginx
[root@web-1 ~]# systemctl start nginx
(2)把LNMP上的nginx的配置复制到web-1和web-2服务器上。
[root@LNMP ~]# scp -rp /etc/nginx/* root@192.168.1.100:/etc/nginx/
[root@LNMP ~]# scp -rp /etc/nginx/* root@192.168.1.102:/etc/nginx/
(3)把LNMP上网页源码复制到web-1和web-2上。
[root@LNMP ~]# scp -rp /zh/ root@192.168.1.100:/
[root@LNMP ~]# scp -rp /zh/ root@192.168.1.102:/
(4)重启服务
[root@web-1 /]# systemctl restart nginx
[root@web-2 /]# systemctl restart nginx
(5)搭建nginx负载均衡
另起一台服务器
[root@lb ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@lb ~]# yum -y install nginx
[root@lb ~]# systemctl start nginx
(6)配置负载均衡
[root@lb ~]# vim /etc/nginx/conf.d/lb.conf
upstream webcluster {
server 192.168.1.100:80;
server 192.168.1.102:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://webcluster;
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;
}
}
server {
listen 80;
server_name zh.benet.com;
location / {
proxy_pass http://webcluster;
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;
}
}
(7)更改php服务器上的配置文件
[root@PHP ~]# vim /etc/php-fpm.d/www.conf
定位:
listen.allowed_clients = 192.168.1.11
修改为:
listen.allowed_clients = 192.168.1.100,192.168.1.102
[root@PHP ~]# systemctl restart php-fpm.service
(8)重启服务,更改Client的hosts文件,验证访问
[root@lb ~]# systemctl restart nginx
Client
[root@Client ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.101 zh.benet.com
(9)lb和web服务器查看日志
lb
[root@lb ~]# tail -f /var/log/nginx/access.log
192.168.1.130 - - [22/Jun/2020:18:30:16 +0800] "GET /static/common/avatar-mid-img.png HTTP/1.1" 304 0 "http://zh.benet.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
192.168.1.130 - - [22/Jun/2020:18:30:16 +0800] "GET /static/css/default/img/logo.png HTTP/1.1" 304 0 "http://zh.benet.com/static/css/default/common.css?v=20191022" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
192.168.1.130 - - [22/Jun/2020:18:30:16 +0800] "GET /static/css/default/img/bg.gif HTTP/1.1" 304 0 "http://zh.benet.com/static/css/default/common.css?v=20191022" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
web1
[root@web-1 /]# tail -f /var/log/nginx/access.log
192.168.1.101 - - [22/Jun/2020:18:30:16 +0800] "GET /static/js/laydate/theme/default/laydate.css?v=5.0.9 HTTP/1.0" 304 0 "http://zh.benet.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
192.168.1.101 - - [22/Jun/2020:18:30:16 +0800] "GET /static/common/avatar-mid-img.png HTTP/1.0" 304 0 "http://zh.benet.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
192.168.1.101 - - [22/Jun/2020:18:30:16 +0800] "GET /static/css/default/img/logo.png HTTP/1.0" 304 0 "http://zh.benet.com/static/css/default/common.css?v=20191022" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
web2
[root@web-2 /]# tail -f /var/log/nginx/access.log
192.168.1.101 - - [22/Jun/2020:18:30:16 +0800] "GET /static/js/layer/theme/default/layer.css?v=3.1.1 HTTP/1.0" 304 0 "http://zh.benet.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
192.168.1.101 - - [22/Jun/2020:18:30:16 +0800] "GET /static/css/default/img/default_class_imgs.png HTTP/1.0" 304 0 "http://zh.benet.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
192.168.1.101 - - [22/Jun/2020:18:30:16 +0800] "GET /static/css/default/img/bg.gif HTTP/1.0" 304 0 "http://zh.benet.com/static/css/default/common.css?v=20191022" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
192.168.1.101 - - [22/Jun/2020:18:30:44 +0800] "GET /?/crond/run/1592821821 HTTP/1.0" 200 0 "http://zh.benet.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "192.168.1.130"
四、拓展(NFS)
搭建nfs共享服务器,把网站静态元素通过挂载方式放在nfs上
(1)开启一台centos7,安装nfs-utils、rpcbind,并且在web服务器也安装。
[root@nfs ~]# yum -y install nfs-utils rpcbind
(2)创建挂载点
[root@nfs ~]# mkdir -p /nfs/zh
(3)发布共享目录
[root@nfs ~]# vim /etc/exports
/nfs/zh 192.168.1.0/24(rw,sync,no_root_squash)
(4)重启nfs服务
[root@nfs ~]# systemctl restart rpcbind.service
[root@nfs ~]# systemctl restart nfs
(5)在nginx服务器上查看nfs共享目录并把知乎(zh)挂载到NFS。
[root@web-1 /]# showmount -e 192.168.1.103
Export list for 192.168.1.103:
/nfs/zh 192.168.1.0/24
[root@web-2 /]# showmount -e 192.168.1.103
Export list for 192.168.1.103:
/nfs/zh 192.168.1.0/24
web服务器
[root@web-1 system]# mount -t nfs 192.168.1.103:/nfs/zh /yonghu/system/
[root@web-2 system]# mount -t nfs 192.168.1.103:/nfs/zh /yonghu/system/
(6)Client访问