随着 node.js 变得越来越流行,越来越多的开发者开始使用它来创建后端应用程序。ghost 是一个基于 node.js 的开源博客平台,它使用了许多流行的 node.js 技术和库来帮助用户创建美观而功能丰富的博客。
本文将向您展示如何在 ghost 上托管一个 node.js 应用程序,以及如何为其配置自定义域名和 ssl 证书。
步骤 1:选择云服务器
首先,您需要选择一款云服务器来托管 ghost 应用程序。市场上有许多云服务器提供商,如 aws、digitalocean 和 linode 等。本文将以 digitalocean 为例进行说明。
步骤 2:创建虚拟机
在 digitalocean 上创建一个虚拟机很简单。按照以下步骤操作:
登录您的 digitalocean 帐户。在控制台页面上,单击“create droplet”。选择您的操作系统、计费计划和数据中心位置。在这个例子中,我们将选择 ubuntu 18.04、standard、sfo2。选择您的 ssh 密钥或创建一个新的 ssh 密钥。点击“create droplet”按钮。digitalocean 将会为您创建并启动一个全新的虚拟机。
步骤 3:安装 node.js 和 ghost
一旦您的虚拟机处于活动状态,您可以使用 ssh 连接到该虚拟机并安装 node.js 和 ghost。
使用 ssh 登录您的虚拟机。更新软件包列表并升级所有已安装的软件包:
sudo apt updatesudo apt upgrade
安装 node.js:
curl -sl https://deb.nodesource.com/setup_14.x | sudo -e bash -sudo apt-get install -y nodejs
下载 ghost 并解压:
curl -l https://ghost.org/zip/ghost-latest.zip -o ghost-latest.zipunzip -uo ghost-latest.zip -d ghost
在 ghost 目录中安装依赖项:
cd ghostnpm install --production
运行 ghost:
npm start --production
如果一切正常的话,您可以在浏览器中输入服务器的 ip 地址 + ghost 的默认端口 2368,查看出现了 ghost 的安装页面。
步骤 4:配置自定义域名和 ssl 证书
默认情况下,ghost 服务器只能通过 ip 地址访问。如果您想为 ghost 应用程序配置自定义域名和 ssl 证书,则必须执行以下步骤。
安装 nginx 作为 ghost 的反向代理服务器。
sudo apt-get install nginx
创建 nginx 配置文件:
sudo nano /etc/nginx/sites-available/ghost
然后,输入以下内容:
server { listen 80; listen [::]:80; server_name your_domain.com; location / { proxy_set_header host $http_host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-proto $scheme; proxy_pass http://127.0.0.1:2368; }}
这个配置文件将允许 nginx 作为 ghost 的网关。请注意将 your_domain.com 替换为您自己的域名。
使 nginx 知道该配置文件的存在:
sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/
重新启动 nginx 服务:
sudo service nginx restart
下载 ssl 证书
使用 letsencrypt certbot 来申请证书。在您的 ssh 终端中,运行以下命令:
sudo apt install certbot python3-certbot-nginx
然后,启用该证书:
sudo certbot --nginx
该命令将使用 nginx 配置文件中指定的域名来给您的 ghost 应用程序生成 ssl 证书。
启用 https
一旦您的 ssl 证书启用并验证成功,您就可以配置 ghost 应用程序只在 https 上运行。
先关闭 ghost:
npm stop --production
打开 ghost 配置文件 config.production.json。在“server”部分中添加以下内容:
"url": "https://your_domain.com","server": { "port": 2368, "host": "127.0.0.1"},"ssl": { "force": true}
重新启动 ghost:
npm start --production
现在,您的 ghost 应用程序已经在自定义域名和 https 上运行。
结论
恭喜!通过本文的介绍,您已经知道了如何在 ghost 上托管一个 node.js 应用程序并配置自定义域名和 ssl 证书。这些步骤也可以用于托管任何其他的 node.js 应用程序。
以上就是ghost nodejs 部署的详细内容。