只会后端不会前端如何做网站,微信网站什么做的,重庆妇科医院免费咨询,河南省住房和城乡建设厅网站首页前言#xff1a;这应该是nginx梳理的最后一章#xff0c;写一些关于网站架构和网站上线的知识内容#xff0c;主要是感觉到运维并不是单一方向的行业#xff0c;这一章概念会有一些广泛#xff0c;但是非常重要#xff0c;都是这几年工作中遇到的情况#xff0c;整理一下…前言这应该是nginx梳理的最后一章写一些关于网站架构和网站上线的知识内容主要是感觉到运维并不是单一方向的行业这一章概念会有一些广泛但是非常重要都是这几年工作中遇到的情况整理一下相关知识遇到时可以直接按照目录寻找解决方案。
1、动态网站简介
当用户访问一个网站时动态网站会根据用户的请求实时生成并展示页面内容而静态网站则是提前生成好的页面内容直接展示给用户。动态网站更适合需要频繁更新和交互性强的场景如LOL官网登录而静态网站则更适合内容稳定的情况
动态网站根据架构不同大致有以下几种
资源文件类型开发语言网站框架index.php 开源的php Windows/Linuxnginxphpmysql index.py 开源python Windows/Linuxapachepythonmysql index.jsp 商业JAVA windows/LinuxtomcatJDKOracle index.asp 商业c# Windowsiisasp.netsqlserver/oracle/mogodb
2、LNMP动态网站部署
2.1、linux部署
设定静态ip为192.168.189.143
修改安全配置这里实验环境直接关闭防火墙和SELINUX
[rootlocalhost ~]# systemctl stop firewalld.service
[rootlocalhost ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[rootlocalhost ~]# setenforce 0
[rootlocalhost ~]# vim /etc/selinux/config
[rootlocalhost ~]# cat /etc/selinux/config
SELINUXdisabled
2.2、nginx部署
nginx部署过程第一章所示
2.3、php环境部署
这里使用rpm包部署
[rootlocalhost ~]# yum install -y php php-mysqlnd gd php-gd2.3.1、测试LNP环境
启动nginx
[rootlocalhost ~]# systemctl start nginx
[rootlocalhost ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.修改nginx默认网站文件
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name localhost;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.php index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location /50x.html {root /usr/share/nginx/html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apaches document root# concurs with nginxs one##location ~ /\.ht {# deny all;#}
}修改部分主要有网站主页将index.php放在优先显示页面需要注意的是fastcgi模块在一些旧版本中默认不调用在遇到特殊适配需求需要注意nginx版本
编辑php测试页面
[rootlocalhost ~]# vim /usr/share/nginx/html/index.php
[rootlocalhost ~]# cat /usr/share/nginx/html/index.php
?php
phpinfo();
?重启nginx使用浏览器访问网站显示如下 2.3.2、故障排除
此处因版本不同需要配置也各不相同遇到访问网页不解析php文件反而开始下载这类问题可以参考下面的排错思路
此处有可能没有开启php解析也有可能php-fpm未启动也有可能php-fpm启动但未监听9000端按照下方步骤排查大概率能找出故障原因。
检查php是否能够正常解析
[rootlocalhost ~]# php -v
PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologieswith Zend OPcache v8.0.30, Copyright (c), by Zend Technologiesnginx开启fastcgi
server {。。。。。。 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;}。。。。。。
检查php-fpm状态
[rootlocalhost ~]# systemctl status php-fpm检查9000端口监听状态
[rootlocalhost php-fpm.d]# netstat -tuln | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
如果发现9000未被监听可以修改php-fpm文件开启监听
[rootlocalhost ~]# vim /etc/php-fpm.d/www.conf
listen 127.0.0.1:9000
[rootlocalhost ~]# systemctl restart php-fpm 这里可以结合下一小节的php-fpm加强理解
2.4、mysql部署
这里使用mariadb平替mysql其实就是同一个软件
[rootlocalhost ~]# yum -y install mariadb-server mariadb
[rootlocalhost ~]# systemctl start mysqld.service
[rootlocalhost ~]# systemctl enable mysqld.service
[rootlocalhost ~]# grep password /var/log/mysqld.log
查看初始密码
[rootlocalhost ~]# mysqladmin -uroot -p4rts?yIgIQl password Liumuquan123
修改数据库root密码
[rootlocalhost ~]# mysql -uroot -pLiumuquan123
登录数据库
mysql create database discuz;
创建一个库
mysql flush privileges;
Query OK, 0 rows affected (0.01 sec
2.5、动态网站部署
下载一个源码包替代工作中前后端工程师提交的代码这里使用开源代码discuz代替
源代码下载地址
将代码上传至网站文件夹解压设置权限
[rootlocalhost html]# unzip Discuz_X3.5_SC_UTF8_20240520.zip
[rootlocalhost html]# cp -rf upload/* /usr/share/nginx/html/
[rootlocalhost html]# chmod -R 777 /usr/share/nginx/html 使用浏览器访问网址按提示配置即可 数据库按照刚才的配置填写 配置成功 3、fastcgi、php-fpm、php-mysql
静态网站nginx服务器能处理的是静态元素 .html .jpg .mp4 .css
动态网站需要多个软件合作完成。
以LNMP为例linux作为操作系统提供了整个服务器环境的基础。常见的 Linux 发行版如 Ubuntu、CentOS、Debian 等都可以用于搭建 LNMP 架构。
nginx借助模块ngx_fastcgi_modul模块通过php与mysql完成互动fastcgi是处理动态请求的接口nginx 通过ngx_fastcgi_modul模块 链接 php-fpm处理动态请求。
PHP通过php-fpm接收前台nginx的动态访问的请求比如向后端Mysql进行查询请求后将查询结果返回给前台nginx。PHP-FPM(FastCGI Process ManagerFastCGI进程管理器) 是一个PHP FastCGI管理器。
php向后端Mysql进行查询请求时需要通过模块php-mysqlcentos9中模块名称为php-mysqlnd
连接mysqlphp-mysql是php连接mysql的接口程序。
mysql负责存储数据这样构成一个基础的LNMP结构。
4、php-fpm初始化配置
4.1、php-fpm相关配置文件
4.1.1、核心配置文件
vim /etc/php.inidate.timezone PRC#设置PHP的时区open_basedir#设置PHP脚本允许访问的目录.
4.1.2、全局配置文件
vim /etc/php-fpm.confpid /run/php-fpm/php-fpm.pid#设置pid文件的位置error_log log/php-fpm.log#记录错误日志的文件log_level notice#记录日志的等级#alert必须立即处理, error错误情况, warning警告情况, notice一般重要信息, debug调试信息. 默认: notice.process.max 3#默认没设置process.max: 控制子进程最大数的全局变量, 后边的设置子进程数量的指令受到这个值的限制, 0表示无限制3代表允许三个人同时访问第四个来了后需要排队等待。daemonize yes#守护进程将fpm转至后台运行
4.1.3、扩展配置文件
vim /etc/php-fpm.d/www.conf
user nginx
#设置用户和用户组此时nginx是php的客户listen.allowed_clients 127.0.0.1
#这里配置的是客户机的ip也就是nginx的IP分离部署的时候ip需要填写正常的iplisten 127.0.0.1:9000
#fpm监听端口即nginx中php处理的地址一般默认值即可。可用格式为: ip:portslowlog /var/log/php-fpm/$pool-slow.log
#开启慢日志慢日志记录请求时间很长的操作用于优化pmdynamic
#动态模式进程管理开启动态调整php进程数量 start_servers5
#最初开启多少进程min_spare_server 5
#最小的空闲进程数。用户访问会消耗进程然后为了满足后续访问随时随地开启进程保持空闲数为5。max_children 50
#最大进程数取决于你的服务器内存。 假设你打算给10G内存给当前配置的PHP-FPM Pool一般一个PHP请求占用内存10M-40M操作简繁程度不同按每个PHP请求占用内存25M这样max_children 10G/25M 409。所以这个值是根据硬件情况算出来的max_spare_servers10
#最大的多余进程。大规模断开后高并发访问过后还剩多少。max_requests 500
#每个子进程能响应的请求数量响应500次后杀死该进程进程处理完请求后会生成一部分缓存用来记录数据需要定时清理。
4.2、php-fpm实操配置
进入刚布置好的LNMP环境查看默认状态 编辑拓展配置文件
vim /etc/php-fpm.d/www.conf
修改对应参数如下pm dynamic
pm.start_servers 10
pm.max_children 100pm.min_spare_servers 10
pm.max_spare_servers 30
pm.max_requests 1000
修改效果 4.3、启动php状态监控页
vim /etc/php-fpm.d/www.conf
修改对应参数如下pm.status_path /php_status.php重启php-fpm然后使用浏览器访问设置的状态页面 5、Nginx Location
在LNMP的nginx配置中有三个配置位置
http{
此处为主配置文件在这里配置的参数将会影响本结构内所有虚拟主机
}server{
此处为虚拟主机配置文件配置内容只影响本台虚拟主机
}server{
location /1.html{
此处配置参数影响上方使用正则匹配到的页面
}
}
Location优先级 ^~ ~|~*|!~|!~* /
精确匹配字符开头正则匹配通配
6、Nginx Rewrite实战
Nginx Rewrite是Nginx服务器软件中的一个模块它允许用户根据一定的规则对请求的URL进行重写和重定向。通过使用Rewrite模块用户可以在服务器级别或特定位置级别配置重写规则以匹配和转换URL实现URL重定向、重写以及其他高级URL操作。主要用途包括以下几点
url伪静态化提升搜索引擎评分。隐藏部分url参数提高服务器安全性。实现网站地址跳转。
6.1、Rewrite示例1
要求
对当用户访问地址192.168.189.143/abc/a/1.html时,通过redirect重定向至192.168.189.143/ccc/bbb/2.html 使用全新安装的nginx此时默认主页为/usr/share/nginx/html/index.html
修改默认主页内容
[rootlocalhost ~]# vim /usr/share/nginx/html/index.html
[rootlocalhost ~]# cat /usr/share/nginx/html/index.html
h1/usr/share/nginx/html/index.html/h1访问效果如下 配置Nginx Rewrite
[rootlocalhost ~]# mkdir -p /usr/share/nginx/html/ccc/bbb
[rootlocalhost ~]# vim /usr/share/nginx/html/ccc/bbb/2.html
[rootlocalhost ~]# cat /usr/share/nginx/html/ccc/bbb/2.html
h1/usr/share/nginx/html/ccc/bbb/2.html/h1#此处写的是跳转终点的内容
修改网站配置文件
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name localhost;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location /abc{rewrite .* /ccc/bbb/2.html permanent;}
#最后一段为rewrite配置意为永久permanent将模糊匹配/abc里面带/abc的url跳转到/ccc/bbb/2.html
重启nginx访问abc页面 示例一结束
关于permanentpermanent 会将地址显示为新的URL地址重定向之后的URL上面的示例中为添加后的效果根据浏览器控制台可以看到添加上permanent后url被替换生成两次请求服务器的角色只转换了url客户端重新申请新获得的url。去掉permanenturl是旧的服务器内部转换请求url此处会略微增加服务器负载。如下图所示。(忽略404报错) 6.2、Rewrite示例2
要求利用正则中的”和\1 “ 替换url中一部分的内容。 将
http://192.168.189.143/2023/a/b/c/1.html 换成
http://192.168.189.143/2024/a/b/c/1.html
使用全新安装的nginx此时默认主页为/usr/share/nginx/html/index.html
步骤如下
[rootlocalhost ~]# mkdir -p /usr/share/nginx/html/2024/a/b/c
[rootlocalhost ~]# vim /usr/share/nginx/html/2024/a/b/c/2024abc.html
[rootlocalhost ~]# cat /usr/share/nginx/html/2024/a/b/c/2024abc.html
h1/usr/share/nginx/html/2024/a/b/c/2024abc.html/h1
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name localhost;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location /2023{rewrite ^/2023/(.*)$ /2024/$1 ;}# (.*)这是一个捕获组用于匹配任意字符并将其保存供后续引用
# $匹配字符串的结束位置。
# ^/2023/(.*)$匹配以 /2023/ 开头并以任意字符结尾的字符串
# $1引用第一个捕获组的值
重启nginx访问2023 6.3、Rewrite示例3
要求
location { rewrite } 只能替换url中的目录路径 使用if (){rewrite}可以替换协议主机目录全部能容。 将http://liumuquan.com 换http://jd.com
使用全新安装的nginx此时默认主页为/usr/share/nginx/html/index.html
步骤如下
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name localhost;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}if ($host ~* liumuquan.com) {rewrite .* http://jd.com;}if ($host ~* liumuquan.com)这个条件表示如果请求的主机名匹配正则表达式 liumuquan.com不区分大小写形式都会匹配。如果请求的主机名符合上述条件就会执行下面的重定向操作。rewrite .* http://jd.com;这条指令表示对匹配条件下的所有请求将URL重写为 http://jd.com。也就是说无论用户请求的是 liumuquan.com 下的任何路径比如 liumuquan.com/some/path都会被重定向到 http://jd.com。此实验需要配置hosts用来解析liumuquan.com
C:\Windows\System32\drivers\etc\hosts
访问liumuquan.com,查看浏览器控制台 6.4、Rewrite示例4
要求
替换掉域名中的主机保留后端url路径。可以使用nginx内置变量调用老的url目录路径。 如将http://liumuquan.com/ccc/bbb/2.html 换成 http://cloud.com/ccc/bbb/2.html
操作步骤
#建立一个新网站cloud.com
[rootlocalhost ~]# vim /etc/nginx/conf.d/cloud.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/cloud.conf
server{
listen 80;
server_name cloud_liumuquan.com;
location / {
root /cloud;
index index.html;
}
}[rootlocalhost ~]# mkdir -p /cloud/ccc/bbb
[rootlocalhost ~]# echo h1/cloud/index.html/h1 /cloud/index.html
[rootlocalhost ~]# echo h1/cloud/ccc/bbb/2.html/h1 /cloud/ccc/bbb/2.html
[rootlocalhost ~]# cat /cloud/index.html
h1/cloud/index.html/h1
[rootlocalhost ~]# cat /cloud/ccc/bbb/2.html
h1/cloud/ccc/bbb/2.html/h1[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf server {listen 80;server_name liumuquan.com;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}if ($host ~* liumuquan.com) {rewrite .* http://cloud_liumuquan.com$request_uri ;}#这里的变量来源均为nginx提供参考日志格式设置第一章..............
[rootlocalhost ~]# systemctl restart nginx尝试访问主页及其他url 6.5、Rewrite示例5
要求
1.输入的URL是目录时自动添加“/” http://www.baidu.com/abc
2.输入的URL是文件时不添加“/” http://www.baidu.com/abc/index.html
3.输入的URL是目录但已经添加/时不添加“/” http://www.baidu.com/abc/
步骤如下
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name liumuquan.com;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}if (-d $request_filename){rewrite ^(.*)([^/])$ http://$host$1$2/;}#if (-d $request_filename)这是一个 if 指令用于检查请求的文件路径是否为目录。如果请求的文件路径是一个目录则条件成立。如果请求的文件路径是一个目录就会执行下面的重定向操作。#rewrite ^(.*)([^/])$ http://$host$1$2/;这是一个 rewrite 指令用于对请求的 URL 进行重写。它的含义如下# ^(.*)([^/])$这是一个正则表达式用于匹配不以斜杠结尾的 URL 路径。(.*)([^/])表示匹配任意字符直到不是斜杠的字符并将其保存供后续引用。http://$host$1$2/如果请求的文件路径是一个目录且路径不以斜杠结尾那么请求的 URL 将被重定向到以斜杠结尾的同一路径并保持原始的协议和主机名不变。#是捕获器 $是行尾 准备用于测试的文件夹
[rootlocalhost ~]# mkdir /usr/share/nginx/html/dir
[rootlocalhost ~]# vim /usr/share/nginx/html/dir/index.html
[rootlocalhost ~]# cat /usr/share/nginx/html/dir/index.html
h1/usr/share/nginx/html/dir/index.html/h1
[rootlocalhost ~]# systemctl restart nginx访问http://liumuquan.com/dir 6.6、Rewrite示例6
将 http://liumuquan.com/dir/11-22-33/1.html 转换为
http://liumuquan.com/dir/11/22/33/1.html
步骤如下
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# mkdir -p /usr/share/nginx/html/dir/11/22/33/
[rootlocalhost ~]# echo h1/usr/share/nginx/html/dir/11/22/33/1.html/h1 /usr/share/nginx/html/dir/11/22/33/1.html
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name liumuquan.com;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location /dir{rewrite ^/dir/([0-9])-([0-9])-([0-9])(.*)$ /dir/$1/$2/$3$4;root /usr/share/nginx/html;}重启nginx使用浏览器访问 6.7、Rewrite示例7
要求
将http://alice.liumuquan.com 转换为http://www.liumuquan.com/alice
将http://jack.liumuquan.com 转换为http://www.liumuquan.com/jack
步骤如下
#环境准备
[rootlocalhost ~]# mkdir /usr/share/nginx/html/{jack,alice}
[rootlocalhost ~]# echo jack /usr/share/nginx/html/jack/index.html
[rootlocalhost ~]# echo alice /usr/share/nginx/html/alice/index.html
[rootlocalhost ~]# vim /etc/nginx/nginx.conf
修改配置文件
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name www.liumuquan.com alice.liumuquan.com jack.liumuquan.com;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}if ($host ~* ^www.liumuquan.com$){break;}if ($host ~* ^(.*)\.liumuquan\.com$){set $user $1;rewrite .* http://www.liumuquan.com/$user;}# 注意server_name处修改# 第一段if防止死循环# set $user $1;的含义是将变量 $user 的值设置为正则表达式捕获组 $1 的值# 客户端也需要添加域名解析
访问alice.liumuquan.com效果如下图 6.8、Rewrite示例8
要求如果访问服务器中的特殊文件。如.sh结尾的文件。则返回403操作拒绝错误
步骤如下
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# vim /usr/share/nginx/html/1.sh
[rootlocalhost ~]# cat /usr/share/nginx/html/1.sh
#!/bin/bash
pwd
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name liumuquan.com;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location ~* \.sh$ {return 403; } 访问1.sh效果如下 6.9、Rewrite示例9
要求
使用last实现重定向跳过暂时隐藏页面
演示步骤
[rootlocalhost ~]# mkdir /usr/share/nginx/html/test
[rootlocalhost ~]# echo break /usr/share/nginx/html/test/break.html
[rootlocalhost ~]# echo last /usr/share/nginx/html/test/last.html
[rootlocalhost ~]# echo test /usr/share/nginx/html/test/test.html
[rootlocalhost ~]# vim /etc/nginx/conf.d/default.conf
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 80;server_name liumuquan.com;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location /break {rewrite .* /test/break.html break;root /usr/share/nginx/html;}location /last {rewrite .* /test/last.html last; # 这个位置的last会导致重定向失效直接去往下面的重定向root /usr/share/nginx/html;}location /test {rewrite .* /test/test.html break;root /usr/share/nginx/html;}
访问/break 访问/last此处为重点 访问/test 7、CA与HTTPS
CACertificate Authority证书颁发机构和HTTPSHypertext Transfer Protocol Secure超文本传输安全协议之间有着密切的关系。
简要了解一下这两个概念
证书颁发机构CA是一个权威的第三方实体负责验证个人、组织或网站的身份并签发数字证书来证明其身份的合法性。HTTPS是一种通过加密和身份验证保护信息安全的通信协议。它使用SSL/TLS协议来创建加密连接确保在客户端和服务器之间传输的数据是安全的。
CA和HTTPS之间的关系可以总结如下
CA负责颁发SSL/TLS数字证书这些证书用于在HTTPS连接中进行身份验证和加密通信。当用户访问一个使用HTTPS的网站时浏览器会检查网站提供的数字证书是否由受信任的CA签发。如果证书是由受信任的CA签发的浏览器就会信任该证书建立安全连接并显示一个锁形状的图标表示连接是安全的。如果证书无效或由不受信任的CA签发浏览器会显示警告信息提示用户存在安全风险。 7.1、私有CA的https部署实战
准备存放证书和秘钥的目录
[rootlocalhost ~]# mkdir /etc/nginx/ssl使用openssl生成基于rsa数学算法长度为2048bit的秘钥,文件必须以key为结尾
centos8以前可以设置为1024长度本次演示用的是9故长度设置为2048
[rootlocalhost ~]# openssl genrsa 2048 /etc/nginx/ssl/server.key
使用秘钥文件生成证书-申请书
[rootlocalhost ~]# 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) []:SC
#省会
Locality Name (eg, city) [Default City]:CD
#城市
Organization Name (eg, company) [Default Company Ltd]:liumuquan
#组织
Organizational Unit Name (eg, section) []:cloud
#单位名
Common Name (eg, your name or your servers hostname) []:liumuquan.com
#网站名
Email Address []:123456qq.com
#邮箱
Please enter the following extra attributes
to be sent with your certificate request
A challenge password []:
#密码空
An optional company name []:
#密码空[rootlocalhost ~]# ls /etc/nginx/ssl/
server.csr证书申请 server.key私钥
同意申请生成证书
[rootlocalhost ~]# openssl req -x509 -days 365 -key /etc/nginx/ssl/server.key -in /etc/nginx/ssl/server.csr /etc/nginx/ssl/server.crt
Warning: Not placing -key in cert or request since request is used
Warning: No -copy_extensions given; ignoring any extensions in the request配置网站
[rootlocalhost ~]# cat /etc/nginx/conf.d/default.conf
server {listen 443 ssl;server_name liumuquan.com;#access_log /var/log/nginx/host.access.log main;ssl_certificate /etc/nginx/ssl/server.crt;ssl_certificate_key /etc/nginx/ssl/server.key;location / {root /usr/share/nginx/html;index index.html index.htm;}#error_page 404 /404.html;测试网站
[rootlocalhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[rootlocalhost ~]# nginx -s reload
[rootlocalhost ~]# s
Display all 287 possibilities? (y or n)
[rootlocalhost ~]# ss -antp | grep nginx
LISTEN 0 511 0.0.0.0:443 0.0.0.0:* users:((nginx,pid1863,fd12),(nginx,pid1862,fd12),(nginx,pid912,fd12))
[rootlocalhost ~]# 使用浏览器访问网站 选择接受风险并继续 7.2、公有CA
公有CACertificate Authority证书颁发机构是指受信任的第三方机构为申请证书的企业组织颁发证书并管理公共密钥和证书。这些证书必须符合严格的基准要求以确保网站的高安全性。
公有云网站都会提供数字证书管理服务SSL证书可以为自己的网站获取免费证书。
8、Nginx的平滑升级
升级会造成程序的关闭和启动平滑是指用户的访问不受影响。程序的升级就是替换文件替换文件不可能不影响用户所以所谓平滑只是相对影响较小。
操作方法是使用源码安装新版本必须使用源码然后使用kill -USR2重启nginxkill -USR2是一种特殊的重启是指服务完当前用户后重启此时并不处理新生成的链接重启后后续用户进入新版本。
(1)编译安装新版本的nginx指定安装目录为新目录
[rootserver nginx]# tar xf nginx-1.14.2.tar.gz -C /usr/local/src/
[rootserver nginx]# cd /usr/local/src/nginx-1.14.2/
[rootserver nginx-1.14.2]# ./configure --usernginx --groupnginx --prefix/usr/local/nginx14 --with-http_stub_status_module --with-http_ssl_module make make install
2查看就的nginx的主进程号和工作进程号
[rootserver ~]# 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[x] 是一个正则表达式中的特殊字符用于匹配前面字符 gin 后跟着任意一个字符的情况。因此ngin[x] 可以匹配到 nginx 这个关键词定位与 Nginx 服务相关的进程信息
3替换旧的执行程序
[rootserver ~]# mv /usr/local/nginx/sbin/nginx{,.bak}#移除并备份这个命令表示在文件名末尾添加 .bak 后缀[rootserver ~]# cp /usr/local/nginx14/sbin/nginx /usr/local/nginx/sbin/nginx #完成主程序替换[rootserver ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2#验证版本此时文件已经替换但nginx仍正常运行
4给主进程发送USR2信号
[rootserver ~]# cat /usr/local/nginx/logs/nginx.pid
68595
[rootserver ~]# kill -USR2 68595#查看nginx pid会出现一个nginx.pid.oldbin[rootserver ~]# cat /usr/local/nginx/logs/nginx.pid.oldbin
68595#给旧的进程发送一个kill -USR2的信号会启动一个新的nginx主进程实现热升级
5给进程发送WINCH信号
[rootserver ~]# kill -WINCH 68595#kill -WINCH“从容关闭”[rootserver ~]# 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