🗣 Wd提问: ecs服务器搭建网站
🤖 Ai回答: 在阿里云 ECS(Elastic Compute Service)服务器 上搭建网站是一个常见且实用的场景。下面我将为你提供一个完整的、从零开始的 基于 Linux(推荐 Ubuntu 20.04/22.04)的 ECS 服务器搭建网站的详细步骤,包括环境配置、安装 Web 服务(Nginx)、部署静态或动态网站。
🎯 一、准备工作
1、购买并开通 ECS 实例
登录 [阿里云官网](https://www.aliyun.com/)
进入 ECS 控制台
创建一台新的 ECS 实例:
镜像:选择 `Ubuntu Server 20.04 LTS`(推荐)或 `CentOS 7/8`
实例规格:建议 `2核4GB` 起步(如 `ecs.t6-c1m2.large`)
公网带宽:至少 1Mbps(可选按量付费)
安全组规则:开放以下端口:
80(HTTP)
443(HTTPS)
22(SSH,仅限你自己的 IP)
⚠️ 注意:确保已绑定公网 IP(或弹性公网 IP)
🛠 二、登录服务器并初始化
1、使用 SSH 登录
bash
ssh root@你的公网IP
密码或密钥登录(推荐使用密钥)
如果是第一次登录,可能需要设置密码或切换用户。
2、更新系统 & 安装基础工具
bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y vim curl wget git net-tools
🌐 三、安装 Nginx(Web 服务器)
1、安装 Nginx
bash
sudo apt install -y nginx
2、启动并设置开机自启
bash
sudo systemctl start nginx
sudo systemctl enable nginx
3、检查是否运行正常
bash
sudo systemctl status nginx
✅ 正常显示 `active (running)` 表示成功。
4、测试访问
打开浏览器访问:
http://你的公网IP
你应该看到默认的 Nginx 欢迎页面。
📁 四、部署网站内容
方式一:部署静态网站(HTML/CSS/JS)
1、进入默认站点目录
bash
cd /var/www/html
2、删除默认文件(可选)
bash
sudo rm -rf *
3、上传你的网站文件
你可以通过以下方式上传:
方法 1:本地打包上传(推荐)
bash
在本地打包你的网站文件
tar -czf mysite.tar.gz ./your-site-folder/
使用 scp 上传到服务器
scp mysite.tar.gz root@你的公网IP:/root/
方法 2:使用 Git(适合开发)
bash
git clone https://github.com/yourusername/your-website.git /var/www/html
4、解压并赋予权限
bash
sudo tar -xzf mysite.tar.gz -C /var/www/html/
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
`www-data` 是 Nginx 默认运行用户(Ubuntu 下)
方式二:部署动态网站(如 WordPress、Django、Node.js)
✅ 常见方案举例:
| 类型 | 技术栈 | 部署方式 |
|
| 博客 | WordPress | PHP + MySQL + Nginx |
| Python 应用 | Django/Flask | Gunicorn + Nginx |
| Node.js | Express/NestJS | PM2 + Nginx |
| Java | Spring Boot | JAR + Nginx |
我们以 部署一个简单的 Node.js 网站 为例:
示例:部署 Node.js 网站
bash
安装 Node.js(使用 NodeSource)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
创建项目目录
mkdir ~/my-node-app && cd ~/my-node-app
创建简单服务器(app.js)
cat app.js << 'EOF'
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) = {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from ECS! This is a Node.js website.\n');
});
server.listen(port, hostname, () = {
console.log(`Server running at http://${hostname}:${port}/`);
});
EOF
安装 PM2(进程管理器)
npm install -g pm2
启动应用
pm2 start app.js name my-node-app
设置开机自启
pm2 startup systemd
pm2 save
配置 Nginx 反向代理(将 80 → 3000)
编辑 Nginx 配置:
bash
sudo nano /etc/nginx/sites-available/default
替换内容为:
nginx
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;
}
}
重启 Nginx:
bash
sudo nginx -s reload
现在访问 `http://你的公网IP` 就能看到 Node.js 的输出了。
🔐 五、配置域名与 HTTPS(可选但强烈推荐)
1、购买域名(如腾讯云、阿里云)
例如:`www.yourwebsite.com`
2、将域名解析到你的 ECS 公网 IP
在域名控制台添加一条 `A 记录`:
主机记录:www
记录值:你的公网IP
3、使用 Let's Encrypt 申请免费 SSL 证书(自动配置)
安装 Certbot:
bash
sudo apt install -y certbot python3-certbot-nginx
运行:
bash
sudo certbot nginx -d yourdomain.com -d www.yourdomain.com
按提示操作,会自动修改 Nginx 配置并启用 HTTPS。
✅ 完成后访问 `https://yourdomain.com` 即可看到安全锁。
🔒 六、安全加固建议
1、不要使用 root 登录
创建普通用户:
bash
sudo adduser username
sudo usermod -aG sudo username
2、禁用 root SSH 登录
编辑 `/etc/ssh/sshd_config`:
PermitRootLogin no
PasswordAuthentication no 改为 yes 仅用于测试
然后重启:
bash
sudo systemctl restart sshd
3、使用防火墙(UFW)
bash
sudo apt install ufw
sudo ufw allow 'Nginx Full'
sudo ufw enable
✅ 总结:你已经完成了!
| 步骤 | 内容 |
| 1 | 购买并配置 ECS 服务器 |
| 2 | 登录并更新系统 |
| 3 | 安装 Nginx 并测试 |
| 4 | 上传网站文件(静态或动态) |
| 5 | 配置反向代理(如需) |
| 6 | 绑定域名 + 申请 HTTPS 证书 |
| 7 | 安全加固 |
📌 常见问题
❓ 无法访问网站?
→ 检查安全组是否放行 80/443;检查 Nginx 是否启动;检查防火墙。
❓ 403 错误?
→ 检查文件权限:`chown -R www-data:www-data /var/www/html`
❓ 502 Bad Gateway?
→ 检查后端服务是否运行(如 Node.js、PHP-FPM)
📚 学习资源推荐
[Nginx 官方文档](https://nginx.org/en/docs/)
[Let's Encrypt 官方](https://letsencrypt.org/docs/)
[阿里云 ECS 教
📣 商家广告 :
👍
0
👎
IP地址: 137.178.227.89
搜索次数: 9
提问时间: 2025-11-29 19:42:10
本站所有
❓
问答
均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
本站由
🟢
豌豆Ai
提供技术支持,使用的最新版:
豌豆Ai站群搜索引擎系统 V.25.10.25
搭建本站。