说实话......厌倦了让您困惑的部署教程?是的,我也是。
看,我在帮助其他开发人员时注意到了一些事情 - 大多数指南都会跳过小但非常重要的步骤。你知道接下来会发生什么吗?您在午夜遇到错误,打开 50 个浏览器选项卡试图找出问题所在。
这就是我创建本指南的原因。我将引导您完成部署服务器的每一个步骤。无论您是否正在与:
我将介绍那些通常会让人绊倒的小细节。
把这想象成你的朋友坐在你旁边,帮助你部署服务器而不费吹灰之力?.
卡在某个地方了吗?只需在下面评论或直接给我留言即可。我真的回答了!
准备好以正确的方式部署您的服务器了吗?让我们开始吧!
✅ 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])?
只需输入“是”即可。
✅ 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。
(注意:输入密码时终端不会显示任何字符。只需按 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 登录到服务器。您现在已准备好执行命令并设置服务器。
✅ 1.清理终端
登录后,输入:
568.82.48.166 root Subham@Xam_08
✅ 2.验证目录
检查当前目录并列出文件:
ssh root@568.82.48.166
示例输出:
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])?
✅ 3. 更新包裹清单
刷新软件包列表以确保最新更新:
Warning: Permanently added '568.82.48.166' (ED25519) to the list of known hosts. root@568.82.48.166's password:
示例输出:
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:~#
如果您遇到警告或错误,请查看它们以确保它们不会阻止重要的更新。
✅ 4. 升级已安装的软件包
安装可用的更新和升级:
clear
提示示例:
pwd ls
输入 yes 继续。
✅ 5. 处理升级提示
升级过程中,您可能会看到配置提示:
(注意:切换选项需要使用tab键)
查看升级完成情况
成功升级后,您将看到有关重新启动服务的消息:
root@hdicj8psx6:~# pwd /root root@hdicj8psx6:~# ls snap
这确认服务已根据需要重新启动或推迟。
✅ 1. 安装 Node.js
要安装 Node.js,请运行:
sudo apt update
这将安装 Node.js。
✅ 2. 安装 Git
要安装 Git,请运行:
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
✅ 3. 将您的存储库推送到 GitHub
如果您的代码尚未推送到 GitHub,请在本地计算机上执行以下步骤:
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.
注意:如果遇到身份验证错误,您需要:
有关指导,请参阅本指南。
✅ 4. 克隆存储库
从 GitHub 复制 HTTPS 链接:
示例:
克隆存储库:
568.82.48.166 root Subham@Xam_08
如果这是您第一次连接,您会看到如下消息:
ssh root@568.82.48.166
✅ 5. 解决身份验证问题
如果您遇到上述错误,请按照以下步骤操作:
安装 GitHub CLI (gh):
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])?
使用 GitHub 进行身份验证:
Warning: Permanently added '568.82.48.166' (ED25519) to the list of known hosts. root@568.82.48.166's password:
按照提示操作:
提示示例:
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:~#
clear
✅ 6. 将 SSH 密钥添加到 GitHub
请参阅本指南将 SSH 密钥添加到 GitHub。
✅ 7. 再次克隆存储库
添加 SSH 密钥后,克隆您的存储库:
pwd ls
✅ 8.验证克隆
克隆后,检查存储库是否存在:
root@hdicj8psx6:~# pwd /root root@hdicj8psx6:~# ls snap
✅ 1.导航到您的项目目录
首先,列出用于定位您的项目的目录:
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
提示: 输入项目文件夹的前 2-3 个字母,然后按 Tab 键自动补全名称。
✅ 2. 安装项目依赖
在项目文件夹中运行以下命令来安装必要的包:
sudo apt upgrade
这可能需要几分钟,具体取决于项目的大小。
✅ 3. 设置 .env 文件
如果您的项目没有 .env 文件(通常排除在非私有存储库中),您可以创建一个来存储环境变量:
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.
Restarting services... /etc/needrestart/restart.d/systemd-manager systemctl restart nginx.service polkit.service ssh.service udisks2.service Service restarts being deferred: systemctl restart networkd-dispatcher.service systemctl restart systemd-logind.service
✅ 4. 验证.env配置
检查您的 .env 文件是否存在并且配置正确:
sudo apt install nodejs
✅ 5.运行应用程序
最后,使用以下命令启动您的应用程序:
sudo apt install git
或者,如果您使用框架或脚本,请遵循其特定命令,例如:
568.82.48.166 root Subham@Xam_08
注意:如果遇到错误,请检查 .env 变量,检查已安装的依赖项,并查找任何丢失的配置。
这是内容的重写版本,具有更好的结构、清晰度和格式:
为了保持应用程序持续运行,您可以使用 PM2,它是 Node.js 应用程序的强大流程管理器。它允许零停机重新启动并简化 DevOps 任务。
运行以下命令全局安装 PM2:
ssh root@568.82.48.166
如果您使用NestJS,请按照此详细指南使用 PM2 部署您的 NestJS 应用:
?在 Linux Ubuntu 服务器上使用 PM2 部署 NestJS 应用
如果您正在使用 Node.js 应用程序,您可以参考本指南:
?如何使用 PM2 启动 Node.js 应用
设置完成后,输入以下命令启动服务器:
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])?
打开浏览器并导航至:
Warning: Permanently added '568.82.48.166' (ED25519) to the list of known hosts. root@568.82.48.166's password:
例如:
http://404.89.46.183:3333/xam
如果服务器无法访问,可能是由于防火墙问题。首先,使用以下命令验证防火墙规则:
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:~#
此命令列出所有当前的防火墙规则。如果端口 3333 未列出或出现错误,您可以通过在 Stack Overflow 等平台或 GPT、Claude、Copilot 或 Llama 等工具上搜索来确定问题。他们经常提供详细的见解来解决特定问题。
如果防火墙阻止端口 3333 上的传入流量,请使用以下命令更新规则:
clear
命令分解:
-A INPUT:向 INPUT 链追加一条规则。
-p tcp:指定TCP协议。
--dport 3333: 定义端口 3333。
-j ACCEPT:允许此端口上的流量。
现在验证更新后的规则:
pwd ls
您应该看到类似这样的内容:
root@hdicj8psx6:~# pwd /root root@hdicj8psx6:~# ls snap
为了确保防火墙规则在重新启动后仍然存在,请安装 netfilter-persistent 软件包:
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
使用 PM2 开始您的应用
使用以下命令启动带有 PM2 的服务器:
sudo apt upgrade
-f 标志强制 PM2 重新启动进程。
删除未使用的进程
如果有以前运行的进程,您可以使用它们的 ID 删除它们:
568.82.48.166 root Subham@Xam_08
这可确保重复进程不会发生冲突。
现在您的应用程序应该可以顺利运行了! ?
_注意:我将用有关 SSL、nginx 和所有其他相关事物的所有信息更新博客 _
以上是您需要的唯一 VPS 指南:从设置到生产的简单步骤的详细内容。更多信息请关注PHP中文网其他相关文章!