标签:domain sub nginx local 证书 centos7 ssl com
* nginx 配置 ssl 证书前需要先安装 SSL 模块,本次操作是在已经安装 nginx 的基础上配置,先安装 SSL 模块,来到 nginx 源码目录,我的是这里,执行进入到自己的安装目录:
cd /usr/local/nginx/nginx-1.8.0
执行语句,重新安装 ssl 模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
如果报 error,一般是没有安装 openssl openssl-devel 导致的,先安装 openssl openssl-devel(没有报错直接跳过)
openssl openssl-devel
然后再:(不可 install )
make
等待执行完成后,我们需要把新编译的nginx模块替换原来的nginx,先备份(记得看好自己的安装路径)
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
关闭nginx(因为要把新的模块覆盖旧的nginx)
可以直接:ps aux | grep nginx 查看端口后:kill -9 xxxx
关闭nginx进程后就可以开始替换了(注意:我当前的位置是在我nginx的源码包中,目录不要搞错了)
cp ./objs/nginx /usr/local/nginx/sbin/
先切换到sbin目录,检测nginx的配置文件是否有错误
cd /usr/local/nginx/sbin/ ./niginx -t
看到这个就是表示 ssl 模块安装成功。
========================
开始使用 Let’s Encrypt 安装证书:
安装 Certbot 客户端:
yum install -y epel-release yum install -y certbot
因为使用 Certbot 获取证书时,Let's Encrypt 服务器会访问 http://sub.domain.com/.well-known 来验证你的域名服务器,因此需要修改 nginx 配置文件,配置 .well-known 指向本地一个目录:
server { ...... location /.well-known { alias /usr/local/nginx/html/.well-known; } ...... }
然后就可以使用 certbot 命令来获取证书了,获取证书时需要输入你的Email并接受用户条款。需要注意:-w 指定的 web 目录需要和前边 nginx 配置的 .well-known 的本地目录一致(/usr/local/nginx/html):
#记得把 sub.domain.com 替换成自己的域名;同时把 xxxxxx@xxx.com 替换成自己的邮箱
certbot certonly --webroot -w /usr/local/nginx/html/ -d sub.domain.com -m xxxxxx@xxx.com --agree-tos
-w 指定 webroot 目录
-d domain 想要获取的证书域名,支持多个域名
但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另外一种模式 --standalone,这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。
certbot certonly --standalone -d sub.domain.com -m xxxxxx@xxx.com --agree-tos //记得替换域名和邮箱
如果成功获取证书,你的密钥和证书存放在 /etc/letsencrypt/live/sub.domain.com/ 目录(sub.domain.com 目录是自己的域名),可以看到如下:
ll /etc/letsencrypt/live/sub.domain.com/ cert.pem -> ../../archive/sub.domain.com/cert1.pem chain.pem -> ../../archive/sub.domain.com/chain1.pem fullchain.pem -> ../../archive/sub.domain.com/fullchain1.pem privkey.pem -> ../../archive/sub.domain.com/privkey1.pem
有时需要删除已生成的证书,重新生成。可使用如下命令进行删除:
certbot delete --cert-name sub.domain.com //记得替换自己的域名
生成 dhparam(这一步花费时间比较多,而且有时候会失败,报找不到 sites-enabled 目录,建议提前在 /etx/nginx/ 目录下新建好 sites-enable 目录)
openssl dhparam -out /etc/nginx/sites-enabled/dh4096.pem 4096
做完以上步骤后,在我们:/usr/local/nginx/conf 和 /etc/nginx 目录下,都会有 conf 目录下的相同文件,如下:
通常我们配置 /usr/local/nginx/conf 下的即可,/etc/nginx 下的也会同步更新
接下来开始配置我们的 nginx.conf 文件:
server { listen 80; server_name sub.domain.com; rewrite ^ https://$server_name$request_uri? permanent; } server { listen 443 ssl; server_name sub.domain.com; include /etc/nginx/sites-enabled/sub.domain.com.ssl; location / { try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; proxy_pass http://127.0.0.1:8080; } }
sub.domain.com.ssl 文件配置内容:
ssl on; ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem; ssl_prefer_server_ciphers on; ssl_dhparam /etc/nginx/sites-enabled/dhparam.pem; //这里的 dhparam.pem 去目录下自己查看,替换一下 ssl_ciphers HIGH:!ADH:!MD5:!aNULL:!eNULL:!MEDIUM:!LOW:!EXP:!kEDH; ssl_protocols TLSv1.2 TLSv1.1 TLSv1; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_stapling on; ssl_stapling_verify on; add_header Strict-Transport-Security max-age=15768000;
之后重启 nginx ,到此 nginx 配置 https 完成。
启动:/usr/local/nginx/sbin/nginx 停止:/usr/local/nginx/sbin/nginx -s stop 重启:/usr/local/nginx/sbin/nginx -s reload
如果配置了服务:(可以看我上一篇文章怎么配置服务)
启动:systemctl start nginx.service 停止:systemctl stop nginx.service 重启:systemctl restart nginx.service
标签:domain,sub,nginx,local,证书,centos7,ssl,com 来源: https://www.cnblogs.com/xuehuashanghe/p/16307784.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。