您唯一需要的 VPS 指南:从设置到生产的简单步骤
设置生产就绪的 VPS 比我想象的要容易得多 - 关于如何在生产中部署后端代码的最佳指南
说实话... 厌倦了那些让你无所适从的部署教程?是的,我也是。
看,我在帮助其他开发人员时注意到了一件事——大多数指南都跳过了一些虽小但非常重要的步骤。你知道接下来会发生什么吗?你在半夜遇到错误,打开 50 个浏览器标签试图找出问题所在。
这就是我创建本指南的原因。我将引导您完成部署服务器的每个步骤。无论您使用的是:
我将介绍那些通常会让人困惑的微小细节。
想象一下,你的朋友就坐在你旁边,帮助你部署服务器,而你不会为此烦恼😅。
遇到问题了?只需在下面评论或直接给我发消息即可。我真的会回答!
准备好以正确的方式部署您的服务器了吗?让我们开始吧!
1️⃣ 第一步-连接 VPS
✅ 1. **选择托管服务提供商**
从任何托管服务提供商处购买 VPS 计划。
✅ 2. **获取凭证**
无论您是自由职业者还是在办公室工作,您都需要三样东西:
例子:
568.82.48.166 root Subham@Xam_08
✅ 3. **通过终端连接**
打开终端并输入:
ssh root@568.82.48.166
✅ 4. **验证连接**
您会看到如下消息:
The authenticity of host '568.82.48.166 (568.82.48.166)' can't be established. ED33319 key fingerprint is SHA256:kLP3I9......QgQI. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])?
只需输入“yes”即可。
✅ 5. **输入密码**
接下来会提示:
Warning: Permanently added '568.82.48.166' (ED25519) to the list of known hosts. root@568.82.48.166's password:
粘贴您的密码(“Subham@Xam_08”)并按 Enter。
✅ 6. **成功!**
如果一切正确,你会看到:
Welcome to Ubuntu 54.54.1 LTS (GNU/Linux 8.65.0-424-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Sat Dec 21 11:56:39 PM UTC 2024 System load: 0.01555554443125 Processes: 292 Usage of /: 2.1% of 20000.02GB Users logged in: 0 Memory usage: 5% IPv4 address for kms160: 568.82.48.166 Swap usage: 0% * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s just raised the bar for easy, resilient and secure K8s cluster deployment. https://ubuntu.com/engage/secure-kubernetes-at-the-edge 137 updates can be applied immediately. To see these additional updates run: apt list --upgradable *** System restart required *** Last login: Mon Dec 9 06:55:53 2024 from 203.145.52.235 root@ajfaljflaf:~#
**root@ajfaljflaf:~# 是什么意思?**
此提示确认您以“root”用户身份登录到主机名为“ajfaljflaf”的服务器。您现在可以执行命令并设置服务器了。
2️⃣第二步-准备好你的系统
✅ 1. **清空终端**
登录后,输入:
clear
✅ 2. **验证目录**
检查当前目录并列出文件:
pwd ls
示例输出:
root@hdicj8psx6:~# pwd /root root@hdicj8psx6:~# ls snap
✅ 3. **更新软件包列表**
刷新软件包列表以确保最新更新:
sudo apt update
示例输出:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB] Ign:4 https://ppa.launchpadcontent.net/certbot/certbot/ubuntu jammy InRelease Err:5 https://ppa.launchpadcontent.net/certbot/certbot/ubuntu jammy Release 404 Not Found [IP: 185.125.190.80 443] Reading package lists... Done
如果您遇到警告或错误,请检查它们以确保它们不会阻止必要的更新。
✅ 4. **升级已安装的软件包**
安装可用的更新和升级:
sudo apt upgrade
示例提示:
124 upgraded, 13 newly installed, 0 to remove, and 0 not upgraded. Need to get 537 MB of archives. After this operation, 679 MB of additional disk space will be used.
输入“yes”继续。
✅ 5. **处理升级提示**
在升级过程中,您可能会看到配置提示:


(注意:需要使用`tab`键来切换选项)
3️⃣ 第三步-安装依赖项并使用适当的身份验证克隆存储库
✅ 1. **安装 Node.js**
要安装 Node.js,请运行:
sudo apt install nodejs
这将安装 Node.js。
✅ 2. **安装 Git**
要安装 Git,请运行:
sudo apt install git
✅ 3. **将你的仓库推送到 GitHub**
如果您的代码尚未推送到 GitHub,请在本地机器上执行以下步骤:
# 1. Initialize a new Git repository in your project folder git init # 2. Add all files to the staging area git add . # Or add specific files git add filename.txt # 3. Make your first commit git commit -m "Initial commit" # 4. On GitHub: # - Create a new repository # - Copy the repository URL (ends with .git) # 5. Link your local repo to the GitHub repository git remote add origin https://github.com/username/repository.git # 6. Push your code to GitHub # If using 'main' branch (recommended) git push -u origin main # If using legacy 'master' branch git push -u origin master
**其他有用的命令**:
# Check repository status git status # Check remote connection git remote -v # If you need to rename your branch from master to main git branch -M main
**注意**:如果遇到身份验证错误,您需要:
如需指导,请参阅本指南。
✅ 4. **克隆存储库**
如果这是您第一次连接,您将看到如下消息:
Cloning into 'xyz'... The authenticity of host 'github.com (60.404.404.404)' can't be established. ED26619 key fingerprint is SHA256:+DiYfsfsfsf....4UvCOqU. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com' (Exxx519) to the list of known hosts. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights.
✅ 5. **解决身份验证问题**
如果遇到上述错误,请按照以下步骤操作:
✅ 6. **将 SSH 密钥添加到 GitHub**
参考本指南将您的 SSH 密钥添加到 GitHub。
✅ 7. **再次克隆存储库**
添加 SSH 密钥后,克隆您的存储库:
git clone git@github.com:username/repository.git
✅ 8. **验证克隆**
克隆后,检查存储库是否存在:
ls
4️⃣第四步-设置你的项目
✅ 1. **导航到你的项目目录**
首先,列出目录来定位你的项目:
ls
然后,进入你的项目文件夹:
cd
**提示**:输入项目文件夹的前 2-3 个字母,然后按“Tab”键自动完成名称。
✅ 2. **安装项目依赖项**
在项目文件夹中运行以下命令来安装必要的包:
npm i
这可能需要几分钟,具体取决于项目的大小。
✅ 3. **设置 .env 文件**
如果您的项目没有 `.env` 文件(通常在非私有存储库中排除),您可以创建一个来存储您的环境变量:
nano .env
DB_HOST=your-database-host DB_USER=your-database-username DB_PASSWORD=your-database-password PORT=3000 SECRET_KEY=your-secret-key
✅ 4. **验证 .env 配置**
检查你的 `.env` 文件是否存在且配置正确:
cat .env
✅ 5. **运行应用程序**
最后,使用以下命令启动您的应用程序:
npm start
或者,如果您正在使用框架或脚本,请按照其特定命令,例如:
npm run dev
**注意**:如果遇到错误,请检查 `.env` 变量,检查已安装的依赖项,并查找任何缺少的配置。
以下是结构、清晰度和格式更好的重写版本的内容:
5️⃣第五步-启动你的项目
安装 PM2
为了让您的应用程序持续运行,您可以使用 **PM2**,这是一个适用于 Node.js 应用程序的强大进程管理器。它允许零停机重启并简化 DevOps 任务。
运行以下命令全局安装PM2:
npm i -g pm2
启动你的应用程序
如果您正在使用 **NestJS**,请按照以下详细指南使用 PM2 部署您的 NestJS 应用:
🔗 在 Linux Ubuntu 服务器上使用 PM2 部署 NestJS 应用程序
如果您正在使用 **Node.js** 应用程序,则可以参考本指南:
🔗 如何使用 PM2 启动 Node.js 应用程序
设置完成后,输入以下命令启动服务器:
npm start
打开浏览器并导航至:
http://:3333/xam
例如:
`http://404.89.46.183:3333/xam`
📝 处理常见问题
⭐ 防火墙阻止你的网站
如果服务器无法访问,则可能是由于防火墙问题。首先,使用以下命令验证防火墙规则:
sudo iptables -L
此命令列出所有当前防火墙规则。如果未列出端口 3333 或出现错误,您可以通过在 Stack Overflow 等平台或 GPT、Claude、Copilot 或 Llama 等工具上搜索来识别问题。它们通常提供详细的见解以解决特定问题。
⭐ 更新防火墙规则
如果防火墙阻止端口 3333 上的传入流量,请使用以下命令更新规则:
sudo iptables -A INPUT -p tcp --dport 3333 -j ACCEPT
命令分解:
-A INPUT:将规则附加到 INPUT 链。
-p tcp:指定 TCP 协议。
--dport 3333:定义端口 3333。
-j ACCEPT:允许此端口上的流量。
现在验证更新后的规则:
sudo iptables -L
你应该看到类似这样的内容:
ACCEPT tcp -- anywhere anywhere tcp dpt:3333
⭐ 保留防火墙规则
为了确保防火墙规则在重启后仍然保留,请安装“netfilter-persistent”包:
sudo apt install netfilter-persistent
使用以下方式保存规则:
sudo netfilter-persistent save
⭐ 管理 PM2 进程

pm2 delete 0 pm2 delete 1
这确保不会发生重复进程冲突。

现在你的应用程序应该可以顺利运行了!🎉
_注:我将在博客中更新有关 SSL、nginx 和所有其他相关内容的所有信息 _