一、HTTP基本知识介绍

1.1、网站页面访问流程

1、客户端:浏览器输入网站信息点击回车(www.baidu.com)
2、客户端:完成域名的解析过程(DNS)
3、客户端:直接访问相应的网站服务器 建立TCP三次握手过程
4、客户端:访问网站服务器 发送HTTP请求报文 多次
5、服务端:响应客户端请求 回复HTTP响应报文 多次
6、客户端:浏览器看到网站页面
7、客户端:结束访问网站过程 完成TCP四次挥手过程
2、HTTP请求和响应过程
HTTP请求报文
1)请求行
请求方法:Get —读/看 —获取/拿过来
POST —写/提交
请求信息:index.html(首页文件)
请求协议:http 1.1
HTTP:1.0 TCP短连接
HTTP:1.1 TCP长连接
HTTP:2.0 对TCP长连接做了优化,提高了并发访问的效率
2)请求头
请求主机信息
3)空行
4)请求主体
使用get方法时,没有请求主体信息
使用post方法时,具有请求主机内容
HTTP响应报文
1)起始行
状态码信息 访问请求是成功响应/失败响应
备注:
200 成功访问
客户端错误
301 永久重定向
302 临时重定向
403 禁止访问
404 不存在
服务端错误
500 服务器内部错误,无法完成请求
501 服务器不支持请求的功能,无法完成请求
502 作为网关或者代理工作的服务器尝试执行请求时,从远程服务器接收到了一个无效的响应
503 由于超载或系统维护,服务器暂时的无法处理客户端的请求。延时的长度可包含在服务器的Retry-After头信息中
504 当网关或代理的服务器,未及时从远端服务器获取请求
505 服务器不支持请求的HTTP协议的版本,无法完成处理
2)响应头部

3)空行

4)响应主体
3、HTTP协议资源信息
URL:全称为统一资源定位符,uniform reource location
URI:统一资源标识符,uniform resource Identifier
http://ansible.com.cn /docs/playbooks_roles.html#playbook-roles-include
URL URI
网站页面静态资源
如图片、声音或者纯文本不需要与后台做交互的信息
网站页面动态资源
通常需要与数据库交互程序处理或许动态资源
伪静态资源(动态页面)
可以便于搜索引擎进行收录
有数据库服务支持,是网页交互功能
4、网站评测指标
a、IP:根据用户IP地址数量进行统计(由于NAT技术的使用,此统计方法并不准确)
b、PV:page vistor 页面访问量
c、UV:记录独立访客数量
cookie:标识用户身份信息,会保存在用户客户端本地 内存中
session:记录用户的一些回话操作,eg:记录用户登录信息,记录在服务端,内存中
image.png
网站的并发:(压测技术 1 10 100 1000 10000 100000)
1、网站服务器每秒能够接收的最大用户请求数
2、网站服务器每秒能够响应的最大用户请求数
3、网站服务器在单位时间内能够处理的最大连接数

二、Nginx基本介绍

2.1、常用的网站服务软件

处理静态资源的服务:
apache软件:http://apache.org/
nginx软件: http://nginx.org

2.2、处理动态资源的服务

PHP:php.net 终端浏览器进行访问
Tomcat(java) 利用移动端查看网页,安卓-java
PYTHON 开发难度比较低

2.3、nginx软件服务的特点

1)支持高并发,小号的内存资源少
2)具有多种功能
网站web服务功能 —apache
网站负载均衡功能 —LVS
网站缓存服务 —Squid
3)在多种系统平台都可以进行部署
4)nginx实现网络通讯时使用的是异步网络IO模型,epoll模型(apache—select模型)
epoll模型:
宿舍管理员:找人,查看人员登记信息
select模型:—线性轮询
宿舍管理员:找人,一个一个屋子问,—线性轮训

2.4、安装部署

1)yum安装软件
a、使用官方的yum源进行安装,安装的是最新的版本,软件目录结构比较标准(推荐)
b、使用非官方的yum源进行安装,安装的不是最新版本,目录结构会发生变化
2)编译安装软件
第一个步骤
wget 下载地址
第二个步骤
解压,并进入软件目录
第三步
编辑安装三部曲
a、进行配置操作
./configure
—prefix=PATH 指定程序安装路径
—user=USER 设置一个虚拟用户管理的worker进程(安全)
—group=GROUP 设置一个虚拟用户组管理worker进程
b 进行软件的编辑过程
make 编译
c 编译安装过程
make install
yum官方源安装过程
第一步、更新nginx官方的yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
第二步、yum安装nginx软件
yum install -y nginx
第三步、启动nginx服务,检查服务是否安装正确
systemctl start nginx
systemctl enable nginx
测试访问服务,浏览器访问

2.5、日志文件切割

1)/etc/logrotate.d
实现nginx日志文件定时切割处理
日志切割方法一、利用脚本实现切割
mv /var/log/nginx/access.log /var/log/nginx/access.log_$(date +%F).log
systemctl restart nginx
日志切割方法二、利用专用文件切割程序—logrotate
[root@web01 ~]# cat /etc/logrotate.conf
# see “man logrotate” for details
# rotate log files weekly
weekly —-定义默认日志切割的周期
# keep 4 weeks worth of backlogs
rotate 4 —-定义只保留几个切割后的文件
# create new (empty) log files after rotating old ones
create —-创建出一个相同的源文件
# use date as a suffix of the rotated file
dateext —-定义角标(扩展名称信息)
# uncomment this if you want your log files compressed
#compress —-是否对切割后的文件进行压缩处理
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d —-加载/etc/logrotate.d/目录中文件配置
# no packages own wtmp and btmp — we’ll rotate them here
/var/log/wtmp { —-单独对某个文件进行切割配置
monthly
create 0664 root utmp
minsize 1M —-最小大小为1M,小于1M不进行切割
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
总结:
1、/etc/nginx/ 配置文件
2、/etc/log/nginx 日志文件
3、/usr/bin/nginx/html 命令文件
4、/usr/share/nginx/html 站点目录

2.6、nginx服务配置文件

/etc/nginx/nginx.conf —-主配置文件
第一个部分:配置文件主区域配置
user nginx; —-定义worker进程管理的用户
补充:nginx的进程
master process: 主进程 —-管理服务是否能够正常运行 boss
worker_process:工作进程 —-处理用户的访问请求 员工
worker_processes 1; —-定义有几个worker进程 ==CPU核数/核数的2两倍
error_log /var/log/nginx/error.log warn; —定义错误日志路径信息
pid /var/run/nginx.pid; —定义pid文件路径信息
第二个部分:配置文件时间区域
events {
worker_connections 1024; —-一个worker进程可以同时接收1024访问请求
}
第三个部分:配置http区域
http {
include /etc/nginx/mime.types; —-加载一个配置文件
default_type application/octet-stream; —-指定默认识别文件类型
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘;
—-定义日志的格式
access_log /var/log/nginx/access.log main; —-指定日志路径
sendfile on;
#tcp_nopush on;
keepalive_timeout 65; —-超时时间
#gzip on;
include /etc/nginx/conf.d/.conf; —加载一个配置文件
}
/etc/nginx/nginx.d/default —-扩展配置(虚拟主机配置文件)
*第四个部分:server区域信息
(配置一个网站www/bbs/blog —一个虚拟主机)
server {
listen 80; —-指定监听的端口
server_name localhost; —-指定网站域名
location / {
root /usr/share/nginx/html; 定义站点目录的位置
index index.html index.htm; 定义首页文件
}
error_page 500 502 503 504 /50x.html; 优雅显示页面信息
location = /50x.html {
root /usr/share/nginx/html;
}
}

三、nginx服务的企业应用

3.1 利用nginx服务搭建一个网站(www)

第一步、编写虚拟主机配置文件
cd /etc/nginx/conf.d/
vim www.conf
server{
listen 80;
server_name www.scxiang.com
location /scxiang {
root /usr/shared/nginx/html;
index oldboy.html;
}
}
image.png
第二步、获取开发人员编写的网站代码
第三步、重启nginx服务,平滑重启
两种方法:
systemctl reload nginx
nginx -s reload
nginx命令参数
-t 检查测试配置文件语法
-T 同-t类似,但是会将配置信息输出至屏幕
-s 控制服务停止或者重启
第四步、编写DNS配置信息
真实域名:在阿里云上进行DNS解析记录配置
模拟域名: 在windows主机的hosts文件中进行配置即可
C:/windows/System32/drivers/etc/hosts
第五步:进行测试访问
浏览器中输入域名信息进行访问
常见错误
1、配置文件编写错误
2、DNS信息配置不正确
3、修改配置之后一定要重启服务

3.2 利用nginx服务搭建多个网站(虚拟主机)

第一步、创建多个虚拟主机配置文件
bbs.conf
server{
listen 80;
server_name bbs.scxiang.com
location / {
root /html/bbs/;
index index.html;
}
}
blog.conf
server{
listen 80;
server_name blog.scxiang.com
location / {
root /html/blog/;
index index.html;
}
}
www.conf
server{
listen 80;
server_name www.scxiang.com
location / {
root /html/www/;
index index.html;
}
}
第二步、创建站点目录和目录中首页文件
[root@web01 html]# mkdir -p /html/{www,blog,bbs}
[root@web01 html]# for name in {bbs,www,bbs}; do echo “10.0.0.7 $name.scxiang.com” >/html/$name/index.html; done
[root@web01 html]# cat */index.html
10.0.0.7 bbs.scxiang.com
10.0.0.7 blog.scxiang.com
10.0.0.7 www.scxiang.com
第三步:编写hosts解析文件
10.0.0.7 www.scxiang.com bbs.scxiang.com blog.scxiang.com
第四步:访问测试

3.3 企业中虚拟主机访问方式

1.基于域名的访问进行访问
2.基于地址的方式进行访问
server{
listen 10.0.0.7:80;
server_name www.scxiang.com
location / {
root /html/www/;
index index.html;
}
}
备注:服务配置文件中涉及到地址修改时,必须重启nginx服务,不能平滑重启
3.基于端口的方式进行访问
server{
listen 8080;
server_name www.scxiang.com
location / {
root /html/www/;
index index.html;
}
}
网站页面的访问原理:
1.将域名进行解析,www.scxiang.com —-10.0.0.7
2.建立TCP的连接
10.0.0.7 目标端口
3.根据应用层协议HTTP协议发出请求
请求报文:hosts: www.scxiang.com
4.没有相同域名的server主机,会找满足端口要求的第一个主机
显示主机的网站页面

3.4 企业中网站的安全访问配置

1.根据用户访问的地址进行控制
10.0.0.0/24 www.scxiang.com 不能访问
172.16.1.0/24 www.scxiang.com 可以访问
nginx访问模块:ngx_http_access_module
举例配置
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32
deny all;
}
配置实例
server{
listen 8080;
server_name www.scxiang.com
location / {
root /html/www/;
index index.html;
deny 10.0.0.0/24;
allow 172.16.1.0/24;
}
}
补充:
location外面的信息,全局配置信息
location里面的信息,局部配置信息
2.根据用户访问进行认证
nginx认证模块:ngx_http_auth_basic_module
举例配置:
location / {
auth_basic “closed site” —-开启认证功能
auth_basic_user_file conf/htpasswd —-加载用户密码文件
}
第一步:编写虚拟主机配置文件
server{
listen 80;
server_name www.scxiang.com
location / {
root /html/www/;
index index.html;
auth_basic “提示内容”;
auth_basic_user_file password/htpasswd;
}
}
第二步:创建密码文件(文件中密码信息必须是密文的)
htpasswd 创建一个密文信息的密码文件
[root@web01 password]# htpasswd -bc htpasswd scxiang abc123..
htpasswd参数说明
-c 创建一个密码文件
-b 免交互方式输入用户密码信息
-m MD5加密算法
-p 不进行加密
修改密码文件权限:???
chmod 600 ./htpasswd
500 Internal Server Error
1.内部程序代码编写有问题
2.程序服务中文件权限步正确
curl命令参数
-u 指定用户名密码
[root@web01 nginx]# curl www.scxiang.com -u scxiang
Enter host password for user ‘scxiang’:
10.0.0.7 www.scxiang.com
[root@web01 nginx]# curl www.scxiang.com -u scxiang:abc123..
10.0.0.7 www.scxiang.com

3.5 利用nginx服务搭建网站文件共享服务器

第一步:编写配置文件(www.conf)
nginx模块功能:ngx_http_autoindex_module
Systax: autoindex on | off
server{
listen 80;
server_name www.scxiang.com
location / {
root /html/www/
autoindex on
}
}
image.png
注意:1.需要将首页文件进行删除
2.mime.types媒体资源类型文件作用
文件中有的扩展名信息资源,进行访问时会直接看到数据信息
文件中没有的扩展名信息资源,进行访问时会直接下载
网站页面目录数据,中文出现乱码,如何解决:
server{
listen 80;
server_name www.scxiang.com
location / {
root /html/www/
autoindex on;
charset utf-8; —-修改目录结构中出现的中问乱码问题
}
}

3.6 利用nginx服务配置文件别名功能

第一步.编写配置文件
server_name www.scxiang.com xiang.com
第二步.配置好解析信息

3.7 利用nginx状态模块功能对网站进行监控

状态模块: ngx_http_stub_status_module
第一步.编写配置文件vim status.conf
server{
listen 80;
server_name status.scxiang.com
stub_status;
}
image.png
Active connections:激活的连接数信息
accepts: 接收的连接数汇总(综台)TCP
handled: 处理的连接数汇总(综合)TCP
requests: 总计的请求数量 HTTP协议请求
Reading: nginx服务读取请求报文的数量100人点餐
writing: nginx服务响应报文信息数量 100人响应
waiting: nginx队列机制,要处理(读取或者响应保存进行保存) 监控

3.8 nginx日志功能配置

备注:日志信息一定要进行切割处理
访问日志:/var/log/nginx/access.log
10.0.0.1 - - [10/Nov/2020:21:31:04 +0800] “GET / HTTP/1.1” 200 99 “-“ “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36” “-“
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘ 定义日志内容格式
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘; 调用日志格式
$remote_addr 显示用户访问源IP地址信息
$remote_user 显示认证的用户名信息
[$time_local] 显示访问网站时间
“$request” 请求报文的请求行信息
‘$status 用户访问网站状态码信息
$body_bytes_sent 显示响应的数据尺寸信息
“$http_referer” 记录调用网站资源的连接地址信息(防止用户盗链)
“$http_user_agent” 记录用户使用什么客户端软件进行访问页面的(谷歌,火狐,IE,安卓,iphone)
$http_x_forwarded_for 负载均衡会用到次参数

错误日志:/var/log/nginx/error.log
error_log /var/log/nginx/error.log warn;
错误级别:
debug:调试级别,服务运行的状态信息和错误信息详细显示 信息越多
info :信息级别,只显示重要的运行信息和错误信息 信息越多
notice :通知级别:更加重要的信息进行通知说明
warn :警告级别:可能出现了一些错误信息,但不影响服务运行
error :错误级别:服务运行己经出现了错误,需要进行纠正 推进
crit :严重级别:必须进行修改调整
alert :严重警告级别:即警告,而且必须进行错误修改 :
emerg :灾难级别:服务己经不能正常运行 信息越少

3.9 nginx服务location作用说明

模块说明:ngx_http_core_module
location进行匹配(URI)
错误页面优雅显示
location /scxiang {
root /html/www;
error_page 404 /test.jpg;
}
location详细配置:
Syntax:location [=|~|~|^~] uri{…}
location @name {…}
location / { —-默认匹配,优先级04,最低
return 401;
}
location = / { —-精确匹配,优先级01,最高
return 402;
}

location = /document/ { —-按照目录进行匹配,优先级03
return 403;
}
location ^~ /p_w_picpaths/ { —-优先匹配/不识别uri信息中符号信息 优先级02
return 404;
}
location ~
.(gif|jpg|jpeg)$ { —-不区分大小写进行匹配,优先级03
return 500;
}
优先级
1、首先精确匹配
2、其次前缀匹配 ^~
3、其次是按文件中顺序的正则匹配
4、然后匹配不带任何修饰的前缀匹配。
5、最后是交给 / 通用匹配
当有匹配成功时候,停止匹配,按当前匹配规则处理请求
注意:前缀匹配,如果有包含关系时,按最大匹配原则进行匹配。比如在前缀匹配:location /dir01与location /dir01/dir02,如有请求http://localhost/dir01/dir02/file 将最终匹配到location /dir01/dir02

3.10 利用nginx实现跳转功能

利用rewrite模块是跳转功能:http_rewirte_module
Syntax:rewrite regex repalcement [flag]; rewrite 匹配的正则信息 替换成什么信息
Default: -
Context: server,location,if
rewrite scxiang.com/(.) http://www.scxiang.com/$1 permanent; 重写配置规则
跳转方式:
永久跳转:permanent 301 会将跳转信息进行缓存
临时跳转:redirect 302 不会缓存跳转信息
出现无线跳转如何解决
第一钟方法:利用不同server区块配置打破循环
server {
server_name xiang.com;
rewrite ^/(.
) http://www.scxiang.com/$1 permanent;
}

  1. [root@web01 conf.d]# curl -Lv xiang.com
  2. curl: no URL specified!
  3. curl: try 'curl --help' or 'curl --manual' for more information
  4. [root@web01 conf.d]# curl -Lv xiang.com
  5. * About to connect() to xiang.com port 80 (#0)
  6. * Trying 10.0.0.7...
  7. * Connected to xiang.com (10.0.0.7) port 80 (#0)
  8. > GET / HTTP/1.1
  9. > User-Agent: curl/7.29.0
  10. > Host: xiang.com
  11. > Accept: */*
  12. >
  13. < HTTP/1.1 301 Moved Permanently
  14. < Server: nginx/1.18.0
  15. < Date: Wed, 11 Nov 2020 07:06:59 GMT
  16. < Content-Type: text/html
  17. < Content-Length: 169
  18. < Connection: keep-alive
  19. < Location: http://www.scxiang.com/
  20. <
  21. * Ignoring the response-body
  22. * Connection #0 to host xiang.com left intact
  23. * Issue another request to this URL: 'http://www.scxiang.com/'
  24. * About to connect() to www.scxiang.com port 80 (#1)
  25. * Trying 10.0.0.7...
  26. * Connected to www.scxiang.com (10.0.0.7) port 80 (#1)
  27. > GET / HTTP/1.1
  28. > User-Agent: curl/7.29.0
  29. > Host: www.scxiang.com
  30. > Accept: */*
  31. >
  32. < HTTP/1.1 200 OK
  33. < Server: nginx/1.18.0
  34. < Date: Wed, 11 Nov 2020 07:06:59 GMT
  35. < Content-Type: text/html; charset=utf-8
  36. < Transfer-Encoding: chunked
  37. < Connection: keep-alive
  38. <
  39. <html>
  40. <head><title>Index of /</title></head>
  41. <body>
  42. <h1>Index of /</h1><hr><pre><a href="../">../</a>
  43. <a href="CentOS7.1/">CentOS7.1/</a> 10-Nov-2020 08:41 -
  44. <a href="CentOS7.4/">CentOS7.4/</a> 10-Nov-2020 08:41 -
  45. <a href="CentOS7.6/">CentOS7.6/</a> 10-Nov-2020 08:41 -
  46. <a href="index.html.back">index.html.back</a> 09-Nov-2020 08:57 25
  47. </pre><hr></body>
  48. </html>
  49. * Connection #1 to host www.scxiang.com left intact

第二种方法:利用if判断实现打破循环
if ($host ~ “^oldboy.com$”) {
rewrite ^/(.
) http://www.scxiang.com/$1 permanent;
}

四 网站的LNMP架构

4.1 数据库安装

第一步.安装数据库软件
yum install mariadb-server mariadb -y
补充:数据库初始化过程 mysql_install_db
—basedir=path 指定mysql程序目录
—datadir=path 指定数据信息保存的目录
—user=mysql 让mysql管理数据目录 700
第二步.启动数据库服务
systemctl start mariadb.service
systemctl enable mariadb.service
第三步.给mysql数据库服务设置密码
mysqladmin -u root password ‘abc123..’ —-设置密码
登录
[root@web01 ~]# mysql -u root -pabc123..

4.2 PHP服务部署流程

第一步.更新yum源/卸载系统自带的PHP软件
yum remove php-mysql php php-fpm php-common
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
第二步.安装软件
yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embeded 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
第三步.编写配置文件
vim /etc/php-fpm.d/www.conf
user = www
group = www
PS:保证nginx进程的管理用户和PHP服务进程的管理用户保持一致
第四步.启动php服务
systemctl start php-fpm
LNMP架构访问过程:
用户访问网站—->nginx(fastcgi_pass)—>FastCGI—->(php-rpm—wrapper) php(php解析器)—->mysql(读取或写入)

4.3 实现LNMP之间建立关系

1.实现nginx+php建立关系
第一步:编写nginx文件
编写nginx配置文件
location ~ .php${
root /html/blog;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
重启nginx服务
第二步.编写动态资源文件
vim /html/blog/test_php.php
<?php
phpinfo();
?>
第三步.测试访问
image.png

4.4 实现php与mysql建立关系

编写php代码文件vim test_mysql.php
<?php
$servername = “localhost”;
$username = “root”;
$password = “abc123..”;
//$link_id=mysql_connect(‘主机名’,’用户’,’密码’);
//mysql -u用户名 -p密码 -h 主机
$conn = mysqli_connect($servername, $username, $password);
if ($conn){
echo “mysql successful by root!”;
}else{
die(“Connetion failed:”. mysqli_connect_error());
}
?>
image.png

4.5 部署发搭建网站页面(代码上线)

第一步:获取代码信息(git)—-使用开源网站代码
www网站页面:http://www.dedecms.com/
bbs网站页面:http://www.discuz.net/forum.php
blog网站页面:https://cn.wordpress.org/
wecenter网站页面:http://www.wecenter.com/?copyright
第二步、将代码解压, 将解压后信息放入到站点目录中
tar -zxvf wordpress-5.2.1.tar.gz
mv 移动到站点目录
第三步、修改站点目录权限
chown -R www.www blog
第四步、进行网站页面初始化操作
第五步、对数据库进行配置
创建数据库:create database wordpress;
检查:show databases;
创建数据库管理用户: grant all on wordpress.* to ‘wordpress’@’localhost’ identified by ‘abc123..’;
检查: select user,host from mysql.user;
第六步.利用blog发布博文

4.6 web服务器与存储服务器建立关系

编写存储服务器配置文件
[root@nfs01 data]# cat /etc/exports
/data/www 172.16.1.0/24(rw,sync)
/data/blog 172.16.1.0/24(rw,sync)
/data/bbs 172.16.1.0/24(rw,sync)
mkdir /data/{www,blog,bbs}
在web服务器上将存储数据的目录进行挂载,在挂载之前注意将数据移走,挂在好之后再将数据迁移回来
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog /html/blog/wp-content/uploads
默认存储服务器无法存储数据:
管理用户无法存储:root_squash —-nfsnobody
普通用户无法存储:no_all_squash
解决:
第一步:修改nfs配置文件,定义映射用户为www
useradd www -u 1002
chown -R www /data
第二步:使root用户可以上传数据
sed -ri.bak “s#(sync)#\1,anonuid=1001,anongid=1001#g” /etc/exports

4.7 如何让LNMP架构和数据库服务器建立关系

第一步.将web服务器本地数据库进行备份
mysqldump -u root -pabc123.. —all-database >/tmp/web_back.sql
第二步.将备份数据进行迁移
scp -rp /tmp/web_back.sql 172.16.1.51:/tmp
第三步.恢复数据信息
yum install -y mariadb-server mariadb
[root@db01 ~]# mysql -uroot -pabc123.. 第四步.修改数据库中数据库用户信息
image.png
优化:删除无用的用户信息
MariaDB [(none)]> delete from mysql.user where user=”” and host=”localhost”;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> delete from mysql.user where user=”” and host=”web01”;
Query OK, 1 row affected (0.00 sec)
添加:添加新的用户信息
MariaDB [(none)]> grant all on wordpress. to ‘wordpress’@’172.16.1.%’ identified by ‘abc123..’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
第五步、修改web服务器代码文件信息
vim wp-config.php
/** Mysql hostname
/
define(‘DB_HOST’,’172.16.1.51’);
第六步、停止web服务器上数据库服务
systemctl stop mariadb.service
systemctl disable mariadb.service