
笔者在 Windows 使用 Node 的本地服务器(http://localhost:${PORT})进行网页开发并测试完成后,想要将其部署到 vps 上。
要解决的问题有:
补充(域名分级结构):
根域名(
.):
- 这是最顶层的域名,通常不显示。
顶级域名(TLD):
- 例如
cn.(中国的顶级域名)。它是域名系统中最顶层的部分。一级域名(一级):
cn.可以视为顶级域名,同时也是一级域名。二级域名(如
xxx.cn.):
- 这部分通常是由用户注册的域名,例如
example.cn。三级域名(如
xxx.xxx.cn.):
- 进一步细分的域名结构,例如
sub.example.cn。
接下来开始网站的部署!
假设:
确保你的系统是最新的,并安装pm2来管理你的Node.js应用:
sudo apt update sudo apt upgrade -y sudo npm install -g pm2 将项目从本地机器上传到服务器的相应目录,我的项目结构为(使用缩进表示结构层次):
/frontend index.html register.html script.js style.css /backend package-lock.json package.json server.js user.db /node_modules /node_modules 文件夹是不用上传到服务器的,因为 /node_modules 文件夹是在 Windows 上开发时生成的,要部署到 ubuntu 上最好重新安装这些包。

/var/www/下面的文件夹,可以新建一个和项目名称有关的使用如下命令解压上传的文件
tar -xvjf filename.tar.bz2 /var/www/xxx/frontend /var/www/xxx/backend 进入你的服务器,导航到你的backend目录并安装依赖项,然后使用pm2启动应用:
cd /var/www/xxx/backend npm install pm2 start server.js --name YourServerName server.js 处填入 Node 代码文件名YourServerName 处可以自己为服务起一个别名要确保你的应用正在运行,可查看 PM2 中的进程列表:
pm2 list 你应该会看到类似如下的输出,显示 YourServerName 正在运行:
┌─────┬─────────────────────┬──────┬─────────┬─────┬────────┬───────────┬──────────┬───────┬──────────┐ │ id │ name │ mode │ status │ cpu │ memory │ uptime │ restarted│ user │ watching │ ├─────┼─────────────────────┼──────┼─────────┼─────┼────────┼───────────┼──────────┼───────┼──────────┤ │ 0 │ YourServerName │ fork │ online │ 0% │ 32.0mb │ 0s │ 0 │ root │ disabled │ └─────┴─────────────────────┴──────┴─────────┴─────┴────────┴───────────┴──────────┴───────┴──────────┘ status 处的 online 就表示服务正在运行。
编辑或创建一个新的Apache虚拟主机配置文件,例如/etc/apache2/sites-available/YourServerName.conf:
ServerName 服务名称 DocumentRoot /前端文件所在目录 Options Indexes FollowSymLinks AllowOverride All Require all granted ProxyRequests Off ProxyPass / http://localhost:node代码中使用的本地服务器端口/ ProxyPassReverse / http://localhost:node代码中使用的本地服务器端口/ ErrorLog ${APACHE_LOG_DIR}/ 服务名称_error.log CustomLog ${APACHE_LOG_DIR}/ 服务名称_access.log combined ServerName xxx.xx.top DocumentRoot /var/www/xxx/frontend Options Indexes FollowSymLinks AllowOverride All Require all granted ProxyRequests Off ProxyPass / http://localhost:9000/ ProxyPassReverse / http://localhost:9000/ ErrorLog ${APACHE_LOG_DIR}/YourServerName_error.log CustomLog ${APACHE_LOG_DIR}/YourServerName_access.log combined sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod rewrite 启用你的站点配置文件并重启 Apache:
sudo a2ensite sharedbill.conf sudo systemctl restart apache2 确保 Apache 和 Node.js 都监听在正确的地址和端口。
sudo nano /etc/apache2/ports.conf 确保包含:
Listen 11111 确保 server.js 中监听的是 localhost 和使用的 Node 服务器端口。
netstat 或 ss 工具检查端口监听情况:sudo netstat -tuln | grep :9000 sudo netstat -tuln | grep :11111 9000 端口,Apache 监听在 11111 端口。确保服务器防火墙允许端口11111的流量:
sudo ufw allow 11111/tcp sudo ufw reload 启用站点配置并重启Apache服务器:
sudo a2ensite sharedbill.conf sudo systemctl restart apache2 尝试直接通过服务器的 IP 地址访问:
http://:11111 假设可以访问,并且可以正常使用其上的服务,那么则说明网页部署成功了。
如果不能访问,比如显示 502 错误,则可以依靠以下手段进行 debug:
Apache 错误日志:
sudo tail -f /var/log/apache2/YourServerName_error.log Node.js 日志(使用 PM2):
pm2 logs YourServerName 使用 curl 测试你的 Node.js 服务器是否在本地正常运行:
curl http://localhost:9000 你应该能够看到来自你的 Node.js 服务器的响应。如果没有响应,则需要检查你的 server.js 文件是否正确启动并监听端口 9000。
有的服务器上,localhost 可能会被解析为 IPv6,有的应用不支持监听 IPv6 地址,所以可以尝试在代码中显式使用 IPv4 地址来解决这个问题:
app.listen(PORT, '127.0.0.1', () => { console.log(`Server running on http://localhost:${PORT}`); }); 下面是配置域名 DNS 指向网站的过程,没有这个需求就不用看了。
确保你的 A 记录看起来像这样:
| Type | Name | Content | TTL | Proxy status |
|---|---|---|---|---|
| A | 要使用的三级域名 | 服务器地址 | Auto | DNS only |
# 启用 Rewrite 引擎 RewriteEngine On # 仅对 xxx.xx.x 执行重定向 RewriteCond %{HTTP_HOST} ^xxx\.xx\.x$ [NC] RewriteRule ^(.*)$ http://xxx.xx.x:11111$1 [R=301,L] ^xxx\.xx\.x$ [NC]:填写三级域名地址,\ 用于转义通过这种方式,你可以确保其他在 80 端口上运行的网站不受影响,同时实现特定域名的重定向。
启用你的站点配置文件并重启 Apache:
sudo a2ensite YourServerName.conf sudo systemctl restart apache2 打开浏览器,访问 http://xxx.xx.x:11111,确保页面加载正常。