ai-要約を取得 文章摘要

首先,官网的docker部署配置如下:

docker run -d \
--name=lychee \
-v /host_path/lychee/conf:/conf \
-v /host_path/lychee/uploads:/uploads \
-v /host_path/lychee/sym:/sym \
-e PUID=1000 \
-e PGID=1000 \
-e PHP_TZ=America/New_York \
-e TIMEZONE=America/New_York \
-e DB_CONNECTION=mysql \
-e DB_HOST=mariadb \
-e DB_PORT=3306 \
-e DB_DATABASE=lychee \
-e DB_USERNAME=user \
-e DB_PASSWORD=password \
-p 90:80 \
--net network_name \
--link db_name \
lycheeorg/lychee

在 1GB 内存的 EC2 免费版实例上同时运行 MySQL、Node.js + Express 后端 API,再加上使用 Docker 部署 Lychee + MySQL,内存负载可能会非常高,甚至导致 OOM(Out of Memory)错误,进程被系统强制终止。

正如大家所知道的那样 mysql 比较吃内存,如果docker当中再添加mysql,那会原本就不富裕的1g内存ec2实例雪上加霜,因此采取下面的方案-使用原本已经安装好的docker外部的mysql(当然去创建一个lychee表是要的):

docker run -d \
--name=lychee \
-v /opt/docker/lychee/conf:/conf \
-v /opt/docker/lychee/uploads:/uploads \
-v /opt/docker/lychee/sym:/sym \
-e PUID=1000 \
-e PGID=1000 \
-e PHP_TZ=America/New_York \
-e TIMEZONE=America/New_York \
-e DB_CONNECTION=mysql \
-e DB_HOST=35.164.210.202 \
-e DB_PORT=3306 \
-e DB_DATABASE=lychee \
-e DB_USERNAME=iooio \
-e DB_PASSWORD=5320di \
-e APP_URL=https://lychee.iooio.cn \ # 这里有个大坑!! 已填平,后期会发布
-e APP_FORCE_HTTPS=true \
-e TRUSTED_PROXIES=35.164.210.202 \
-p 90:80 \
lycheeorg/lychee

其中,-p 90:80 不能少一个,否则会导致lychee部署后不显示图片(what?),别问我是怎么知道的。

这里插播一下,EC2 免费实例默认 无 SWAP,可以手动增加:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

这里看似简单,实则一点也不难,很快搞定。

docker 常用命令如下:

# 查看所有(包括已停止)的容器
docker ps -a

# 运行指定容器
docker start lychee

# 停止容器
docker stop lychee

# 删除容器
docker rm lychee

# 删除所有镜像
docker rmi $(docker images -q)

# 删除所有数据卷
docker volume prune

# 删除所有自定义网络(注意:不会删除默认网络)
docker network prune

接下来,一起配置一下nginx反向代理

📌 1. 确保 Nginx 已安装

如果你还没有安装 Nginx,可以执行:

sudo apt update
sudo apt install nginx -y

然后启动并设置开机自启:

sudo systemctl start nginx
sudo systemctl enable nginx

📌 2. 获取 Lychee 容器的 IP 或端口

如果 Lychee 运行在 Docker 中,你需要找到它的内部 IP 或端口

1:检查 Docker 容器的端口

执行:

docker ps

输出示例:

CONTAINER ID   IMAGE              COMMAND                  CREATED       STATUS                 PORTS                               NAMES
40f57733218b lycheeorg/lychee "/entrypoint.sh nginx" 4 hours ago Up 3 hours (healthy) 0.0.0.0:90->80/tcp, :::90->80/tcp lychee

这表示:

  • Lychee 运行在容器(NAMES) lychee
  • 宿主机 90 端口 映射到 容器的 80 端口

如果你的 Lychee 没有暴露端口,可以检查 IP。

2:获取 Docker 内部 IP

如果你使用了 Docker 网络(bridge 模式),可以获取容器 IP:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' lychee

返回实例:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' lychee
172.17.0.2

假设返回 172.17.0.2,说明 Lychee 在 172.17.0.2:80


📌 3. 创建一个新的 Nginx 配置文件

你可以在 /etc/nginx/sites-available/ 下创建一个新的配置文件,比如 lychee.conf

sudo nano /etc/nginx/sites-available/lychee.conf

配置文件内容(HTTP 版本,稍后配置 HTTPS)

server {
listen 80;
server_name yourdomain.com;

location / {
proxy_pass http://localhost:90; # 如果 Lychee 运行在 90 端口
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;
}
}
额外配置 - nginx 对文件大小的默认限制

默认情况下,Nginx 对文件上传的大小是没有限制的,通常默认大小是 1MB(如果没有明确设置)。为了避免文件上传时遇到 413 Request Entity Too Large 错误,你可以手动添加这个配置。

在 Nginx 中,client_max_body_size 参数通常会放在 http 配置块、server 配置块或者 location 配置块中。默认情况下,Nginx 没有设置文件大小限制,所以可能是其他地方的配置导致了文件上传限制。

添加 client_max_body_size 参数

你可以在 nginx.conf 中的 http 配置块,或者你的网站相关的 serverlocation 配置块中加入这个参数。

  1. http 配置块中添加

    找到 http 配置块,并在里面添加 client_max_body_size,例如:

    http {
    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;

    # 增加上传文件大小限制
    client_max_body_size 100M; # 设置为100MB,或根据你的需求进行调整

    # 其他配置...
    }
  1. server 配置块中添加

    你也可以将它放在 server 配置块中,特别是如果你只想限制某个特定站点或虚拟主机的上传大小:

    server {
    listen 80;
    server_name example.com;

    # 增加上传文件大小限制
    client_max_body_size 100M;

    # 其他配置...
    }
  1. location 配置块中添加

    如果你只想对某个特定路径或 location 进行限制,也可以在相应的 location 配置中设置:

    location /upload {
    client_max_body_size 100M;
    # 其他配置...
    }

完整示例:

假设你希望在整个网站上都能支持上传更大的文件,可以将 client_max_body_size 加入 http 配置块:

http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;

client_max_body_size 100M; # 设置为100MB

include /etc/nginx/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 /var/log/nginx/access.log main;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

重新加载 Nginx 配置:

修改配置文件后,记得检查配置是否正确,并重启 Nginx 使更改生效。

  1. 检查 Nginx 配置是否正确:

    sudo nginx -t
  2. 重启 Nginx:

    sudo systemctl restart nginx

或者:

sudo service nginx restart

通过这些设置,你应该能够解决 413 Request Entity Too Large 错误。

💡 如果 Lychee 运行在 Docker 内部 IP(比如 172.17.0.2),请修改:

proxy_pass http://172.17.0.2:80;
关于内外端口

解释:

Docker 容器运行时,可以选择是否将容器的端口映射到宿主机的端口。没有进行端口映射的容器只能在容器内进行访问,外部的服务或用户无法通过宿主机的 IP 地址或端口直接访问到容器内的服务。

例如,下面的命令仅在容器内启动服务,没有将端口映射到宿主机:

docker run -d --name=lychee lycheeorg/lychee

在这种情况下,容器内的 Lychee 服务可能运行在 80 端口,但外部无法直接访问它。

端口映射:

如果你希望外部能够访问容器中的服务,必须进行 端口映射。通过 -p 参数来实现:

docker run -d --name=lychee -p 90:80 lycheeorg/lychee

在这个例子中,容器的 80 端口映射到宿主机的 90 端口。这样,外部可以通过宿主机的 IP 地址和端口 90 来访问容器内的 Lychee 服务。

总结:

  • 没有端口映射:服务仅能在容器内访问,外部无法访问。
  • 有端口映射:容器内的服务可以通过宿主机的 IP 地址和映射的端口访问。

如果你希望从外部访问 Lychee 服务,必须将端口映射到宿主机。


📌 4. 启用新的 Nginx 配置

创建软链接

sudo ln -s /etc/nginx/sites-available/lychee.conf /etc/nginx/sites-enabled/

这条命令的作用是创建一个符号链接(软链接),用于启用 Nginx 的站点配置

点击查看详情 - 为什么创建软连接

📌 详细解释

  • ln -s:创建符号链接(软链接)。
  • /etc/nginx/sites-available/lychee.conf:Lychee 的 Nginx 配置文件,存放在 sites-available 目录中(仅存储,不默认启用)。
  • /etc/nginx/sites-enabled/:Nginx 会自动加载 sites-enabled 目录下的所有配置文件。

作用: ✅ sites-available 目录存放所有 Nginx 配置文件,但默认不会生效。
sites-enabled 目录中的文件才会被 Nginx 识别并启用。
ln -slychee.confsites-enabled 里有一个“快捷方式”,Nginx 读取它,就能启用 Lychee 的反向代理配置。


📌 为什么要用符号链接?

  • 管理方便:你可以在 sites-available 里集中管理所有站点配置,启用/禁用只需创建或删除软链接,不用改动原配置。

  • 快速启用/禁用站点

    • 禁用 Lychee 站点(但不删除配置):

      sudo rm /etc/nginx/sites-enabled/lychee.conf
      sudo systemctl reload nginx
    • 再次启用:

      sudo ln -s /etc/nginx/sites-available/lychee.conf /etc/nginx/sites-enabled/
      sudo systemctl reload nginx

📝 需要注意
执行 ln -s 之后,建议运行:

sudo nginx -t

如果返回 syntax is ok,再重启 Nginx:

sudo systemctl restart nginx

测试 Nginx 配置

sudo nginx -t

如果没有错误,重启 Nginx:

sudo systemctl restart nginx

测试 HTTP 访问

在浏览器输入:

http://yourdomain.com

如果页面正常打开,说明反向代理成功!

📌 5. 启用 HTTPS(使用 Let’s Encrypt)

现在,我们使用 Certbot 申请 SSL 证书。

安装 Certbot

sudo apt install certbot python3-certbot-nginx -y

申请证书

sudo certbot --nginx -d yourdomain.com

Certbot 会自动: ✅ 申请 fullchain.pem 证书
✅ 修改 Nginx 配置,启用 HTTPS
✅ 重启 Nginx

如果成功,你会看到:

Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/yourdomain.com/fullchain.pem
我记得还有一个命令,可以为所有配置文件中的域名自动配置证书

如果你想为所有配置文件中已配置的 域名 自动申请 SSL 证书,可以使用以下命令:

sudo certbot --nginx

📌 详细解释

  • sudo certbot --nginx:这个命令会自动扫描 Nginx 配置文件 中的所有域名,并为它们申请 SSL 证书。Certbot 会自动为配置文件中的每个域名配置 SSL,并自动更新 Nginx 配置启用 HTTPS。
  • -d:如果你希望指定单独的域名,可以使用 -d yourdomain.com。但如果你希望 所有 配置文件中列出的域名都可以自动申请证书,--nginx 就足够了,它会自动识别 Nginx 配置中的域名。

📌 步骤

  1. 运行命令:

    sudo certbot --nginx
  2. Certbot 会自动扫描 Nginx 的配置文件并列出所有找到的域名。你会看到类似这样的提示:

    Which names would you like to activate HTTPS for?
  3. 输入要为其启用 HTTPS 的域名,或选择 所有 域名。

  4. Certbot 会自动申请证书并为所有选定的域名配置 HTTPS。

📌 Certbot 的工作流程

  1. Certbot 会扫描 Nginx 配置文件,找出所有列出的域名。
  2. 它会为这些域名自动申请 Let’s Encrypt 的免费 SSL 证书。
  3. 然后 Certbot 会更新 Nginx 配置文件,启用 SSL,重启 Nginx,使得新证书生效。

📌 自动续期

完成上述操作后,Certbot 会设置好自动续期机制,无需手动干预。

如果你希望确保自动续期工作正常,可以执行:

sudo certbot renew --dry-run

这会模拟续期过程并确保一切正常。


总结

使用 sudo certbot --nginx 命令,可以为 所有 Nginx 配置文件中列出的域名 自动申请并配置 SSL 证书。这是一个方便的方式,特别是当你有多个域名需要配置时。


📌 6. 确保 HTTPS 访问生效

你可以手动检查 lychee.conf(Certbot 会自动修改它):

sudo nano /etc/nginx/sites-available/lychee.conf

应该包含类似的 HTTPS 配置:

server {
listen 443 ssl;
server_name yourdomain.com;

ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

location / {
proxy_pass http://127.0.0.1:8080;
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;
}
}

server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}

💡 这里的 301 重定向会将 HTTP 访问强制跳转到 HTTPS。

最后,重启 Nginx:

sudo systemctl restart nginx

📌 7. 设置自动续期

Let’s Encrypt 证书有效期为 90 天,你可以使用以下命令测试自动续期:

sudo certbot renew --dry-run

如果没问题,就可以定期自动更新:

sudo crontab -e

添加:

0 3 * * * certbot renew --quiet && systemctl reload nginx

ps:手动运行 Certbot 续期命令来测试是否会为新域名自动续期:

sudo certbot renew --dry-run

这个命令真的很好用,实测之前配置过自动续期的,新添加的域名也会同样自动续期。

这是一个定时任务 - 点击查看详情

这条命令是一个定时任务(Cron job),它的作用是 每天凌晨 3 点自动检查并续期 SSL 证书,并且在续期成功后重新加载 Nginx 配置。具体来说:

📌 详细解释

  1. 0 3 \* \* \*:这是 Cron 的时间设置部分,表示:
    • 0:分钟,指在每小时的第 0 分钟。
    • 3:小时,指每天凌晨 3 点。
    • * * *:分别表示日期、月份、星期,* 代表任意值。所以这部分表示每天的 3 点。
  2. certbot renew --quiet
    • certbot renew:用于检查并自动续期 SSL 证书。
    • --quiet:表示只输出必要的信息,避免显示过多的日志,减少不必要的输出。
  3. &&:表示“如果前一个命令成功,则执行后面的命令”。
  4. systemctl reload nginx
    • systemctl reload nginx:重新加载 Nginx 配置,使得新续期的证书生效。

📌 整体作用

这条命令的作用是 每天自动检测 SSL 证书是否需要续期,如果需要,certbot 会自动完成续期操作,并且 重新加载 Nginx 配置 使得新的证书生效。


📌 如何添加 Cron 任务

要确保每天自动执行这个任务,你需要将这条命令添加到 Cron 表中。具体步骤:

  1. 打开 Cron 表:

    sudo crontab -e
  2. 在文件的末尾添加:

    0 3 * * * certbot renew --quiet && systemctl reload nginx
  3. 保存并退出编辑器(通常是按 Ctrl + X,然后按 Y 确认保存)。


📌 测试 Cron 任务

你可以通过以下命令手动运行 certbot renew,测试是否能正确续期:

sudo certbot renew --dry-run

如果没有问题,表示证书续期工作正常,Cron 任务也会顺利执行。


📌 额外注意

  • 证书有效期:Let’s Encrypt 的证书有效期是 90 天,所以定期续期非常重要。

  • Cron 的日志

    :如果你想查看 Cron 任务的执行日志,可以在

    /var/log/syslog

    中查找与 Cron 相关的日志:

    grep CRON /var/log/syslog

📌 8. 访问网站测试 HTTPS

在浏览器输入:

arduino


复制编辑
https://yourdomain.com

如果能看到 Lychee 并且是 HTTPS 🔒,说明一切配置成功!


📌 9. 总结

1️⃣ 检查 Lychee 的 Docker 端口/IP
2️⃣ 创建新的 Nginx 配置文件 /etc/nginx/sites-available/lychee.conf
3️⃣ 测试 HTTP 反向代理是否成功
4️⃣ 使用 Certbot 申请 SSL 证书,启用 HTTPS
5️⃣ 重启 Nginx,确保 HTTPS 访问正常
6️⃣ 配置自动续期,避免证书过期

现在,你的 Lychee 就可以通过 Nginx 反向代理,并使用 HTTPS 访问