Ubuntu 16.04上安装和配置Ghost 博客

Ubuntu 16.04上安装和配置Ghost 博客

介绍

Ghost是一个轻量级的开源博客平台,易于使用。 Ghost是完全可定制的,有很多主题可用。

先决条件

要完成本教程,您需要:

第1步 – 安装Ghost

首先,我们需要安装Ghost。 我们将Ghost放在var/www/ghost目录中,这是推荐的安装位置。

使用wget从Ghost的GitHub存储库下载最新版本的Ghost:

wget https://ghost.org/zip/ghost-latest.zip

要解压缩存档,请首先使用软件包管理器安装unzip程序:

sudo apt-get install unzip

然后将下载的软件包解压缩到/var/www/ghost目录:

sudo unzip -d /var/www/ghost ghost-latest.zip

切换到/var/www/ghost/目录:

cd /var/www/ghost/

然后安装Ghost依赖项,但只有生产所需的依赖项。 这跳过任何依赖项,只有开发Ghost的人才需要。

sudo npm install --production

这个过程完成后会安装Ghost,但是我们需要设置Ghost才能启动它。

第2步 – 配置Ghost

Ghost使用位于/var/www/ghost/config.js的配置文件。 此文件不是现成的,但Ghost安装包括文件config.example.js ,我们将用作起点。

将示例配置文件复制到/var/www/ghost/config.js 我们将复制该文件,而不是移动它,以便我们有一个原始配置文件的副本,以防我们需要恢复您的更改。

sudo cp config.example.js config.js

打开文件进行编辑:

sudo nano config.js

首先,我们必须更改Ghost使用的URL。 如果我们没有,博客上的链接会将访问者带到my-ghost-blog.com 。 url字段的值更改为您的域名,或者更改为服务器的IP地址,如果您现在不想使用域名。

/var/www/ghost/config.js
...

config ={// ### Production// When running Ghost in the wild, use the production environment// Configure your URL and mail settings here
    production:{
        url:'http://your_domain_or_ip_address',
        mail:{},...

url值必须采用URL的形式,例如http:// example.comhttp:// 11.11.11.11 如果此值格式不正确,Ghost将无法启动。

Ghost可以在没有邮件设置的情况下工作; 它们只有当您需要支持Ghost用户的密码恢复时才需要。 我们将在本教程中跳过配置此设置。

您可以通过遵循官方网站上的配置详细信息进一步自定义Ghost。

保存文件并退出编辑器。

仍然在/var/www/ghost目录中,使用以下命令启动Ghost:

sudo npm start --production

输出应类似于以下内容:

> ghost@0.11.7 start /var/www/ghost
> node index

WARNING:Ghostis attempting to use a direct method to send email.Itis recommended that you explicitly configure an email service.Helpand documentation can be found at http://support.ghost.org/mail.Migrations:Creating tables......Ghostis running in production...Your blog is now available on http://your_domain_or_ip_addressCtrl+C to shut down

Ghost在端口2368上监听,如果您已按先决条件文章中所述启用UFW防火墙,则您将无法直接访问它。 让我们在Ghost前面设置Nginx。

Ubuntu 16.04上安装和配置Ghost 博客

第3步 – 配置Nginx到Ghost的代理请求

下一步是设置Nginx为我们的Ghost博客服务。 这将允许端口80上的连接连接到运行Ghost的端口,因此人们可以访问您的Ghost博客,而不必在地址末尾添加:2368 它还添加了一个间接层,并设置你扩展你的博客,如果它增长。

如果Ghost仍在您的终端中运行,请按CTRL+C关闭Ghost实例,然后继续。

现在让我们配置Nginx。 更改到/etc/nginx目录,并删除/etc/nginx/sites-enabled中的默认Nginx配置文件:

cd /etc/nginx/
sudo rm sites-enabled/default

/etc/nginx/sites-available/ called中创建一个新文件ghost

sudo nano /etc/nginx/sites-available/ghost

将以下配置放在该文件中,并将your-domain-name更改为您的域名,如果没有域,则将您的服务器IP地址更改your-domain-name

/ etc / nginx / sites-available / ghost
server {
    listen 80;
    server_name your_domain_or_ip_address;
    location /{
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass         http://127.0.0.1:2368;}}

此基本配置将此服务器的所有请求发送到在端口2368上运行的Ghost博客,并设置适当的HTTP标头,以便在查看Ghost日志时,您将看到访问者的原始IP地址。

保存文件,退出编辑器,并通过在/etc/nginx/sites-enabled目录中为此文件创建符号链接来/etc/nginx/sites-enabled配置:

sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

然后测试配置以确保没有问题:

sudo nginx -t

如果一切正确,您将看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果您看到任何错误,请修复它们并重新测试配置。

使用正在运行的配置文件,重新启动Nginx以应用更改:

sudo service nginx restart

在我们再次启动Ghost之前,让我们创建一个新的用户帐户来运行Ghost。

第4步 – 将Ghost作为单独用户运行

为了提高安全性,我们将在单独的用户帐户下运行Ghost。 此用户将只能访问/var/www/ghost目录及其主文件夹。 这样,如果Ghost被破坏,您最小化对系统的潜在损害。

使用以下命令创建一个新的ghost用户:

sudo adduser --shell /bin/bash --gecos 'Ghost application'ghost

然后使此新用户成为/var/www/ghost目录的所有者:

sudo chown -R ghost:ghost /var/www/ghost/

现在让我们确保这个用户可以运行Ghost。 ghost用户身份登录:

su - ghost

现在启动此用户下的Ghost,并确保它运行:

cd /var/www/ghost
npm start --production

您应该可以访问http:// your_domain_or_ip_address访问您的博客。 Nginx将发送请求到您的Ghost实例。

事情工作很好,但让我们确保Ghost在未来继续良好运行。

第5步 – 将Ghost作为系统服务运行

目前,Ghost正在我们的终端上运行。 如果我们注销,我们的博客将关闭。 让我们得到Ghost在后台运行,并确保它重新启动时系统重新启动。 为此,我们将创建一个systemd单元文件,指定systemd应如何管理Ghost。

创建一个新文件来保存systemd单元文件的定义:

nano /etc/systemd/system/ghost.service

将以下配置添加到文件,该文件定义服务的名称,服务的组和用户以及如何启动它的信息:

/etc/systemd/system/ghost.service
[Unit]Description=GhostAfter=network.target

[Service]Type=simple

WorkingDirectory=/var/www/ghostUser=ghostGroup=ghostExecStart=/usr/bin/npm start --production
ExecStop=/usr/bin/npm stop --production
Restart=always
SyslogIdentifier=Ghost[Install]WantedBy=multi-user.target

保存文件并退出编辑器。 然后启用和启动服务:

systemctl enable ghost.service
sytemctl start ghost.service

再次访问http:// your_domain_or_ip_address ,您就会看到您的博客。

结论

在本教程中,您安装了Ghost,配置Nginx来代理对Ghost的请求,并确保Ghost作为系统服务运行。

猜你喜欢

转载自www.linuxidc.com/Linux/2017-07/145695.htm
今日推荐