文章目录

一、LNMP架构基本概念

1.1 什么是LNMP

  • LNMP 是一套技术的组合,L=Linux、N=Nginx、M=[MySQL8.0|Mariadb5.5]、P=[PHP|Python]
  • nginx仅支持解析html文件;图片传输;视频传输;不支持 php、python、java等脚本文件

1.2 LNMP实现过程

当用户请求http://www.bertwu.com/index.php时,对于nginx服务器而言,是无法处理.php这样的脚本,那么nginx如何配置,才支持这样的动态请求呢?
第一步:当用户发起 HTTP 请求,请求首先被 Nginx 接收;
第二步:Nginx 通过预先定义好的 location 规则进行匹配;
第三步:Nginx 将匹配到的动态内容,通过 fastcgi 协议传到给后端的 php 应用服务处理
在这里插入图片描述

1.3 LNMP实现细节

1.用户首先通过 http 协议发起请求,请求会先抵达Nginx;
2.Nginx 根据用户的请求进行 Location 规则匹配;
3.Location 如果匹配到请求是静态,则由 Nginx 读取本地直接返回;
4.Location如果匹配到请求是动态,则由Nginx将请求转发给fastcgi协议;
5.fastgi收到后会将请求交给php-fpm管理进程;
6.php-fpm管理进程接收到后会调用具体的worker工作进程, worker进程会调用php解析器解析代码,php解析后直接返回
7.如果有查询数据库操作,则由php连接数据库(用户 密码 IP)发起查询的操作
8.用户->http->nginx->fastcgi->php-fpm->php->tcp->mysql
9.最终数据由mysql->tcp->php->php-fpm->fastcgi->nginx->http->user
在这里插入图片描述

二、LNMP架构环境安装

2.1 nginx安装

1.使用官方仓库安装Nginx
详见:nginx安装
2.配置 Nginx 进程运行用户

[root@web01 nginx]# groupadd -g 666 www
[root@web01 nginx]# useradd -u 666 -g 666 www
[root@web01 nginx]sed -i '/^user/c user www;' /etc/nginx/nginx.conf

3.启动nginx,并加入开机自启

[root@web01 nginx]# systemctl start nginx 
[root@web01 nginx]# systemctl enable nginx

2.2 php安装

方式1. 手动新增 repo 文件,但是速度很慢,建议本地安装

# 手动配置yum源
[root@oldxu ~]# cat /etc/yum.repos.d/php.repo
[webtatic-php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

2.卸载系统中存在的低版本php

[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common

3.安装高版本的 php

[root@oldxu ~]# yum -y install 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

方式2. 下载包到本地,上传服务器解压到~目录,执行命令(推荐这种方式)
php下载:
链接:https://pan.baidu.com/s/1xFqGMSywuY7Pe4CHVETc_Q
提取码:nlgp
安装:

[root@web01 ~]# yum localinstall ~/php/*.rpm

4.配置php-fpm用户与Nginx的运行用户保持一致

[root@web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
[root@web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf

5.启动php-fpm 并将其加入开机自启

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

2.3 MySQL安装

1.安装 Mariadb 数据库

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

2.启动 Mariadb 数据库, 并加入开机自动

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

3.查看是否启动成功

[root@web01 nginx]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      12022/php-fpm: mast 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      12388/mysqld        
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      538/rpcbind         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1353/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      875/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1018/master         
tcp6       0      0 :::111                  :::*                    LISTEN      538/rpcbind         
tcp6       0      0 :::22                   :::*                    LISTEN      875/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1018/master         
[root@web01 nginx]# 

可以看到mysql在3306端口,php监听在127.0.0.1:9000上是为了减少本机随机端口的损耗。

三、LNMP架构环境配置

在配置 Nginx 与 PHP 集成之前, 我们需要先了解 Nginx 的 Fastcgi 代理配置语法

3.1 Fastcgi代理语法

1.设置 fastcgi 服务器的地址,该地址可以指定为域名或IP地址,以及端口

Syntax: fastcgi_pass address;
Default: —
Context: location, if in location

#语法示例
fastcgi_pass localhost:9000;

2.设置fastcgi默认的首页文件,需要结合fastcgi_param一起设置

Syntax: fastcgi_index name;
Default: —
Context: http, server, location

3.通过fastcgi_param设置变量,并将设置的变量传递到后端的fastcgi服务

Syntax: fastcgi_param parameter value [if_not_empty];
Default: —
Context: http, server, location

#语法示例
fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

4.通过图形方式展示fastcgi_index与fastcgi_param作用。
在这里插入图片描述

3.2 Nginx集成PHP

1.创建php.conf文件

[root@web01 ~]# cat /etc/nginx/conf.d/php.conf 
server {
	listen 80;
	server_name php.bertwu.com;
	root /code/php;
	location / {
		index index.php index.html;
	}

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

2.创建/code目录修改属组属主为www用户

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

3.创建/code/php/index.php文件

[root@web01 php]# cat /code/php/index.php 
<?php
	phpinfo();
?>
[roo

[root@web01 ~]# systemctl reload nginx

4.修改win hosts文件,然后浏览器访问 http://php.bertwu.com/
在这里插入图片描述

3.3 PHP集成MySQL

1.安装mysql见上文mariadb
2. 为默认用户root设置密码

[root@web01 ~]# mysqladmin password '123456'

3.登录

[root@web01 ~]# mysql -u root -p 123456
MariaDB [(none)]> show databases; # 查看系统默认的库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test; # 选择库
Database changed
MariaDB [test]> show tables; # 查看test库中的表

Empty set (0.00 sec)

MariaDB [test]> 

4.编写php脚本测试连接mysql数据库,如果能成功,说明php与mysql的集成环境搭建成功。

[root@web01 ~]# cat /code/php/mysqli.php 
<?php
	$servername = 'localhost';
	$username = 'root';
	$password = '123456';
	// 创建连接
	$conn = mysqli_connect($servername,$username,$password);
	// 检查连接
	if (!$conn) {
		die("Connection failed: " . mysqli_connect_error());
	
	}
	echo "php连接MySQL数据库成功";
?>

5.使用 php 命令直接解析文件


[root@web01 ~]# php /code/php/mysqli.php 
php连接MySQL数据库成功

6.也可以通过浏览器访问 /mysqli.php 文件,获取解析结果
在这里插入图片描述

四、LNMP架构在单台主机上部署开源产品

4.1 部署开源知乎产品Wecenter

1.配置Nginx
配置 Nginx 虚拟主机站点,域名为 zh.bertwu.net

[root@web01 conf.d]# cat zh.conf 
server {
	listen 80;
	server_name zh.bertwu.net;
	root /code/zh;
	
	location / {		
		index index.php index.html;
	}
	
	location ~ .*\.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}
}

2.检查语法,重新启动nginx

[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx

3.下载Wecenter
4. 解压到/code/zh目录,并且修改权限

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

5.创建数据库
由于wecenter产品需要依赖数据库, 所以需要手动建立数据库

[root@oldxu ~]# mysql -uroot -p123456
mysql> create database zh;
mysql> exit

6.配置hosts文件,浏览器访问zh.bertwu.net

在这里插入图片描述


4.2 部署开源博客系统wordpress

1.获取wordpress产品,解压并部署 wordress
wordpress官网
或者用wget下载

[root@web01 ~]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz

2.解压到 /code/目录下,解压后本身就带wordpress文件夹,所以不需要手动创建

[root@web01 ~]# tar -xf latest-zh_CN.tar.gz -C /code

3.授权目前目录的权限与进程运行身份保持一致,php,nginx服务都应该用相同的(www用户)身份运行,避免权限过低

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

4.nginx站点配置

[root@web01 conf.d]# cat wordpress.conf 
server {
	listen 80;
	server_name blog.bertwu.net;
	client_max_body_size 100m;
	root /code/wordpress;
	charset utf-8;
	location / {
		index index.php index.html;
	}
	location ~ .*\.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;
	}
}

5.检测语法,并重启 nginx 服务

[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx

6.配置MySQL
由于wordpress产品需要依赖数据库, 所以需要手动建立数据库

[root@oldxu ~]# mysql -uroot -p123456
mysql> create database wordpress;
mysql> exit

7.配置hosts文件,浏览器访问blog.bertwu.net
在这里插入图片描述

4.3 部署相亲站点OElove

1.下载软件包OElove官网
也可以命令行下载

[root@web01 ~]# wget http://dl.oephp.com/oelove/source/OElove_Free_v8.1.R210428.zip

2.解压到/code/love目录(解压后自己带了个目录upload,建议先解压到/tmp中,然后再手动移动到/code/love中),并修改权限

[root@web01 tmp]# chown -R www.www /code/love

3.配置nginx站点

[root@web01 conf.d]# cat love.conf 
server {
	listen 80;
	server_name love.bertwu.net;
	root /code/love;
	charset utf-8;
	location / {
		index index.php index.html;
	}
	location ~ .*\.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;
	}
}

4.检测语法,并重启 nginx 服务

[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx

5.配置mysql,创建love数据库

[root@oldxu ~]# mysql -uroot -p123456
mysql> create database love;
mysql> exit

6.配置hosts文件,浏览器访问love.bertwu.net
根据安装向导页面展示的实验数据地址下载实验数据,解压到/code/love目录中,以便于显示网站上的图片。

[root@web01 tmp]# wget http://dl.oephp.com/data/free_data.zip
[root@web01 tmp]# unzip free_data.zip -d /code/love

8.访问
在这里插入图片描述

4.4 部署商城网站ShopXO

1.下载ShopXO官网
也可以命令下载

[root@web01 202108]# wget https://gitee.com/zongzhige/shopxo/repository/archive/v2.1.0.zip

2.解压到/code/shop目录,并修改权限

[root@web01 tmp]# chown -R www.www /code/shop

3.配置nginx站点

[root@web01 conf.d]# cat shop.conf 
server {
	listen 80;
	server_name shop.bertwu.net;
	charset utf-8;
	root /code/shop;
	location / {
		index index.php index.html;
	}
	location ~ .*\.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;
	}
}

4.检测语法,并重启 nginx 服务

[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx

5.配置mysql,创建shop数据库

[root@oldxu ~]# mysql -uroot -p123456
mysql> create database shop;
mysql> exit

6.修改hosts文件,访问站点shop.bertwu.net
在这里插入图片描述
7.报错解决方案
/code/shop/runtime/log/当天日期的 .log 文件中查看错误信息

[ 2021-08-15T14:38:44+08:00 ] 10.0.0.1 GET shop.bertwu.net/admin.php
[ error ] [2]session_start(): open(/var/lib/php/session/sess_0a394ba31263333b5af7cc3459be7dd9, O_RDWR) failed: Permission denied (13)

发现没有权限,于是可以修改权限

[root@web01 202108]# pwd
/code/shop/runtime/log/202108
[root@web01 202108]# ll -d /var/lib/php/session/
drwxrwx--- 2 root apache 6 Sep 12  2019 /var/lib/php/session/
[root@web01 202108]# chown -R www.www /var/lib/php/session/

8.再次访问,成功。
在这里插入图片描述

五、LNMP架构多台主机部署开源项目

5.1 拆分数据库至独立服务器

为何要拆分数据库

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

拆分数据库能解决什么问题

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

5.2 数据库拆分架构演变

在这里插入图片描述

5.3 环境准备

主机名应用环境外网内网
web01nginx+php10.0.0.7172.16.1.7
db01mysql(mariadb)172.16.1.51

5.4 数据库实现步骤

5.4.1 web服务操作如下

1.备份 web01 上的数据库

[root@web01 ~]# mysqldump -uroot -p123456 --all-databases > /tmp/data.sql

2.将 web01 上备份的数据库拷贝至 db01 服务器上

[root@web01 ~]# rsync  -avz  /tmp/data.sql root@172.16.1.51:/root
或者
[root@web01 ~]# scp  /tmp/data.sql root@172.16.1.51:/root

5.4.2 数据库服务器db01操作如下

1.安装mariadb数据库,并加入开机自启

[root@db01 ~]## yum install mariadb mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
[root@db01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      995/master          
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1542/mysqld         
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      567/rpcbind  

2.将 web01 服务器上推送的数据库备份文件恢复至 db01 服务器新数据库中

[root@db01 ~]# mysql -uroot < /root/data.sql 

3.数据库导入完成后,重启数据库,使用新密码进行登录,并检查数据库已被导入成功

[root@db01 ~]# systemctl restart mariadb
[root@db01 ~]# mysql -uroot -p123456
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| love               |
| mysql              |
| performance_schema |
| shop               |
| test               |
| wordpress          |
| zh                 |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> 

3.在新数据库上授权,允许172.16.1.%网段,通过 web01 账户密码123456连接并操作该数据库
因为mysql默认不支持远程连接,需要创建一个新用户并开启远程连接的权限。

# 默认没开启远程连接权限,所以在web01上是无法连接的
[root@web01 tmp]# mysql -h 172.16.1.51 -uroot -p123456
ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MariaDB server


# 在db01上开启远程连接权限(当然也可以先创建用户,后授予权限)
MariaDB [(none)]> grant all privileges on *.* to 'web01'@'172.16.1.%' identified by '123456';


# 开启后重新连接,发现已经能连接了。
[root@web01 tmp]# mysql -h 172.16.1.51 -uweb01 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
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)]> 

mysql 配置权限语法解释:

1.查看权限
  语法:show grants for 'username'@'hostname'
  例如:查看root用户的权限
      show grants for 'root'@'localhost'

2.授予权限
   语法:grant 权限类型1,权限类型1.... on  数据库名.表
        to  'username'@'hostname' [,'username'@'hostname'][with grant option] 
  		所有库中所有表用  *.* 表示


  例如:新创建test04 用户 密码test04
       create user 'test04'@'localhost' identified by '密码'
       授予所有库下所有表的查询  删除权限
       grant select ,delete on *.* to 'test04'@'localhost' with grant option
      	
      	授予所有权限
      	grant all privileges on.........
       	开启所有网段
       	'name'@'%'
       
       查看test04的权限
       show grants for 'test04'@'localhost'

3.收回权限
   revoke  权限类型1,权限类型2.... on 库名.表名
   from 'username'@hostname'' [,'username'@hostname'']...

 例如:收回test04的delete权限
    revoke delete on *.* from 'test04'@'localhost'

5.6停止web01上的数据库服务,让它不在提供数据库服务,启用新的数据库

[root@web01 ~]# systemctl stop mariadb
[root@web01 ~]# systemctl disable mariadb
Removed symlink /etc/systemd/system/multi-user.target.wants/mariadb.service.

5.7 修改web01上部署的项目代码,让其指向新数据库db01

注:由于安装部署开源软件时候有安装向导,它会自动创建脚本文件连接我们的数据库,实际生产环境中需要我们自己手动写或者开发写脚本,由于我们不知道具体是哪个文件包含数据库连接信息,可以用find命令查找
例如

[root@web01 ~]# find /code/wordpress/ -type f | xargs grep "123456"

这样就可以列出包含密码的文件,但是由于123456很多文件都有,所以建议事先设计数据库密码时候要有标识
1.修改 Wordpress 产品代码连接数据库的配置文件

[root@web01 ~]# vim /code/wordpress/wp-config.php
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'web01' );

/** MySQL database password */
define( 'DB_PASSWORD', '123456' );

/** MySQL hostname */
define( 'DB_HOST', '172.16.1.51' );

2.修改 wecenter 产品代码连接数据库的配置文件

[root@web01 ~]# vim /code/zh/system/config/database.php 

<?php

$config['charset'] = 'utf8';^M
$config['prefix'] = 'aws_';^M
$config['driver'] = 'MySQLi';^M
$config['master'] = array (
  'charset' => 'utf8',
  'host' => '172.16.1.51',
  'username' => 'web01',
  'password' => '123456',
  'dbname' => 'zh',
);^M
$config['slave'] = false;^M                           

3.修改 OElove 产品代码连接数据库的配置文件

[root@web01 ~]# vim /code/love/source/conf/db.inc.php 

define('DB_TYPE', 'mysql');
///数据库编码
define('DB_CHARSET', 'utf8');
///数据库服务器
define('DB_HOST', '172.16.1.51:3306');
///数据库名
define('DB_DATA', 'love');
///数据库登录帐号
define('DB_USER', 'web01');
///数据库登录密码
define('DB_PASS', '123456');
///数据表前缀
define('DB_PREFIX', 'oepre_');
///数据库持久连接 0=关闭, 1=打开

4.修改OXshop产品配置信息

[root@web01 ~]# vim /code/shop/config/database.php 
return [
    // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => '172.16.1.51',
    // 数据库名
    'database'        => 'shop',
    // 用户名
    'username'        => 'web01',
    // 密码
    'password'        => '123456',
    // 端口
    'hostport'        => '3306',

5.由于停止了web01服务器上的数据库服务,网站都不能访问了,但是经过上述配置后,浏览器又能访问了。

六、扩展多台相同的Web服务器组成web集群

6.1 为何要扩展多台web节点

单台web服务器能抗住的访问量是有限的,而且单一的添加硬件配置并不能解决端口不够的问题。配置多台web服务器能提升更高的访问速度。
扩展多台节点解决什么问题
1.单台web节点如果故障,会导致业务down机;
2.多台web节点能保证业务的持续稳定,扩展性高;
3.多台web节点能有效的提升用户访问网站的速度;

6.2 扩展多web节点架构演变

在这里插入图片描述

6.3 扩展多web节点环境准备

主机名称应用环境外网地址内网地址
web01nginx+php10.0.0.7172.16.1.7
web02nginx+php10.0.0.8172.16.1.8
db01mysql(mariadb)172.16.1.51

6.4 扩展多台web节点实现步骤

可以根据web01上的配置,快速扩展一台web02,数据库统一使用db01。

6.4.1 创建相同用户身份便于运行进程

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

6.4.2 安装nginx

由于安装nginx需要手动配置源,可以从web01上拖过来

[root@web02 ~]# rsync -avz root@172.16.1.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
安装yum优先级插件
yum install yum-priorities

在想优先检测的repo文件中加入
priority=1 
#数字越小,优先级越高


[root@web01 ~]# vim /etc/yum/pluginconf.d/priorities.conf 
确定这个配置文件里的 enabled = 1
yum clean all
yum makecache

就可以优先从我们指定的源里安装了

[root@web02 ~]# yum install nginx
[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web02 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      539/rpcbind         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1847/nginx: master  

nginx 依赖安装
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-toolsyum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-tools

6.4.3 安装php

安装见上文

把包下载下来放入~目录解压
[root@web02 ~]# unzip php.zip
安装
[root@web02 ~]# yum localinstall php/*.rpm

6.4.4 nginx与php配置导入

[root@web02 php-fpm.d]# rsync -avz root@172.16.1.7:/etc/nginx /etc
[root@web02 ~]# rsync -avz root@172.16.1.7:/etc/php-fpm.d /etc
[root@web02 conf.d]# rsync -avz root@172.16.1.7:/etc/php.ini /etc

6.4.5 将web01 上部署的所有代码打包上传到web02

1.在web01上打包
[root@web01 /]# cd / && tar czf /tmp/code.tar.gz /code/
2.推送
[root@web01 /]# scp -rp /tmp/code.tar.gz root@172.16.1.8:/tmp
3. 在web02上解压
[root@web02 ~]# tar -xf code.tar.gz -C /

如果要部署商城项目需要再次修改权限

[root@web02 ~]# chown -R www.www /var/lib/php/session/

6.4.6 在web02上启动nginx与php并加入开机自启

[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
[root@web02 ~]# systemctl start php-fpm
[root@web02 ~]# systemctl enable php-fpm
[root@web02 ~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      539/rpcbind         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1847/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      861/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1020/master         
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      2092/php-fpm: maste 

6.4.7 修改本地hosts文件,浏览器访问测试。

10.0.0.8 zh.bertwu.net
10.0.0.8 blog.bertwu.net
10.0.0.8 love.bertwu.net
10.0.0.8 shop.bertwu.net

七、拆分静态资源至独立服务器

7.1 为何要拆分静态资源

当后端的 web 节点出现多台时,如果用户上传的图片、视频附件等内容仅在一台 web 服务器上,由于负载均衡,下一次访问会随机调度到其他节点,那么其他的 web 服务器则无法访问到该图片。
如果增加一台共享存储能解决什么问题
1.保证了多台 web 节点静态资源一致。
2.有效节省多台 web 节点的存储空间。
3.统一管理静态资源,便于后期推送至 CDN 进行静态资源加速

7.2 拆分静态资源架构演变

在这里插入图片描述

7.3 增加共享存储nfs环境准备

主机名称应用环境外网地址内网地址
web01nginx+php10.0.0.7172.16.1.7
web02nginx+php10.0.0.8172.16.1.8
db01mysql(mariadb)172.16.1.51
nfsnfs172.16.1.32

7.4 共享存储实现步骤

7.4.1 配置nfs 建议另外配置硬盘

1.创建www用户
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -g 666 -u 666 www
[root@nfs ~]# yum install nfs-utils -y
2.修改nfs配置文件修改
[root@nfs ~]# cat /etc/exports
/data/blog 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)
/data/love 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/shop 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

3.添加一块新硬盘
[root@nfs ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 47.6G  0 part /
sdb      8:16   0 1000G  0 disk 
sr0     11:0    1  4.4G  0 rom 

4.制作文件系统
[root@nfs ~]# mkfs.xfs /dev/sdb
[root@nfs data]# mkdir /data

5.挂载
[root@nfs data]# mount -t xfs /dev/sdb /data
[root@nfs data]# echo '/dev/sdb /data xfs defaults 0 0' >>/etc/fstab
[root@nfs data]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  7.6M  479M   2% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda3        48G  1.8G   46G   4% /
/dev/sda1       497M  125M  373M  25% /boot
tmpfs            98M     0   98M   0% /run/user/0
/dev/sdb       1000G   33M 1000G   1% /data

6.创建对应文件夹并修改权限
[root@nfs data]# mkdir /data/{blog,zh,love,shop}
[root@nfs data]# chown -R www.www /data

[root@nfs data]# ll
total 0
drwxr-xr-x 2 www www 6 Aug 15 22:25 blog
drwxr-xr-x 2 www www 6 Aug 15 22:25 love
drwxr-xr-x 2 www www 6 Aug 15 22:25 shop
drwxr-xr-x 2 www www 6 Aug 15 22:25 zh
7.加入开机自启
[root@nfs data]# systemctl start nfs-server
[root@nfs data]# systemctl enable nfs-server

7.4.2 导入静态资源至共享存储

1.web01节点安装nfs,然后使用showmount查看服务端共享的资源;

[root@web01 ~]# yum install nfs-utils -y
[root@web01 ~]# showmount -e 172.16.1.32
Export list for 172.16.1.32:
/data/shop 172.16.1.0/24
/data/love 172.16.1.0/24
/data/zh   172.16.1.0/24
/data/blog 172.16.1.0/24

2.查找Wordpress静态资源推送过去然后再挂载

在这里插入图片描述
可以看到静态资源存储目录为http://blog.bertwu.net/wp-content/uploads/2021/08/16290384211-3.png

1.web02上事先有图片需要先推送一下
[root@web02 wordpress]# rsync -avz /code/wordpress/wp-content/uploads/ root@172.16.1.32:/data/blog

[root@web02 wordpress]# mount -t nfs 172.16.1.32:/data/blog /code/wordpress/wp-content/uploads/
[root@web02 wordpress]# df -h
Filesystem              Size  Used Avail Use% Mounted on
devtmpfs                476M     0  476M   0% /dev
tmpfs                   487M     0  487M   0% /dev/shm
tmpfs                   487M  7.6M  479M   2% /run
tmpfs                   487M     0  487M   0% /sys/fs/cgroup
/dev/sda3                48G  2.5G   46G   6% /
/dev/sda1               497M  125M  373M  25% /boot
tmpfs                    98M     0   98M   0% /run/user/0
172.16.1.32:/data/blog 1000G   40M 1000G   1% /code/wordpress/wp-content/uploads

# 开机自启挂载
[root@web01 ~]echo '172.16.1.32:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0' >> /etc/fstab

2.web01上没有图片直接远程挂载
[root@web01 wp-content]# mount -t nfs 172.16.1.32:/data/blog /code/wordpress/wp-content/uploads/
[root@web01 wp-content]# df -h
Filesystem              Size  Used Avail Use% Mounted on
devtmpfs                476M     0  476M   0% /dev
tmpfs                   487M     0  487M   0% /dev/shm
tmpfs                   487M  7.6M  479M   2% /run
tmpfs                   487M     0  487M   0% /sys/fs/cgroup
/dev/sda3                48G  3.3G   45G   7% /
/dev/sda1               497M  125M  373M  25% /boot
tmpfs                    98M     0   98M   0% /run/user/0
172.16.1.32:/data/blog 1000G   40M 1000G   1% /code/wordpress/wp-content/uploads
# 开机自启挂载
[root@web01 ~]echo '172.16.1.32:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0' >> /etc/fstab

八、展节点带来的新问题

如果我们添加了一台C应用服务器,如何能实现快速扩展?
1.准备LNP环境
2.拷贝任意A或B上的配置文件,代码
3.挂载NFS存储

现在有多个WEB服务器,该如何进行访问?
1.DNS轮询
(1)需要所有的web节点具备公网IP地址
(2)公网独立IP需要费用,而且不便宜。
(3)所有的web节点有公网IP,不安全。
(4)DNS只有轮询机制,没有健康检查功能。
2.负载均衡
(1)所有的web节点不需要有公网IP,能节省成本、并保证安全
(2)能够对后端的web节点进行健康检查机制;
(3)负载均衡有多种调度算法来满足企业不同需求;

	rr;wrr;url_hash;ip_hash;一致性hash;

在这里插入图片描述

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐