在阿里云ECS上搭载 Alibaba Cloud Linux 3.2104 LTS 配置 Node.js 环境并部署前端服务,可以按照以下步骤操作。该系统基于 RHEL/CentOS,使用 yum 包管理器。
✅ 第一步:登录 ECS 实例
通过 SSH 登录你的 ECS 实例:
ssh root@<你的公网IP>
✅ 第二步:更新系统软件包
sudo yum update -y
✅ 第三步:安装 Node.js(推荐使用 NodeSource 源)
Alibaba Cloud Linux 默认仓库中的 Node.js 版本较旧,建议使用官方 NodeSource 提供的源安装最新稳定版。
1. 安装 NodeSource 仓库(以 Node.js 18.x 为例)
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
⚠️ 注意:可替换为
setup_20.x如果需要 Node.js 20(根据项目需求选择 LTS 版本)
2. 安装 Node.js 和 npm
sudo yum install -y nodejs
3. 验证安装
node --version
npm --version
输出类似:
v18.18.2
9.8.1
✅ 第四步:安装 PM2(用于进程管理)
sudo npm install -g pm2
验证:
pm2 --version
✅ 第五步:上传或克隆前端项目代码
方法一:从 Git 克隆项目(推荐)
git clone https://github.com/your-username/your-frontend-project.git /opt/frontend-app
cd /opt/frontend-app
方法二:本地上传文件
使用 scp 或 SFTP 工具将打包好的前端项目(如 dist/ 目录)上传到服务器:
# 本地执行
scp -r dist/ root@<ECS_IP>:/opt/frontend-app/dist
然后在服务器上创建项目目录:
mkdir -p /opt/frontend-app && cd /opt/frontend-app
✅ 第六步:安装依赖并构建(如需)
如果你上传的是源码(含 package.json),需要安装依赖并构建:
cd /opt/frontend-app
npm install
npm run build # 构建生成 dist 目录
如果你已经本地构建好,只需确保
/opt/frontend-app/dist存在静态文件即可。
✅ 第七步:使用 Express 或 serve 快速部署静态文件
方式一:使用 serve 快速启动(简单快捷)
sudo npm install -g serve
启动服务:
serve -s dist -p 3000
-s表示单页应用(SPA),支持 history 模式路由
后台运行:
nohup serve -s dist -p 3000 > /var/log/frontend.log 2>&1 &
方式二:使用 Express 自定义服务(更灵活)
创建一个简单的 server.js:
// /opt/frontend-app/server.js
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'dist')));
// 支持 SPA 的 history 模式
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, '0.0.0.0', () => {
console.log(`Frontend server running on http://0.0.0.0:${PORT}`);
});
安装 express:
npm install express
使用 PM2 启动:
pm2 start server.js --name "frontend"
设置开机自启:
pm2 startup
pm2 save
✅ 第八步:配置防火墙和安全组
1. 开放端口(如 3000)
在 Alibaba Cloud 控制台中:
- 进入 ECS 实例详情
- 找到 安全组,点击配置规则
- 添加安全组规则:
- 协议类型:自定义 TCP
- 端口范围:
3000 - 授权对象:
0.0.0.0/0(或限制为你的 IP)
2. (可选)开启系统防火墙端口(如果启用 firewalld)
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload
Alibaba Cloud Linux 默认可能未启用
firewalld,主要靠安全组控制。
✅ 第九步:访问前端服务
浏览器访问:
http://<你的ECS公网IP>:3000
应能看到你的前端页面。
✅ 第十步:(可选)使用 Nginx 反向(推荐生产环境)
为了更好的性能、HTTPS 和域名支持,建议用 Nginx 反向。
1. 安装 Nginx
sudo yum install -y nginx
2. 配置 Nginx
编辑配置文件:
sudo vim /etc/nginx/conf.d/frontend.conf
写入:
server {
listen 80;
server_name your-domain.com; # 或直接用 IP
location / {
proxy_pass http://127.0.0.1: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;
proxy_cache_bypass $http_upgrade;
}
}
3. 启动 Nginx
sudo systemctl enable nginx
sudo systemctl start nginx
4. 修改 PM2 应用监听为 127.0.0.1:3000(更安全)
确保你的服务监听 0.0.0.0 或 127.0.0.1,Nginx 才能。
✅ 常用 PM2 命令
pm2 status
pm2 logs frontend
pm2 restart frontend
pm2 delete frontend
✅ 总结
| 步骤 | 内容 |
|---|---|
| 1 | 更新系统 |
| 2 | 安装 Node.js(NodeSource) |
| 3 | 安装 PM2 进程管理 |
| 4 | 上传前端代码 |
| 5 | 构建项目(如需要) |
| 6 | 使用 serve 或 Express 启动服务 |
| 7 | 配置安全组开放端口 |
| 8 | (推荐)使用 Nginx 反向 |
✅ 生产建议:
- 使用 Nginx + HTTPS(Let’s Encrypt)
- 使用域名绑定
- 日志监控(
pm2 log或结合 ELK) - 自动化部署脚本(如 Jenkins、GitHub Actions)
如有具体框架(Vue/React/Vite等),可进一步优化构建命令。
需要我提供 Vue + Vite + Nginx 的完整部署脚本吗?欢迎继续提问!
CLOUD技术笔记