Lupinus

Re:从零开始的go学习生活(`・ω・´)

0%

[Nginx服务]

Nginx服务

Nginx概述

Nginx是一个开源且高性能、可靠的Http Web服务、代理服务

开源:直接获取源代码

高性能:支持海量并发

可靠:服务稳定

Nginx非常轻量

功能模块少 (源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)

代码模块化 (易读,便于二次开发,对于开发人员非常友好)

互联网公司都选择Nginx

  • Nginx技术成熟,具备的功能是企业最常使用而且最需要的

  • 适合当前主流架构趋势, 微服务、云架构、中间层

  • 统一技术栈, 降低维护成本, 降低技术更新成本

Nginx采用Epool网络模型,Apache采用Select模型

  • Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下

  • Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制

Nginx应用场景

360截图1872011793144109

静态WEB软件(主要跑前端代码)

1
2
3
4
5
6
nginx
apache
IIS
lighttpd
tengine
openresty-nginx

动态WEB软件(主要跑后端代码)

1
2
3
4
Tomcat
Resin
weblogic
Jboss

Nginx快速安装

Nginx的安装方式

1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐 4.规范 5.便于管理)

2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)

3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读)

Nginx官方网站:https://nginx.org/

image-20220530203631900

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#1.添加nginx官方源 
[root@web01 ~]# 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

#2.安装nginx
[root@web01 ~]# yum install -y nginx

#3.启动nginx并加入开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

#4.查看nginx的版本
[root@web01 ~]# nginx -v
nginx version: nginx/1.22.0

#5.查看nginx的版本和源码安装生成步骤的参数有哪些
[root@web01 ~]# nginx -V
--prefix=/application/nginx-1.22.0 --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with- http_dav_module --with-http_flv_module --with-http_gunzip_module --with- http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with- http_realip_module --with-http_secure_link_module --with-http_slice_module --with- http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with- stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,- D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 - grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now - pie'

Nginx的启停

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#1.启动 
systemctl start nginx
nginx
/目录/nginx/sbin/nginx

#2.停止 systemctl stop nginx
nginx -s stop
/目录/nginx/sbin/nginx -s stop

#3.重新加载
systemctl reload nginx
nginx -s reload
/app/nginx/sbin/nginx -s reload

-c:指定配置文件的路径
-t:检查配置文件的语法(无法检测单词拼写)
-s:启停重载,服务操作
-v:查看版本号
-V:查看版本和编译参数

#nginx启动脚本systemd管理
[root@web01 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

#源码安装,使用fpm打包,脚本内容:
##先写脚本
[root@web01 ~]# vim post_install_nginx.sh
ln -s /application/nginx-1.20.2 /opt/nginx
echo 'PATH="/usr/local/nginx/sbin:$PATH"' > /etc/profile.d/nginx.sh
cat >> /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/application/nginx/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /application/nginx/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /application/nginx/nginx.pid)"

[Install]
WantedBy=multi-user.target
EOF

Nginx配置文件

  • Nginx主配置文件
路径 类型 作用
/etc/nginx/nginx.conf 配置文件 nginx主配置文件
/etc/nginx/conf.d/default.conf 配置文件 nginx网站示例配置文件
  • Nginx代理相关参数文件
路径 类型 作用
/etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件
/etc/nginx/scgi_params 配置文件 scgi代理配置文件
/etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件
  • Nginx编码相关配置文件
路径 类型 作用
/etc/nginx/win-utf 配置文件 Nginx编码转换映射文件
/etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件
/etc/nginx/koi-win 配置文件 Nginx编码转换映射文件
/etc/nginx/mime.types(*) 配置文件 Content-Type与扩展名
  • Nginx管理相关命令
路径 类型 作用
/usr/sbin/nginx 配置文件 Nginx命令行管理终端工具
/usr/sbin/nginx-debug 配置文件 Nginx命令行与终端调试工具
  • Nginx日志相关目录与文件
路径 类型 作用
/var/log/nginx 目录 Nginx默认存放日志目录
/etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割

Nginx配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#nginx主配置文件 
[root@web02 nginx]# grep -Ev '^$|#' /etc/nginx/nginx.conf
注意:nginx配置文件,每一行,都';'结尾
[root@web01 nginx]# vim /etc/nginx/nginx.conf
##核心层(核心模块)、全局配置
#nginx启动用户配置
user nginx;
#nginx工作线程数量(cpu亲和)
worker_processes auto; #auto:自动根据cpu的核心数来启动对应的工作进程数
#错误日志 日志路径 日志级别
error_log /var/log/nginx/error.log notice;
#程序启动进程号(pid号)存放的路径
pid /var/run/nginx.pid;

##事件层(事件模块)
events {
#一个worker进程的最大连接数
worker_connections 1024;
}
##http层,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;

#长链接,超时时间 65s
keepalive_timeout 65;

#数据传输过程中,使用gzip压缩
#gzip on;

#包含 nginx其他子配置文件(网站虚拟主机配置文件server)
include /etc/nginx/conf.d/*.conf;
}

#日志格式
$remote_addr:远端的IP(上一个节点的IP)
$remote_user:登录的用户
[$time_local]:时间
"$request":请求方式、请求uri、HTTP协议版本号
$status:状态码
$body_bytes_sent:流量
"$http_referer":跳转地址(从哪个网站跳转过来的)
"$http_user_agent":客户端浏览器相关信息
"$http_x_forwarded_for":记录透传IP地址(获取用户的真实IP)
$request_length:请求的长度(包括请求行, 请求头和请求正文)。
$request_time:请求花费的时间,单位为秒,精度毫秒
#注:如果Nginx位于负载均衡器,nginx反向代理之后,web服务器无法直接获取到客 户端真实的IP地址
#$remote_addr获取的是反向代理的IP地址。反向代理服务器在转发请求的http头信息中,增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址

#access_log日志配置语法
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except


#虚拟主机配置文件
server {
#该网站的监听端口
listen 80;
#该网站的主机IP或域名
server_name localhost;
#该网站的日志路径及日志格式
#access_log /var/log/nginx/host.access.log main;

#uri跳转
location / {
#站点目录
root /usr/share/nginx/html;
#默认首页,索引页面
index index.html roger.html;
}
}

http{
access_log /var/log/nginx/access.log main;

server {
listen 80; server_name www.baidu.com;
...
access_log /var/log/nginx/www.baidu.com.access.log main;
}
server {
listen 80; server_name zhidao.baidu.com;
...
access_log /var/log/nginx/zhidao.baidu.com.access.log main;
}
server {
listen 80; server_name map.baidu.com;
...
access_log /var/log/nginx/map.baidu.com.access.log main;
}
}

多虚拟主机(多web网站配置)

在企业中,是不可能用一个nginx对应一套业务,多个网站都在一个nginx中配置

15652467829612

基于IP的多虚拟主机

image-20220531214136912

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@web01 conf.d]# ifconfig eth0:0 10.0.0.10 
[root@web01 conf.d]# ifconfig eth0:1 10.0.0.11

[root@web01 conf.d]# vim 1_game.conf
server {
listen 80;
server_name 10.0.0.10;

location / {
root /game/h5_games;
index index.html;
}
}

[root@web01 conf.d]# vim 1_roger.conf
server {
listen 80;
server_name 10.0.0.11;
root /code;

location / {
index index.html;
}
location /roger {
index index_1.html roger.html;
}
}

基于多端口的虚拟主机

image-20220531214817767

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@web01 conf.d]# vim game.conf 
server{
listen 8081;
server_name 10.0.0.7;

location / {
root /game/h5_games;
index index.html;
}
}

[root@web01 conf.d]# vim roger.conf
server{
listen 8082;
server_name 10.0.0.7;
root /code;

location / {
index index.html;
}
location /roger {
index index_1.html roger.html;
}
}

基于多域名的虚拟主机

image-20220531214931959

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@web01 conf.d]# vim blog.roger.com.conf 
server {
listen 80;
server_name blog.roger.com;
root /roger1;
index index.html;
}

[root@web01 conf.d]# vim www.roger.com.conf
server {
listen 80;
server_name www.roger.com;
root /roger;
index index.html;
}

在windows系统中,配置本地的DNS

1.按win+r打开运行

2.输入:drivers

3.进入与etc目录

image-20220531222142820

访问不到:

image-20220531222619360

Nginx日志切割

使用logrotate切割日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@nginx conf.d]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily #每天切割日志
missingok #日志丢失忽略
rotate 52 #日志保留52天
compress #日志文件压缩
delaycompress #延迟压缩日志
notifempty #不切割空文件
create 640 nginx adm #日志文件权限
sharedscripts
postrotate #切割日志执行的命令
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}

日志切割后的效果

1
2
3
4
5
6
[root@oldboy ~]# ll /var/log/nginx/
total 4044
-rw-r----- 1 www adm 54438 Oct 12 03:28 access.log-20181012.gz
-rw-r----- 1 www adm 28657 Oct 13 03:48 access.log-20181013.gz
-rw-r----- 1 www adm 10135 Oct 12 03:28 error.log-20181130.gz
-rw-r----- 1 www adm 7452 Oct 13 03:48 error.log-20181201.gz

Nginx常用模块

Nginx目录索引模块

ngx_http_autoindex_module 模块处理以斜杠字符(’/‘)结尾的请求,并生成目录列表

ngx_http_index_module 模块找不到索引文件时,通常会将请求传递给 ngx_http_autoindex_module 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Syntax: autoindex on | off; 
Default:
autoindex off;
Context: http, server, location

server{
#监听端口
listen 80;
#域名(ip,localhost,_,域名)
server_name _;

#uri
location / {
#站点目录(代码存放目录)
root /test;
#目录索引模块 开启;
autoindex on;
#显示带单位的大小
autoindex_exact_size off;
#目录索引页面显示格式(默认html)
#autoindex_format json;
#显示本地时间
autoindex_localtime on;
}
}

windows启用telnet命令

image-20220606203421218

Nginx状态模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@web01 conf.d]# cat sub_stat.conf
server{
listen 80;
server_name _;

location / {
root /test;
autoindex on;
autoindex_exact_size off;
#autoindex_format json;
autoindex_localtime on;
}

location /roger {
stub_status;
}
}

#状态属性
Active connections #当前活动的连接数
accepts #当前的总连接数TCP
handled #成功的连接数TCP
requests #总的http请求数

Reading #请求
Writing #响应
Waiting #等待的请求数,开启了keepalive

注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; #类似于关闭长连接
keepalive_timeout 65; #65s没有活动则断开连接

Nginx访问控制模块

基于用户密码(auth_basic)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#安装htpasswd命令 
[root@web01 conf.d]# yum install -y httpd
#创建存放认证文件的目录
[root@web01 nginx]# mkdir /etc/nginx/auth
#创建认证文件
-b:允许命令行中输入密码
-c:创建一个新文件,将用户名和密码保存到指定文件中
[root@web01 nginx]# htpasswd -b -c /etc/nginx/auth/roger_auth roger 123
#查看认证文件内容
[root@web01 nginx]# cat /etc/nginx/auth/roger_auth
roger:$apr1$ohAvPJlj$I8viSCSG8FjwOE4z8VelQ.
#修改nginx配置文件,添加认证
[root@web01 nginx]# vim /etc/nginx/conf.d/sub_stat.conf
server{
listen 80;
server_name _;
auth_basic "password is 123";
auth_basic_user_file /etc/nginx/auth/roger_auth;

location / {
root /test;
autoindex on;
autoindex_exact_size off;
#autoindex_format json;
autoindex_localtime on;
}

location /roger {
stub_status;
}
}

#追加用户
[root@web01 ~]# htpasswd -b /etc/nginx/auth/roger_auth zls 123
[root@web01 ~]# cat /etc/nginx/auth/roger_auth
roger:$apr1$ohAvPJlj$I8viSCSG8FjwOE4z8VelQ.
zls:$apr1$txPXciw9$mgO3UhGuXkNhu7j4WvLre.

基于IP访问控制(access)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server{ 
listen 80; server_name _;
auth_basic "password is 123";
auth_basic_user_file auth/zls_auth;

location / {
root /test;
autoindex on;
autoindex_exact_size off;
#autoindex_format json;
autoindex_localtime on;
#只允许10.0.0.8访问
allow 10.0.0.8;
deny all;
}

location /roger {
stub_status;
}
}

curl http://用户名:密码@10.0.0.7

注意:默认nginx是allow all;如果只允许某一个IP需要配合deny all使用,deny all;要放在最下面

访问频率限制

连接频率限制(limit_conn)

1
2
3
4
5
6
7
8
9
http{
limit_conn_zone $remote_addr zone=内存空间的名字:10m;
server {
limit_conn 内存空间的名字 1;
}
}

conn_zone:内存空间的名字
1:连接次数

请求频率限制(limit_req)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
http {
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
server {
listen 80;
server_name module.oldboy.com;
#1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
#limit_req zone=req_zone;
#请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
limit_req zone=req_zone burst=3 nodelay;

location / {
root /code;
index index.html;
}
}

#请求频率限制错误页面优化
[root@web01 ~]# vim /etc/nginx/conf.d/sub_stat.conf
http {
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
server{
listen 80;
server_name _;
auth_basic "password is 123";
auth_basic_user_file auth/zls_auth;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 508; #(400 - 599之间)
error_page 508 /508.html;

location /{
root /test;
autoindex on;
autoindex_exact_size off;
#autoindex_format json;
autoindex_localtime on;
#allow 10.0.0.8;
#deny all;
}

location /roger {
stub_status;
}
}

location优先级

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
!~ 区分大小写不匹配的正则 5
!~* 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到 7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#通用匹配,任何请求都会匹配到 
location / {
...
}

#严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}

#严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}

#不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
...
}

location ~* \.(jpg|gif|png|js|css)$ {
...
}

#不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
...
}

Nginx实现web架构

企业中网站架构

1
2
3
4
5
6
7
8
LNMP:Linux Nginx MySQL PHP 
LAMP:Linux Apache MySQL PHP
LNMT:Linux Nginx MySQL Tomcat
LAMT:Linux Apache MySQL Tomcat

Nginx Apache:运行html css js
PHP:运行php代码
Tomcat:运行Java代码

LNMP架构概述

什么是LNMP

LNMP是一套技术的组合,L=Linux、N=Nginx、M=MySQL、P=PHP

LNMP架构是如何工作的

当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示

360截图174209167911470

Nginx与Fast-CGO详细工作流程

image-20220607201555268

1.用户通过http协议发起请求,请求会先抵达LNMP架构中的Nginx

2.Nginx会根据用户的请求进行判断,这个判断是有Location进行完成

3.判断用户请求的是静态页面,Nginx直接进行处理

4.判断用户请求的是动态页面,Nginx会将该请求交给fastcgi协议下发

5.fastgi会将请求交给php-fpm管理进程, php-fpm管理进程接收到后会调用具体的工作进程warrap

6.warrap进程会调用php程序进行解析,如果只是解析代码php直接返回

7.如果有查询数据库操作,则由php连接数据库(用户 密码 IP)发起查询的操作

8.最终数据由mysql->php->php-fpm->fastcgi->nginx->http->user

部署PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#1.卸载Linux自带的旧版本php 
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common

#2.添加php第三方源
[root@nginx ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

yum -y install epel-release yum-utils
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install -y --enablerepo=remi --enablerepo=remi-php74 mod_php php-gd* php php-opcache php-mbstring php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-fpm
yum install -y --enablerepo=remi --enablerepo=remi-php74 php-cli php-redis php-pecl-mcrypt php-process php-devel php-pear
php --version #查看版本
php -m #查看安装模块,需要用到的模块,没有安装的需要YUM安装

#3.安装php
[root@web01 ~]# 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 --downloadonly --downloaddir=/tmp

#4.创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

#5.修改nginx运行用户
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;
[root@web01 ~]# systemctl reload nginx

php-fpm作用:用来管理php程序运行
#php相关配置文件
/etc/php-fpm.conf #php管理进程配置文件
/etc/php.ini #php程序配置文件
/etc/php-fpm.d/www.conf #php管理进程的子配置文件

#6.修改php的启动用户
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
[www]
user = www
group = www

注意:php配置文件中;是注释

#7.启动php并加入开机自启
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm

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

#8.检查php进程和端口
[root@web01 ~]# ps -ef|grep php
root 7764 1 0 06:02 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www 7765 7764 0 06:02 ? 00:00:00 php-fpm: pool www
www 7766 7764 0 06:02 ? 00:00:00 php-fpm: pool www
www 7767 7764 0 06:02 ? 00:00:00 php-fpm: pool www
www 7768 7764 0 06:02 ? 00:00:00 php-fpm: pool www
www 7769 7764 0 06:02 ? 00:00:00 php-fpm: pool www
root 7772 7043 0 06:02 pts/1 00:00:00 grep --color=auto php

[root@web01 ~]# netstat -lntup|grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7764/php-fpm: maste

配置nginx连接php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@web01 movie]# vim /etc/nginx/conf.d/movie.conf 
server{
listen 80;
server_name movie.roger.com;

location / {
root /movie;
index index.php index.html;
}

location ~ \.php$ {
root /movie;
#nginx调用本机的9000端口(php-fpm程序)
fastcgi_pass 127.0.0.1:9000;
#用php程序,解析哪个目录下的哪个.php的文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#包含php变量的文件
include /etc/nginx/fastcgi_params;
}
}

#修改/movie的使用权限
[root@web01 movie]# chown www.www /movie/

[root@web01 movie]# vim php_info.php
<?php
phpinfo();
?>

部署wordpress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#1.编辑nginx配置文件 
[root@web01 ~]# vim /etc/nginx/conf.d/blog.roger.com.conf
server{
listen 80;
server_name blog.roger.com;

location / {
root /blog;
index index.php index.html;
}

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

#2.重新加载nginx
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl reload nginx

#3.创建站点目录并授权
[root@web01 ~]# mkdir /blog/wordpress
[root@web01 ~]# chown -R www.www /blog/wordpress/

#4.测试nginx连接php(编写php info代码)
[root@web01 ~]# vim /blog/info.php
<?php
phpinfo();
?>

#5.windows域名解析
打开路径:C:\Windows\System32\drivers\etc
编辑hosts文件:10.0.0.7 blog.roger.com

#6.打开浏览器访问
http://blog.roger.com/info.php

#7.下载wordpress代码
wordpress官网:https://wordpress.org/
[root@web01 blog]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz

#8.解压代码
[root@web01 blog]# tar xf latest-zh_CN.tar.gz

#9.修改nginx配置文件
[root@web01 blog]# vim /etc/nginx/conf.d/blog.roger.com.conf
server{
listen 80;
server_name blog.roger.com;
root /blog/wordpress;

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;
}
}

#10.重新加载nginx
[root@web01 blog]# nginx -t
[root@web01 blog]# systemctl reload nginx

#11.打开浏览器,访问
http://blog.roger.com/

安装mariadb数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#1.安装mariadb 
[root@web01 ~]# yum install -y mariadb-server

#2.启动数据库并加入开机自启
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb

#3.登录数据库
[root@web01 ~]# mysql

#4.查看所有库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+

#5.切换数据库
MariaDB [(none)]> use mysql

#6.查看该库中的所有表
MariaDB [mysql]> show tables;

#7.创建数据库
MariaDB [mysql]> create database 库名字;
MariaDB [mysql]> create database wordpress;

#8.创建用户
MariaDB [(none)]> grant all on 所有库.所有表 to 用户名@'主机IP' identified by '密码';
MariaDB [(none)]> grant all on *.* to wp@'localhost' identified by '123';

#9.查看用户
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| wp | localhost |
| | web01 |
| root | web01 |
+------+-----------+

#10.退出数据库
MariaDB [(none)]> exit
MariaDB [(none)]> quit

数据库名字:wordpress
连接用户名:wp
连接密码:123
连接IP:localhost

#11.设置数据库用户密码
[root@web01 ~]# mysqladmin -uroot -p password '123'
Enter password:

#12.测试php是否可以跟MySQL建立连接
[root@web01 ~]# vim /blog/pm.php
<?php
$servername = "localhost";
$username = "wp";
$password = "123";

//创建连接
$conn = mysqli_connect($servername, $username, $password);

//检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?>

<img style='width:100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/php_mysql.png>

#13.主题下载
[root@web01 ~]# wget http://test.driverzeng.com/Nginx_Code/QQ2.8.zip

通过socket文件启动php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#启动php服务时,创建php.sock安全套接字文件,该文件的属主和属组
listen = /dev/shm/php.sock
listen.owner = www
listen.group = www

#完整php配置文件
[root@web01 ~]# grep -EV "^;|^$" /etc/php-fpm.d/www.conf
[www]
user = www
group = www
;listen = 0.0.0.0:9000
listen = /dev/shm/php.sock
listen.owner = www
listen.group = www
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache

#配置nginx连接php
[root@web01 ~]# vim /etc/nginx/conf.d/blog.roger.com.conf
server{
listen 80;
server_name blog.roger.com;
root /blog;
index index.php index.html;

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

优化Nginx配置

1
2
3
4
5
6
7
#nginx上传文件大小限制优化
vim /etc/nginx/nginx.conf
http {
...
client_max_body_size 500m; #推荐500m
...
}

wordpress更换域名排坑

image-20220609154934739

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#1.后台更新
浏览器访问:blog.roger.com/wp-admin
修改wordpress地址和站点地址

#2.修改nginx配置文件域名
[root@web01 conf.d]# vim blog.roger.com.conf
server {
listen 80;
server_name www.roger.com;
root /blog/wordpress;
index index.php index.html;

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/opt/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#3.重新加载nginx
[root@web01 conf.d]# systemctl reload nginx

[root@web01 conf.d]# wget https://www.php.net/distributions/php-7.3.15.tar.gz
[root@web01 conf.d]# tar xf php-7.3.15.tar.gz
./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--bindir=/usr/local/php/bin \
--sbindir=/usr/local/php/sbin \
--includedir=/usr/local/php/include \
--libdir=/usr/local/php/lib/php \
--mandir=/usr/local/php/php/man \
--with-config-file-path=/usr/local/php/etc \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--without-gdbm \
--enable-fileinfo \
--disable-fileinfo

LNMP环境搭建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#1.关闭防火墙
[root@web01 ~]# systemctl stop firewalld
[root@web01 ~]# systemctl disable firewalld

#2.关闭selinux
## 临时关闭
[root@web01 ~]# setenforce 0
## 永久关闭
[root@web01 ~]# sed -i.bak 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux

#3.添加yum源
## nginx yum源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[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

## php yum源
[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

#4.安装LNMP
#安装php依赖
[root@web01 ~]# yum -y install epel-release
[root@web01 ~]# wget http://us-east.repo.webtatic.com/yum/el7/webtatic-release.rpm
[root@web01 ~]# rpm -ivh webtatic-release.rpm 或 rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@web01 ~]# 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 nginx

[root@web01 ~]# rz (php.tgz)
[root@web01 ~]# mkdir php && mv php.tgz php
[root@web01 ~]# cd php
[root@web01 ~]# tar xf php.tgz
[root@web01 ~]# yum install libmemcached
[root@web01 ~]# yum localinstall -y *.rpm 或 rpm -ivh *.rpm

yum -y install epel-release yum-utils
rpm -Uvh http://mirror.webtatic.com/yum/el7/latest.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install -y --enablerepo=remi --enablerepo=remi-php74 mod_php php-gd* php php-opcache php-mbstring php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-fpm
yum install -y --enablerepo=remi --enablerepo=remi-php74 php-cli php-redis php-pecl-mcrypt php-process php-devel php-pear
php --version #查看版本
php -m #查看安装模块,需要用到的模块,没有安装的需要YUM安装

#5.创建用户和组
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

#6.统一用户
## 修改nginx配置文件
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

## 修改php配置文件
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
[www]
user = www
group = www

## 启动php服务时,创建php.sock安全套接字文件,该文件的属主和属组
listen = /dev/shm/php.sock
listen.owner = www
listen.group = www

## 完整php配置文件
### 过滤配置文件
[root@web01 ~]# grep -Ev "^;|^$" /etc/php-fpm.d/www.conf
[www]
user = www
group = www
;listen = 0.0.0.0:9000
listen = /dev/shm/php.sock #闪存目录
listen.owner = www
listen.group = www
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache

#7.配置nginx连接php
[root@web01 ~]# vim /etc/nginx/conf.d/blog.roger.com.conf
server {
listen 80;
server_name blog.roger.com;
root /blog;
index index.php index.html;

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

# 8.启动nginx和php并加入开机自启
[root@web01 ~]# systemctl start nginx php-fpm
[root@web01 ~]# systemctl enable nginx php-fpm

#9.创建站点目录
[root@web01 ~]# mkdir /blog

#10.测试nginx连接php
## 编写phpinfo文件
[root@web01 ~]# vim /blog/info.php
<?php
phpinfo();
?>

#11.windows上域名解析
10.0.0.7 blog.roger.com

## 浏览器访问:http://blog.roger.com/info.php

#12.启动MySQL并加入开机自启
[root@web01 ~]# yum install -y mariadb-server
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb

#13.给MySQL超级管理员用户root设置密码
[root@web01 ~]# mysqladmin -uroot password '123'

#14.连接数据库
[root@web01 ~]# mysql -uroot -p123

#15.创建数据库
MariaDB [(none)]> create database wp;

#16.创建用户
MariaDB [(none)]> grant all on *.* to wp_user@'localhost' identified by '111';

#17.查看数据库是否创建成功
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wp |
+--------------------+

#18.查看用户是否创建成功
MariaDB [(none)]> select user,host from mysql.user;
+---------+-----------+
| user | host |
+---------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| wp_user | localhost |
| | web01 |
| root | web01 |
+---------+-----------+

## MySQL相关库和用户
wordpress库:wp
wordpress用户:wp_user
wordpress用户密码:111
wordpres用户IP:localhost

#19.测试php是否可以跟MySQL建立连接
[root@web01 ~]# vim /blog/pm.php
<?php
$servername = "localhost";
$username = "wp_user";
$password = "123";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?>

<img style='width:100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/php_mysql.png>

## 浏览器访问:http://blog.roger.com/pm.php

部署wordpress代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#1.下载wordpress
https://cn.wordpress.org/download/
[root@web01 ~]# cd /blog/
[root@web01 blog]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz

[root@web01 blog]# wget http://test.driverzeng.com/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz

#2.解压
[root@web01 blog]# tar xf latest-zh_CN.tar.gz

#3.修改nginx配置文件
[root@web01 blog]# vim /etc/nginx/conf.d/blog.roger.com.conf
server {
listen 80;
server_name blog.roger.com;
root /blog/wordpress;
index index.php index.html;

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#4.重新加载nginx
[root@web01 blog]# systemctl reload nginx

#5.给站点目录授权
[root@web01 blog]# chown -R www.www /blog/wordpress

#6.打开浏览器访问域名:http://blog.roger.com/

image-20220619045946413

1
2
3
4
5
6
#nginx上传文件大小限制优化
http {
...
client_max_body_size 500m; #推荐500m
...
}

image-20220619045914630

wordpress更换域名排坑

image-20220619045847245

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#1.修改nginx配置文件域名
[root@web01 conf.d]# vim blog.roger.com.conf
server {
listen 80;
server_name www.roger.com;
root /blog/wordpress;
index index.php index.html;

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#2.重新加载nginx
[root@web01 conf.d]# systemctl reload nginx

Web集群数据拆分及共享存储

数据库拆分

环境准备

主机名 WanIP LanIP 角色 安装应用
web01 10.0.0.7 172.16.1.7 web网站 nginx php
db01 10.0.0.51 172.16.1.51 数据库 MySQL(mariadb)

旧数据库操作

1
2
3
4
5
6
7
8
9
#1.wp库数据备份
[root@web01 ~]# mysqldump -u用户名 -p密码 -B 库名字 > /tmp/wordpress.sql
[root@web01 ~]# mysqldump -uroot -p -B wordpress > /tmp/wordpress.sql

#2.将备份好的数据拷贝到新数据库服务器中
[root@web01 ~]# scp /tmp/wordpress.sql root@172.16.1.51:/tmp/

#3.在下面新数据库操作做完之后,停掉旧数据库
[root@web01 ~]# systemctl stop mariadb

新数据库操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#1.安装数据库
[root@db01 ~]# yum install -y mariadb-server

#2.启动数据库并加入开机自启
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb

#3.给MySQL的root用户设置密码
[root@db01 ~]# mysqladmin -uroot -p password '123'
Enter password: #如果没有密码则直接回车,如果有密码就输入旧密码

#4.导入数据
[root@db01 ~]# mysql -uroot -p123 < /tmp/wordpress.sql

#5.检查是否导入成功
[root@db01 ~]# mysql -uroot -p123

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+

#6.创建用户
MariaDB [(none)]> grant all on *.* to wp@'%' identified by '123';

#7.测试web01是否可以远程连接db01的MySQL
[root@web01 ~]# mysql -uwp -p123 -h10.0.0.51

#8.修改wordpress连接数据库的配置
[root@web01 wordpress]# vim /blog/wordpress/wp-config.php
define( 'DB_NAME', 'wordpress' );

/** Database username */
define( 'DB_USER', 'wp' );

/** Database password */
define( 'DB_PASSWORD', '123' );

/** Database hostname */
define( 'DB_HOST', '10.0.0.51' );

多台web部署

环境准备

主机名 WanIP LanIP 角色 安装应用
web01 10.0.0.7 172.16.1.7 web网站 nginx php
web02 10.0.0.8 172.16.1.8 web网站 nginx php
db01 10.0.0.51 172.16.1.51 数据库 MySQL(mariadb)

部署web02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#1.添加yum源
[root@web01 yum.repos.d]# scp /etc/yum.repos.d/nginx.repo /etc/yum.repos.d/php.repo 172.16.1.8:/etc/yum.repos.d/

#2.创建www用户和组
[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

#3.安装nginx和php
[root@web02 ~]# 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 nginx mariadb-server

#4.添加nginx虚拟主机配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/blog.roger.com.conf
server {
listen 80;
server_name blog.roger.com;
root /blog/wordpress;
index index.php index.html;

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/opt/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#5.修改nginx启动用户
[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;

#6.修改php启动用户
[root@web02 ~]# vim /etc/php-fpm.d/www.conf
[www]
user = www
group = www

;listen = 127.0.0.1:9000
listen = /dev/shm/php.sock
listen.owner = www
listen.group = www

#7.创建站点目录
[root@web02 ~]# mkdir /blog

############ 8.下载wordpress代码
[root@web02 ~]# cd /blog/
[root@web02 blog]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
############ 9.解压wordpress
[root@web02 blog]# tar xf latest-zh_CN.tar.gz
############# 10.授权
[root@web02 blog]# chown -R www.www /blog/

#8.拷贝web01代码到web02上
[root@web01 php]# scp -r /blog/wordpress/ 172.16.1.8:/blog

#9.授权
[root@web02 blog]# chown -R www.www /blog/

#10.启动nginx和php
[root@web02 blog]# systemctl start nginx php-fpm

#11.域名解析到web02
10.0.0.8 www.roger.com

解决用户数据不一致问题

环境准备

主机名 WanIP LanIP 角色 安装应用
web01 10.0.0.7 172.16.1.7 web网站、共享存储客户端 nginx php nfs
web02 10.0.0.8 172.16.1.8 web网站、共享存储客户端 nginx php nfs
db01 10.0.0.51 172.16.1.51 数据库 MySQL(mariadb)
nfs 10.0.0.31 172.16.1.31 共享存储服务端 nfs

部署nfs服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#1.安装nfs服务
[root@nfs ~]# yum install -y nfs-utils

#2.修改nfs配置文件
[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)

#3.创建共享存储目录
[root@nfs ~]# mkdir /data

#4.创建www用户和组
[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

#5.授权共享存储目录
[root@nfs ~]# chown www.www /data

#6.启动服务并加入开机自启
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl enable nfs

#7.检查nfs配置文件是否生效
[root@nfs ~]# cat /var/lib/nfs/etab

nfs客户端操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#1.安装nfs服务
[root@web01 ~]# yum install -y nfs-utils
[root@web02 ~]# yum install -y nfs-utils

#2.查看挂载点
[root@web01 ~]# showmount -e 172.16.1.31
[root@web02 ~]# showmount -e 172.16.1.31

#3.拷贝已有数据到共享目录
[root@web01 ~]# scp -r /blog/wordpress/wp-content/uploads/2022 172.16.1.31:/data
[root@web02 ~]# scp -r /blog/wordpress/wp-content/uploads/2022 172.16.1.31:/data

#4.给nfs的/data目录授权
[root@nfs ~]# chown -R www.www /data

#5.挂载
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /blog/wordpress/wp-content/uploads/
[root@web02 ~]# mount -t nfs 172.16.1.31:/data /blog/wordpress/wp-content/uploads/

LNMP架构搭建WeCenter

作业需求

1.实现LNMP架构

2.独立的数据库

3.共享存储

4.实现用户数据的实时同步(nfs /data目录数据实时同步到 backup服务器上)

5.不使用wordpress,使用wecenter

环境准备

主机名 WanIP LanIP 角色 安装应用
web01 10.0.0.7 172.16.1.7 wordpress、wecenter网站
nfs客户端
nginx
php
nfs
wordpress
wecenter
web02 10.0.0.8 172.16.1.8 wordpress、wecenter网站nfs客户端
nginx
php
nfs
wordpress
wecenter
nfs 10.0.0.31 172.16.1.31 nfs服务端
rsync客户端
nfs-utils
sersync
rsync
backup 10.0.0.41 172.16.1.41 rsync服务端(备份数据、实时同步数据)
nfs服务端
rsync
nfs-utils
db01 10.0.0.51 172.16.1.51 数据库(博客内容、点赞数量、评论…文字数据) MySQL(MariaDB)

架构图

image-20220614213606480

rsync服务端部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#1.安装rsync
[root@backup ~]# yum install -y rsync

#2.修改rsync配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[wp_data]
comment = welcome to oldboyedu backup!
path = /wp_data

[zh_data]
comment = welcome to oldboyedu backup!
path = /zh_data

#3.创建www用户和组
[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

#4.创建密码文件
[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd

#5.修改密码文件的权限
[root@backup ~]# chmod 600 /etc/rsync.passwd

#6.创建备份目录
[root@backup ~]# mkdir /{wp,zh}_data

#7.授权备份目录
[root@backup ~]# chown www.www /{wp,zh}_data

#8.启动rsync服务并加入开机自启
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd

#9.测试
[root@nfs ~]# rsync -avz /tmp rsync_backup@172.16.1.41::wp_data
[root@nfs ~]# rsync -avz /tmp rsync_backup@172.16.1.41::zh_data

NFS服务端部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#1.安装nfs服务
[root@nfs ~]# yum install -y nfs-utils
[root@backup ~]# yum install -y nfs-utils

#2.修改nfs配置文件
[root@nfs ~]# vim /etc/exports
/wp_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)
/zh_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)

[root@backup ~]# vim /etc/exports
/wp_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)
/zh_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)

#3.创建www用户和组
[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

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

#4.创建共享目录
[root@nfs ~]# mkdir /{wp,zh}_data

#5.共享目录授权
[root@nfs ~]# chown www.www /{wp,zh}_data

#6.启动nfs服务
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl enable nfs

[root@backup ~]# systemctl start nfs
[root@backup ~]# systemctl enable nfs

部署web服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#1.更改yum源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

[root@web02 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

## 开启keepcache,下载软件包
[root@web01 ~]# vim /etc/yum.conf
keepcache=1
[root@web02 ~]# vim/etc/yum.conf
keepcache=1
## 查看下载的软件包
[root@web01 ~]# /var/cache/yum/x86_64/7/
[root@web02 ~]# /var/cache/yum/x86_64/7/
## 复制软件包到/opt下
[root@web01 ~]# find /var/cache/yum/x86_64/7/ -type f | grep '.rpm$' | xargs cp -t /opt/
[root@web02 ~]# find /var/cache/yum/x86_64/7/ -type f | grep '.rpm$' | xargs cp -t /opt/

#2.安装nginx和php
[root@web01 ~]# 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 nginx

[root@web02 ~]# 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 nginx

#3.创建www用户和组
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

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

#4.修改nginx主配置文件(启动用户)
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;

#5.修改php配置文件(启动用户)
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
[www]
user = www
group = www

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

#6.编写wordpress配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/blog.roger.com.conf
server {
listen 80;
server_name blog.roger.com;
root /code/wordpress;
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;
}
}

[root@web02 ~]# vim /etc/nginx/conf.d/blog.roger.com.conf
server {
listen 80;
server_name blog.roger.com;
root /code/wordpress;
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;
}
}

#7.编写wecenter配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/zh.roger.com.conf
server {
listen 80;
server_name zh.roger.com;
root /code/wecenter;
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;
}
}

[root@web02 ~]# vim /etc/nginx/conf.d/zh.roger.com.conf
server {
listen 80;
server_name zh.roger.com;
root /code/wecenter;
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;
}
}

#8.启动nginx和php并加入开机自启
[root@web01 ~]# systemctl start nginx php-fpm
[root@web01 ~]# systemctl enable nginx php-fpm

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

#9.创建站点目录
[root@web01 ~]# mkdir /code
[root@web02 ~]# mkdir /code

#10.下载代码
[root@web01 code]# wget http://test.driverzeng.com/Nginx_Code/WeCenter_3-2-1.zip
[root@web01 code]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz

########################
#11.部署wordpress(只在web01部署)
[root@web01 code]# tar xf latest-zh_CN.tar.gz

#12.授权站点目录
[root@web01 code]# chown -R www.www /code/

数据库部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#1.安装MariaDB
[root@db01 ~]# yum install -y mariadb-server

#2.启动MariaDB并加入开机自启
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb

#3.设置数据库的管理用户 root 初始密码
[root@db01 ~]# mysqladmin -uroot -p password '123'
Enter password: ## root默认没有密码,直接回车

#4.连接数据库
[root@db01 ~]# mysql -uroot -p123

#5.创建wordpress数据库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

#6.创建wecenter数据库
MariaDB [(none)]> create database wecenter;
Query OK, 1 row affected (0.00 sec)

#7.创建wordpress程序连接数据库的用户
创建一个用户:
用户名:wordpress_user
密码:123
允许该用户连接的IP地址:172.16.1.0/24网段
针对wordpress库下面的所有表,有所有权限

MariaDB [(none)]> grant all on wordpress.* to wordpress_user@'172.16.1.%' identified by '123';

#8.创建wecenter程序连接数据库的用户
MariaDB [(none)]> grant all on wecenter.* to wecenter_user@'172.16.1.%' identified by '123';

windows域名解析

1
2
10.0.0.7 blog.roger.com zh.roger.com
#10.0.0.8 blog.roger.com zh.roger.com

浏览器配置

image-20220619203210684

image-20220619203256529

image-20220619203332207

image-20220619203426444

image-20220619203552215

image-20220619203623872

报错

image-20220619204157761

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#原因:连接不上数据库

#排查:
1)在代码所在的服务器上,安装MySQL客户端命令:mysql
[root@web01 code]# yum install -y mariadb

2)测试连接
[root@web01 code]# mysql -uwordpress_user -p123 -h10.0.0.51
ERROR 1130 (HY000): Host '10.0.0.7' is not allowed to connect to this MariaDB server

[root@web01 code]# mysql -uwordpress_user -p123 -h172.16.1.51

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
| wordpress |
+--------------------+

web02部署wordpress

1
2
3
4
5
#1.将web01部署好的wordpress拷贝到web02
[root@web01 code]# scp -r /code/wordpress/ 172.16.1.8:/code

#2.授权站点目录
[root@web02 code]# chown -R www.www /code/

web01部署wecenter代码

1
2
3
4
5
6
7
8
#1.解压wecenter代码
[root@web01 code]# unzip WeCenter_3-2-1.zip

#2.改名
[root@web01 code]# mv WeCenter_3-2-1 wecenter

#3.授权
[root@web01 code]# chown -R www.www /code/

wecenter页面配置

image-20220619211114487

image-20220619211235516

image-20220619211310082

web02部署wecenter

1
2
3
4
5
#1.拷贝配置好的wecenter代码
[root@web01 code]# scp -r /code/wecenter 172.16.1.8:/code/

#2.授权
[root@web02 code]# chown -R www.www /code/

nfs客户端(共享存储)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#1.安装nfs-utils
[root@web01 ~]# yum install -y nfs-utils
[root@web02 ~]# yum install -y nfs-utils

#2.查看挂载点
[root@web01 ~]# showmount -e 172.16.1.31
[root@web02 ~]# showmount -e 172.16.1.31

#3.用户上传目录
[root@web01 ~]# mkdir /code/wordpress/wp-content/uploads
[root@web02 ~]# mkdir /code/wordpress/wp-content/uploads

#4.挂载wp共享存储目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/wp_data /code/wordpress/wp-content/uploads
[root@web02 ~]# mount -t nfs 172.16.1.31:/wp_data /code/wordpress/wp-content/uploads

#5.挂载zh共享存储目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/zh_data /code/wecenter/uploads/
[root@web02 ~]# mount -t nfs 172.16.1.31:/zh_data /code/wecenter/uploads/

rsync客户端部署(sersync实时同步)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#1.下载sersync
[root@nfs ~]# wget http://test.driverzeng.com/other/sersync2.5.4_64bit_binary_stable_final.tar.gz

#2.安装sersync依赖(inotify rsync)
[root@nfs ~]# yum install -y rsync inotify-tools

#3.创建sersync安装目录
[root@nfs ~]# mkdir /app

#4.解压sersync
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /app

#5.改名
[root@nfs app]# mv /app/GNU-Linux-x86 /app/sersync

#6.修改配置文件
[root@nfs app]# vim /app/sersync/zh_data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>

<sersync>
<localpath watch="/zh_data">
<remote ip="172.16.1.41" name="zh_data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>

<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>

<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>


[root@nfs app]# vim /app/sersync/wp_data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>

<sersync>
<localpath watch="/wp_data">
<remote ip="172.16.1.41" name="wp_data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>

<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>

<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>

#7.创建密码文件
[root@nfs app]# echo '123' > /etc/rsync.passwd

#8.授权密码文件
[root@nfs app]# chmod 600 /etc/rsync.passwd

#9.启动sersync
[root@nfs ~]# /app/sersync/sersync2 -rdo /app/sersync/zh_data.xml
[root@nfs ~]# /app/sersync/sersync2 -rdo /app/sersync/wp_data.xml

Nginx反向代理

什么是代理

正向代理:网络代理(代理客户端访问外网)

15658750705026

反向代理:用于公司集群架构,代理服务端

15658751321865

正向代理和反向代理的区别

1.区别在于形式上服务的”对象”不一样

2.正向代理代理的对象是客户端,为客户端服务

3.反向代理代理的对象是服务端,为服务端服务

为什么学代理

nginx代理PHP服务(fastcgi_pass:127.0.0.1:9000)

1.代理后端语言的服务(PHP、JAVA、Python…)

2.国外服务器代理国内服务器,方便国外用户上网

正向代理使用的模块

15658754480444

反向代理使用的模块

15658755446851

反向代理模块总结

反向代理模式与Nginx代理模块总结如表格所示

反向代理模式 Nginx配置模块
http、websocket、https ngx_http_proxy_module
fastcgi ngx_http_fastcgi_module
uwsgi ngx_http_uwsgi_module
grpc ngx_http_v2_module

Nginx反向代理配置

环境准备

主机名 WanIP LanIP 角色 应用
web01 10.0.0.7 172.16.1.7 web网站 nginx、php、wordpress
lb01 10.0.0.5 172.1.6.1.5 反向代理服务器 nginx
db01 10.0.0.51 172.16.1.51 数据库 MariaDB

安装LNMP环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#1.yum下载下来的所有rpm结尾的包
[root@web01 ~]# find /var/cache/yum/ -type f -name '*.rpm'

#2.开启yum缓存
[root@web01 ~]# vim /etc/yum.conf
[main]
keepcache=1

#3.添加nginx源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

#4.添加PHP源
[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

#5.安装nginx和php
[root@web01 ~]# 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 nginx

#6.将所有rpm包拷贝到opt目录下
[root@web01 ~]# find /var/cache/yum/ -type f -name '*.rpm'|xargs cp -t /opt/

#7.进入opt目录打包
[root@web01 ~]# cd /opt/
[root@web01 opt]# tar zcf nginx_php.tgz *.rpm

#依赖安装
yum install libevent-2.0.21-4.el7
yum install libmemcached-1.0.16-5.el7

#### 安装
[root@web02 nginx_php]# rpm -Uvh *.rpm
或者
[root@web02 nginx_php]# yum localinstall -y *.rpm

#8.安装数据库
[root@db01 ~]# yum install -y mariadb-server

#wordpress账号
用户名:admin
密码:OKaQ@LGQ4qkF7WtRu0

Nginx做代理服务器(lb01)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#1.安装nginx
[root@lb01 ~]# yum install -y nginx

#2.添加nginx代理配置文件
[root@lb01 ~]# vim /etc/nginx/conf.d/blog.roger.com_proxy.conf
server {
listen 80;
server_name blog.roger.com;

location / {
proxy_pass http://172.16.1.7:80;
}
}

#3.启动nginx并加入开机自启
[root@lb01 ~]# systemctl start nginx
[root@lb01 ~]# systemctl enable nginx

#4.域名解析
10.0.0.5 blog.roger.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#以上配置存在的问题
lb01 通过 172.16.1.7 80端口访问后端的web01
因为是通过IP访问,所以,配置文件谁在上面,则访问哪个页面

解决方案:将域名加入到,lb01请求web01的请求头中

[root@lb01 ~]# vim /etc/nginx/conf.d/blog.roger.com_proxy.conf
server {
listen 80;
server_name blog.roger.com;

location / {
proxy_pass http://172.16.1.7:80;
#在代理服务器中的请求头中,加上域名,携带域名去访问后端的web01服务器
proxy_set_header Host $host;
}
}

10.0.0.1 - - [14/Jun/2022:20:01:54 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://blog.roger.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36" "-"

172.16.1.5 - - [14/Jun/2022:20:01:54 +0800] "GET /favicon.ico HTTP/1.0" 404 555 "http://blog.roger.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36" "-"

#上面的配置依然存在问题
web01上的nginx日志,只显示lb01的服务器IP地址,无法显示用户的真实IP地址

解决方案:在lb01的请求头中,加上用户真实IP去访问web01

server {
listen 80;
server_name blog.roger.com;

location / {
proxy_pass http://172.16.1.7:80;
#在代理服务器的请求头中,加上域名,携带域名去访问后端的web01服务器
proxy_set_header Host $host;
#在代理服务器的请求头中,透传用户的真实IP地址给web01
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

#优化后的代理配置文件
server {
listen 80;
server_name blog.roger.com;

location / {
proxy_pass http://172.16.1.7:80;
#在代理服务器的请求头中,加上域名,携带域名去访问后端的web01服务器
proxy_set_header Host $host;
#在代理服务器的请求头中,透传用户的真实IP地址给web01
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#b01连接web01的超时时间(代理服务器,连接后端服务的超时时间)
proxy_connect_timeout 60s;
#lb01代理服务器读取web01返回的数据超时时间(代理后端的服务器响应代理服务器的超时时间)
proxy_read_timeout 60s;
#后端服务器回传给代理服务器数据的超时时间
proxy_send_timeout 60s;

#开启代理服务器的缓冲区,代理服务器接收到web01返回的数据,接收一条,返回给用户一条
proxy_buffering on;
#开启存放头部信息的缓冲区大小为 32k
proxy_buffer_size 32k;
#开启4个128k的存放数据主体的缓冲区
proxy_buffers 4 128k;
}
}

#避免配置文件重复使用
[root@lb01 ~]# vim /etc/nginx/conf.d/zh.roger.com_proxy.conf
server {
listen 80;
server_name blog.roger.com;

location /{
proxy_pass http://172.16.1.7:80;
include proxy_params;
}
}

[root@lb01 ~]# cat /etc/nginx/proxy_params
#在代理服务器的请求头中,加上域名,携带域名去访问后端的web01服务器
proxy_set_header Host $host;
#在代理服务器的请求头中,透传用户的真实IP地址给web01
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#lb01连接web01的超时时间(代理服务器,连接后端服务的超时时间)
proxy_connect_timeout 60s;
#lb01代理服务器读取web01返回的数据超时时间(代理后端的服务器响应代理服务器的超时时间)
proxy_read_timeout 60s;
#后端服务器回传给代理服务器数据的超时时间
proxy_send_timeout 60s;

#开启代理服务器的缓冲区,代理服务器接收到web01返回的数据,接收一条,返回给用户一条
proxy_buffering on;
#开启存放头部信息的缓冲区大小为 32k
proxy_buffer_size 32k;
#开启4个128k的存放数据主体的缓冲区
proxy_buffers 4 128k;

Nginx实现七层负载均衡

为什么要使用负载均衡

1.解决web服务器的单点故障,让web服务器做成一个集群

2.将请求平均下发给后端的web服务器

15663052248716

负载均衡的叫法

LB:Load Balance

SLB:Server Load Balance

公有云中的叫法

阿里云:SLB

腾讯云:CLB

青云:QLB(LB)

ucloud:ULB

AWS:ELB

负载均衡产品

  • 软件
    • Nginx
    • HAproxy
    • LVS
  • 硬件
    • F5

四层负载均衡和七层负载均衡的区别

1.一个是四层:传输层,一个是七层:应用层

2.四层传输速度要比七层快

3.四层无法识别域名,七层可以识别域名

负载均衡实现场景

Nginx要实现负载均衡需要用到proxy_pass代理模块配置.

Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池

15663059199814

负载均衡配置语法

1
2
3
4
5
6
7
8
Syntax:	upstream name { ... }
Default: —
Context: http

upstream name {
server xxx;
server xxx;
}

官方案例配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## upstream模块配置
模块名 后端主机池:名字(根据网站域名来起名)
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;

server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}

server {
location / {
proxy_pass http://backend;
}
}

upstream blog.zls.com {
server 172.16.1.7:8888;
server 172.16.1.8;
}

server {
location / {
proxy_pass http://blog.zls.com;
}
}

配置负载均衡

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 负载均衡 nginx
web01 10.0.0.7 172.16.1.7 web网站 nginx、php
web02 10.0.0.8 172.16.1.8 web网站 nginx、php

编辑nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[root@web01 conf.d]# vim /etc/nginx/conf.d/lb.roger.com.conf
server{
listen 9999;
server_name lb.roger.com;
root /code/lb;
index index.html;
}

[root@web02 conf.d]# vim /etc/nginx/conf.d/lb.roger.com.conf
server{
listen 9999;
server_name lb.roger.com;
root /code/lb;
index index.html;
}

[root@web01 conf.d]# mkdir /code/lb
[root@web02 conf.d]# mkdir /code/lb

[root@web01 conf.d]# echo 'web01' > /code/lb/index.html
[root@web02 conf.d]# echo 'web02' > /code/lb/index.html

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl reload nginx

[root@web02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 conf.d]# systemctl reload nginx


## 域名解析
10.0.0.7 lb.zls.com
#10.0.0.8 lb.zls.com

## 配置负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/lb.roger.com.conf
upstream lb.roger.com {
server 172.16.1.7:9999;
server 172.16.1.8:9999;
}

server {
listen 80;
server_name lb.roger.com;

location /{
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

负载均衡常见典型故障

如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500,这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500…等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## 解决方案
### 遇到如下状态码的机器,跳过请求的下发,直接下发到其他正常的服务器
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;

upstream lb.roger.com {
server 172.16.1.7:9999;
server 172.16.1.8:9999;
server 172.16.1.9:9999;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
include proxy_params;
}
}

负载均衡调度算法

调度算法 概述
轮询(rr) nginx做负载均衡默认使用轮询的调度算法:将请求平均下发到后端的web服务器
加权轮询(wrr) 增加权重,根据服务器的配置,给轮询加上权重
源IP(ip_hash) 根据用户的IP,将同一IP地址的请求,下发到同一台服务器上
源url(url_hash) 根据用户访问的URL,将同一URL的请求,下发到同一台服务器上
最小连接数(least_conn) 哪台服务器的连接数最少,就将请求下发到该服务器上

调度算法配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#1.加权轮询
upstream lb.roger.com {
server 172.16.1.7:9999 weight=5;
server 172.16.1.8:9999;
server 172.16.1.9:9999;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

#2.ip_hash
upstream lb.roger.com {
ip_hash;
server 172.16.1.7:9999;
server 172.16.1.8:9999;
server 172.16.1.9:9999;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

负载均衡后端状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#1.down状态:只是负载均衡不对该标识的服务器下发请求,后端的服务器并没有真正宕机
upstream lb.roger.com {
server 172.16.1.7:9999;
server 172.16.1.8:9999 down;
server 172.16.1.9:9999;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

#2.backup 状态:备份,当前其它没有backup标识机器都宕机时,才会给该服务器发请求
upstream lb.roger.com {
server 172.16.1.7:9999;
server 172.16.1.8:9999 backup;
server 172.16.1.9:9999;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

#3.额外参数
max_fails:负载均衡访问后端,最大错误次数,到该指定次数后,不给该服务器发送请求
fail_timeout:配合max_fails使用,规定不发请求的时间段
[root@lb01 ~]# vim /etc/nginx/conf.d/lb.roger.com.conf
upstream lb.roger.com {
server 172.16.1.7:9999 max_fails=3 fail_timeout=10s;
server 172.16.1.8:9999 max_fails=3 fail_timeout=10s;
server 172.16.1.9:9999 max_fails=3 fail_timeout=10s;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

#4.max_conn:限制该后端web服务器最大连接数为1024个
upstream lb.roger.com {
server 172.16.1.7:9999 max_fails=3 fail_timeout=10s;
server 172.16.1.8:9999 max_fails=3 fail_timeout=10s;
server 172.16.1.9:9999 max_fails=3 fail_timeout=10s max_conns=1024;
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}
}

Nginx负载均衡健康检查模块

作用:为了检测后端web的健康状态

项目地址:https://github.com/yaoweibin/nginx_upstream_check_module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#0.安装依赖
[root@lb01 nginx-1.22.0]# yum install -y pcre-devel openssl-devel

#1.停掉yum安装的nginx
[root@lb01 ~]# systemctl stop nginx

#2.下载nginx源码包
[root@lb01 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz

#3.下载nginx健康检查第三方模块
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

#4.解压nginx源码包和第三方模块包
[root@lb01 ~]# mkdir /app
[root@lb01 ~]# tar xf nginx-1.22.0.tar.gz
[root@lb01 ~]# unzip master.zip

#5.打补丁
[root@lb01 ~]# cd nginx-1.22.0/
[root@lb01 nginx-1.22.0]# yum install -y patch
[root@lb01 nginx-1.22.0]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.20.1+.patch

#6.生成
[root@lb01 nginx-1.22.0]# ./configure --prefix=/app/nginx-1.22.0 \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module --with-http_secure_link_module --with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module --with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
--add-module=/root/nginx_upstream_check_module-master

#7.编译 && 安装
[root@lb01 nginx-1.22.0]# make && make install

#8.nginx主配置文件,添加conf.d
[root@lb01 conf]# cat nginx.conf
#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include 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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

include /app/nginx-1.22.0/conf/conf.d/*.conf;
}

#9.创建虚拟主机配置文件存放目录
[root@lb01 conf]# mkdir /app/nginx-1.22.0/conf/conf.d

#10.编写负载均衡配置文件,添加location
[root@lb01 conf]# vim /app/nginx-1.22.0/conf/conf.d/lb.roger.com.conf
upstream lb.roger.com {
server 172.16.1.7:9999 max_fails=3 fail_timeout=10s;
server 172.16.1.8:9999 max_fails=3 fail_timeout=10s;
server 172.16.1.9:9999 max_fails=3 fail_timeout=10s max_conns=1024;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval 检测间隔时间,单位为毫秒
#rise 表示请求2次正常,标记此后端的状态为up
#fall 表示请求3次失败,标记此后端的状态为down
#type 类型为tcp
#timeout 超时时间,单位为毫秒
}

server {
listen 80;
server_name lb.roger.com;

location / {
proxy_pass http://lb.roger.com;
include proxy_params;
}

location /check_health {
check_status;
}
}

#11.语法检测
[root@lb01 conf]# /app/nginx-1.22.0/sbin/nginx -t

#12.启动nginx
[root@lb01 conf]# /app/nginx-1.22.0/sbin/nginx

image-20220621185641111

刚启动需要等一下

image-20220621185742189

Nginx七层负载-会话共享

会话保持相关信息存储

  • cookie
    • 前端开发人员将用户登录的信息,保存到浏览器中(开发者工具->Application->Cookies)
    • 如果仅将用户的登录信息记录在Cookie中,随时可以在浏览器中篡改
  • session
    • 后端开发人员,将用户登录信息记录在 服务器上(共享存储,某一个文件夹下的某个文件、数据库中、缓存数据库中….)session是对cookie做的加密,保存在服务器上

部署phpMyadmin

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 负载均衡 nginx
web01 10.0.0.7 172.16.1.7 phpmyadmin网站 nginx、php
web02 10.0.0.8 172.16.1.8 phpmyadmin网站 nginx、php
db01 10.0.0.51 172.16.1.51 数据库 MariaDB

部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#1.下载phpmyadmin代码
[root@web01 code]# wget http://test.driverzeng.com/Nginx_Code/phpMyAdmin-4.9.0.1-all-languages.zip

#2.解压代码
[root@web01 code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[root@web01 code]# ln -s phpMyAdmin-4.9.0.1-all-languages phpmyadmin

#3.添加nginx虚拟主机配置文件
[root@web01 code]# vim /etc/nginx/conf.d/php.roger.com.conf
server{
listen 80;
server_name php.roger.com;
root /code/phpmyadmin;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

[root@web02 code]# vim /etc/nginx/conf.d/php.roger.com.conf
server{
listen 80;
server_name php.roger.com;
root /code/phpmyadmin;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#4.修改代码连接数据库的配置文件
将站点目录下的案例配置文件改名
[root@web01 phpmyadmin]# cp config.sample.inc.php config.inc.php
[root@web01 phpmyadmin]# vim config.inc.php
将第31行的localhost改成自己数据库的ip地址
$cfg['Servers'][$i]['host'] = '172.16.1.51';

#5.web01上的代码发送到web02站点目录下
[root@web01 phpmyadmin]# scp -r /code/phpmyadmin 172.16.1.8:/code/

#6.nginx重启
[root@web01 phpmyadmin]# systemctl restart nginx
[root@web02 phpmyadmin]# systemctl restart nginx

报错:

存放session的目录没有权限

image-20220621215214005

1
2
[root@web01 phpmyadmin]# chown www.www /var/lib/php/session/
[root@web02 phpmyadmin]# chown www.www /var/lib/php/session/

image-20220621222731649

使用数据库的用户名和密码登录:之前的wordpress用户名和密码就可以使用

1
2
3
4
5
6
7
8
#1.连接数据库
[root@db01 ~]# mysql -uroot -p123

#2.创建一个数据库的新用户
MariaDB [(none)]> grant all on *.* to phpadmin@'%' identified by '123';

登录的用户名:phpadmin
登录的密码:123

添加phpmyadmin的负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#1.在负载均衡上添加nginx配置文件
[root@lb01 conf.d]# vim /etc/nginx/conf.d/php.roger.com_proxy.conf
upstream php.roger.com {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name php.roger.com;

location / {
proxy_pass http://php.roger.com;
include proxy_params;
}
}

#2.将域名解析到负载均衡
10.0.0.5 php.roger.com

报错

image-20220621223642842

用户的登录信息,session没有做共享

制作session共享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## 注意:redis端口:6379

#1.在db01上安装redis数据库
[root@db01 ~]# yum install -y redis

#2.修改redis配置文件
[root@db01 ~]# vim /etc/redis.conf
将第61行的bind后面IP地址改为 0.0.0.0
bind 0.0.0.0

#3.启动服务
[root@db01 ~]# systemctl start redis

#4.修改php程序配置文件
[root@web01 phpmyadmin]# vim /etc/php.ini
1231 session.save_handler = files
将以上内容改为以下内容
session.save_handler = redis

1265 ;session.save_path = "/tmp"
将以上内容改为以下内容
session.save_path = "tcp://172.16.1.51:6379"

1295 session.auto_start = 0
将以上内容改为以下内容
session.auto_start = 1

#5.修改php启动程序配置文件
[root@web01 phpmyadmin]# vim /etc/php-fpm.d/www.conf
源配置
398 php_value[session.save_handler] = files
399 php_value[session.save_path] = /var/lib/php/session

将以上两行内容使用';'注释
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session

#6.重启php
[root@web01 phpmyadmin]# systemctl restart php-fpm

#7.将改好的配置文件拷贝到web02
[root@web01 phpmyadmin]# scp /etc/php.ini 172.16.1.8:/etc/
[root@web01 phpmyadmin]# scp /etc/php-fpm.d/www.conf 172.16.1.8:/etc/php-fpm.d/

#8.重启web02上的php
[root@web02 phpmyadmin]# systemctl restart php-fpm

报错502

后端的服务无法建立连接

image-20220621231855403

找不到网站标签上的图标文件

image-20220621231944835

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 报错原因:
连接后端upstream被拒绝
172.16.1.7 和 172.16.1.8 的80端口拒绝

# 排查流程
#1.检查IP
[root@lb01 ~]# ping 172.16.1.7
ping icmp协议

#2.检查端口
[root@lb01 ~]# telnet 172.16.1.7 80
Trying 172.16.1.7...
telnet: connect to address 172.16.1.7: Connection refused

1)upstream配置的端口和后端的nginx启动的端口不一致
upstream php.roger.com {
server 172.16.1.7;
server 172.16.1.8;
}

listen 80;

2)nginx服务没有启动(没起80端口)
[root@web01 ~]# ps -ef|grep nginx
root 30074 21443 0 22:27 pts/2 00:00:00 grep --color=auto nginx
[root@web01 ~]# netstat -lntup|grep nginx
[root@web01 ~]# netstat -lntup|grep 80

image-20220621232047274

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## 连接数据库需要的信息:
数据库的IP地址:172.16.1.51
数据库的用户名:wp_user
数据库的密码:123

-----
数据库的库名:wordpress

## 排错流程:
哪个服务要连接数据库,该服务装在哪,确认该服务使用哪个IP地址连接数据库
php的代码服务(wordpress)要连接数据库,装在web上,查看对应程序的连接数据库配置文件找到IP

1)检查IP
[root@web01 wordpress]# ping 172.16.1.51

2)检查端口
[root@web01 wordpress]# telnet 172.16.1.51 3306
Trying 172.16.1.51...
telnet: connect to address 172.16.1.51: Connection refused

配置的端口和后端数据库启动的端口不一致
数据库服务没有启动
[root@db01 ~]# systemctl start mariadb

3)检查数据库的用户名和密码
- 检查用户的权限,允不允许web的ip连接
MariaDB [(none)]> select user,host from mysql.user;
+----------+-------------+
| user | host |
+----------+-------------+
| wp_user | 172.16.1.% | 允许
| wp_user | 172.16.1.5% |
+----------+-------------+
- 检查密码
在web服务器上,安装数据库客户端命令:mysql
[root@web01 ~]# yum install -y mariadb
[root@web01 ~]# mysql -uwp_user -p123 -h172.16.1.51

image-20220621234911565

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
redis的IP地址:172.16.1.51

1)检查IP
[root@web01 ~]# ping 172.16.1.51

2)检查端口
[root@web01 ~]# telnet 172.16.1.51 6379
Trying 172.16.1.51...
telnet: connect to address 172.16.1.51: Connection refused

3)服务启动,再检查该端都让哪些IP连接
[root@db01 ~]# ps -ef|grep redis
redis 29523 1 0 22:52 ? 00:00:00 /usr/bin/redis-server 127.0.0.1:6379
root 29527 25636 0 22:52 pts/0 00:00:00 grep --color=auto redis
[root@db01 ~]# netstat -lntup|grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 29523/redis-server

[root@db01 ~]# grep -n '^bind' /etc/redis.conf
48:bind 0.0.0.0
61:bind 127.0.0.1

image-20220621234945720

1
2
3
4
问题:读取不到redis中的session信息
解决:检查php的配置
[root@web01 ~]# vim /etc/php.ini
[root@web01 ~]# vim /etc/php-fpm.d/www.conf

Nginx四层负载

什么是四层负载均衡

OSI七层模型中,四层是传输层,传输层使用端口到端口的通信方式

四层负载均衡,就是在传输层做端口的转发(端口映射)

注意:四层负载不识别域名

四层负载应用场景

1.四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;如:nginx就无法保证自己的服务高可用,需要依赖LVS或者keepalive

2.如:tcp协议的负载均衡,有些请求是TCP协议的(mysql、ssh),或者说这些请求只需要使用四层进行端口的转发就可以了,所以使用四层负载均衡

15663853490233

Nginx四层负载

负载均衡软件:

  • Nginx
    • 四层负载(nginx 1.9版本以后有stream模块,才可以做四层负载)
      • stream
    • 七层负载
      • upstream
  • LVS
    • 四层负载
  • HAproxy
    • 四层负载
    • 七层负载

四层负载均衡配置

环境准备

主机名 WANIP LANIP 角色 应用
lb01 10.0.0.5 172.16.1.5 七层负载 nginx
lb02 10.0.0.6 172.16.1.6 四层负载 nginx
web01 10.0.0.7 172.16.1.7 web网站 nginx、php
web02 10.0.0.8 172.16.1.8 web网站 nginx、php
db01 10.0.0.51 172.16.1.51 数据库 MariaDB

部署四层负载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#1.添加nginx官方源
[root@lb02 ~]# 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

#2.安装nginx
[root@lb02 ~]# yum install -y nginx

#3.配置四层负载
stream {
upstream backend {
server 172.16.1.5:80;
}

server {
listen 90;
proxy_pass backend;
}
}

http {
...
}

使用stream做端口转发

实现22端口转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#在10.0.0.6机器上开456端口,映射到10.0.0.8的22端口
ssh 10.0.0.6 -p 456

#主配置文件,添加include
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
...
}

include /etc/nginx/stream.d/*.conf;

http {
...
}

#实现456映射22端口需求
[root@lb02 nginx]# vim /etc/nginx/stream.d/456_22.conf
#stream模块
stream {
#定义主机池
upstream web02_ssh {
server 172.16.1.8:22;
}

server {
listen 456;
proxy_pass web02_ssh;
}

upstream backend {
server 172.16.1.5:80;
}

server {
listen 90;
proxy_pass backend;
}
}

映射数据库端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
stream {
upstream web02_ssh {
server 172.16.1.8:22;
}
server {
listen 456;
proxy_pass web02_ssh;
}

upstream backend {
sercer 172.16.1.5:80
}
server {
listen 90;
proxy_pass backend;
}

upstream db01_mysql {
server 172.16.1.51:3306;
}
server {
listen 3307;
proxy_pass db01_mysql;
}
}

Nginx动静分离和资源分离

动静分离

Q:什么是静态资源?

类似于 .jpg、.png、.css、.js….不需要访问数据库的资源,属于静态资源

Q:什么是动态资源?

需要访问数据库的代码文件,.php、.jsp、.py….

Q:什么是静态请求?

用户发起的请求只访问前端资源,不访问数据库

Q:什么是动态请求?

用户发起的请求访问后端资源,访问数据库

注意:不是页面会动,就一定是动态请求

Q:什么是动静分离?

又叫做前后端分离,将前端代码和后端代码区分开,前端代码,前端开发人员来写,后端代码,后端的开发人员来写

前端和后端建立连接使用AJAX

15664680392966

实践动静分离

主机名 WanIP LanIP 角色 应用
web01 10.0.0.7 172.16.1.7 代理 nginx
web02 10.0.0.8 172.16.1.8 静态服务器 nginx
web03 10.0.0.9 172.16.1.9 动态服务器 tomcat

部署前端(静态页面)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#1.安装nginx服务
[root@web01 ~]# vim /etc/yum.repos.d/ngx.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

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

[root@web02 ~]# vim /etc/yum.repos.d/ngx.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

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

#2.配置nginx静态资源配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/static.conf
server {
listen 80;
server_name pic.roger.com;
root /code;
index index.html;
charset utf-8;

location ~* .*\.(jpg|png|gif)$ {
root /code/images;
}
}

#3.检查语法,启动nginx
[root@web02 ~]# nginx -t
[root@web02 ~]# systemctl restart nginx

#4.域名解析
10.0.0.8 pic.drz.com

#5.创建站点目录
[root@web02 ~]# mkdir -p /code/images

#6.部署前端代码
[root@web02 ~]# echo '这是静态资源页面' > /code/index.html

#7.浏览器访问页面

#8.上传图片到/code/images
[root@web02 images]# ll
total 224
-rw-r--r-- 1 root root 228072 Jun 8 11:41 1.jpg
[root@web02 images]# pwd
/code/images

部署后端(动态页面)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#1.安装JAVA环境
[root@web03 ~]# yum install -y tomcat

#2.启动tomcat
[root@web03 ~]# systemctl start tomcat

#3.查看端口
[root@web03 ~]# netstat -lntup|grep java
tcp6 0 0 :::8009 :::* LISTEN 12611/java
tcp6 0 0 :::8080 :::* LISTEN 12611/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 12611/java

#4.tomcat站点目录
[root@web03 ~]# rpm -ql tomcat
/usr/share/tomcat/webapps

#5.部署后端代码,需要在站点目录下创建一个ROOT目录,将代码部署在ROOT目录下
[root@web03 webapps]# mkdir /usr/share/tomcat/webapps/ROOT
[root@web03 webapps]# vi /usr/share/tomcat/webapps/ROOT/test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>曾老湿JSP Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>曾老湿随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>

#6.打开浏览器访问:http://10.0.0.9:8080/test.jsp

image-20220622231117732

在代理上整合资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#1.编辑代理配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/proxy.conf
upstream static {
server 172.16.1.8:80;
}

upstream java {
server 172.16.1.9:8080;
}

server {
listen 80;
server_name pic.roger.com;

location ~* \.(jpg|png|gif)$ {
proxy_pass http://static;
proxy_set_header Host $http_host;
}

location ~ \.jsp {
proxy_pass http://java;
proxy_set_header Host $http_host;
}
}

#2.启动nginx
[root@web01 ~]# systemctl start nginx

#3.域名解析
10.0.0.7 pic.roger.com

#4.浏览器访问:http://pic.roger.com/1.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#1.修改nginx代理配置文件 加 location /
[root@web01 ~]# vim /etc/nginx/conf.d/proxy.conf
upstream static {
server 172.16.1.8:80;
}

upstream java {
server 172.16.1.9:8080;
}

server {
listen 80;
server_name pic.roger.com;

location / {
root /code;
index index.html;
}

location ~* \.(jpg|png|gif)$ {
proxy_pass http://static;
proxy_set_header Host $http_host;
}

location ~ \.jsp {
proxy_pass http://java;
proxy_set_header Host $http_host;
}
}

#2.创建站点目录
[root@web01 ~]# mkdir /code

#3.编写资源整合代码
[root@web01 ~]# vim /code/index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Roger测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://pic.roger.com/test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>Roger带你测试动静分离</h1>
<img src="http://pic.roger.com/33_cc.jpg">
<div id="get_data"></div>
</body>
</html>

#4.重启nginx
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx

#5.打开浏览器访问:http://pic.roger.com/

Nginx实现资源分离

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 代理 nginx
web01 10.0.0.7 172.16.1.7 PC端页面 nginx、PC端的代码
web02 10.0.0.8 172.16.1.8 安卓端页面 nginx、安卓端的代码
web03 10.0.0.9 172.16.1.9 IOS端页面 nginx、IOS端的代码

部署PC端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#1.编写PC端nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/pc.conf
server {
listen 9090;
server_name pc.roger.com;
charset utf-8;

location / {
root /code/pc;
index index.html;
}
}

#2.创建站点目录
[root@web01 ~]# mkdir /code/pc

#3.部署pc端代码
[root@web01 ~]# echo '这里是PC端页面' > /code/pc/index.html

#4.重新启动nginx
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx

#5.域名解析
10.0.0.7 pc.roger.com

#6.浏览器访问:http://pc.roger.com:9090/

部署安卓端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#1.编写安卓端nginx配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/andorid.conf
server {
listen 9091;
server_name andorid.zls.com;
charset utf-8;

location / {
root /code/andorid;
index index.html;
}
}

#2.创建站点目录
[root@web02 ~]# mkdir /code/andorid

#3.部署安卓端代码
[root@web02 ~]# echo '这里是andorid页面' > /code/andorid/index.html

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

#5.域名解析
10.0.0.8 andorid.zls.com

#6.浏览器访问:http://andorid.zls.com:9091/

部署IOS页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#1.编写ios页面nginx配置文件
[root@web03 ~]# vim /etc/nginx/conf.d/ios.conf
server {
listen 9092;
server_name ios.zls.com;
charset utf-8;

location / {
root /code/ios;
index index.html;
}
}

#2.创建站点目录
[root@web03 ~]# mkdir /code/ios

#3.部署IOS代码
[root@web03 ~]# echo '这里是IOS页面' > /code/ios/index.html

#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.域名解析
10.0.0.9 ios.zls.com

#6.浏览器访问:http://ios.zls.com:9092

资源分离配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@lb01 ~]# vim /etc/nginx/conf.d/source.conf
upstream pc {
server 172.16.1.7:9090;
}

upstream android {
server 172.16.1.8:9091;
}

upstream ios {
server 172.16.1.9:9092;
}

server {
listen 80;
server_name www.roger.com;
charset 'utf-8';

location / {
#如果客户端来源是Android则跳转到Android的资源;
if ($http_user_agent ~* "Android") {
proxy_pass http://android;
}

#如果客户端来源是Iphone则跳转到Iphone的资源;
if ($http_user_agent ~* "Iphone") {
proxy_pass http://ios;
}

#如果客户端是IE浏览器则返回403错误;
if ($http_user_agent ~* "(MSIE|WOW64)") {
return 403;
}

#默认跳转pc资源;
proxy_pass http://pc;
}
}

Nginx实现Rewrite重写

Q:什么是rewrite?

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程

做伪静态,将动态页面url转换成静态的页面url

Rewrite使用场景

  • 地址跳转
  • 协议跳转
  • 伪静态
    • 将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性
    • 搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入

伪静态的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
句法:Syntax:  rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if

rewrite:模块
regex:正则表达式(匹配当前的url)
replacement:要替换成的url

rewrite http://blackgoatking.com http://www.blackgoatking.com;

#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;

如果懂shell脚本的,这两个就类似于脚本中的,breakcontinue

rewrite的flag

概述 flag
匹配到last的规则后可以继续匹配后面的location last
匹配到break的规则后,无法再匹配后面的location break
302临时重定向 redirect
301永久重定向 permanent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#redirect临时重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /test {
rewrite ^(.*)$ https://www.baidu.com redirect;
}
}

server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /test {
#rewrite ^(.*)$ https://www.baidu.com redirect;
return 302 http://baidu.com;
}
}

#重新加载nginx
[root@web01 ~]# systemctl reload nginx

#域名解析
10.0.0.7 rewrite.roger.com

image-20220624224201082

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#permanent永久重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /test {
rewrite ^(.*)$ https://www.baidu.com permanent;
}
}

server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /test {
#rewrite ^(.*)$ https://www.baidu.com permanent;
return 301 https://www.baidu.com;
}
}

rewrite实践

开启rewrite日志

1
2
3
4
5
6
7
8
9
10
11
#开启rewrite日志,错误日志的级别要改成 notice,在http层加上rewrite_log on;
[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;

http{
rewrite_log on;
...
}

#重启nginx
[root@web01 nginx]# systemctl restart nginx

案例一

用户访问 /abc/1.html 实际上真实访问的是 /ccc/bbb/2.html

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /abc/1.html {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
}
}

案例二

用户访问/2018/ccc/2.html实际上真实访问的是/2014/ccc/bbb/2.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## rewrite写死。。。
server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /2018/ccc {
rewrite ^(.*)$ /2014/ccc/bbb/2.html redirect;
}
}

## 正则后向引用匹配
[root@web01 nginx]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /2018 {
rewrite ^/2018/(.*) /2014/$1 redirect;
}
}

案例三

用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

1
2
3
4
5
6
7
8
9
10
11
[root@web01 nginx]# vim /etc/nginx/conf.d/rewrite.conf 
server {
listen 80;
server_name rewrite.roger.com;
root /code;
index index.html;

location /course {
rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;
}
}

案例四(☆☆☆☆☆)

80端口强制跳转443端口

1
2
3
4
5
6
server {
listen 80;
server_name www.dirverzeng.com;
rewrite ^(.*) https://$server_name redirect;
#return 302 https://$server_name$request_uri;
}

rewrite做wordpress伪静态

1
2
3
4
5
6
7
8
9
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( -f $request_filename/index.php ){
rewrite (.*) $1/index.php;
}
if ( !-f $request_filename ){
rewrite (.*) /index.php;
}

image-20220626104549197

rewrite做Discuz伪静态

image-20220626105746637

image-20220626105809597

image-20220626105828849

image-20220626105857646

image-20220626105925078

image-20220626105944335

image-20220626110000320

1
2
3
4
5
6
7
8
9
10
11
12
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
if (!-e $request_filename) {
return 404;
}

image-20220626110019213

Keepalived负载均衡高可用

Keepalived概述

Keepalived是一个高可用软件,可以和任何应用配合使用

Q:什么是高可用?

一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的

高可用软件

  • 硬件

    • F5
  • 软件

    • keepalived
    • heartbeat
    • MySQL
    • MGR
    • MHA
    • Redis
    • Redis-Cluster
    • Sentine

keepalived实现原理

keepalived底层协议:VRRP(虚拟路由冗余协议)

高可用keepalived使用场景

通常业务系统需要保证7×24小时不DOWN机,比如公司内部的OA系统,每天公司人员都需要使用,则不允许Down机,作为业务系统来说随时都可用

15706088579575

keepalived核心概念

  1. 通过选举投票,决定谁是主节点谁是备节点(选举)

  2. 如果Master故障,Backup自动接管,那么Master恢复后会夺权吗(抢占试、非抢占式)

  3. 两台服务器都认为自己是master,那么会出现一个故障(脑裂)

keepalived安装配置

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 Master keepalived主节点 keepalived
lb02 10.0.0.6 172.16.1.6 Backup keepalived备节点 keepalived

部署keepalived

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#1.安装keepalived 
[root@lb01 ~]# yum install -y keepalived
[root@lb02 ~]# yum install -y keepalived

#2.查找keepalived配置文件
[root@lb01 ~]# rpm -ql keepalived /etc/keepalived/keepalived.conf

#3.修改Master配置文件
[root@lb01 ~]# vim /etc/keepalived/keepalived.conf
global_defs { #全局配置
router_id lb01 #标识身份->名称
}

vrrp_instance VI_1 {
state MASTER #标识角色状态
interface eth0 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 150 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.0.0.3 #虚拟的VIP地址
}
}

#4.修改Backup配置文件
[root@lb02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
Keepalived配置区别 Master节点配置 Backup节点配置
router_id lb01 lb02
state MASTER BACKUP
priority 150 100
1
2
3
4
5
6
7
#1.启动master上的keepalived 
[root@lb01 ~]# systemctl start keepalived
[root@lb01 ~]# systemctl enable keepalived

#2.启动backup上的keepalived
[root@lb02 ~]# systemctl start keepalived
[root@lb02 ~]# systemctl enable keepalived

注意:只要停止掉Keepalived,VIP会漂移到另外一个节点

非抢占式配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
## 配置需求 
1、两个节点的state都必须配置为BACKUP
2、两个节点都必须加上配置 nopreempt
3、其中一个节点的优先级必须要高于另外一个节点的优先级

## master节点配置
[root@lb01 ~]# vim /etc/keepalived/keepalived.conf
global_defs { #全局配置
router_id lb01 #标识身份->名称
}

vrrp_instance VI_1 {
state BACKUP #标识角色状态
interface eth0 #网卡绑定接口
nopreempt
virtual_router_id 50 #虚拟路由id
priority 150 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.0.0.3 #虚拟的VIP地址
}
}

## BACKUP节点配置
[root@lb02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}

脑裂的原因

  1. 服务器网线松动等网络故障
  2. 服务器硬件故障发生损坏现象而崩溃
  3. 主备都开启Firewalld防火墙
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#解决脑裂故障 
[root@lb02 ~]# vim check_split_brain.sh
#!/bin/sh
vip=10.0.0.3
lb01_ip=10.0.0.5

while true;do
ping -c 2 $lb01_ip &>/dev/null
if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then
echo "ha is split brain.warning."
else
echo "ha is ok"
fi
sleep 5
done

keepalive结合nginx做高可用

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 Master keepalived主节点、nginx负载均衡 keepalived、nginx
lb02 10.0.0.6 172.16.1.6 Backup keepalived备节点、nginx负载均衡 keepalived、nginx
web01 10.0.0.7 172.16.1.7 web网站 nginx、php
web02 10.0.0.8 172.16.1.8 web网站 nginx、php

关联nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
## 公司使用脚本 
[root@lb01 ~]# vim check_web.sh
#!/bin/sh
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginx_count -eq 0 ];then
systemctl start nginx
sleep 3
#2.等待3秒后再次获取一次Nginx状态
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
#3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本
if [ $nginx_count -eq 0 ];then
systemctl stop keepalived
fi
fi

## 上课使用脚本
# 1.写检测nginx健康状态的脚本
[root@lb01 ~]# vim check_web.sh
#!/bin/sh
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginx_count -eq 0 ];then
systemctl stop keepalived
fi

先配置两台负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
## web01 
upstream blog_roger_com {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name blog.roger.com;
rewrite (.*) https://blog.roger.com;
}

server{
listen 443 ssl;
server_name blog.roger.com;
ssl_certificate ssl/20220623_blog.roger.com.pem;
ssl_certificate_key ssl/20220623_blog.roger.com.key;

location / {
proxy_pass http://blog_roger_com;
proxy_set_header Host $host;
}
}

## web02
upstream blog_roger_com {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name blog.roger.com;
rewrite (.*) https://blog.roger.com;
}

server{
listen 443 ssl;
server_name blog.roger.com;
ssl_certificate ssl/20220623_blog.roger.com.pem;
ssl_certificate_key ssl/20220623_blog.roger.com.key;

location / {
proxy_pass http://blog_roger_com;
proxy_set_header Host $host;
}
}

[root@lb02 ~]# mkdir /etc/nginx/ssl
[root@lb01 ~]# scp /etc/nginx/ssl/* 172.16.1.6:/etc/nginx/ssl

keepalived关联nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#修改keepalived配置文件 
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
vrrp_script check_web_roger {
script "/root/check_web.sh"
interval 5
}

vrrp_instance VI_1 {
...
track_script {
check_web_roger
}
}

#给脚本加执行权限
[root@lb01 ~]# chmod +x /root/check_web.sh

#域名解析在vip上
10.0.0.3 blog.roger.com

注意:在Master的keepalived中调用脚本,抢占式,仅需在master配置即可,如果配置为非抢占式,那么需要两台服务器都使用该脚本

Nginx常见问题

nginx多server优先级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## 优先级匹配顺序 
1.首先选择所有的字符串完全匹配(精确匹配)的server_name。(完全匹配)
2.选择通配符在前面的server_name
3.选择通配符在后面的server_name
4.正则表达式的server_name
5.所有匹配规则相同时,哪个配置文件listen...后面加了default_server哪个优先级就最高
6.按照配置文件的顺序访问第一个配置文件

[root@web01 conf.d]# vim 1.conf
server {
listen 80;
server_name (blog|www).zls.com;
root /code/1; index index.html;
}

[root@web01 conf.d]# cat 2.conf
server {
listen 80;
server_name www.zls.com;
root /code/2; index index.html;
}

[root@web01 conf.d]# cat 3.conf
server {
listen 80;
server_name *.zls.com;
root /code/3; index index.html;
}

禁止IP访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## 禁止IP访问,并返回错误页面 
server {
listen 80 default_server;
server_name _;
charset utf-8;
default_type text/json;
return 500 "页面500 ,给爷爬~";
}

server {
listen 80;
server_name www.zls.com;
root /code/2;
index index.html;
}

## 禁止IP访问,并跳转到主站点
server {
listen 80 default_server;
server_name _;
charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}

server {
listen 80;
server_name www.zls.com;
root /code/2;
index index.html;
}

nginx包含其他子配置文件include

1
2
3
4
5
6
7
8
include /etc/ngxinx/stream.d/*.conf 
include /etc/nginx/fastcgi_params
include /etc/nginx/proxy_parmas #自己创建的 代理优化参数

## 需要生效的配置文件
[root@web01 conf.d]# mkdir /etc/nginx/conf.d/online #不需要生效的配置文件
[root@web01 conf.d]# mkdir /etc/nginx/conf.d/offline #修改nginx主配置文件
include /etc/nginx/conf.d/online/*.conf;

站点目录路径root和alias区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
## root指定站点目录 
vim /etc/nginx/conf.d/online/2.conf
server {
listen 80 default_server;
server_name _; charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}

server {
listen 80;
server_name www.zls.com;

location / {
root /code/2;
index index.html;
}

location ~ /images {
root /code/images;
}
}
## 图片路径:/code/images/images/1.png

## alias指定站点目录
vim /etc/nginx/conf.d/online/2.conf
server {
listen 80 default_server;
server_name _;
charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}
server {
listen 80;
server_name www.zls.com;
root /code/2;
index index.html;

location /images {
alias /images;
}
}

location /images {
alias /code/images;
}
## /code/images/1.png

location /images {
root /code/images;
}
## /code/images/images/1.png

nginx try_file路径匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@web01 code]# vim /etc/nginx/conf.d/online/2.conf
server {
listen 80 default_server;
server_name _;
charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}

server {
listen 80;
server_name www.zls.com;
root /code;
index index.html;

location / {
try_files $uri $uri/ @zls;
}

location @zls {
proxy_pass http://172.16.1.8:8080;
}
}

nginx调整上传文件的大小

1
2
3
Syntax: client_max_body_size size; 
Default: client_max_body_size 1m;
Context: http, server, location

nginx优雅的显示404错误页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## 跳转页面 
vim /etc/nginx/conf.d/online/2.conf
server {
listen 80 default_server;
server_name _;
charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}

server {
listen 80;
server_name www.zls.com;

location / {
root /code;
index index.html;
error_page 404 http://blog.driverzeng.com;
}
}

## 前端页面
[root@web01 code]# vim /etc/nginx/conf.d/online/2.conf
server {
listen 80 default_server;
server_name _;
charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}

server {
listen 80;
server_name www.zls.com;

location / {
root /code;
index index.html;
error_page 404 /404.html;
}
}

[root@web01 code]# cat /code/404.html
<img style='width:100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/404_page.png>

[root@web01 code]# cat /code/404.html
<html>
<head>
<meta charset="utf-8">
<title>xxxx错误页面</title>
</head>
<body>
<center>
<h1 style='background-color:red'>给爷爬~ 瞎访问,没有这个页面</h1>
</center>
<img style='width:100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/404_page.png>
</body>
</html>

隐藏nginx版本号

1
2
3
4
http {
server_tokens off;
...
}

图片防盗链

服务器上配置防盗链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#以下配置含义表示,所有来自*.driverzeng.com都可以访问到当前站点的图片,如果来源域名不在这个列表中,那么$invalid_referer等于1,在if语句中返回一个403个客户,这样用户便会看到一个403的页面
location ~ .*\.(jpg|png|gif) {
root /var/www/wordpress/wp-content/extra/;
valid_referers none blocked *.driverzeng.com;
if ( $invalid_referer ) {
return 403;
}
}

#如果不使用return而是用rewrite,那么盗链图片会返回一个pei.jpg给用户
## 防盗链demo
location ~ .*\.(jpg|png|gif) {
root /var/www/wordpress/wp-content/extra/;
valid_referers none blocked *.driverzeng.com;
if ( $invalid_referer ) {
rewrite ^(.*)$ /Picture/daolian1.gif break;
}
}

## 如果希望某些网站可以盗链
location ~ .*\.(jpg|png|gif) {
root /data;
valid_referers none blocked *.driverzeng.com server_name ~\.google\. ~\.baidu\.;
if ( $invalid_referer ) {
return 403;
}
}

#当然这种防护并不能百分百保证资源不被盗链,因为我们可以通过命令来修改来源的refer信息
## 伪造协议头访问
[root@web01 code]# curl -e "https://www.baidu.com" -I http://test.driverzeng.com/Picture/daolian.jpg
HTTP/1.1 403 Forbidden
Server: nginx
Date: Thu, 10 Oct 2019 09:01:05 GMT
Content-Type: text/html; charset=utf-8,gbk
Content-Length: 162
Connection: keep-alive

## 伪造协议头访问
[root@web01 code]# curl -e "https://blog.driverzeng.com" -I http://test.driverzeng.com/Picture/daolian.jpg
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 10 Oct 2019 09:01:35 GMT
Content-Type: image/jpeg
Content-Length: 556417
Last-Modified: Thu, 10 Oct 2019 07:14:19 GMT
Connection: keep-alive
ETag: "5d9eda4b-87d81"
Accept-Ranges: bytes

#模拟盗链实例
## 配置被盗链机器
10.0.0.8 static.drz.com

#1.配置Nginx
[root@web02 conf.d]# cat static.conf
server {
listen 80;
server_name static.drz.com;
root /code;

location / {
index index.html;
}
}

#2.上传2张图片
一张是可以被盗链的图片
一张是广告位的图片

#3.重启服务器
[root@web02 code]# systemctl restart nginx

## 另一台机器盗链
10.0.0.7 dl.drz.com ###盗链服务器

#1.配置Nginx
[root@web01 conf.d]# cat try.conf
server {
server_name dl.drz.com;
listen 80;
root /code;
location / {
index index.html;
}
}

#2.配置盗链的页面
[root@web01 code]# cat /code/tt.html
<html>
<head>
<meta charset="utf-8">
<title>drzedu.com</title>
</head>
<body style="background-color:pink;">
<img src="http://static.drz.com/daolian.jpg"/> #根据情况修改你的服务器地址
</body>
</html>

#添加防盗链
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.drz.com;
if ($invalid_referer) {
return 403; #可以选择直接返回403
rewrite ^(.*)$ /daolian.png break; #也可以选择返回一张水印的图片,给公司做广告
}

允许跨域访问

什么是跨域访问,当我们通过浏览器访问a网站时,同事会利用到ajax或其他方式,同时也请求b网站,这样的话就出现了请求一个页面,使用了两个域名,这种方式对浏览器来说默认是禁止的。

截屏2023-02-23 16.37.16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#那Nginx语序跨站访问与浏览器有什么关系呢,因为浏览器会读取Access-Control-Allow-Origin的头信息,如果服务端允许,则浏览器不会进行拦截。

Syntax: add_header name value [always];
Default: -;
Context: http, server, location, if in location

#在网站上准备跨域访问的文件
#编辑nginx配置文件
[root@web02 code]# vim /etc/nginx/conf.d/s.conf
server {
listen 80;
server_name s.drz.com;
location / {
root /code;
index index.html;
}
}

#编辑html文件
[root@Nginx ~]# cat /code/tt.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>曾老湿测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://www.drz.com",
success: function(data) {
alert("sucess 卧槽 卧槽 卧槽 成功了!!!");
},
error: function() {
alert("fail!!,跨不过去啊,不让进去啊,只能蹭蹭!");
}
});
});
</script>
<body>
<h1>曾老湿测试跨域访问</h1>
</body>
</html>

截屏2023-02-23 16.39.31

1
2
3
4
5
6
7
8
9
10
11
12
13
#在被关联的网站配置
server {
listen 80;
server_name www.drz.com;
root /code;
index index.html;
charset utf-8;

location ~ .*\.(html|htm)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
}
}

截屏2023-02-23 16.40.39

PHP服务优化

php程序配置管理文件/etc/php.ini,主要调整日志、文件上传、禁止危险函数、关闭版本号显示等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#;;;;;;;;;;;;;;;;;
# Error logging ; #错误日志设置
#;;;;;;;;;;;;;;;;;
expose_php = Off # 关闭php版本信息
display_error = Off # 屏幕不显示错误日志
error_reporting = E_ALL # 记录PHP的每个错误
log_errors = On # 开启错误日志
error_log = /var/log/php_error.log # 错误日志写入的位置
date.timezone = Asia/Shanghai # 调整时区,默认PRC

#;;;;;;;;;;;;;;;
# File Uploads ; #文件上传设置
#;;;;;;;;;;;;;;;
file_uploads = On # 允许文件上传
upload_max_filesize = 300M # 允许上传文件的最大大小
post_max_size = 300M # 允许客户端单个POST请求发送的最大数据
max_file_uploads = 20 # 允许同时上传的文件的最大数量
memory_limit = 128M # 每个脚本执行最大内存

[Session] #会话共享
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"

#php禁止危险函数执行(取决于实际情况,需要和开发沟通)
disable_functions = chown,chmod,pfsockopen,phpinfo

禁用php的危险函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
phpinfo() 
功能描述:输出 PHP 环境信息以及相关的模块、WEB 环境等信息。
危险等级:中

passthru()
功能描述:允许执行一个外部程序并回显输出,类似于 exec()。
危险等级:高

exec()
功能描述:允许执行一个外部程序(如 UNIX Shell 或 CMD 命令等)。
危险等级:高

system()
功能描述:允许执行一个外部程序并回显输出,类似于 passthru()。
危险等级:高

chroot()
功能描述:可改变当前 PHP 进程的工作根目录,仅当系统支持 CLI 模式
PHP 时才能工作,且该函数不适用于 Windows 系统。
危险等级:高

scandir()
功能描述:列出指定路径中的文件和目录。
危险等级:中

chgrp()
功能描述:改变文件或目录所属的用户组。
危险等级:高

chown()
功能描述:改变文件或目录的所有者。
危险等级:高

shell_exec()
功能描述:通过 Shell 执行命令,并将执行结果作为字符串返回。
危险等级:高

proc_open()
功能描述:执行一个命令并打开文件指针用于读取以及写入。
危险等级:高

proc_get_status()
功能描述:获取使用 proc_open() 所打开进程的信息。
危险等级:高

error_log()
功能描述:将错误信息发送到指定位置(文件)。
安全备注:在某些版本的 PHP 中,可使用 error_log() 绕过 PHP safe mode,
执行任意命令。
危险等级:低

ini_alter()
功能描述:是 ini_set() 函数的一个别名函数,功能与 ini_set() 相同。
具体参见 ini_set()。
危险等级:高

ini_set()
功能描述:可用于修改、设置 PHP 环境配置参数。
危险等级:高

ini_restore()
功能描述:可用于恢复 PHP 环境配置参数到其初始值。
危险等级:高

dl()
功能描述:在 PHP 进行运行过程当中(而非启动时)加载一个 PHP 外部模块。
危险等级:高

pfsockopen()
功能描述:建立一个 Internet 或 UNIX 域的 socket 持久连接。
危险等级:高

syslog()
功能描述:可调用 UNIX 系统的系统层 syslog() 函数。
危险等级:中

readlink()
功能描述:返回符号连接指向的目标文件内容。
危险等级:中

symlink()
功能描述:在 UNIX 系统中建立一个符号链接。
危险等级:高

popen()
功能描述:可通过 popen() 的参数传递一条命令,并对 popen() 所打开的文件进行执行。
危险等级:高

stream_socket_server()
功能描述:建立一个 Internet 或 UNIX 服务器连接。
危险等级:中

putenv()
功能描述:用于在 PHP 运行时改变系统字符集环境。在低于 5.2.6 版本的 PHP 中,可利用该函数
修改系统字符集环境后,利用 sendmail 指令发送特殊参数执行系统 SHELL 命令。
危险等级:高

禁用方法如下:
打开/etc/php.ini文件,
查找到 disable_functions ,添加需禁用的函数名,如下:
phpinfo,eval,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,fsockopen

编写配置文件测试优化参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@web01 conf.d]# cat php.conf 
server {
listen 80;
server_name php.drz.com;
root /code;

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;
}
}
[root@web01 conf.d]# cat /code/index.php
<?php
phpinfo();
?>

php-fpm进程管理配置文件/etc/php-fpm.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#第一部分,fpm配置
;include=etc/fpm.d/*.conf

#第二部分,全局配置
[global]
;pid = /var/log/php-fpm/php-fpm.pid #pid文件存放的位置
;error_log = /var/log/php-fpm/php-fpm.log #错误日志存放的位置
;log_level = error #日志级别, alert, error, warning, notice, debug
rlimit_files = 65535 #php-fpm进程能打开的文件数
;events.mechanism = epoll #使用epoll事件模型处理请求

#第三部分,进程池定义
[www] #池名称
user = www #进程运行的用户
group = www #进程运行的组
;listen = /dev/shm/php-fpm.sock #监听在本地socket文件
listen = 127.0.0.1:9000 #监听在本地tcp的9000端口
;listen.allowed_clients = 127.0.0.1 #允许访问FastCGI进程的IP,any不限制

pm = dynamic #动态调节php-fpm的进程数
pm.max_children = 512 #最大启动的php-fpm进程数
pm.start_servers = 32 #初始启动的php-fpm进程数
pm.min_spare_servers = 32 #最少的空闲php-fpm进程数
pm.max_spare_servers = 64 #最大的空闲php-fpm进程数
pm.max_requests = 1500 #每一个进程能响应的请求数
pm.process_idle_timeout = 15s;
pm.status_path = /phpfpm_status #开启php的状态页面

#第四部分,日志相关
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/phpfpm_error.log
php_admin_flag[log_errors] = on

#慢日志
request_slowlog_timeout = 5s #php脚本执行超过5s的文件
slowlog = /var/log/php_slow.log #记录至该文件中

#慢日志示例
[21-Nov-2013 14:30:38] [pool www] pid 11877
script_filename = /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php
[0xb70fb88c] file_get_contents() /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php:2