阿里云服务器部署 Cloudreve 完整教程

阿里云服务器部署 Cloudreve 完整教程


一、环境准备

1.1 阿里云服务器要求

*最低配置建议:*

  • CPU: 1核

  • 内存: 1GB

  • 硬盘: 20GB+

  • 操作系统: Ubuntu 20.04 LTS / CentOS 7+ / Debian 10+

*推荐配置:*

  • CPU: 2核

  • 内存: 2GB+

  • 硬盘: 50GB+ SSD

  • 带宽: 3Mbps+

1.2 安全组配置

在阿里云控制台配置安全组规则,开放以下端口:

端口 协议 用途 授权对象
22 TCP SSH 远程连接 0.0.0.0/0 或指定IP
80 TCP HTTP 访问 0.0.0.0/0
443 TCP HTTPS 访问 0.0.0.0/0
5212 TCP Cloudreve 默认端口 127.0.0.1(仅本地)

*配置步骤:*

  1. 登录阿里云控制台

  2. 进入 ECS 实例详情

  3. 点击"安全组" -> "配置规则"

  4. 添加入方向规则,按上表配置


二、服务器初始化配置

2.1 连接到服务器

ssh root@your_server_ip

2.2 更新系统

*Ubuntu/Debian:*

apt update && apt upgrade -y

*CentOS:*

yum update -y

2.3 创建专用用户(推荐)

# 创建新用户

adduser cloudreve



# 添加到 sudo 组

usermod -aG sudo cloudreve  # Ubuntu/Debian

# 或

usermod -aG wheel cloudreve  # CentOS



# 切换到新用户

su - cloudreve

2.4 配置防火墙

*Ubuntu (UFW)😗

ufw allow 22/tcp

ufw allow 80/tcp

ufw allow 443/tcp

ufw enable

ufw status

*CentOS (firewalld)😗

firewall-cmd --permanent --add-service=ssh

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-service=https

firewall-cmd --reload


三、安装必要软件

3.1 安装 Nginx

*Ubuntu/Debian:*

apt install nginx -y

*CentOS:*

yum install epel-release -y

yum install nginx -y

启动 Nginx:

systemctl start nginx

systemctl enable nginx

systemctl status nginx

验证安装:浏览器访问 http://your_server_ip,应看到 Nginx 欢迎页面。

3.2 安装 MariaDB/MySQL(可选,用于持久化存储)

*Ubuntu/Debian:*

apt install mariadb-server mariadb-client -y

*CentOS:*

yum install mariadb-server mariadb-client -y

配置数据库:

systemctl start mariadb

systemctl enable mariadb

mysql_secure_installation

创建数据库和用户:

mysql -u root -p



CREATE DATABASE cloudreve CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE USER 'cloudreve'@'localhost' IDENTIFIED BY 'your_strong_password';

GRANT ALL PRIVILEGES ON cloudreve.* TO 'cloudreve'@'localhost';

FLUSH PRIVILEGES;

EXIT;

3.3 安装其他工具

# 安装 wget/curl

apt install wget curl unzip -y  # Ubuntu/Debian

# 或

yum install wget curl unzip -y  # CentOS



# 安装 systemd 服务管理工具(通常已预装)

apt install systemd -y


四、下载并配置 Cloudreve

4.1 下载 Cloudreve

访问 Cloudreve GitHub Releases 获取最新版本。

# 创建工作目录

mkdir -p ~/cloudreve

cd ~/cloudreve



# 下载最新版本(替换为实际版本号)

wget https://github.com/cloudreve/Cloudreve/releases/download/3.8.3/cloudreve_3.8.3_linux_amd64.tar.gz



# 解压

tar -zxvf cloudreve_3.8.3_linux_amd64.tar.gz



# 赋予执行权限

chmod +x cloudreve

4.2 首次运行

# 首次运行,生成配置文件和初始管理员账号

./cloudreve

*重要:* 首次运行后会显示管理员账号和密码,请妥善保存!

[Info]   2024-01-01 12:00:00 初始管理员账号:admin@cloudreve.org

[Info]   2024-01-01 12:00:00 初始管理员密码:xxxxxx

Ctrl+C 停止运行。

4.3 配置 Cloudreve

编辑配置文件 conf.ini

nano conf.ini

*基础配置示例:*

[System]

Mode = master

Listen = :5212

Debug = false

SessionSecret = your_random_secret_key_here

HashIDSalt = another_random_salt_here



[Database]

Type = mysql

Port = 3306

User = cloudreve

Password = your_strong_password

Host = 127.0.0.1

Name = cloudreve

TablePrefix = cd_

Charset = utf8mb4



[Redis]

Server = 127.0.0.1:6379

Password = 

DB = 0



[SSL]

Listen = :443

CertPath = /etc/nginx/ssl/your_domain.crt

KeyPath = /etc/nginx/ssl/your_domain.key



[UnixSocket]

Listen = /run/cloudreve/cloudreve.sock

Perm = 0660



[Proxy]

UseProxy = false

ProxyURL = 



[Advanced]

TempPath = temp

Version = 3.8.3

*简化配置(使用 SQLite,无需数据库):*

[System]

Mode = master

Listen = :5212

Debug = false



[Database]

Type = sqlite3

DBFile = cloudreve.db

4.4 创建数据目录

# 创建上传文件存储目录

mkdir -p ~/cloudreve/uploads

mkdir -p ~/cloudreve/avatar



# 设置权限

chmod 755 ~/cloudreve/uploads

chmod 755 ~/cloudreve/avatar

4.5 测试运行

./cloudreve

如果无错误输出,按 Ctrl+C 停止,继续下一步配置开机自启。


五、配置反向代理(Nginx)

5.1 创建 Nginx 配置文件

sudo nano /etc/nginx/sites-available/cloudreve

*HTTP 配置示例:*

server {

  listen 80;

  server_name your_domain.com www.your_domain.com;

  

  # 日志文件

  access_log /var/log/nginx/cloudreve_access.log;

  error_log /var/log/nginx/cloudreve_error.log;

  

  # 客户端最大上传大小

  client_max_body_size 10G;

  

  location / {

​    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

​    proxy_set_header Host $host;

​    proxy_set_header X-Real-IP $remote_addr;

​    proxy_redirect off;

​    proxy_pass http://127.0.0.1:5212;

​    

​    # WebSocket 支持

​    proxy_http_version 1.1;

​    proxy_set_header Upgrade $http_upgrade;

​    proxy_set_header Connection "upgrade";

  }

}

*HTTPS 配置示例(推荐):*

server {

  listen 80;

  server_name your_domain.com www.your_domain.com;

  return 301 https://$server_name$request_uri;

}



server {

  listen 443 ssl http2;

  server_name your_domain.com www.your_domain.com;

  

  # SSL 证书

  ssl_certificate /etc/nginx/ssl/your_domain.crt;

  ssl_certificate_key /etc/nginx/ssl/your_domain.key;

  ssl_protocols TLSv1.2 TLSv1.3;

  ssl_ciphers HIGH:!aNULL:!MD5;

  ssl_prefer_server_ciphers on;

  

  # 日志文件

  access_log /var/log/nginx/cloudreve_access.log;

  error_log /var/log/nginx/cloudreve_error.log;

  

  # 客户端最大上传大小

  client_max_body_size 10G;

  

  location / {

​    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

​    proxy_set_header Host $host;

​    proxy_set_header X-Real-IP $remote_addr;

​    proxy_redirect off;

​    proxy_pass http://127.0.0.1:5212;

​    

​    \# WebSocket 支持

​    proxy_http_version 1.1;

​    proxy_set_header Upgrade $http_upgrade;

​    proxy_set_header Connection "upgrade";

  }

}

5.2 启用配置

# 创建符号链接

sudo ln -s /etc/nginx/sites-available/cloudreve /etc/nginx/sites-enabled/



# 删除默认配置(可选)

sudo rm /etc/nginx/sites-enabled/default



# 测试配置

sudo nginx -t



# 重启 Nginx

sudo systemctl restart nginx


六、配置 HTTPS(可选但推荐)

6.1 使用 Let's Encrypt 免费证书

安装 Certbot:

*Ubuntu/Debian:*

apt install certbot python3-certbot-nginx -y

*CentOS:*

yum install certbot python3-certbot-nginx -y

获取证书:

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

按照提示完成验证,Certbot 会自动配置 Nginx。

6.2 自动续期

# 测试自动续期

sudo certbot renew --dry-run



# 添加定时任务

sudo crontab -e

添加以下内容:

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


七、设置开机自启

7.1 创建 Systemd 服务文件

sudo nano /etc/systemd/system/cloudreve.service

写入以下内容:

[Unit]

Description=Cloudreve File Manager

Documentation=https://docs.cloudreve.org

After=network.target

After=mariadb.service

Wants=mariadb.service



[Service]

User=cloudreve

Group=cloudreve

WorkingDirectory=/home/cloudreve/cloudreve

ExecStart=/home/cloudreve/cloudreve/cloudreve

Restart=on-failure

RestartSec=5s



# 安全增强

NoNewPrivileges=true

ProtectSystem=strict

ReadWritePaths=/home/cloudreve/cloudreve/uploads

ReadWritePaths=/home/cloudreve/cloudreve/avatar



[Install]

WantedBy=multi-user.target

7.2 启用服务

# 重新加载 systemd

sudo systemctl daemon-reload



# 启动 Cloudreve

sudo systemctl start cloudreve



# 设置开机自启

sudo systemctl enable cloudreve



# 查看状态

sudo systemctl status cloudreve



# 查看日志

sudo journalctl -u cloudreve -f


八、访问与管理

8.1 首次访问

  1. 浏览器访问:http://your_domain.comhttps://your_domain.com

  2. 使用首次运行时生成的管理员账号登录

  3. 立即修改默认密码

8.2 基本配置

登录后进行以下配置:

  1. *站点设置*
  • 站点名称

  • 站点描述

  • Logo 上传

  1. *存储策略*
  • 本地存储(默认)

  • 对象存储(阿里云 OSS、腾讯云 COS、七牛云等)

  1. *用户注册*
  • 是否允许公开注册

  • 注册邮箱验证

  • 初始容量设置

  1. *分享设置*
  • 分享有效期

  • 分享密码要求

8.3 创建普通用户

  1. 进入管理面板 -> 用户管理

  2. 点击"新建用户"

  3. 填写用户信息

  4. 设置存储空间配额

8.4 配置对象存储(推荐)

以阿里云 OSS 为例:

  1. 进入管理面板 -> 存储策略

  2. 点击"新建存储策略"

  3. 选择"阿里云 OSS"

  4. 填写配置:

  • Endpoint: oss-cn-hangzhou.aliyuncs.com

  • Bucket: 你的 bucket 名称

  • AccessKey ID

  • AccessKey Secret

  • 访问域名


九、常见问题排查

9.1 Cloudreve 无法启动

*检查日志:*

sudo journalctl -u cloudreve -n 50 --no-pager

*常见原因:*

  • 端口被占用:修改 conf.ini 中的监听端口

  • 权限问题:确保用户对目录有读写权限

  • 数据库连接失败:检查数据库配置和连接信息

9.2 Nginx 502 Bad Gateway

*检查 Cloudreve 是否运行:*

sudo systemctl status cloudreve

*检查端口监听:*

netstat -tlnp | grep 5212

*检查 Nginx 错误日志:*

sudo tail -f /var/log/nginx/cloudreve_error.log

9.3 上传文件失败

*检查目录权限:*

ls -la ~/cloudreve/uploads

chmod 755 ~/cloudreve/uploads

chown -R cloudreve:cloudreve ~/cloudreve/uploads

*检查 Nginx 配置:*

确保 client_max_body_size 设置足够大。

*检查 PHP/后端限制:*

如果使用对象存储,检查 OSS 配置。

9.4 HTTPS 证书问题

*检查证书路径:*

ls -la /etc/nginx/ssl/

*检查证书有效期:*

openssl x509 -in /etc/nginx/ssl/your_domain.crt -noout -dates

*手动续期:*

sudo certbot renew

sudo systemctl reload nginx

9.5 数据库连接错误

*检查 MariaDB 状态:*

sudo systemctl status mariadb

*测试数据库连接:*

mysql -u cloudreve -p cloudreve

*检查配置文件:*

确认 conf.ini 中的数据库配置正确。

9.6 性能优化建议

  1. *启用 Redis 缓存*
apt install redis-server -y

systemctl enable redis-server

conf.ini 中配置 Redis。

  1. *调整 Nginx 配置*
\# 启用 gzip 压缩

gzip on;

gzip_types text/plain text/css application/json application/javascript;



\# 静态文件缓存

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {

​    expires 30d;

​    add_header Cache-Control "public, immutable";

}

  1. *使用 CDN*

将静态资源通过 CDN 分发,提升访问速度。


十、备份与恢复

10.1 备份数据

*备份数据库:*

mysqldump -u cloudreve -p cloudreve > cloudreve_backup_$(date +%Y%m%d).sql

*备份上传文件:*

tar -czf uploads_backup_$(date +%Y%m%d).tar.gz ~/cloudreve/uploads

*备份配置文件:*

cp ~/cloudreve/conf.ini conf.ini.backup

10.2 恢复数据

*恢复数据库:*

mysql -u cloudreve -p cloudreve < cloudreve_backup_20240101.sql

*恢复上传文件:*

tar -xzf uploads_backup_20240101.tar.gz -C ~/cloudreve/


十一、安全建议

  1. *定期更新系统*
apt update && apt upgrade -y

  1. *使用强密码*
  • 管理员密码

  • 数据库密码

  • SSH 密钥认证

  1. *配置 Fail2Ban*
apt install fail2ban -y

systemctl enable fail2ban

  1. *限制 SSH 访问*

编辑 /etc/ssh/sshd_config

PermitRootLogin no

PasswordAuthentication no

AllowUsers cloudreve

  1. *定期备份*

设置自动化备份脚本和异地备份。

  1. *监控日志*

定期检查系统和服务日志,发现异常及时处理。


附录

A. 常用命令速查

\# 服务管理

sudo systemctl start cloudreve    # 启动

sudo systemctl stop cloudreve    # 停止

sudo systemctl restart cloudreve   # 重启

sudo systemctl status cloudreve   # 状态

sudo systemctl enable cloudreve   # 开机自启



\# 日志查看

sudo journalctl -u cloudreve -f   # 实时日志

sudo journalctl -u cloudreve -n 100 # 最近100行



\# Nginx 管理

sudo nginx -t            # 测试配置

sudo systemctl restart nginx     # 重启



\# 数据库管理

sudo systemctl status mariadb    # 状态

mysqldump -u cloudreve -p cloudreve > backup.sql  # 备份

B. 参考资源

C. 版本信息

  • 教程适用版本:Cloudreve 3.x

  • 最后更新:2026-04-30


posted @ 2026-04-30 22:33  PUYUNQIU  阅读(114)  评论(0)    收藏  举报