目录
1.动态网站架构
2.LNMP动态网站环境部署
1.LINUX部署
stop firewalld
disable selinux
setenforce 0
2.Nginx部署
yum install -y nginx
3.php-fpm部署
rpm部署:
yum install -y php-fpm php-mysql php-gd
php-fpm:php接收动态请求的程序
php-mysql:php链接mysql的程序
php-gd:图形库程序(GD库可以处理图片,或者生成图片)
systemctl restart php-fpm
systemctl enable php-fpm
netstat -anpt | grep 9000
vim /etc/nginx/nginx.conf (增加PHP主页名称:index.php)
server {
location / {
...
index index.php index.html;
...
}
}
vim /etc/nginx/nginx.conf
server {
location / {
index index.php;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
systemctl restart nginx
php部署
准备前台html页面
[root@localhost html]# cd /usr/share/nginx/html/
# vim 1.html
<html>
<body>
<img src="logo.jpg" />
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
准备php中间件
# vim insert.php
<?php
$con = mysql_connect("192.168.100.10","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
准备表和库 (安装了mariadb)
mysql > create database my_db;
mysql > create table Persons (FirstName varchar(50), LastName varchar(50),Age int );
mysql > grant all on *.* to root@'%' identified by '123456';
mysql > flush privileges;
输入页面
后台数据库也会有对应的内容
4.mysql 部署
yum -y install mariadb-server mariadb
systemctl start mariadb
systemctl enable mariadb
mysqladmin password '123456'
3. fastcgi & php-fpm
静态网站
nginx服务器能处理的是静态元素 .html .jpg .mp4 .css
NGINX
1.ngx_fastcgi_modul
处理动态请求的接口
nginx 通过ngx_fastcgi_modul模块 链接 php-fpm处理动态请求。
PHP
2.php-fpm
PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)
是一个PHP FastCGI管理器。
PHP通过php-fpm接收前台nginx的动态访问的请求,比如向后端Mysql进行查询请求后,将查询结果返回给前台nginx。
PHP-MYSQL
php-mysql
php-mysql:是php连接mysql的接口程序。
MYSQL
存储数据
4.php-fpm初始化配置
1.了解php-fpm相关配置文件
1.核心配置文件
vim /etc/php.ini
date.timezone = PRC 设置PHP的时区
open_basedir 设置PHP脚本允许访问的目录.
2.全局配置文件
vim /etc/php-fpm.conf
pid = /run/php-fpm/php-fpm.pid 设置pid文件的位置
error_log = log/php-fpm.log 记录错误日志的文件
log_level = notice 记录日志的等级
process.max = 3 默认没设置
daemonize = yes 将fpm转至后台运行
3.扩展配置文件
vim /etc/php-fpm.d/www.conf
pm=dynamic 动态模式进程管理开启
start_servers=5 最初开启多少进程
min_spare_server =5 最小的多余进程数。最少空闲。用户访问会消耗掉进程。
max_children = 50 最大进程数
max_children 是PHP-FPM Pool 最大的子进程数,他数值取决于你的服务器内存。
max_spare_servers=10 最大的多余进程。大规模断开后,还剩 多少。
max_requests = 500 每个子进程能响应的请求数量,到达此数字,进程就被释放。
max_requests 是每个子进程重生之前处理的请求数, 默认值为unlimited(1024)
可以设置小一点(如500左右),这样可以避免内存泄露带来的问题
user = nginx 设置用户和用户组
listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接
listen = 127.0.0.1:9000 fpm监听端口,即nginx中php处理的地址,
slowlog = /var/log/php-fpm/$pool-slow.log 开启慢日志
2.初始化php-fpm
修改 vim /etc/php-fpm.d/www.conf
进程数会发生变化 ps aux |grep php
3.启动php状态监控页面功能
1.启动测试页功能
vim /etc/php-fpm.d/www.conf
pm.status_path = /php_status
2.nginx配置页面转发
vim /etc/nginx/conf.d/default.conf (include fastcgi_params是常用变量所在的文件名。)
location = /php_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
systemctl restart nginx
systemctl restart php-fpm
3.访问测试页
192.168.31.172/php_status
pool – fpm池子名称,大多数为www
process manager – 进程管理方式,值:static, dynamic or ondemand. dynamic
start time – 启动日期,如果reload了php-fpm,时间会更新
start since – 运行时长
accepted conn – 当前池子接受的请求数
listen queue – 请求等待队列,如果这个值不为0,那么要增加FPM的进程数量
max listen queue – 请求等待队列最高的数量
listen queue len – socket等待队列长度
idle processes – 空闲进程数量
active processes – 活跃进程数量
total processes – 总进程数量
max active processes – 最大的活跃进程数量(FPM启动开始算)
max children reached - 进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量太小了,请改大一点。
slow requests – 启用了php-fpm slow-log,缓慢请求的数量
5.Nginx Location
语法规则:
location [=|~|~*|!~|!~*|^~] /uri/ {
module;
module;
}
= 表示精确匹配,优先级也是最高的
~ 区分大小写的正则匹配
~* 不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到
^~ 以某些字符串开头
!~ 非(区分大小写匹配的正则)
!~* 非(不区分大小写匹配的正则)
Location优先级:
= 》 ^~ 》 ~|~*|!~|!~* 》 /
精确匹配》字符开头》正则匹配》通配
6.Nginx Rewrite
Nginx URL重写
一、什么是Rewrite
1. URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如
http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123
.com/news/123.html对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。
理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所
以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。
2. 从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客
利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。
3. 实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。
二、Rewrite相关指令
重定向
rewrite 将用户的访问(url),更换成指定的文件。
if 语句
应用环境:server,location
语法:if (condition) { … }
条件判断
~* 正则匹配 (不区分大小写)
!~ 非正则匹配 (区分大小写)
!~* 非正则 匹配 (不区分大小写)
-f 和!-f 用来判断是否存在文件
-d 和!-d 用来判断是否存在目录
-e 和!-e 用来判断是否存在文件或目录
-x 和!-x 用来判断文件是否可执行
全局变量
$document_root
针对当前请求的根路径设置值;
$remote_addr
客户端地址;
$request_filename
当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images/a.jpg)
$request_uri
当前请求的文件路径名(不带网站的主目录/images/a.jpg)
$scheme
用的协议,比如http或者是https
$server_name
请求到达的服务器名;
$args
请求中的参数;
$host
请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
$limit_rate
对连接速率的限制;
$request_method
请求的方法,比如"GET"、"POST"等;
$remote_port
客户端端口号;
$remote_user
客户端用户名,认证用;
$query_string
与$args相同;
$server_protocol
请求的协议版本,"HTTP/1.0"或"HTTP/1.1";
$server_addr
服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$document_uri
与$uri一样,URI地址;
$server_port
请求到达的服务器端口号;
三、Rewrite flag
break 本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
redirect url permanent 返回301永久重定向,浏览器地址会显示跳转后URL地址
7.CA&HTTPS
私有CA
1、生成证书及秘钥文件
1)准备存放证书和秘钥的目录
[root@nginx ~]# mkdir -p /etc/nginx/ssl
2)生成私钥
使用openssl生成基于rsa数学算法长度为1024bit的秘钥,文件必须以key为结尾
[root@nginx ~]# openssl genrsa 1024 > /etc/nginx/ssl/server.key
Generating RSA private key, 1024 bit long modulus
...............................................................++++++
................................................................++++++
e is 65537 (0x10001)
3) 使用秘钥文件生成证书-申请书
[root@nginx ~]# openssl req -new -key /etc/nginx/ssl/server.key > /etc/nginx/ssl/server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN ###国家名(两个字
State or Province Name (full name) []:BJ ###省会(两个字
Locality Name (eg, city) [Default City]:BJ ###城市
Organization Name (eg, company) [Default Company Ltd]::qf ###组织名 (千锋QF
Organizational Unit Name (eg, section) []:cloud ##组织单位名
Common Name (eg, your name or your server's hostname) []:nginx.linux.com ##服务器的名字或者你的名字
Email Address []:12345678@qq.com ###可选
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ###密码为空
An optional company name []: ####密码为空
Common Name (eg, your name or your server's hostname) []: ###公司名空
查看申请书
[root@nginx ~]# ls /etc/nginx/ssl/
server.csr (证书申请) server.key (私钥)
4)同意申请,生成证书
[root@nginx ~]# openssl req -x509 -days 365 -key /etc/nginx/ssl/server.key -in /etc/nginx/ssl/server.csr > /etc/nginx/ssl/server.crt
注释
-x509:证书的格式,固定的
days:证书的有效期,生产生活中时间不同,价格不同
key:指定秘钥文件
in:指定证书申请文件
查看证书
[root@nginx ~]# ll /etc/nginx/ssl/
总用量 12
-rw-r--r--. 1 root root 1021 7月 12 17:31 server.crt ####证书文件
-rw-r--r--. 1 root root 676 7月 12 17:30 server.csr ####申请书。可以销毁
-rw-r--r--. 1 root root 887 7月 12 17:12 server.key ####私钥文件
2、私有CA的https部署实战
1、创建目录
[root@nginx ~]# mkdir /bj
[root@nginx ~]# echo "bj ssl web" > /bj/index.html
2、编辑nginx.conf文件
[root@nginx ~]# vim /etc/nginx/conf.d/bj.conf
server {
listen 443 ssl;
server_name www.bj.com;
ssl_certificate /etc/nginx/ssl/server.crt; ##路径自定义
ssl_certificate_key /etc/nginx/ssl/server.key;
location / {
root /bj;
index index.html index.htm;
}
}
[root@nginx ~]# nginx -t
[root@nginx ~]# nginx -s reload
[root@nginx ~]# ss -antp | grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",pid=11700,fd=6),("nginx",pid=11699,fd=6),("nginx",pid=8347,fd=6))
LISTEN 0 128 *:443 *:* users:(("nginx",pid=11700,fd=20),("nginx",pid=11699,fd=20),("nginx",pid=8347,fd=20))
3、测试访问
https://www.bj.com
风险提示
8.Nginx的平滑升级
原理
当需要将正在运行中的nginx升级,添加/删除服务模块时,可以在不中断服务的情况下,使用新版本,重编译的Nginx可执行程序替换旧版本的可执行程序,步骤如下:
• 使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx,可以将新版本编译安装到旧版本的nginx安装路径中.替换之前,最好备份一下旧的可执行程序
• 发送以下指令: Kill –USR2 旧版本的nginx主进程号
• 旧版本的主进程将重命名它的pid文件为.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程.
• 此时,新,旧版本的nginx实例会同时运行,共同处理输入的请求.要逐步停止旧版本的nginx实例,你必须发送WINCH信号给旧的主进程,然后,它的工作进程就将开始从容关闭:kill –WINCH 旧版本的Nginx主进程号
• 一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了.
• 这时候,我们可以决定是使用新版本,还是恢复到旧的版本;
Kill –HUP 旧的主进程号:Nginx将在不重载配置文件的情况下启动它的工作进程;
Kill –QUIT 新的主进程号:从容关闭其他工作进程(woker process);
Kill –TERM 新的主进程号:强制退出;
Kill 新的主进程号或旧的主进程号:如果因为某些原因新的工作进程不能退出,则向其发送kill信号.
新的主进程退出后,旧的主进程会移除.oldbin前缀,恢复为他的.pid文件,这样,一切就都恢复到升级之前了,如果尝试升级成功,而你也希望保留新的服务器时,可发送QUIT信号给旧的主进程,使其退出而只留下新的服务器运行:
平滑升级1.12版本到1.14版本
1、编译安装新版本的nginx,指定安装目录为新目录
[root@server nginx]# tar xf nginx-1.14.2.tar.gz -C /usr/local/src/
[root@server nginx]# cd /usr/local/src/nginx-1.14.2/
[root@server nginx-1.14.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx14 --with-http_stub_status_module --with-http_ssl_module && make && make install
2、查看就的nginx的主进程号和工作进程号
[root@server ~]# ps aux |grep ngin[x]
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 75083 0.0 0.1 21060 1632 ? S 12:17 0:00 nginx: worker process
3、替换旧的执行程序
[root@server ~]# mv /usr/local/nginx/sbin/nginx{,.bak}
[root@server ~]# cp /usr/local/nginx14/sbin/nginx /usr/local/nginx/sbin/nginx
[root@server ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2
4、 给主进程发送USR2信号
[root@server ~]# cat /usr/local/nginx/logs/nginx.pid
68595
[root@server ~]# kill -USR2 68595
[root@server ~]# cat /usr/local/nginx/logs/nginx.pid.oldbin
68595
旧版本的主进程将重命名它的pid文件为.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程.
5、给进程发送WINCH信号
[root@server ~]# kill -WINCH 68595
[root@server ~]# ps aux |grep ngin[x]
root 58943 0.0 0.3 45940 3260 ? S 13:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 58944 0.0 0.1 46388 1888 ? S 13:34 0:00 nginx: worker process
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了.
回退到以前版本
原理
这时因为旧的服务器还尚未关闭它监听的套接字,所以通过下面的几步还可以恢复旧版本:
• 发送 HUP 信号给旧的主进程 - 它将在不重载配置文件的情况下启动它的工作进程。
• 发送 QUIT 信号给新的主进程,要求其从容关闭其工作进程
• 发送 TERM 信号给新的主进程,迫使其退出
• 如果因为某些原因新的工作进程不能退出,则直接将其杀死 KILL 信号
第一步
[root@server ~]# kill -HUP 68595
[root@server ~]# ps aux |grep ngin[x]
root 58943 0.0 0.3 45940 3260 ? S 13:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 58944 0.0 0.1 46388 1888 ? S 13:34 0:00 nginx: worker process
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 80008 0.0 0.1 21060 1388 ? S 13:50 0:00 nginx: worker process
第二步
[root@server ~]# kill -QUIT 58943
[root@server ~]# ps aux |grep ngin[x]
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 80008 0.0 0.1 21060 1388 ? S 13:50 0:00 nginx: worker process
[root@server ~]# cat /usr/local/nginx/logs/nginx.pid
68595
总结
新的主进程退出后,旧的主进程会自动移除 .oldbin 后缀,恢复为.pid的后缀名,如此:一切就都恢复到升级之前了。如果尝试升级成功,而你也希望保留新的服务器时,发送 QUIT 信号给旧的主进程使其退出而只留下新的服务器运行。