1、nginx负载均衡中常见的算法及原理有哪些?
Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能
http upstream配置参数
#自定义一组服务器,配置在http块内
upstream name {
server .....
......
}
#示例
upstream backend {
server backend1.example.com weight=5;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
server backup1.example.com backup;
}
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number #给当前server设置最大活动链接数,默认为0表示没有限制
max_fails=number #对后端服务器连续监测失败多少次就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测
fail_timeout=time #对后端服务器的单次监测超时时间,默认为10秒
backup #设置为备份服务器,当所有服务器不可用时将重新启用次服务器
down #标记为down状态
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx
nginx负载均衡中常见的算法
1、rr轮询
rr轮询算法为nginx默认调度算法,按客户端请求顺序把客户端的请求逐一分配到不同的后端节点服务器,这相当于LVS中的rr轮询算法。如果后端节点服务器宕机,宕机的服务器会被自动从节点服务器池中剔除,以使客户端的用户访问不受影响。新的请求会分配给正常的服务器。
upstream webservers {
server 10.0.0.17;
server 10.0.0.27;
}
#代码实现
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
server 10.0.0.17:80;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
location / {
root /data/nginx/html/pc;
proxy_pass http://webservers;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#测试
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
#nginx默认带健康性检查
#后端服务器健康性检查,停掉后端10.0.0.27的Apache服务器,再次测试
[root@centos7 ~]#hostname -I
10.0.0.27
[root@centos7 ~]#systemctl stop httpd
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
2、wrr加权轮询
在rr轮询算法的基础上加上权重,即为权重轮询算法。权重越高,在被访问的概率越大 。可以根据服务器的配置和性能指定权重值大小,达到合理有效的地利用主机资源 。
upstream webservers {
server 10.0.0.17 weight=2;
server 10.0.0.27 weight=5;
}
#代码实现
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
server 10.0.0.17:80 weight=3;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#测试
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
3、ip_hash源地址hash
源地址hash调度算法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计算,以实现会话保持。该方法确保来自同一客户端的请求将始终传递到同一服务器,除非该服务器不可用。在该服务器不可用的情况下,客户端请求将被传递到另一台服务器。很可能,它也将始终是同一台服务器。
注意:当负载均衡算法为ip_hash时,后端服务器在负载均衡调度中的状态不能有weight和backup。
upstream webservers {
ip_hash;
server 10.0.0.17;
server 10.0.0.27;
}
如果需要临时下线其中一台服务器,则应使用down参数对其进行标记,以保留客户端 IP 地址的当前散列。
upstream backend {
ip_hash;
server 10.0.0.17;
server 10.0.0.27;
server 10.0.0.27 down;
}
#代码实现
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
ip_hash;
server 10.0.0.17:80;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#测试
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
4、least_conn最少连接
最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC加权最少连接算法。同时考虑服务器的权重,如果后端服务器的连接数都相同时,则使用WRR加权轮询调度算法。
upstream backserver {
least_conn;
server 10.0.0.17;
server 10.0.0.27;
}
#代码实现
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
least_conn;
server 10.0.0.17:80;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#测试验证,后端服务器的连接数都相同时,则使用WRR加权轮询调度算法,默认权重是1
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.17
[root@ubuntu1804 ~]#curl http://www.magedu.org/
10.0.0.27
5、hash KEY [consistent] 一致性hash算法
基于指定请求报文中首部字段或者URI等key做hash计算。可以包含文本、key变量及其组合。
如果consistent指定了参数,将使用ketama一致性hash算法。该方法确保在将服务器添加到组或从组中删除时,只有少数密钥将重新映射到不同的服务器。这有助于为缓存服务器实现更高的缓存命中率。
一致性hash算法适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算
注意:从组中添加或删除服务器可能会导致将大部分密钥重新映射到不同的服务器。
hash $request_uri consistent; #基于用户请求的uri做hash,使用一致性hash运算
hash $cookie_sessionid; #基于cookie中的sessionid这个key进行hash调度,实现会话绑定
#基于用户请求的uri做hash
#代码实现
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
hash $request_uri;
server 10.0.0.17:80;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#在10.0.0.17上
[root@centos7 html]#pwd
/var/www/html
[root@centos7 html]#for i in {1..10};do echo 10.0.0.17 test$i > test$i.html;done
[root@centos7 html]#ls
50x.html test10.html test2.html test4.html test6.html test8.html
index.html test1.html test3.html test5.html test7.html test9.html
[root@centos7 html]#cat test1.html
10.0.0.17 test1
[root@centos7 html]#cat test2.html
10.0.0.17 test2
#在10.0.0.27上
[root@centos7 html]#pwd
/var/www/html
[root@centos7 html]#for i in {1..10};do echo 10.0.0.17 test$i > test$i.html;done
[root@centos7 html]#ls
index.html test1.html test3.html test5.html test7.html test9.html
test10.html test2.html test4.html test6.html test8.html
#测试验证
[root@ubuntu1804 ~]#curl http://www.magedu.org/test1.html
10.0.0.27 test1
[root@ubuntu1804 ~]#curl http://www.magedu.org/test1.html
10.0.0.27 test1
[root@ubuntu1804 ~]#curl http://www.magedu.org/test1.html
10.0.0.27 test1
[root@ubuntu1804 ~]#curl http://www.magedu.org/test2.html
10.0.0.27 test2
[root@ubuntu1804 ~]#curl http://www.magedu.org/test3.html
10.0.0.27 test3
[root@ubuntu1804 ~]#curl http://www.magedu.org/test4.html
10.0.0.17 test4
[root@ubuntu1804 ~]#curl http://www.magedu.org/test4.html
10.0.0.17 test4
[root@ubuntu1804 ~]#curl http://www.magedu.org/test4.html
10.0.0.17 test4
[root@ubuntu1804 ~]#curl http://www.magedu.org/test4.html
10.0.0.17 test4
#基于用户请求的uri做hash,使用一致性hash运算
#代码实现
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
hash $request_uri consistent;
server 10.0.0.17:80;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#测试验证
[root@ubuntu1804 ~]#curl http://www.magedu.org/test3.html
10.0.0.27 test3
[root@ubuntu1804 ~]#curl http://www.magedu.org/test3.html
10.0.0.27 test3
[root@ubuntu1804 ~]#curl http://www.magedu.org/test3.html
10.0.0.27 test3
[root@ubuntu1804 ~]#curl http://www.magedu.org/test3.html
10.0.0.27 test3
#基于cookie实现会话绑定
[root@ubuntu1804 ~]#curl -b 'name=xiaoming' http://www.magedu.org/test3.html
10.0.0.27 test3
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
http {
upstream webservers {
hash $cookie_name consistent;
server 10.0.0.17:80;
server 10.0.0.27:80;
}
}
[root@centos8 ~]#nginx -t
[root@centos8 ~]#nginx -s reload
#测试验证
[root@ubuntu1804 ~]#curl -b 'name=xiaoming' http://www.magedu.org/test4.html
10.0.0.17 test4
[root@ubuntu1804 ~]#curl -b 'name=xiaoming' http://www.magedu.org/test3.html
10.0.0.17 test3
[root@ubuntu1804 ~]#curl -b 'name=xiaoming' http://www.magedu.org/test2.html
10.0.0.17 test2
[root@ubuntu1804 ~]#curl -b 'name=xiaoming' http://www.magedu.org/test1.html
10.0.0.17 test1
#修改cookie值
[root@ubuntu1804 ~]#curl -b 'name=xiaohong' http://www.magedu.org/test1.html
10.0.0.17 test1
#再次测试
[root@ubuntu1804 ~]#curl -b 'name=xiaohong' http://www.magedu.org/test2.html
10.0.0.17 test2
[root@ubuntu1804 ~]#curl -b 'name=xiaohong' http://www.magedu.org/test3.html
10.0.0.17 test3
[root@ubuntu1804 ~]#curl -b 'name=xiaohong' http://www.magedu.org/test4.html
10.0.0.17 test4
#停掉后端一个服务器
[root@centos7 ~]#hostname -I
10.0.0.17
[root@centos7 ~]#systemctl stop httpd
#再次测试,之前绑定的会话会丢失,会自动调度到另一台服务器
[root@ubuntu1804 ~]#curl -b 'name=xiaohong' http://www.magedu.org/test4.html
10.0.0.27 test4
2、使用rewrite规则实现将所有到a域名的访问rewrite到b域名
利用nginx的rewrite的指令,可以实现url的重新跳转,rewrite有四种不同的flag,分别是redirect(临时重定向302)、permanent(永久重定向301)、break和last。其中前两种是跳转型的flag,后两种是代理型的flag
跳转型指由客户端浏览器重新对新地址进行请求
代理型是在WEB服务器内部实现跳转
rewrite 格式
Syntax: rewrite regex replacement [flag]; #通过正则表达式处理用户请求并返回替换后的数据
包。
Default: -
Context: server, location, if
flag 说明
redirect;
#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求;
使用相对路径,或者http://或https://开头,状态码:302
permanent;
#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301
break;
#重写完成后,停止对当前URL在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用
#适用于一个URL一次重写
last;
#重写完成后,停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
#适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户
1、301永久重定向
域名永久型调整,即域名永远跳转至另外一个新的域名,之前的域名再也不使用,跳转记录可以缓存到客户端浏览器
永久重定向会缓存DNS解析记录,浏览器中有 from disk cache 信息,即使nginx服务器无法访问,浏览器也会利用缓存进行重定向
#实现单页301重定向
[root@centos8 conf.d]#pwd
/apps/nginx/conf.d
[root@centos8 conf.d]#vim pc.conf
server {
listen 80;
server_name www.magedu.org
location / {
root /data/nginx/html/pc;
}
location /gz {
rewrite ^/gz(.*) /guangzhou/$1 permanent;
}
}
[root@centos8 conf.d]#nginx -t
[root@centos8 conf.d]#nginx -s reload
[root@centos8 conf.d]#cd /data/nginx/html/pc
[root@centos8 pc]#mkdir guangzhou/
[root@centos8 pc]#pwd
/data/nginx/html/pc
[root@centos8 pc]#pwd > guangzhou/index.html
[root@centos8 pc]#vim guangzhou/index.html
/data/nginx/html/pc/guangzhou/index.html
#测试验证
[root@ubuntu1804 ~]#curl http://www.magedu.org/gz/
<html>
<head><title>301 Moved Permanently</head></title>
<body>
<center><h1>301 Moved Permanently</center></h1>
<hr><center>nginx</center>
</body>
</html>
[root@ubuntu1804 ~]#curl http://www.magedu.org/gz/ -L
/data/nginx/html/pc/guangzhou/index.html
[root@ubuntu1804 ~]#curl http://www.magedu.org/guangzhou/ -L
/data/nginx/html/pc/guangzhou/index.html
#实现域名301重定向
[root@centos8 conf.d]#pwd
/apps/nginx/conf.d
[root@centos8 conf.d]#vim pc.conf
server {
listen 80;
server_name www.magedu.org
location / {
root /data/nginx/html/pc;
rewrite / http://www.magedu.com permanent;
}
location /gz {
rewrite ^/gz(.*) /guangzhou/$1 permanent;
}
}
server {
listen 80;
server_name www.magedu.com
location / {
root /data/nginx/html/product;
rewrite / http://www.magedu.com permanent;
}
[root@centos8 conf.d]#nginx -t
[root@centos8 conf.d]#nginx -s reload
[root@centos8 conf.d]#cd /data/nginx/html/pc/
[root@centos8 pc]#mkdir product
[root@centos8 pc]#vim product/index.html
www.magedu.com/product
#测试验证
[root@ubuntu1804 ~]#vim /etc/hosts
10.0.0.8 www.magedu.org m.magedu.org www.magedu.com
[root@ubuntu1804 ~]#curl http://www.magedu.org -I
HTTP/1.1 301 Moved Permanently
Date:Tue,05 Apr 2022 04:28:45 GMT
Content-Type:text/html
Content-Length:162
Connection:keep-alive
Location:http://www.magedu.com
2、302 临时重定向
域名临时重定向,告诉浏览器域名不是固定重定向到当前目标域名,后期可能随时会更改,因此浏览器不会缓存当前域名的解析记录,而浏览器会缓存永久重定向的DNS解析记录,这也是临时重定向与永久重定向最大的本质区别
#实现域名301重定向
[root@centos8 conf.d]#pwd
/apps/nginx/conf.d
[root@centos8 conf.d]#vim pc.conf
server {
listen 80;
server_name www.magedu.org
location / {
root /data/nginx/html/pc;
rewrite / http://www.magedu.com redirect;
}
}
[root@centos8 conf.d]#nginx -t
[root@centos8 conf.d]#nginx -s reload
#测试验证
[root@ubuntu1804 ~]#curl http://www.magedu.org -I
HTTP/1.1 302 Moved Temporarily
Server:nginx
Date:Tue,05 Apr 2022 05:20:49 GMT
Content-Type:text/html
Content-Length:138
Connection:keep-alive
Location:http://www.magedu.com
3、实现反向代理客户端IP透传
一、一级代理实现客户端IP透传
[root@proxy ~]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
location / {
root /data/nginx/html/pc;
proxy_pass http://10.0.0.18;
#proxy_set_header X_Real-IP $remote-addr #只添加客户端IP到请求报文头部,转发至后端服务器
proxy_set_header X_Forwarded_For $proxy_add_x_forwarded_for; #添加客户端IP和反向代理服务器IP到请求报文头部
}
}
#重启nginx
[root@proxy ~]#nginx -s reload
#后端Apache配置
[root@centos8 ~]#vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#重启apache访问web界面并验证apache日志
[root@centos8 ~]#systemctl restart httpd
[root@centos8 ~]#tail -f /var/log/httpd/access_log
10.0.0.100 10.0.0.8 - - [15/April/2022:00:40:46 +0800] "GET / HTTP/1.0" 200 19 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36"
#Nginx配置:
[root@centos8 conf.d]# cat /apps/nginx/conf/nginx.conf
"$http_x_forwarded_for" #默认日志格式就有此配置
#重启nginx访问web界面并验证日志格式:
10.0.0.8 - - [15/April/2022:16:40:51 +0800] "GET / HTTP/1.0" 200 24 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36" "10.0.0.100"
二、多级代理实现客户端 IP 透传
#第一个代理服务器
[root@nginx1 ~]#vim /apps/nginx/conf/nginx.conf
#开启日志格式,记录x_forwarded_for
http {
include mime.types;
default_type application/octet-stream;
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forward_for"';
access_log logs/access.log main;
}
#定义反向代理
[root@ nginx1 ~]#vim /apps/nginx/conf.d/pc.conf
server {
location / {
proxy_pass http://10.0.0.18;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
.......
}
#第二个代理服务器
[root@ nginx2 ~]#vim /apps/nginx/conf/nginx.conf
#开启日志格式,记录x_forwarded_for
http {
include mime.types;
default_type application/octet-stream;
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forward_for"';
access_log logs/access.log main;
#定义反向代理
[root@nginx2 ~]#vim /apps/nginx/conf.d/pc.conf
server {
location / {
proxy_pass http://10.0.0.28;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
.......
}
#在第一个proxy上面查看日志
[root@nginx1 ~]#tail -f /apps/nginx/logs/access.log
10.0.0.7 - - [11/Oct/2020:14:37:00 +0800] "GET /index.html HTTP/1.1" 200 11 "-" "curl/7.58.0" "-"
#在第二个proxy上面查看日志
[root@nginx2 ~]#tail -f /apps/nginx/logs/access.log
10.0.0.8 - - [11/Oct/2020:14:37:00 +0800] "GET /index.html HTTP/1.0" 200 11 "-" "curl/7.58.0" "10.0.0.7"
#后端服务器日志格式
[root@centos8 ~]#vim /etc/httpd/conf/httpd.conf
LogFormat "\"%{x-Forwarded-For}i\" %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" testlog
CustomLog "logs/access_log" testlog
#测试访问
[root@centos7 ~]#curl www.magedu.org/index.html
<h1> web site on 10.0.0.28 </h1>
#后端服务器查看日志
[root@centos8 ~]#tail -f /var/log/httpd/access_log
"10.0.0.7, 10.0.0.8" 10.0.0.18 - - [11/Oct/2020:14:37:00 +0800] "GET
/index.html HTTP/1.0" 200 34 "-" "curl/7.29.0"
4、利用LNMP实现wordpress站点搭建
LNMP项目实战环境说明
L:Linux(CentOS7)https://mirrors.aliyun.com/centos/7/isos/x86_64/
N:Nginx(1.18.0) https://nginx.org/en/download.html
M:MySQL(8.0.19) https://dev.mysql.com/downloads/mysql/
P:PHP(7.4.10) http://php.net/downloads.php
Wordpress(5.4.2):https://cn.wordpress.org/download/
#部署规划:
10.0.0.7:Nginx php-fpm 运行web服务
10.0.0.17:运行MySQL数据库,Redis服务
1 部署数据库
在10.0.0.17主机部署MySQL服务
1.1 二进制部署MySQL数据库
[root@centos7 ~]#ll
total 473728
-rw-r--r-- 1 root root 2433 Sep 8 23:09 install_mysql5.7or8.0_for_centos.sh
-rw-r--r-- 1 root root 485074552 Aug 22 23:40 mysql-8.0.19-linux-glibc2.12-
x86_64.tar.xz
#一键安装脚本
[root@centos7 ~]#cat install_mysql5.7or8.0_for_centos.sh
#!/bin/bash
#
. /etc/init.d/functions
SRC_DIR=`pwd`
MYSQL='mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz'
COLOR='echo -e \E[01;31m'
END='\E[0m'
MYSQL_ROOT_PASSWORD=magedu
check (){
if [ $UID -ne 0 ]; then
action "当前用户不是root,安装失败" false
exit 1
fi
cd $SRC_DIR
if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
action "数据库已存在,安装失败" false
exit
else
return
fi
}
install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
yum -y -q install libaio numactl-libs &> /dev/null
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
chown -R root.root /usr/local/mysql/
id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "创建mysql用户"; }
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/null
action "数据库安装完成"
}
check
install_mysql
#运行脚本安装数据
[root@centos7 ~]#bash install_mysql5.7or8.0_for_centos.sh
1.2 创建wordpress数据库和用户并授权
[root@centos7 ~]#mysql -uroot -pmagedu
mysql > create database wordpress;
mysql > create user wordpress@'10.0.0.%' identified by '123456';
mysql > grant all on wordpress.* to wordpress@'10.0.0.%';
1.3 验证MySQL账户权限
在WordPress服务器使用授权的MySQL账户远程登录测试权限
[root@centos7 ~]#mysql -uroot -p123456 -h 10.0.0.17
mysql > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wordpress |
+--------------------+
2 rows in set (0.00 sec)
2 部署PHP
在10.0.0.7主机部署php-fpm服务
2.1 编译安装 php
[root@centos7 ~]#yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel
[root@centos7 ~]#cd /usr/local/src
[root@centos7 src]#wget https://www.php.net/distributions/php-7.4.11.tar.xz
[root@centos7 src]#ll -h php-7.4.11.tar.xz
-rw-r--r-- 1 root root 9.9M Sep 29 18:40 php-7.4.11.tar.xz
[root@centos7 src]#cd php-7.4.11/
[root@centos7 php-7.4.11]#./configure --prefix=/apps/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
[root@centos7 php-7.4.11]#make -j 8 && make install
2.2 准备 php 配置文件
#生成配置文件
[root@centos7 php-7.4.11]#cp /usr/local/src/php-7.4.11/php.ini-production /etc/php.ini
[root@centos7 php-7.4.11]#cd /apps/php74/etc
[root@centos7 etc]#cp php-fpm.conf.default php-fpm.conf
[root@centos7 etc]#cd php-fpm.d/
[root@centos7 php-fpm.d]#cp www.conf.default www.conf
[root@centos7 php-fpm.d]#vim www.conf
[www]
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /pm_status
ping.path = /ping
access.log = log/$pool.access.log
slowlog = log/$pool.log.slow
#创建用户
[root@centos7 php-fpm.d]#useradd -r -s /sbin/nologin www
#创建访问日志文件路径
[root@centos7 php-fpm.d]#mkdir /apps/php74/log
2.3 启动并验证 php-fpm 服务
[root@centos7 ~]#/apps/php74/sbin/php-frm -t
[22-Oct-2020 10:55:19] NOTICE: configuration file /apps/php74/etc/php-fpm.conf
test is successful
[root@centos7 ~]#cp /usr/local/src/php-7.4.11/sapi/fpm/php-fpm.service
/usr/lib/systemd/system/
[root@centos7 ~]#systemctl daemon-reload
[root@centos7 ~]#systemctl enable --now php-fpm
[root@centos7 ~]#ss -ntl #127.0.0.1:9000端口打开
3 部署 Nginx
在10.0.0.7主机部署nginx服务
3.1 编译安装 nginx
[root@centos7 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel
[root@centos7 ~]#cd /usr/local/src/
[root@centos7 src]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@centos7 src]#tar xf nginx-1.18.0.tar.gz
[root@centos7 src]#cd nginx-1.18.0/
[root@centos7 nginx-1.18.0]#./configure --prefix=/apps/nginx \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@centos7 nginx-1.18.0]#make && make install
#修改权限
[root@centos7 nginx-1.18.0]#chown -R nginx.nginx /apps/nginx
#创建软链接
[root@centos7 nginx-1.18.0]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
3.2 准备服务文件并启动 nginx
[root@centos7 ~]#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=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
#创建目录
[root@centos7 ~]#mkdir /apps/nginx/run/
#修改配置文件
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
[root@centos7 ~]#systemctl daemon-reload
[root@centos7 ~]#systemctl enable --now nginx
[root@centos7 ~]#ss -ntl #*:80端口打开
3.3 配置 Nginx 支持 fastcgi
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
worker_processes 1;
pid /apps/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.magedu.org; #指定主机名
location / {
root /data/nginx/wordpress; #指定数据目录
index index.php index.html index.htm; #指定默认主页
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ { #实现php-fpm
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/(ping|pm_status)$ { #实现状态页
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
}
}
[root@centos7 ~]# nginx -t
[root@centos7 ~]# nginx -s reload
3.4 准备 php 测试页
[root@centos7 ~]#mkdir -p /data/nginx/wordpress
[root@centos7 ~]#vim /data/nginx/wordpress/test.php
<?php
phpinfo();
?>
3.5 验证 php 测试页
#浏览器访问
www.magedu.org/ping
www.magedu.org/pm_status
www.magedu.org/test.php
4 部署 WordPress
在10.0.0.7主机部署 wordpress
4.1 准备 WordPress 文件
[root@centos7 ~]#tar xf wordpress-5.4.2-zh_CN.tar.gz
[root@centos7 ~]#cp -r wordpress/* /data/nginx/wordpress
[root@centos7 ~]#chown -R www.www /data/nginx/wordpress/
4.2 初始化web页面
打开浏览器访问下面链接
http://www.magedu.org/
4.3 登录后台管理界面并发表文章
4.4 验证发表的文章
#浏览器访问
http://www.magedu.org/
#可以看到上传的图片
[root@centos7 ~]#tree /data/nginx/wordpress/wp-content/uploads/
/data/nginx/wordpress/wp-content/uploads/
└── 2022
└── 04
└── magedu.jpg
2 directories, 1 file
4.5 配置允许上传大文件
#注意:默认只支持1M以下文件上传,要利用php程序上传大图片,还需要修改下面三项配置,最大上传由三项值的最小值决定
#直接上传大于1M文件,会出现下面413错误
[root@centos7 ~]#tail -f /apps/nginx/logs/access.log
10.0.0.1 - - [27/Nov/2020:12:21:16 +0800] "POST /wp-admin/async-upload.php
HTTP/1.1" 413 585 "http://10.0.0.7/wp-admin/upload.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67
Safari/537.36 Edg/87.0.664.47"
#nginx上传文件大小限制
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
server {
client_max_body_size 10m; #默认值为1M
.....
#php上传文件大小限制
[root@centos7 ~]#vim /etc/php.ini
post_max_size = 30M #默认值为8M
upload_max_filesize = 20M #默认值为2M
[root@centos7 ~]#systemctl restart nginx php-fpm
4.6 安全加固
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
worker_processes 1;
pid /apps/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.magedu.org;
server_tokens off; #添加此行
location / {
root /data/nginx/wordpress;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By; #添加此行
}
location ~ ^/(ping|pm_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
}
}
4.7 配置 php 开启 opcache 加速
在10.0.0.7主机进行以下修改配置
#编辑php.ini配置文件
[root@centos7 ~]#vim /etc/php.ini
[opcache]
; Determines if Zend OPCache is enabled
zend_extension=opcache.so
opcache.enable=1
.....
[root@centos7 ~]#systemctl restart php-fpm
#访问测试页确认开启opcache加速
http://www.magedu.org/test.php
5 PHP 扩展session模块支持redis
PECL是 PHP 扩展的存储库,提供用于下载和开发 PHP 扩展的所有已知扩展和托管功能的目录
官方链接:http://pecl.php.net/package-stats.php
github:https://github.com/phpredis/phpredis
github安装文档:
https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown
开始在 PHP 中使用 Redis 前, 需要确保已经安装了 redis 服务及 PHP redis 驱动,
PHP redis 驱动下载地址为:https://github.com/phpredis/phpredis/releases
5.1编译安装phpredis模块
在10.0.0.7主机进行以下编译安装phpredis模块
[root@centos7 ~]#cd /usr/local/src
[root@centos7 src]#wget http://pecl.php.net/get/redis-5.3.1.tgz
[root@centos7 src]#tar xf redis-5.3.1.tgz
[root@centos7 src]#cd redis-5.3.1/
[root@centos7 redis-5.3.1]#ls
arrays.markdown config.m4 INSTALL.markdown README.markdown redis.c
redis_sentinel.c sentinel_library.h
cluster_library.c config.w32 liblzf redis_array.c
redis_cluster.c redis_sentinel.h sentinel.markdown
cluster_library.h COPYING library.c redis_array.h
redis_cluster.h redis_session.c tests
cluster.markdown crc16.h library.h redis_array_impl.c
redis_commands.c redis_session.h
common.h CREDITS php_redis.h redis_array_impl.h
redis_commands.h sentinel_library.c
#如果是yum安装php,需要执行yum -y install php-cli php-devel
#以下为编译安装php的对应方式
[root@centos7 redis-5.3.1]#/apps/php74/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
Cannot find autoconf. Please check your autoconf installation and the #报错提示
$PHP_AUTOCONF environment variable. Then, rerun this script.
[root@centos7 redis-5.3.1]#yum -y install autoconf
#重新执行成功
[root@centos7 redis-5.3.1]#/apps/php74/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
#查看生成configure脚本
[root@centos7 redis-5.3.1]#ls
arrays.markdown common.h COPYING library.h
redis_array_impl.h redis_sentinel.c sentinel_library.h
autom4te.cache config.h.in crc16.h php_redis.h redis.c
redis_sentinel.h sentinel.markdown
build config.m4 CREDITS README.markdown
redis_cluster.c redis_session.c tests
cluster_library.c configure INSTALL.markdown redis_array.c
redis_cluster.h redis_session.h
cluster_library.h configure.ac liblzf redis_array.h
redis_commands.c run-tests.php
cluster.markdown config.w32 library.c redis_array_impl.c
redis_commands.h sentinel_library.c
#如果是基于yum安装php,无需指定--with-php-config
[root@centos7 redis-5.3.1]#./configure --with-php-config=/apps/php74/bin/php-config
[root@centos7 redis-5.3.1]#make -j 8 && make install
#验证redis模块
#yum安装php,模块文件默认存放在 /usr/lib64/php/modules/redis.so
[root@centos7 redis-5.3.1]#ll /apps/php74/lib/php/extensions/no-debug-zts-
20190902/
total 9584
-rwxr-xr-x 1 root root 4647668 Oct 22 10:44 opcache.a
-rwxr-xr-x 1 root root 2509416 Oct 22 10:44 opcache.so
-rwxr-xr-x 1 root root 2651320 Oct 22 12:10 redis.so
5.2 编辑php配置文件支持redis
在10.0.0.7主机进行以下修改配置
#编辑php.ini配置文件,扩展redis.so模块
[root@centos7 ~]#vim /etc/php.ini
.....
#extension=/apps/php74/lib/php/extensions/no-debug-zts-20190902/redis.so
extension=redis.so #文件最后一行添加此行,路径可省略
[root@centos7 ~]#systemctl restart php-fpm
5.3 验证加载 redis 模块
访问测试页确认redis模块开启
http://www.magedu.org/test.php
5.4 安装和配置 redis 服务
在10.0.0.17主机进行安装redis 服务
#在10.0.0.17主机安装redis服务
[root@centos7 ~]#yum -y install redis
[root@centos7 ~]#vim /etc/redis.conf
bind 0.0.0.0
requirepass 123456
[root@centos7 ~]#systemctl enable --now redis
[root@centos7 ~]#ss -ntl #*:6379端口打开
5.5 配置 php 支持 redis 保存 session
#在10.0.0.7主机配置php的session保存在redis服务
[root@centos7 ~]#vim /etc/php.ini
[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = redis
session.save_path = "tcp://10.0.0.17:6379?auth=123456"
[root@centos7 ~]#systemctl restart php-fpm
5.6 准备 php实现 session 的测试页面
在10.0.0.7主机进行准备相关文件
[root@centos7 ~]#cat /data/nginx/wordpress/session.php
<?php
session_start();
//redis用session_id作为key 并且是以string的形式存储
$redisKey = 'PHPREDIS_SESSION:' . session_id();
// SESSION 赋值测试
$_SESSION['message'] = "Hello, I'm in redis";
$_SESSION['arr'] = [1, 2, 3, 4, 5, 6];
echo $_SESSION["message"] , "<br/>";
echo "Redis key = " . $redisKey . "<br/>";
echo "以下是从Redis获取的数据", "<br/>";
// 取数据'
$redis = new Redis();
$redis->connect('10.0.0.17', 6379);
$redis->auth('123456');
echo $redis->get($redisKey);
?>
5.7 访问 web 页面测试实现session保存在redis服务
www.magedu.org/session.php
5.8 redis 主机验证 session 数据
在10.0.0.17主机进行验证
[root@centos7 ~]#redis-cli -h 10.0.0.17 -a 123456
10.0.0.17:6379> keys *
1) "PHPREDIS_SESSION:ad3nujfqfvcltkg2ml4snsf72h"
10.0.0.17:6379> get PHPREDIS_SESSION:ad3nujfqfvcltkg2ml4snsf72h
"message|s:19:\"Hello, I'm in redis\";arr|a:6:
{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;}"