🗣 Wd提问: linux云服务器部署
🤖 Ai回答: 在Linux云服务器上部署应用是一个常见且重要的运维任务。以下是一个系统、全面、专业的部署指南,适用于主流云平台(如阿里云、腾讯云、华为云、AWS、Google Cloud等)的Linux服务器(推荐CentOS 7+/Ubuntu 20.04+)。
🧭 一、准备工作
1、选择云服务器
操作系统:推荐 Ubuntu 22.04 LTS 或 CentOS Stream / Rocky Linux 9
配置:根据应用需求选择CPU、内存、磁盘(建议至少2核4G,SSD硬盘)
网络:公网IP + 安全组开放端口(如80, 443, 22)
2、连接服务器
使用SSH连接:
bash
ssh root@your_server_ip
或者使用密钥登录
ssh -i your_key.pem root@your_server_ip
🛠️ 二、基础环境配置
1、更新系统 & 安装常用工具
bash
Ubuntu
sudo apt update && sudo apt upgrade -y
sudo apt install curl wget git vim net-tools htop ufw -y
CentOS/Rocky
sudo yum update -y
sudo yum install epel-release -y
sudo yum install curl wget git vim net-tools htop firewalld -y
2、设置防火墙(可选但推荐)
bash
Ubuntu (ufw)
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
CentOS (firewalld)
sudo firewall-cmd permanent add-service=http
sudo firewall-cmd permanent add-service=https
sudo firewall-cmd permanent add-port=22/tcp
sudo firewall-cmd reload
3、创建普通用户(安全最佳实践)
bash
sudo adduser deploy
sudo usermod -aG sudo deploy
su deploy
📦 三、安装运行环境(以Node.js + Nginx为例)
1、安装Node.js(以v18.x为例)
bash
Ubuntu
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
CentOS
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs
验证:
bash
node -v
npm -v
2、安装Nginx
bash
Ubuntu
sudo apt install nginx -y
CentOS
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
3、安装PM2(Node.js进程管理器)
bash
npm install -g pm2
🚀 四、部署你的应用
1、上传代码(Git克隆或SCP上传)
bash
cd ~
git clone https://github.com/yourname/yourapp.git
cd yourapp
npm install
npm run build 如果是前端项目
2、启动应用(PM2)
bash
pm2 start npm start no-daemon
或者指定脚本
pm2 start server.js
pm2 startup
pm2 save
✅ 推荐使用 `pm2 ecosystem.config.js` 配置文件管理多应用。
🌐 五、配置Nginx反向代理
编辑Nginx配置:
bash
sudo nano /etc/nginx/sites-available/yourapp
内容示例:
nginx
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
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_cache_bypass $http_upgrade;
}
}
启用站点:
bash
sudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
🔐 六、配置HTTPS(Let’s Encrypt免费证书)
1、安装Certbot
bash
Ubuntu
sudo apt install certbot python3-certbot-nginx -y
CentOS
sudo yum install certbot python3-certbot-nginx -y
2、获取并自动配置证书
bash
sudo certbot nginx -d yourdomain.com -d www.yourdomain.com
Certbot会自动修改Nginx配置并设置自动续期。
🔄 七、自动化部署(可选)
使用GitHub Actions / GitLab CI / Jenkins
示例:GitHub Actions 自动部署到服务器
yaml
.github/workflows/deploy.yml
name: Deploy to Server
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
name: Checkout code
uses: actions/checkout@v3
name: Deploy via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /home/deploy/yourapp
git pull origin main
npm install
pm2 restart all
📊 八、监控与日志
1、查看PM2日志
bash
pm2 logs
pm2 monit
2、Nginx日志
bash
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
3、系统监控
bash
htop
df -h
free -m
🛡️ 九、安全加固建议
关闭密码登录,仅允许密钥登录(修改 `/etc/ssh/sshd_config`)
修改SSH默认端口(如22 → 2222)
安装Fail2ban防止暴力破解
定期更新系统和软件包
使用云服务商的DDoS防护和WAF
✅ 十、最终检查清单
| 项目 | 是否完成 |
| 服务器连通性 | ✅ |
| 环境安装(Node/Nginx/PM2) | ✅ |
| 应用启动成功 | ✅ |
| Nginx反向代理配置 | ✅ |
| HTTPS证书生效 | ✅ |
| 防火墙开放必要端口 | ✅ |
| 日志监控正常 | ✅ |
| 自动化部署配置(可选) | ✅ |
📚 附录:常用命令速查
| 功能 | 命令 |
| 重启Nginx | `sudo systemctl restart nginx` |
| 查看PM2进程 | `pm2 list` |
| 查看端口占用 | `netstat -tlnp \| grep :3000` |
| 查看系统资源 | `htop` |
| 查看磁盘空间 | `df -h` |
✅ 恭喜!你已成功在Linux云服务器上完成应用部署。
如需部署其他类型应用(Python/Django、Java/Spring Boot、PHP/Laravel、Go等),请告诉我具体技术栈,我可以为你定制详细部署方案。
📌 提示:首次部署建议在测试环境中演练,避免影响生产服务。
💡 如需一键部署脚本或Ansible Playbook,也可提供。欢迎继续提问!
📣 商家广告:





0
IP地址: 26.162.223.121
搜索次数: 211
提问时间: 2025-12-15 07:28:43
本站所有
❓
问答
均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
本站由
🟢
豌豆Ai
提供技术支持,使用的最新版:
豌豆Ai站群搜索引擎系统 V.25.10.25
搭建本站。