第二节:架构拆分

nfs一、回顾

1.搭建LNMP环境

#搭建nginx
1.配置yum源
2.安装nginx
3.配置nginx
4.创建用户
5.启动服务

#搭建php
1.上传服务包
2.解压包
3.安装本地rpm包
4.配置php
5.启动

#搭建mariadb
1.安装
2.启动
3.连接测试
4.设置数据库的用户名密码

2.搭建wordpress

1.配置nginx
2.创建站点目录
3.上传代码包
4.解压
5.授权代码
6.重启nginx
7.配置hosts访问测试
8.数据库建库
9.根据页面提示配置数据库信息
10.使用博客

二、搭建LNMP

1.配置官方源

[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2.yum安装nginx

[root@web01 ~]# yum install -y nginx

3.配置nginx

[root@web03 ~]# vim /etc/nginx/nginx.conf 
user  www;
...
http {
    client_max_body_size 200m;
}

4.创建用户

[root@web03 ~]# groupadd www -g 666
[root@web03 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

5.启动

[root@web03 ~]# systemctl start nginx
[root@web03 ~]# systemctl enable nginx

6.上传php包

[root@web03 ~]# rz
[root@web03 ~]# ll
-rw-r--r--  1 root root 19889622 Nov 22 15:52 php.tar.gz

7.安装

[root@web03 ~]# tar xf php.tar.gz 
[root@web03 ~]# yum localinstall -y *.rpm

8.配置PHP

[root@web03 ~]# vim /etc/php-fpm.d/www.conf 
user = www
group = www

[root@web03 ~]# vim /etc/php.ini
upload_max_filesize = 200M
post_max_size = 200M

9.启动

[root@web03 ~]# systemctl start php-fpm
[root@web03 ~]# systemctl enable php-fpm

10.安装mariadb

[root@web03 ~]# yum install -y mariadb-server

11.启动

[root@web03 ~]# systemctl start mariadb
[root@web03 ~]# systemctl enable mariadb

12.设置数据库密码

[root@web03 ~]# mysqladmin -uroot password
New password: 123
Confirm new password: 123

13.使用密码连接数据库测试

[root@web03 ~]# mysql -uroot -p
Enter password: 123

三、搭建wordpress、知乎、edusoho

1.上传代码包

[root@web03 ~]# mkdir /code
[root@web03 ~]# cd /code/
[root@web03 code]# rz
[root@web03 code]# ll
total 86372
-rw-r--r-- 1 root root 68889387 Dec  1 09:07 edusoho-8.3.36.tar.gz
-rw-r--r-- 1 root root  8451194 Dec  1 09:07 WeCenter_3-2-1.zip
-rw-r--r-- 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz

2.解压代码包

[root@web03 code]# tar xf edusoho-8.3.36.tar.gz
[root@web03 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@web03 code]# unzip WeCenter_3-2-1.zip

[root@web03 code]# mv WeCenter_3-2-1 zhihu

[root@web03 code]# ll
drwxr-xr-x 10  501 games      115 Jul 18  2019 edusoho
drwxr-xr-x  5 1006  1006     4096 Jan 11  2019 wordpress
drwx------ 14 root root       296 Jun  4  2018 zhihu

3.授权代码

[root@web03 code]# chown -R www.www /code/

4.配置nginx

1)配置wordpress的nginx

[root@web03 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}

2)配置知乎的nginx配置

[root@web03 ~]# vim /etc/nginx/conf.d/linux.zh.com.conf
server {
    listen 80;
    server_name linux.zh.com;
    root /code/zhihu;

    location / {
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

3)配置edusoho的nginx配置

server {
    listen 80;
    server_name linux.edu.com;
    root /code/edusoho/web;
    
    location / {
        index app.php;
        try_files $uri @rewriteapp;
    }
    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }
    location ~ ^/udisk {
        internal;
        root /var/www/edusoho/app/data/;
    }
    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
        fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 128k;
    }
    # 配置设置图片格式文件
    location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
        # 过期时间为3年
        expires 3y;
        # 关闭日志记录
        access_log off;
        # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
        gzip off;
    }
    # 配置css/js文件
    location ~* \.(css|js)$ {
        access_log off;
        expires 3y;
    }
    # 禁止用户上传目录下所有.php文件的访问,提高安全性    
    location ~ ^/files/.*\.(php|php5)$ {
        deny all;
    }
    # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
    location ~ \.php$ {
        # [改] 请根据实际php-fpm运行的方式修改
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

4)重启nginx

[root@web03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 ~]# systemctl restart nginx

5.配置本地hosts访问测试

10.0.0.9 linux.wp.com linux.zh.com

10.0.0.9 linux.wp.com 
10.0.0.9 linux.zh.com

6.数据库建库

[root@web03 ~]# mysql -uroot -p
Enter password: 123

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create database zhihu;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| zhihu              |
+--------------------+
6 rows in set (0.00 sec)

7.创建数据库用户并授权

MariaDB [(none)]> grant all on wordpress.* to wp@'localhost' identified by '123';
Query OK, 0 rows affected (0.07 sec)

MariaDB [(none)]> grant all on zhihu.* to zh@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

#查看用户
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| wp   | localhost |
| zh   | localhost |
+------+------------+
8 rows in set (0.00 sec)

8.根据页面提示操作

四、拆分数据库

1.为什么要拆分

由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现out of memory从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。

2.数据库拆分后解决了什么问题

1.缓解web网站的压力
2.增强数据库读写性能
3.提高用户访问速度

3.环境准备

主机 IP 部署的服务
web01 10.0.0.7,172.16.1.7 nginx+php
db01 172.16.1.51 mariadb

4.在新的服务器上搭建数据库(盖新房子)

[root@db01 ~]# yum install -y mariadb-server

5.配置数据库密码(装修)

[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# mysqladmin -uroot password
New password: 123
Confirm new password: 123

6.测试连接远程数据库(测试房子能不能住人)

[root@web01 ~]# mysql -uroot -p -h 172.16.1.51
Enter password: 
ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MariaDB server

mysql           #数据库命令
-u              #指定用户
root            #root用户
-p              #使用数据库root用户的密码
123             #数据库root用户的密码
-h              #指定数据库的主机
172.16.1.51     #主机IP

7.授权用户远程连接(想办法住)

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on wordpress.* to wp@'172.16.1.%' identified by '1qaz@WSX';
Query OK, 0 rows affected (0.00 sec)

grant               #数据库授权命令
all                 #所有权限
on                  #在...上面
wordpress.*          #wordpress下面的所有表   库.表
to                  #给...
wp@'172.16.1.%'    #数据库用户
identified          #设置密码
by                  #密码是...
'1qaz@WSX';            #密码内容

8.再次测试连接(房子可以住了)

[root@web01 ~]# mysql -uwp -p -h 172.16.1.51
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| wordpress          |
+--------------------+
3 rows in set (0.00 sec)

9.旧数据库导出数据(打包行李搬出房子)

[root@web01 ~]# mysqldump -uroot -p -B wordpress > /tmp/wp.sql
Enter password: 123

10.将数据推送到新服务器(汽车运送行李)

[root@web01 ~]# scp /tmp/wp.sql 172.16.1.51:/tmp/

11.将数据导入新数据库(将行李放入新房子)

1)库外导入

[root@db01 ~]# mysql -uroot -p < /tmp/wp.sql 
Enter password: 

2)库内读取

MariaDB [wordpress]> source /tmp/wp.sql;

3)任意门方式导数据

[root@web01 ~]# mysql -uwp -p -h 172.16.1.51 < /tmp/wp.sql 
Enter password: 1qaz@WSX

12.查看数据

MariaDB [(none)]> use wordpress;
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
MariaDB [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)

13.修改项目中数据库地址(告知亲戚新家地址)

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

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

/** MySQL数据库密码 */
define('DB_PASSWORD', '1qaz@WSX');

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

[root@web03 ~]# vim /code/zhihu/system/config/database.php

14.停掉旧数据库(拆迁旧家)

[root@web01 ~]# systemctl stop mariadb

15.访问页面测试

http://linux.wp.com/

五、扩展web服务器

1.环境准备

主机 IP 部署服务
web01 10.0.0.7,172.16.1.7 nginx+php
web02 10.0.0.8,172.16.1.8 nginx+php
db01 172.16.1.51 mariadb

2.web02搭建nginx

1)配置官方源

[root@web01 ~]# scp /etc/yum.repos.d/nginx.repo 172.16.1.8:/etc/yum.repos.d/

2)安装nginx

[root@web02 ~]# yum install -y nginx

3)创建用户

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

4)上传php包

[root@web02 ~]# rz

5)解压安装php

[root@web02 ~]# tar xf php.tar.gz 
[root@web02 ~]# yum localinstall -y *.rpm

6)同步web01的配置文件到web02

#同步nginx配置文件
[root@web01 ~]# scp /etc/nginx/nginx.conf 172.16.1.8:/etc/nginx/ 
[root@web01 ~]# scp /etc/nginx/conf.d/linux.wp.com.conf 172.16.1.8:/etc/nginx/conf.d/

#同步php的配置
[root@web01 ~]# scp /etc/php-fpm.d/www.conf 172.16.1.8:/etc/php-fpm.d/ 
[root@web01 ~]# scp /etc/php.ini 172.16.1.8:/etc/

7)启动nginx和php服务

[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
[root@web02 ~]# systemctl start php-fpm
[root@web02 ~]# systemctl enable php-fpm

8)推送web01站点目录到web02

[root@web01 ~]# scp -r /code 172.16.1.8:/

9)授权站点目录

[root@web02 ~]# chown -R www.www /code/

10)修改本地hosts访问测试

#10.0.0.7 linux.wp.com
10.0.0.8 linux.wp.com

六、搭建文件共享

1.环境准备

主机 IP 部署服务
web01 10.0.0.7 nginx+php
web02 10.0.0.8 nginx+php
db01 172.16.1.51 mariadb
nfs 172.16.1.31 nfs

2.搭建NFS服务器

1)安装nfs

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

2)创建挂载目录

[root@nfs ~]# mkdir /data/wp -p

3)配置NFS

[root@nfs ~]# vim /etc/exports
/data/wp 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

4)创建用户

5)授权

[root@nfs ~]# chown -R www.www /data/

6)启动NFS

[root@nfs ~]# systemctl start nfs

7)检查配置

[root@nfs ~]# cat /var/lib/nfs/etab 
/data/wp    172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)

3.配置客户端

1)安装nfs和rpcbind

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

2)启动rpcbind

3)查看挂载点

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wp 172.16.1.0/24

4)确定挂载目录

[root@web01 ~]# ll /code/wordpress/wp-content/uploads/

5)先推送挂载目录下的文件

[root@web01 ~]# rsync -avz /code/wordpress/wp-content/uploads/ 172.16.1.31:/data/wp/
[root@web02 ~]# rsync -avz /code/wordpress/wp-content/uploads/ 172.16.1.31:/data/wp/

#验证文件
[root@nfs ~]# ll /data/wp/2020/12/

6)挂载

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

七、实时备份

作业:

1.恢复快照
2.搭建lnmp架构
3.搭建博客与知乎
4.两台web服务器实现文件同步
5.nfs文件实时备份到backup
上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术