在阿里云轻量应用服务器上部署 Web 服务是一个非常常见且实用的操作。下面是一个完整的步骤指南,帮助你从零开始部署一个简单的 Web 应用(如使用 Nginx + Node.js/Python/静态网页)。
✅ 一、准备工作
-
购买轻量应用服务器
- 登录 阿里云控制台
- 进入「轻量应用服务器」产品页
- 选择地区、镜像(建议选 CentOS 或 Ubuntu,或直接选「应用镜像」如 LAMP/Node.js)
- 创建实例并设置登录密码
-
获取公网 IP
- 实例创建完成后,可在控制台看到分配的公网 IP 地址
-
开放端口
- 在轻量服务器的「防火墙」设置中,确保开放以下端口:
80(HTTP)443(HTTPS)22(SSH)- 如使用其他端口(如 3000、8080),也需开放
- 在轻量服务器的「防火墙」设置中,确保开放以下端口:
✅ 二、连接服务器(SSH)
使用终端或工具(如 Xshell、PuTTY、VS Code Remote SSH)连接:
ssh root@你的公网IP
输入密码即可登录。
✅ 三、部署 Web 服务(以 Nginx + 静态网页 为例)
1. 安装 Nginx(以 Ubuntu 为例)
sudo apt update
sudo apt install nginx -y
2. 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
3. 检查是否运行
浏览器访问:http://你的公网IP
如果看到 Nginx 欢迎页,说明部署成功 ✅
✅ 四、部署自己的网页
方法一:替换默认网页
- 进入网站根目录:
cd /var/www/html
- 备份原文件并上传你的网页:
sudo mv index.html index.html.bak
echo "<h1>Hello, My Website!</h1>" | sudo tee index.html
刷新浏览器即可看到新内容。
你可以通过
scp、rsync或 FTP 工具上传完整的 HTML/CSS/JS 文件。
✅ 五、部署动态应用(如 Node.js)
示例:部署一个简单的 Node.js 应用
- 安装 Node.js(推荐使用 nvm)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 18
- 创建应用目录
mkdir ~/myapp && cd ~/myapp
- 创建
app.js
const http = require('http');
const PORT = 3000;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello from Node.js on Alibaba Cloud!</h1>');
});
server.listen(PORT, () => {
console.log(`Server running at http://0.0.0.0:${PORT}/`);
});
- 后台运行
nohup node app.js > app.log 2>&1 &
- 配置 Nginx 反向X_X
编辑 Nginx 配置:
sudo nano /etc/nginx/sites-available/default
修改 location / 部分:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
- 重启 Nginx
sudo systemctl restart nginx
现在访问 http://你的公网IP 就能看到 Node.js 应用!
✅ 六、绑定域名(可选)
-
在阿里云「域名控制台」解析域名到服务器公网 IP
- 添加 A 记录:
@→你的IP - 添加
www记录(可选)
- 添加 A 记录:
-
配置 Nginx server_name
server_name yourdomain.com www.yourdomain.com;
- (可选)使用 Let’s Encrypt 配置 HTTPS
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
✅ 七、其他提示
-
使用宝塔面板?
可安装宝塔面板(一键部署环境):wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh安装后通过
http://IP:8888管理网站、数据库等。 -
数据库?
轻量服务器可自行安装 MySQL、Redis 等,或使用阿里云 RDS。 -
自动重启?
推荐使用pm2管理 Node.js 应用:npm install -g pm2 pm2 start app.js pm2 startup
✅ 总结
| 步骤 | 内容 |
|---|---|
| 1 | 购买轻量服务器,开放端口 |
| 2 | SSH 登录 |
| 3 | 安装 Web 服务(Nginx/Apache/Node) |
| 4 | 部署网页或应用 |
| 5 | 配置反向X_X(如需要) |
| 6 | 绑定域名 + HTTPS(可选) |
如果你告诉我你想部署的具体技术栈(如 Vue、React、Django、Spring Boot 等),我可以提供更详细的部署方案。欢迎继续提问!
云知识