引言:为什么VPS是生产环境的最佳选择
在Node.js上开发JavaScript应用程序只是完成了一半工作。另一半同样重要的部分是部署到生产服务器。许多初级开发者使用免费托管服务(Heroku、Render、Vercel),但对于严肃的项目,最终会需要一台VPS(虚拟专用服务器)。
VPS让您完全掌控运行环境:您可以选择Node.js版本、配置Web服务器、管理进程和资源。本文将逐步讲解如何使用现代工具将JavaScript应用程序部署到VPS:使用Nginx作为反向代理、PM2进行进程管理、Let's Encrypt配置SSL证书,以及GitHub Actions实现自动化部署。
第一步:服务器准备与安装必要软件
第一步是连接到您的VPS(通常通过SSH)并安装基础工具集。假设您使用的是Ubuntu 22.04 LTS。
# Update packagessudo apt update && sudo apt upgrade -y
# Install Node.js (recommended via NodeSource)curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejs
# Check versionsnode -vnpm -v
# Install Git (if not already installed)sudo apt install git -y
# Install Nginxsudo apt install nginx -y同时,请务必配置基本防火墙(UFW):
sudo ufw allow OpenSSHsudo ufw allow 'Nginx Full'sudo ufw enable第二步:克隆仓库并配置应用程序
现在需要将应用程序代码传输到服务器。最简单的方法是从GitHub/GitLab克隆仓库。
# Navigate to the /var/www directory (standard for web projects)cd /var/www
# Clone the repository (replace with your URL)sudo git clone https://github.com/your-username/your-repository.git myapp
# Give permissions to the current usersudo chown -R $USER:$USER /var/www/myapp
# Navigate to the application foldercd /var/www/myapp
# Install dependenciesnpm install --production为环境变量(端口、API密钥、数据库连接字符串)创建.env文件:
# Example .envPORT=3000NODE_ENV=productionDATABASE_URL=mongodb://localhost:27017/myappJWT_SECRET=your-secret-key第三步:配置PM2进行进程管理
PM2是一个Node.js进程管理器,能在应用程序崩溃时自动重启,并使其在后台持续运行。
# Install PM2 globallysudo npm install -g pm2
# Start the application (if entry point is app.js or server.js)pm2 start app.js --name "myapp"
# Save the process list for auto-start on rebootpm2 savepm2 startup systemd执行pm2 startup后,您会看到一条需要以root权限执行的命令。请复制并运行它。
您可以使用pm2 status命令检查状态。如果应用程序正在运行,它将可以通过http://your-ip:3000(.env中配置的端口)访问。
第四步:配置Nginx作为反向代理
直接通过端口3000访问Node.js并非最佳实践。更好的方式是使用Nginx作为代理服务器,它将在端口80(HTTP)接收请求并将其转发给您的应用程序。
为您的域名(或IP)创建配置文件:
sudo nano /etc/nginx/sites-available/myapp插入以下代码:
server { listen 80; server_name your-domain.ru www.your-domain.ru;
location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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; }
location /socket.io/ { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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; }
# Gzip compression gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;}启用配置文件并重启Nginx:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/sudo nginx -t # 检查配置是否存在语法错误sudo systemctl restart nginx现在您的应用程序应可通过http://your-domain.ru访问,Nginx将自动转发所有请求到端口3000的Node.js应用。
第五步:使用Let's Encrypt配置SSL证书
没有HTTPS的生产服务器是不完整的。Let's Encrypt提供免费的SSL证书,可通过Certbot工具自动获取和续期。
# Install Certbot and Nginx pluginsudo apt install certbot python3-certbot-nginx -y
# Obtain and install SSL certificatesudo certbot --nginx -d your-domain.ru -d www.your-domain.ru按照交互提示操作:输入有效的邮箱地址,同意服务条款,选择是否将HTTP流量重定向到HTTPS(强烈建议选择重定向)。
证书将自动配置到Nginx,并设置自动续期(通常通过systemd定时器)。手动测试续期:
sudo certbot renew --dry-run第六步:使用GitHub Actions设置自动部署
手动通过SSH更新代码效率低下。让我们配置GitHub Actions,在每次推送到主分支时自动部署。
首先,在服务器上生成部署密钥对: