将任务部署到腾讯云轻量应用服务器(Lighthouse)上的 Ubuntu 系统,主要涉及以下几个步骤。下面是一个完整的部署流程指南,适合部署 Web 应用、脚本任务或定时任务等。
一、准备工作
-
购买并登录腾讯云轻量应用服务器
- 登录 腾讯云控制台
- 购买轻量应用服务器(选择 Ubuntu 镜像,如 Ubuntu 20.04 或 22.04)
- 获取公网 IP、用户名(默认是
ubuntu)和密钥(或密码)
-
连接服务器
- 使用 SSH 连接到服务器:
ssh ubuntu@你的公网IP- 如果使用密钥登录:
ssh -i /path/to/your/private-key.pem ubuntu@你的公网IP
二、配置服务器环境
-
更新系统
sudo apt update && sudo apt upgrade -y -
安装必要软件
根据你的任务类型安装依赖,例如:-
安装 Python:
sudo apt install python3 python3-pip -y -
安装 Node.js(用于前端或 Node 服务):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs -
安装 Nginx(可选,用于反向X_X):
sudo apt install nginx -y sudo systemctl start nginx sudo systemctl enable nginx -
安装 Git(用于拉取代码):
sudo apt install git -y
-
三、部署你的任务
方式一:部署一个 Web 应用(如 Flask、Node.js)
-
克隆代码
git clone https://github.com/yourusername/your-repo.git cd your-repo -
安装依赖
-
Python 示例:
pip3 install -r requirements.txt -
Node.js 示例:
npm install
-
-
运行应用
-
Python(Flask):
nohup python3 app.py > app.log 2>&1 &或使用 Gunicorn + Nginx 更稳定。
-
Node.js:
nohup node app.js > app.log 2>&1 &
使用
nohup可让程序在后台持续运行。 -
方式二:部署定时任务(Cron Job)
-
编辑 crontab:
crontab -e -
添加定时任务,例如每天凌晨 2 点运行一个 Python 脚本:
0 2 * * * /usr/bin/python3 /home/ubuntu/myscript.py >> /home/ubuntu/cron.log 2>&1确保脚本路径和 Python 路径正确(用
which python3查看)。
方式三:使用 systemd 管理后台服务(推荐)
-
创建服务文件:
sudo nano /etc/systemd/system/mytask.service -
写入内容(以 Python 脚本为例):
[Unit] Description=My Custom Task After=network.target [Service] Type=simple User=ubuntu WorkingDirectory=/home/ubuntu/myproject ExecStart=/usr/bin/python3 /home/ubuntu/myproject/app.py Restart=always StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target -
启用并启动服务:
sudo systemctl daemon-reexec sudo systemctl enable mytask.service sudo systemctl start mytask.service -
查看日志:
sudo journalctl -u mytask.service -f
四、开放端口(防火墙设置)
-
在腾讯云控制台中配置 防火墙规则:
- 进入轻量服务器详情页 → 防火墙
- 添加规则:允许 TCP 端口(如 80、443、8000、3000 等)
-
本地防火墙(UFW,可选):
sudo ufw allow 8000 sudo ufw enable
五、域名与 HTTPS(可选)
-
绑定域名:
- 在腾讯云 DNS 控制台添加 A 记录指向服务器公网 IP
-
配置 Nginx 反向X_X:
server { listen 80; server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } -
使用 Let’s Encrypt 免费 HTTPS:
sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d yourdomain.com
六、安全建议
- 修改默认 SSH 端口,禁用密码登录,仅用密钥
- 定期更新系统和软件
- 使用非 root 用户运行服务
- 设置自动备份(轻量服务器支持快照)
总结
| 步骤 | 内容 |
|---|---|
| 1 | SSH 登录 Ubuntu 服务器 |
| 2 | 安装依赖(Python/Node.js/Git 等) |
| 3 | 部署代码(Git 拉取或上传) |
| 4 | 启动任务(nohup / systemd / cron) |
| 5 | 开放端口(腾讯云防火墙) |
| 6 | (可选)配置域名和 HTTPS |
如果你告诉我你具体要部署什么类型的任务(如 Python 脚本、Node 服务、爬虫、网站等),我可以提供更具体的部署脚本和配置示例。
秒懂云