LNMP平台拆分
1.LNMP工作原理
1.用户通过浏览器输入url http://jxl.wecenter.com 2.先查看app缓存,也就是浏览器cookie缓存 3.如果app没有缓存再查看DNS解析 1)本地DNS缓存 2)主机hosts解析 3)本地DNS服务器,通过递归查询将解析请求发送给内网、网关、ISP 4)本地DNS服务器再通过迭代查询将解析请求发送至公网DNS服务器 4.通过解析到的web服务器的IP地址跟web服务器建立tcp连接 1)通过私网交换机、路由器、防火墙等网络设备传递或验证 2)和调度器建立三次握手 5.调度器会和web集群建立三次握手,将HTTP请求发送至web服务器 发送过程中有三个动作 1)general URL http://jxl.wecenter.com 动作 get 状态码 200 解析的IP 10.0.0.7 2)Req header 属性 文件类型、加密、编码、压缩等 长连接 keepalive Host jxl.rewrite.com User-Agent 用户使用的浏览器 3)Rep header 长连接 属性 文件类型、编码 server 服务器使用的软件 6.web服务器收到http请求 1)静态页面 .html nginx自己处理 图片、视频、附件类文件:NFS 网页内容、用户信息:MySQL 2)动态页面 .php Nnginx无法处理,交给php-fpm(127.0.0.1:9000)插件来实现 7.Nginx通过fastcgi协议,将请求发送至php-fpm 8.php-fpm调用wrapper线程,将php代码交于php解析器 9.php解析器解析php代码 1)如果是代码直接返回至nginx 2)如果涉及到查询数据库,就会与mysql建立tcp连接 10.返回
2.将原有LNMP平台进行拆分
2.1.拆分MySQL
1.在web上备份数据库 [root@jxl log]# mysqldump -uroot -p123 --all-databases --single-transaction > `date +%F%H`-mysql-all.sql mysqldump -uroot -p123 使用root用户来做mysql备份 --all-databases 所有数据库 --single-transaction 数据一致性 2.拷贝备份数据库文件至新的数据库服务器上 [root@jxl log]# scp 2020-04-2915-mysql-all.sql root@192.168.81.230:/root/ 3.导入数据库 扩展:mysql自身提供安全初始化脚本 [root@localhost ~]# mysql_secure_installation 输出数据库的root密码,全程y,是否禁止root登录可以选择,如果要改密码中间需要输入两次新密码 [root@localhost ~]# mysql -uroot -p123 < 2020-04-2915-mysql-all.sql 4.登录数据库验证导入是否成功 [root@localhost ~]# mysql -uroot -p123 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | wecenter | | wordpress | | xyshop | +--------------------+ 6 rows in set (0.00 sec) mysql> use wecenter; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> desc aws_users_qq; +---------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------------+------+-----+---------+----------------+ | id | bigint(11) unsigned | NO | PRI | NULL | auto_increment | | uid | int(11) | NO | MUL | NULL | | | nickname | varchar(64) | YES | | NULL | | | openid | varchar(128) | YES | MUL | | | | gender | varchar(8) | YES | | NULL | | | add_time | int(10) | YES | MUL | NULL | | | access_token | varchar(64) | YES | MUL | NULL | | | refresh_token | varchar(64) | YES | | NULL | | | expires_time | int(10) | YES | | NULL | | | figureurl | varchar(255) | YES | | NULL | | +---------------+---------------------+------+-----+---------+----------------+ 10 rows in set (0.00 sec) 5.关闭原来服务器的mysql后,会发现页面访问失败 6.修改应用配置文件,指定新的数据库位置 利用grep找到配置文件路径 [root@jxl wecenter]# grep -R '192.168.81.220' | grep -v cache system/config/database.php: 'host' => '192.168.81.220', 编辑此文件 [root@jxl wecenter]# vim system/config/database.php <?php $config['charset'] = 'utf8mb4'; $config['prefix'] = 'aws_'; $config['driver'] = 'MySQLi'; $config['master'] = array ( 'charset' => 'utf8mb4', 'host' => '192.168.81.230', 'username' => 'root', 'password' => '123', 'dbname' => 'wecenter', ); $config['slave'] = false; 7.刷新页面即可
拆分后页面正常显示
2.2.拆分PHP
查看这个博客 https://blog.csdn.net/weixin_43815140/article/details/105311944 也就是把php的监听地址修成0.0.0.0即可
2.3.增加NFS存储服务器
抓取图片代码
1.安装nfs [root@localhost ~]# yum -y install nfs-util rpcbind 2.配置共享目录 [root@localhost ~]# vim /etc/exprots /data/wordpress 172.16.1.0/24(rw,sync,no_root_squash) /data/wecenter 172.16.1.0/24(rw,sync,no_root_squash) 3.启动nfs [root@localhost ~]# systemctl restart nfs rpcbind 4.查看共享目录 [root@localhost ~]# showmount -e Export list for localhost.localdomain: /data/wecenter 172.16.1.0/24 /data/wordpress 172.16.1.0/24 5.挂载 [root@localhost ~]# mount 172.16.1.10:/data/wordpress web/wordpress/wp-content
3.部署多web
1.安装nginx [root@localhost ~]# scp -rp /etc/nginx/* root@192.168.81.230:/etc/nginx/ 2.安装php [root@localhost ~]# scp -rp /etc/php-fpm.d/* root@192.168.81.230:/etc/php-fpm.d 3.将网站源码直接从web01上拷贝过来 [root@localhost ~]# tar cvfP web.tar.gz /web [root@localhost ~]# scp web.tar.gz root@192.168.81.230:/root 4.常见用户和组 [root@localhost ~]# useradd www 5.挂载nfs [root@localhost ~]# mount 172.16.1.10:/data/wordpress web/wordpress/wp-content 6.启动nginx、php-fpm [root@localhost ~]# systemctl reload nginx php-fpm
4.LNMP拆分完整部署
4.1.环境规划
4.2.思路分析
1.安装软件包
2.部署nginx、配置虚拟主机,下载网页源码
3.部署mysql,提供远程访问及网站数据库
4.部署NFS,提供共享目录
4.3.Web实施
1.安装nginx、php [root@localhost ~]# mkdir soft [root@localhost ~]# cd soft [root@localhost soft]# rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm [root@localhost php]# sh php_install.sh 2.创建用户 [root@localhost ~]# groupadd -g 666 www [root@localhost ~]# useradd -u 666 -g 666 -s /sbin/nologin www 3.配置Nginx、PHP [root@localhost ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf [root@localhost conf.d]# sed -ri '/^user/c user = www' /etc/php-fpm.d/www.conf [root@localhost conf.d]# sed -ri '/^group/c group = www' /etc/php-fpm.d/www.conf 4.启动nginx、php [root@localhost conf.d]# systemctl restart nginx php-fpm [root@localhost conf.d]# systemctl enable nginx php-fpm 5.创建知乎、wordpress站点配置文件 [root@localhost conf.d]# rename .conf .conf.bak *.conf [root@localhost conf.d]# vim wordpress.conf #wordpress server { listen 80; server_name jxl.wordpress.com; root /web/wordpress; index index.php index.html; access_log /nginx_log/jxl_wordpress_access.log main; location ~ \.php$ { root /web/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@localhost conf.d]# vim wecenter.conf #wecenter server { listen 80; server_name jxl.wecenter.com; root /web/wecenter; index index.php index.html; access_log /nginx_log/jxl_wecenter_access.log main; location ~ \.php$ { root /web/wecenter; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 6.获取wordpress、wecenter源码 [root@localhost src]# unzip -d wecenter WeCenter_3-3-4.zip [root@localhost src]# unzip wordpress-5.2.3-zh_CN.zip [root@localhost src]# mv wecenter/ wordpress /web/ [root@localhost /]# chown -R www:www /web/ 7.重载服务器 [root@localhost conf.d]# systemctl restart nginx php-fpm
4.4.MySQL实施
1.安装mysql [root@jxl soft]# rpm -ivh mysql-community-common-5.6.47-2.el7.x86_64.rpm [root@jxl soft]# rpm -ivh mysql-community-libs-5.6.47-2.el7.x86_64.rpm [root@jxl soft]# rpm -ivh mysql-community-client-5.6.47-2.el7.x86_64.rpm [root@jxl soft]# rpm -ivh mysql-community-server-5.6.47-2.el7.x86_64.rpm 2.启动mysql 3.初始化mysql [root@jxl ~]# mysql_secure_installation mysql_root密码---y---设置新密码---y----n----y----y 4.创建wordpress、wecenter所用数据库并创建用户 [root@jxl ~]# mysql -uroot -p123 mysql> create database wordpress; mysql> create database wecenter; mysql> grant all on wordpress.* to 'wpadmin'@'%' identified by '123'; mysql> grant all on wecenter.* to 'wcadmin'@'%' identified by '123';
4.5.安装NFS
[root@localhost ~]# yum -y install nfs-utils [root@localhost ~]# vim /etc/exports /data/wordpress /data/wordpress 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/wecenter 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) [root@localhost ~]# systemctl restart nfs rpcbind [root@localhost ~]# showmount -e Export list for localhost.localdomain: /data/wecenter 172.16.1.0/24 /data/wordpress 172.16.1.0/24 注意NFS如果指定uid、gid,客户端上必须存在和他一样uid、gid的用户
4.6.web挂载NFS
wordpress 1.先将站点存放文件的目录备份便于挂载后还原 [root@localhost wordpress]# cp -r wp-content/ wp-content_bak 2.挂载 [root@localhost wordpress]# mount 172.16.1.10:/data/wordpress /web/wordpress/wp-content 3.还原 [root@localhost wordpress]# mv wp-content_bak/* wp-content/ 4.永久挂载 [root@localhost wordpress]# vim /etc/fstab 172.16.1.10:/data/wordpress /web/wordpress/wp-content nfs defaults 0 0 wecenter 1.知乎默认没有任何附件,直接挂载即可 2.挂载 mount 172.16.1.10:/data/wecenter /web/wecenter/uploads/ 3.永久挂载 [root@localhost wordpress]# vim /etc/fstab 172.16.1.10:/data/wecenter /web/wecenter/uploads nfs defaults 0 0
4.7.页面安装
wordpress安装
填写数据库地址为拆分后的MySQL地址
NFS上已经有文件
wecenter安装
填写数据库地址为拆分后的MySQL地址
NFS查看
5.其他节点快速部署
1.将源码打包 [root@localhost web]# tar cfP wecenter.tar.gz wecenter/ [root@localhost web]# tar cfP wordpress.tar.gz wordpress/ 2.将源码推送其他web节点 [root@localhost ~]# scp /web/*.tar.gz root@192.168.81.220:/web/ [root@localhost ~]# scp /web/*.tar.gz root@192.168.81.230:/web/ 3.将站点配置文件推送至web节点 [root@localhost ~]# scp /etc/nginx/conf.d/*.conf root@192.168.81.220:/etc/nginx/conf.d/ [root@localhost ~]# scp /etc/nginx/conf.d/*.conf root@192.168.81.230:/etc/nginx/conf.d/ 4.在web节点解包 [root@jxl ~]# tar xf wecenter.tar.gz [root@jxl ~]# tar xf wordpress.tar.gz 5.挂载nfs [root@jxl ~]# vim /etc/fstab 172.16.1.10:/data/wordpress /web/wordpress/wp-content nfs defaults 0 0 172.16.1.10:/data/wecenter /web/wecenter/uploads nfs defaults 0 0 [root@jxl ~]# mount -a 6.创建用户赋权限 [root@jxl ~]# groupadd -g 666 www [root@jxl ~]# useradd -u 666 -g 666 -s /sbin/nologin www 7.重载nginx [root@jxl ~]# systemctl reload nginx
r.tar.gz
[root@jxl ~]# tar xf wordpress.tar.gz
5.挂载nfs
[root@jxl ~]# vim /etc/fstab
172.16.1.10:/data/wordpress /web/wordpress/wp-content nfs defaults 0 0
172.16.1.10:/data/wecenter /web/wecenter/uploads nfs defaults 0 0
[root@jxl ~]# mount -a
6.创建用户赋权限
[root@jxl ~]# groupadd -g 666 www
[root@jxl ~]# useradd -u 666 -g 666 -s /sbin/nologin www
7.重载nginx
[root@jxl ~]# systemctl reload nginx