docker拉取Nginx创建网站
# 需求分析
因为使用宝塔的Nginx创建网站,我创建一个网站使用的宝塔上安装的Nginx,那么我再 创建一个网站的时候,就会出现各种情况,那么我重启失败的话就会影响别的网站
所以我希望能使用docker进行隔离,每一个网站都有自己的Nginx
相互之间互不影响.
# 首先创建第一个网站
# 一、创建项目目录结构
为第一个网站创建专门的目录
mkdir -p ~/qinglong-nginx/{conf.d,ssl,logs}
# 二、创建Nginx配置文件
vi ~/qinglong-nginx/conf.d/qinglong.conf
写入以下配置:
server {
listen 80;
server_name qinglong.cqzhz.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name qinglong.cqzhz.com;
# SSL配置
ssl_certificate /etc/nginx/ssl/qinglong.cqzhz.com.pem;
ssl_certificate_key /etc/nginx/ssl/qinglong.cqzhz.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# 反向代理配置
location / {
proxy_pass http://127.0.0.1:5885;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 日志配置
access_log /var/log/nginx/qinglong.access.log;
error_log /var/log/nginx/qinglong.error.log;}
# 三、申请SSL证书
我是contos系统,如果你们是别的系统,请自行变化命令
- 首先安装必要的依赖
yum install python3-pip
- 使用pip3安装certbot
pip3 install certbot
# 暂时停止可能占用80端口的服务 安装之间的操作
docker stop $(docker ps -q)
3.开始安装证书
certbot certonly --standalone
然后依次输入自己的邮箱和域名
最后就会得到这个成功的提示
4.成功的结果
Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/qinglong.cqzhz.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/qinglong.cqzhz.com/privkey.pem
把得到的证书复制到对应的文件夹下面
5.复制证书到nginx ssl目录
cp /etc/letsencrypt/live/qinglong.cqzhz.com/fullchain.pem ~/qinglong-nginx/ssl/qinglong.cqzhz.com.pem
cp /etc/letsencrypt/live/qinglong.cqzhz.com/privkey.pem ~/qinglong-nginx/ssl/qinglong.cqzhz.com.key
6.重启之前停止的服务
docker start $(docker ps -a -q)
# 四、创建并运行Nginx容器
docker run -d --name nginx-qinglong --restart=always --network host -v ~/qinglong-nginx/conf.d:/etc/nginx/conf.d -v ~/qinglong-nginx/ssl:/etc/nginx/ssl -v ~/qinglong-nginx/logs:/var/log/nginx nginx
至此访问域名就能得到一个https域名的网站了.
# 五、检查和测试
查看容器状态
docker ps | grep nginx-qinglong
# 六、设置证书自动续期
vi ~/qinglong-nginx/renew-cert.sh
粘贴以下全部内容
`certbot renew
cp /etc/letsencrypt/live/qinglong.cqzhz.com/fullchain.pem ~/qinglong-nginx/ssl/qinglong.cqzhz.com.pem
cp /etc/letsencrypt/live/qinglong.cqzhz.com/privkey.pem ~/qinglong-nginx/ssl/qinglong.cqzhz.com.key
docker exec nginx-qinglong nginx -s reload`
添加执行权限:
chmod +x ~/qinglong-nginx/renew-cert.sh
添加到定时任务:
首先执行
crontab -e
然后粘贴内容:
`# 添加以下行(每月1日凌晨2点执行)
0 2 1 * * /root/qinglong-nginx/renew-cert.sh`
# 七、新增网站配置
# 1. 创建项目目录结构
为gotify网站创建专门的目录:
mkdir -p ~/gotify-nginx/{conf.d,ssl,logs}
# 2. 创建Nginx配置文件
vi ~/gotify-nginx/conf.d/gotify.conf
写入以下配置:
server {
listen 80;
server_name gotify.cqzhz.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name gotify.cqzhz.com;
# SSL配置
ssl_certificate /etc/nginx/ssl/gotify.cqzhz.com.pem;
ssl_certificate_key /etc/nginx/ssl/gotify.cqzhz.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# 反向代理配置
location / {
proxy_pass http://127.0.0.1:8358;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 日志配置
access_log /var/log/nginx/gotify.access.log;
error_log /var/log/nginx/gotify.error.log;
}
# 3. 申请SSL证书
# 暂时停止所有容器
docker stop $(docker ps -q)
# 申请证书
certbot certonly --standalone -d gotify.cqzhz.com
# 复制证书到nginx ssl目录
cp /etc/letsencrypt/live/gotify.cqzhz.com/fullchain.pem ~/gotify-nginx/ssl/gotify.cqzhz.com.pem
cp /etc/letsencrypt/live/gotify.cqzhz.com/privkey.pem ~/gotify-nginx/ssl/gotify.cqzhz.com.key
# 重启之前的容器
docker start $(docker ps -a -q)
# 4. 创建并运行Nginx容器
docker run -d --name nginx-gotify --restart=always --network host -v ~/gotify-nginx/conf.d:/etc/nginx/conf.d -v ~/gotify-nginx/ssl:/etc/nginx/ssl -v ~/gotify-nginx/logs:/var/log/nginx nginx
# 5. 检查和测试
查看容器状态:
docker ps | grep nginx-gotify
# 6. 设置证书自动续期
创建续期脚本:
vi ~/gotify-nginx/renew-cert.sh
粘贴以下内容:
#!/bin/bash certbot renew cp /etc/letsencrypt/live/gotify.cqzhz.com/fullchain.pem ~/gotify-nginx/ssl/gotify.cqzhz.com.pem cp /etc/letsencrypt/live/gotify.cqzhz.com/privkey.pem ~/gotify-nginx/ssl/gotify.cqzhz.com.key docker exec nginx-gotify nginx -s reload
添加执行权限:
chmod +x ~/gotify-nginx/renew-cert.sh
添加到定时任务:
crontab -e
然后粘贴内容:
# 添加以下行(每月1日凌晨2点执行) 0 2 1 * * /root/gotify-nginx/renew-cert.sh
至此,新网站 gotify.cqzhz.com 的配置就完成了。请确保:
- 域名已正确解析到服务器IP
- gotify服务(8358端口)已经正常运行
- 80和443端口未被占用
- 证书申请成功并正确复制到指定目录
- 01
- 免费的在线logo设计,uugai找了我好就12-24
- 02
- Untitled12-24