首页 > 技术文章 > Nginx架构的演进

dlq-52 2019-09-19 16:40 原文

1.如何将LNMP拆分为LNP+MYSQL

(1)备份172.16.1.7上的数据库信息

[root@web01 ~]# mysqldump -uroot -p'oldxu.com' --all-databases > mysql-all.sql

(2)将172.16.1.7上的数据推送至172.16.1.51

[root@web01 ~]# scp mysql-all.sql root@172.16.1.51:/tmp

(3)登录172.16.1.51 恢复数据

[root@db01~]#yum install mariadb mariadb-sever -y
[root@db01~]#systemctl enable mariadb
[root@db01~]#systemctl start mariadb

(4)读取sql文件至数据库中

[root@db01~]#mysql -uroot </tmp/mysql-all.sql
[root@db01~]#systemctl restart mariadb

 

(5)配置一个远程用户允许其他服务器能通过远程的方式连接

[root@db01~]#mysql -uroot -poldxu.com
MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'oldxu.com';
MariaDB [(none)]> flush privileges;

 

(6)将172.16.1.7程序连接本地的数据库的数据库,修改为远程的数据库(应用割接)

[root@web01 ~]#systemctl disable mariadb
[root@web01 ~]#systemctl stop mariadb

 

2.案例:将wordpressLNMP拆分成LNP+MYSQL

  (1)接上面的步骤进入wordpress查找出存放数据库的文件

[root@web01 ~]#cd /code/wordpress
[root@web01 wordpress]#find ./ -type f |xargs grep "oldxu.com"
./wp-config.php:define( 'DB_PASSWORD', 'oldxu.com' );

 

(2)修改数据库

[root@web01 wordpress]#vi ./wp-config.php
/** WordPress数据库的名称 */
    define( 'DB_NAME', 'wordpress' );

    /** MySQL数据库用户名 */
    define( 'DB_USER', 'all' );

    /** MySQL数据库密码 */
    define( 'DB_PASSWORD', 'oldxu.com' );

    /** MySQL主机 */
    define( 'DB_HOST', '172.16.1.51' );

 

(3)访问页面测试

3.将wecenteLNMP拆分成LNP+MYSQL

修改数据库

[root@web01 zh]# find ./ -type f | xargs grep "oldxu.com"
./system/config/database.php: 'password' => 'oldxu.com',

4.将edusohoLNMP拆分成LNP+MYSQL

[root@web01 edusoho]# find ./ -type f | xargs grep "oldxu.com"
./app/config/parameters.yml

清理缓存
[root@web01 edusoho]# rm -rf /code/edusoho/app/cache/*

5.如何扩展多台web节点,简称web集群

(1)准备一台172.16.1.8的服务器

(2)确保172.16.1.8上安装了Nginx和php

yum install nginx -y
yum -y install nginx php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

 

(3)确保172.16.1.8nginx配置php配置 代码和172.16.1.7一致

创建用户和用户组

[root@web02 ~]# groupadd -g 666 www
[root@web02
~]# useradd -u666 -g666 www

 

切到172.16.1.7 上执行如下操作

[root@web01 ~]# rsync -avz --delete  /etc/nginx root@172.16.1.8:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/php.ini root@172.16.1.8:/etc/
[root@web01 ~]# rsync -avz --delete  /etc/php-fpm.d root@172.16.1.8:/etc/

 

打包代码

[root@web01 ~]# tar czf code.tar.gz /code

 

拷贝代码

[root@web01 ~]# scp code.tar.gz root@172.16.1.8:/tmp

 

(4)回到172.16.1.8  然后解包  授权  重启服务,并加入开机自启

[root@web02 ~]# tar xf /tmp/code.tar.gz -C /
[root@web02 ~]# systemctl restart nginx php-fpm
[root@web02 ~]# systemctl enable nginx php-fpm

 (5)host劫持,浏览器测试

10.0.0.8 blog.oldxu.com  zh.oldxu.com e.oldxu.com

6.如何将多台节点的静态资源共享至NFS

(1)准备172.16.1.31 nfs存储服务器

安装

[root@nfs ~]#yum install nfs-utils -y

 

配置

[root@nfs ~]# cat /etc/exports
        /data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
        /data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
        /data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

 

初始化环境

[root@nfs ~]# mkdir -p /data/{blog,zh,edu}
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# chown -R www.www /data/

 

启动

[root@nfs ~]# systemctl enable nfs
[root@nfs ~]# systemctl restart nfs

 (2)web1安装nfs,然后使用showmount查看服务端共享的资源

yum install nfs-utils -y
showmount -e 172.16.1.31

 

(3).查找wordpress静态资源存放的位置

浏览器----右键-----检查----network------选择左上角的select按钮----点击对应的图片,获取URL地址

 

(4)找到web存储的图片所在的路径 http://blog.oldxu.com/wp-content/uploads/2019/09/tt.jpeg

[root@web01 ~]#cd /code/wordpress/wp-content 
[root@web01 wp-content]# mv uploads/ uploads_bak 
[root@web01 wp
-content]# scp -rp uploads_bak/* root@172.16.1.31:/data/blog/
[root@web01 wp-content]# mkdir uploads

 

(5)在172.16.1.7....应用服务器上进行挂载

[root@web01 wp-content]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads

PS: 注意权限问题
[root@nfs ~]# chown -R www.www /data/

 (6)将挂载信息加入开机自启

 

[root@web01 wp-content]# tail -l  /etc/fstab
[root@web01 wp-content]# mount -a

  

(5)访问网站测试

 

推荐阅读