v1.1.4: 添加智能端口检测和自定义配置

主要更新:
- 自动检测端口占用(80, 443, 40001)
- 支持自定义HTTP/HTTPS/后端端口
- 端口冲突时智能提示并允许自定义
- 所有配置(Nginx、.env)自动使用自定义端口
- 完成提示中显示实际使用的端口
- 健康检查使用动态端口验证

功能特性:
- HTTP端口(默认80,可自定义如8080)
- HTTPS端口(默认443,可自定义如8443)
- 后端端口(默认40001,可自定义如40002)
- 使用netstat/ss检测端口占用
- 端口范围验证(1024-65535)
- 避免与现有项目冲突

更新内容:
- 全局变量: HTTP_PORT, HTTPS_PORT, BACKEND_PORT
- 新增: check_port_available() 检测端口
- 新增: configure_ports() 配置端口
- 修改: configure_nginx_http() 使用自定义端口
- 修改: configure_nginx_https() 使用自定义端口
- 修改: create_env_file() PORT使用BACKEND_PORT
- 修改: health_check() 检查自定义后端端口
- 修改: print_completion() 显示端口信息
- 新增: 一键部署命令.txt 到仓库
This commit is contained in:
WanWanYun
2025-11-10 23:57:16 +08:00
parent 4a38026c07
commit 49e0f53cce
2 changed files with 289 additions and 12 deletions

View File

@@ -34,6 +34,9 @@ ADMIN_PASSWORD=""
DOMAIN="" DOMAIN=""
USE_DOMAIN=false USE_DOMAIN=false
SSL_METHOD="" SSL_METHOD=""
HTTP_PORT="80"
HTTPS_PORT="443"
BACKEND_PORT="40001"
################################################################################ ################################################################################
# 工具函数 # 工具函数
@@ -532,6 +535,135 @@ install_pm2() {
print_success "PM2 安装完成" print_success "PM2 安装完成"
} }
################################################################################
# 端口检测和配置
################################################################################
check_port_available() {
local port=$1
if command -v netstat &> /dev/null; then
if netstat -tuln | grep -q ":${port} "; then
return 1 # 端口被占用
fi
elif command -v ss &> /dev/null; then
if ss -tuln | grep -q ":${port} "; then
return 1 # 端口被占用
fi
fi
return 0 # 端口可用
}
configure_ports() {
print_step "端口配置"
echo ""
# 检测80端口
if ! check_port_available 80; then
print_warning "检测到 80 端口已被占用"
echo ""
echo "80端口被其他服务占用您可以:"
echo -e "${GREEN}[1]${NC} 使用其他HTTP端口 (例如: 8080, 8888)"
echo -e "${GREEN}[2]${NC} 停止占用80端口的服务"
echo ""
read -p "请选择 [1-2]: " port_choice < /dev/tty
if [[ "$port_choice" == "1" ]]; then
while true; do
read -p "请输入HTTP端口 [建议: 8080]: " custom_port < /dev/tty
custom_port=${custom_port:-8080}
if [[ ! "$custom_port" =~ ^[0-9]+$ ]] || [[ $custom_port -lt 1024 ]] || [[ $custom_port -gt 65535 ]]; then
print_error "端口范围: 1024-65535"
continue
fi
if ! check_port_available $custom_port; then
print_error "端口 $custom_port 已被占用,请选择其他端口"
continue
fi
HTTP_PORT=$custom_port
print_success "将使用 HTTP 端口: $HTTP_PORT"
break
done
else
print_info "请手动停止占用80端口的服务后重新运行此脚本"
echo ""
print_info "查看端口占用: netstat -tunlp | grep :80"
print_info "或者: ss -tunlp | grep :80"
exit 1
fi
else
print_success "80 端口可用"
fi
# 检测443端口仅在使用HTTPS时需要
if [[ "$USE_DOMAIN" == "true" ]] && [[ "$SSL_METHOD" != "8" ]]; then
if ! check_port_available 443; then
print_warning "检测到 443 端口已被占用"
echo ""
while true; do
read -p "请输入HTTPS端口 [建议: 8443]: " custom_https_port < /dev/tty
custom_https_port=${custom_https_port:-8443}
if [[ ! "$custom_https_port" =~ ^[0-9]+$ ]] || [[ $custom_https_port -lt 1024 ]] || [[ $custom_https_port -gt 65535 ]]; then
print_error "端口范围: 1024-65535"
continue
fi
if ! check_port_available $custom_https_port; then
print_error "端口 $custom_https_port 已被占用,请选择其他端口"
continue
fi
HTTPS_PORT=$custom_https_port
print_success "将使用 HTTPS 端口: $HTTPS_PORT"
break
done
else
print_success "443 端口可用"
fi
fi
# 检测后端端口
if ! check_port_available 40001; then
print_warning "检测到 40001 端口已被占用"
echo ""
while true; do
read -p "请输入后端服务端口 [建议: 40002]: " custom_backend_port < /dev/tty
custom_backend_port=${custom_backend_port:-40002}
if [[ ! "$custom_backend_port" =~ ^[0-9]+$ ]] || [[ $custom_backend_port -lt 1024 ]] || [[ $custom_backend_port -gt 65535 ]]; then
print_error "端口范围: 1024-65535"
continue
fi
if ! check_port_available $custom_backend_port; then
print_error "端口 $custom_backend_port 已被占用,请选择其他端口"
continue
fi
BACKEND_PORT=$custom_backend_port
print_success "将使用后端端口: $BACKEND_PORT"
break
done
else
print_success "40001 端口可用"
fi
echo ""
print_info "端口配置摘要:"
echo " - HTTP端口: $HTTP_PORT"
if [[ "$USE_DOMAIN" == "true" ]] && [[ "$SSL_METHOD" != "8" ]]; then
echo " - HTTPS端口: $HTTPS_PORT"
fi
echo " - 后端端口: $BACKEND_PORT"
echo ""
}
################################################################################ ################################################################################
# 访问模式选择 # 访问模式选择
################################################################################ ################################################################################
@@ -977,7 +1109,7 @@ DATABASE_PATH=./data/database.db
STORAGE_ROOT=./storage STORAGE_ROOT=./storage
# 服务端口 # 服务端口
PORT=40001 PORT=${BACKEND_PORT}
# 环境 # 环境
NODE_ENV=production NODE_ENV=production
@@ -1028,7 +1160,7 @@ configure_nginx_http() {
cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF
server { server {
listen 80; listen ${HTTP_PORT};
server_name ${server_name}; server_name ${server_name};
# 前端静态文件 # 前端静态文件
@@ -1040,7 +1172,7 @@ server {
# 后端API # 后端API
location /api { location /api {
proxy_pass http://localhost:40001; proxy_pass http://localhost:${BACKEND_PORT};
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade; proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade'; proxy_set_header Connection 'upgrade';
@@ -1080,13 +1212,13 @@ EOF
configure_nginx_https() { configure_nginx_https() {
cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF
server { server {
listen 80; listen ${HTTP_PORT};
server_name ${DOMAIN}; server_name ${DOMAIN};
return 301 https://\$server_name\$request_uri; return 301 https://\$server_name:\${HTTPS_PORT}\$request_uri;
} }
server { server {
listen 443 ssl http2; listen ${HTTPS_PORT} ssl http2;
server_name ${DOMAIN}; server_name ${DOMAIN};
ssl_certificate /etc/nginx/ssl/${DOMAIN}.crt; ssl_certificate /etc/nginx/ssl/${DOMAIN}.crt;
@@ -1105,7 +1237,7 @@ server {
# 后端API # 后端API
location /api { location /api {
proxy_pass http://localhost:40001; proxy_pass http://localhost:${BACKEND_PORT};
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade; proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade'; proxy_set_header Connection 'upgrade';
@@ -1174,8 +1306,8 @@ health_check() {
fi fi
# 检查端口 # 检查端口
if netstat -tunlp | grep -q ":40001"; then if netstat -tunlp | grep -q ":${BACKEND_PORT}"; then
print_success "后端端口监听正常 (40001)" print_success "后端端口监听正常 (${BACKEND_PORT})"
else else
print_error "后端端口监听异常" print_error "后端端口监听异常"
return 1 return 1
@@ -1225,19 +1357,42 @@ print_completion() {
# 访问地址 # 访问地址
if [[ "$USE_DOMAIN" == "true" ]]; then if [[ "$USE_DOMAIN" == "true" ]]; then
if [[ "$SSL_METHOD" == "8" ]]; then if [[ "$SSL_METHOD" == "8" ]]; then
echo -e "${CYAN}访问地址:${NC} http://${DOMAIN}" if [[ "$HTTP_PORT" == "80" ]]; then
echo -e "${CYAN}访问地址:${NC} http://${DOMAIN}"
else
echo -e "${CYAN}访问地址:${NC} http://${DOMAIN}:${HTTP_PORT}"
fi
else else
echo -e "${CYAN}访问地址:${NC} https://${DOMAIN}" if [[ "$HTTPS_PORT" == "443" ]]; then
echo -e "${CYAN}访问地址:${NC} https://${DOMAIN}"
else
echo -e "${CYAN}访问地址:${NC} https://${DOMAIN}:${HTTPS_PORT}"
fi
fi fi
else else
PUBLIC_IP=$(curl -s ifconfig.me || curl -s icanhazip.com || echo "服务器IP") PUBLIC_IP=$(curl -s ifconfig.me || curl -s icanhazip.com || echo "服务器IP")
echo -e "${CYAN}访问地址:${NC} http://${PUBLIC_IP}" if [[ "$HTTP_PORT" == "80" ]]; then
echo -e "${CYAN}访问地址:${NC} http://${PUBLIC_IP}"
else
echo -e "${CYAN}访问地址:${NC} http://${PUBLIC_IP}:${HTTP_PORT}"
fi
fi fi
echo -e "${CYAN}管理员账号:${NC} ${ADMIN_USERNAME}" echo -e "${CYAN}管理员账号:${NC} ${ADMIN_USERNAME}"
echo -e "${CYAN}管理员密码:${NC} ********" echo -e "${CYAN}管理员密码:${NC} ********"
echo "" echo ""
# 端口信息
if [[ "$HTTP_PORT" != "80" ]] || [[ "$HTTPS_PORT" != "443" ]] || [[ "$BACKEND_PORT" != "40001" ]]; then
echo -e "${YELLOW}端口配置:${NC}"
echo " HTTP端口: $HTTP_PORT"
if [[ "$USE_DOMAIN" == "true" ]] && [[ "$SSL_METHOD" != "8" ]]; then
echo " HTTPS端口: $HTTPS_PORT"
fi
echo " 后端端口: $BACKEND_PORT"
echo ""
fi
# 常用命令 # 常用命令
echo -e "${YELLOW}常用命令:${NC}" echo -e "${YELLOW}常用命令:${NC}"
echo " 查看服务状态: pm2 status" echo " 查看服务状态: pm2 status"
@@ -1570,6 +1725,9 @@ main() {
# 选择访问模式 # 选择访问模式
choose_access_mode choose_access_mode
# 端口配置
configure_ports
# 配置管理员账号 # 配置管理员账号
configure_admin_account configure_admin_account

119
一键部署命令.txt Normal file
View File

@@ -0,0 +1,119 @@
╔═══════════════════════════════════════════════════════════════╗
║ ║
║ 🌩️ 玩玩云 - 一键部署命令 ║
║ ║
╚═══════════════════════════════════════════════════════════════╝
📦 项目地址:
https://gitee.com/yu-yon/vue-driven-cloud-storage
🚀 一键安装命令(在服务器上执行):
方式1 (推荐):
curl -fsSL https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash
方式2:
wget -qO- https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash
🗑️ 一键卸载命令(在服务器上执行):
方式1:
curl -fsSL https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash -s -- --uninstall
方式2:
wget -qO- https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash -s -- --uninstall
方式3 (如果已下载脚本):
bash install.sh --uninstall
✅ v1.1.3 更新说明:
- 集成一键卸载功能到主脚本
- 双重确认机制防止误删
- 可选数据备份功能
- 完整清理项目文件、PM2进程、Nginx配置、SSL证书
- 保留系统环境Node.js、Nginx、PM2、编译工具
- 智能残留检查
✅ v1.1.2 更新说明:
- 修复npm依赖安装失败问题
- 降级better-sqlite3以兼容Node.js 18.x+
- 自动安装C++编译工具gcc-c++, make, python3
- 确保native模块编译成功
✅ v1.1.1 更新说明:
- 升级到 Node.js 20.x LTS长期支持版本
- 解决 Node.js 18.x 即将停止支持的警告问题
- 更长的官方支持周期至2026年4月
- 更好的性能和安全性
✅ v1.1.0 更新说明:
- 新增支持 Rocky Linux、AlmaLinux、Fedora、openSUSE
- 实现统一包管理器检测,自动适配不同系统
- 优化阿里云镜像源配置,支持更多发行版
- 改进系统兼容性,未识别系统可自动检测包管理器
- 全面支持 APT、YUM、DNF、Zypper 包管理器
✨ 支持的操作系统:
- ✅ Ubuntu 18.04 / 20.04 / 22.04 / 24.04
- ✅ Debian 10 / 11 / 12
- ✅ CentOS 7 / 8
- ✅ RHEL 7 / 8 / 9
- ✅ Rocky Linux 8 / 9
- ✅ AlmaLinux 8 / 9
- ✅ Fedora 35+
- ✅ openSUSE Leap
- ✅ 其他基于以上系统的发行版
✨ 脚本特性:
- ✅ 完全自动化部署,无需手动操作
- ✅ 支持 Ubuntu/Debian/CentOS/RHEL/Rocky/AlmaLinux/Fedora/openSUSE
- ✅ 自动检测系统并安装对应依赖
- ✅ 自动安装 Node.js、Nginx、PM2 等依赖
- ✅ 提供官方源和阿里云镜像源选择
- ✅ 支持域名/IP 两种访问模式
- ✅ 6种SSL证书自动部署方案
- ✅ 智能容错和重试机制
- ✅ 健康检查和完整提示
📋 用户只需:
1. 选择软件源(官方/阿里云)
2. 选择访问模式(域名/IP
3. 如果是域名模式选择SSL证书方案
4. 输入管理员账号和密码
5. 等待自动部署完成
💡 使用示例:
1. 登录服务器SSH
2. 复制一键安装命令并执行
3. 按照提示输入选项(例如输入 2 选择阿里云源)
4. 等待部署完成
5. 访问您的域名或IP地址
📖 详细文档:
https://gitee.com/yu-yon/vue-driven-cloud-storage/blob/master/INSTALL_GUIDE.md
🔧 部署后管理:
- 查看服务状态: pm2 status
- 查看日志: pm2 logs wanwanyun-backend
- 重启服务: pm2 restart wanwanyun-backend
- 配置文件: /var/www/wanwanyun/backend/.env
📞 获取帮助:
- 提交Issue: https://gitee.com/yu-yon/vue-driven-cloud-storage/issues
- 查看文档: 项目README.md
═══════════════════════════════════════════════════════════════
祝您使用愉快! 🎉