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:
182
install.sh
182
install.sh
@@ -34,6 +34,9 @@ ADMIN_PASSWORD=""
|
||||
DOMAIN=""
|
||||
USE_DOMAIN=false
|
||||
SSL_METHOD=""
|
||||
HTTP_PORT="80"
|
||||
HTTPS_PORT="443"
|
||||
BACKEND_PORT="40001"
|
||||
|
||||
################################################################################
|
||||
# 工具函数
|
||||
@@ -532,6 +535,135 @@ install_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
|
||||
|
||||
# 服务端口
|
||||
PORT=40001
|
||||
PORT=${BACKEND_PORT}
|
||||
|
||||
# 环境
|
||||
NODE_ENV=production
|
||||
@@ -1028,7 +1160,7 @@ configure_nginx_http() {
|
||||
|
||||
cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF
|
||||
server {
|
||||
listen 80;
|
||||
listen ${HTTP_PORT};
|
||||
server_name ${server_name};
|
||||
|
||||
# 前端静态文件
|
||||
@@ -1040,7 +1172,7 @@ server {
|
||||
|
||||
# 后端API
|
||||
location /api {
|
||||
proxy_pass http://localhost:40001;
|
||||
proxy_pass http://localhost:${BACKEND_PORT};
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
@@ -1080,13 +1212,13 @@ EOF
|
||||
configure_nginx_https() {
|
||||
cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF
|
||||
server {
|
||||
listen 80;
|
||||
listen ${HTTP_PORT};
|
||||
server_name ${DOMAIN};
|
||||
return 301 https://\$server_name\$request_uri;
|
||||
return 301 https://\$server_name:\${HTTPS_PORT}\$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen ${HTTPS_PORT} ssl http2;
|
||||
server_name ${DOMAIN};
|
||||
|
||||
ssl_certificate /etc/nginx/ssl/${DOMAIN}.crt;
|
||||
@@ -1105,7 +1237,7 @@ server {
|
||||
|
||||
# 后端API
|
||||
location /api {
|
||||
proxy_pass http://localhost:40001;
|
||||
proxy_pass http://localhost:${BACKEND_PORT};
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
@@ -1174,8 +1306,8 @@ health_check() {
|
||||
fi
|
||||
|
||||
# 检查端口
|
||||
if netstat -tunlp | grep -q ":40001"; then
|
||||
print_success "后端端口监听正常 (40001)"
|
||||
if netstat -tunlp | grep -q ":${BACKEND_PORT}"; then
|
||||
print_success "后端端口监听正常 (${BACKEND_PORT})"
|
||||
else
|
||||
print_error "后端端口监听异常"
|
||||
return 1
|
||||
@@ -1225,19 +1357,42 @@ print_completion() {
|
||||
# 访问地址
|
||||
if [[ "$USE_DOMAIN" == "true" ]]; 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
|
||||
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
|
||||
else
|
||||
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
|
||||
|
||||
echo -e "${CYAN}管理员账号:${NC} ${ADMIN_USERNAME}"
|
||||
echo -e "${CYAN}管理员密码:${NC} ********"
|
||||
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 " 查看服务状态: pm2 status"
|
||||
@@ -1570,6 +1725,9 @@ main() {
|
||||
# 选择访问模式
|
||||
choose_access_mode
|
||||
|
||||
# 端口配置
|
||||
configure_ports
|
||||
|
||||
# 配置管理员账号
|
||||
configure_admin_account
|
||||
|
||||
|
||||
Reference in New Issue
Block a user