前提: 安装https前提下,必须要安装了nginx,没安装的看我上一篇博客 Centos7安装并配置nginx
1. https证书
这里我推荐两种方式
(1). 阿里云免费申请(有效期一年)
(2). acme.sh 脚本生成 (有效期三个月) (这也是我这里要用到的)
如果你用的是阿里云的证书,将证书的两个文件放到指定目录下,然后看nginx配置就行
这
两
种
生
成
的
证
书
是
有
差
距
的
,
具
体
什
么
差
距
,
就
去
问
度
娘
吧
\color{red}{这两种生成的证书是有差距的,具体什么差距,就去问度娘吧}
这两种生成的证书是有差距的,具体什么差距,就去问度娘吧,在不仅仅是追求“
能
用
\color{red}{能用}
能用”为主的情况下,还是根据自身情况来定,选择哪种证书
2. 安装acme
安装脚本
curl https://get.acme.sh | sh
这里安装报错了,因为系统缺少 acme.sh 所需要的依赖项,acme.sh 的依赖项主要是 socat,我们通过下面这个命令来安装这些依赖项,
然
后
重
新
执
行
安
装
脚
本
\color{red}{然后重新执行安装脚本}
然后重新执行安装脚本
Centos5以上
yum -q -y install openssl crontabs socat curl
Ubuntu/Debian
apt-get install openssl cron socat curl
alpine
apk --no-cache add -f openssl curl socat
kalilinux
apt-get -qqy install openssl cron socat curl
这里看到没有报错
3.生成证书
这里要注意下生成的时候,他会暂时占用80端口,请确保nginx已经关闭,并且没有其他进程占用80端口
先去把nginx关闭
# 进入nginx安装目录
cd /usr/local/nginx/sbin/
# nginx停止命令
./nginx -s stop
生成https证书命令, 注 意 安 装 a c m e . s h 的 位 置 , 我 的 是 在 / r o o t 目 录 下 \color{red}{注意安装acme.sh的位置,我的是在/root目录下} 注意安装acme.sh的位置,我的是在/root目录下
/root/.acme.sh/acme.sh --issue -d 替换成你的域名 --standalone --keylength ec-256 --force
证书更新
由于 Let’s Encrypt 的证书有效期只有 3 个月,因此需要 90 天至少要更新一次证书,acme.sh 脚本会每 60 天自动更新证书。也可以手动更新。
手动更新证书,执行:
还
是
注
意
安
装
a
c
m
e
.
s
h
的
位
置
,
我
的
是
在
/
r
o
o
t
目
录
下
\color{red}{还是注意安装acme.sh的位置,我的是在/root目录下}
还是注意安装acme.sh的位置,我的是在/root目录下
/root/.acme.sh/acme.sh --renew -d mydomain.com --force --ecc
安装证书和密钥
# 新建文件夹用来存储证书
mkdir /etc/https/
# 将证书安装到新建的文件夹
/root/.acme.sh/acme.sh --installcert -d 你的域名地址 --ecc \
--fullchain-file /etc/https/你的域名地址.crt \
--key-file /etc/https/你的域名地址.key
到此为止,证书就有了
接下来配置nginx
4. nginx配置
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;
# 端口转发 80端口强制跳转443
server {
listen 80 default_server;
server_name 你的域名;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen 443 ssl;
ssl_certificate /etc/https/你的域名.crt;
ssl_certificate_key /etc/https/你的域名.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name 你的域名;
location / {
root html; #站点目录,绝对路径
index index.html index.htm;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}