Introduction

Ghost blog is a very popular, fast and open source blogging and publishing platform,  written in JavaScript, using the Node.js framework. As stated on the their website, “the inventors of JavaScript, Android and StackOverflow all use Ghost for their blogs”.

So let’s install and configure Ghost on an Ubuntu 16.04

Getting started – Install Node.js

It is possible to install Node.js using NodeSource binary distributions repository. To accomplish this task, execute the following commands:

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install -y nodejs

Install build tools, required for building and installing npm, with apt:

# apt-get -y install build-essential

Install Ghost

If it doesn’t already exist, create the directory /var/www/:

# mkdir -p /var/www/

There, download Ghost, grabbing the latest version from Ghost.org:

# cd /var/www
# curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip

Next, unzip the downloaded archive:

# unzip -uo ghost.zip -d ghost

Move to the new ghost directory, and install Ghost using npm:

# cd ghost
# npm install --production

This is a very fast process, and is totally automated. At the end, Ghost will be correctly installed on the server. Start Ghost (for this moment, just for testing) :

# npm start --production

By default, Ghost runs on http://192.168.1.1:2368 (replace 192.168.1.1 with your server IP Address)

Let’s open http://192.168.1.1:2368 in our browser, we should now see:

In the terminal used for starting Ghost, the process should display a WARNING message:

WARNING: Ghost is attempting to use a direct method to send email.
It is recommended that you explicitly configure an email service.

Right now, this is not a problem and can be ignored. Stop the process with CTRL+C.

Ghost configuration

First of all, create a new user used to run Ghost with the following command:

# useradd -d /var/www -s /bin/bash ghost
# passwd ghost

Change the owner of the Ghost root directory:

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

After running Ghost for the first time, looking in its root directory you should see different files. We are interested in the one named config.js .

If you haven’t yet run Ghost for the first time, you will see only config.example.js. Copy it:

# cp config.example.js config.js

Open this file with a text editor:

# $EDITOR config.js

In that, we are interested in the production block:

production: { 
 url: 'http://my-ghost-blog.com',
 mail: {},
 database: {
 client: 'sqlite3',
 connection: {
 filename: path.join(__dirname, '/content/data/ghost.db')
 },
 debug: false
 }, 

 server: {
 host: '127.0.0.1',
 port: '2368'
 }
 },

This is the default. We will change it after configuring the server.

Install and configure Apache Web Server

Install Apache with apt:

# apt-get install apache2

Next, enable SSL for Ghost. To do this, create a directory which will contain the SSL certificates:

# mkdir -p /etc/apache2/certs

Generate the certificate (self-signed) with OpenSSL, executing the command:

# openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout /etc/apache2/certs/ghost.key -out /etc/apache2/certs/ghost.crt

Change permissions on the certificate file:

# chmod 600 /etc/apache2/certs/*

Next step is to create a new Virtual Host file for Ghost. With a text editor, create a new file:

# $EDITOR /etc/apache2/sites-available/ghost.conf

In this file, paste the following content:

<VirtualHost *:80>
    ServerName myblog.com
    ServerAlias www.myblog.com
    # Force http to https
    Redirect permanent / https://myblog.com/
#    ProxyRequests off 
#    ProxyPass / http://127.0.0.1:2368/ 
#    ProxyPassReverse / http:/127.0.0.1:2368/     
</VirtualHost>

<VirtualHost *:443>

   ServerName myblog.com

   SSLEngine on
   SSLCertificateFile /etc/apache2/certs/ghost.crt
   SSLCertificateKeyFile /etc/apache2/certs/ghost.key

   ProxyPass / http://127.0.0.1:2368/
   ProxyPassReverse / http:/127.0.0.1:2368/
   ProxyPreserveHost   On

   RequestHeader set X-Forwarded-Proto "https"

</VirtualHost>

Save, exit, and activate the SSL Apache module with the following command:

# a2enmod ssl headers

Next, restart Apache:

# systemctl restart apache2

Edit Ghost configuration file

Open the config.js file we have already seen:

# $EDITOR /var/www/ghost/config.js

Change the URL line in the production block, as follows:

url: 'https://myblog.com'

Save and exit. With this change, all requests made via plain HTTP will be redirected to HTTPS.

Create a new systemd service for Ghost

To make it easier to start Ghost, we will create a new systemd service. With a text editor, create a new file:

# $EDITOR /lib/systemd/system/ghost.service

In this empty file, paste the following content:

[Unit]
Description=Ghost - Blogging platform
After=network.target

[Service]
Type=simple
WorkingDirectory=/var/www/ghost
User=ghost
Group=ghost
ExecStart=/usr/bin/npm start --production
ExecStop=/usr/bin/npm stop --production
Restart=always
SyslogIdentifier=Ghost

[Install]
WantedBy=multi-user.target

Save and exit.

Reload the systemd daemon:

# systemd daemon-reload

Using systemd, start Ghost and enable it to start at boot time:

# systemctl start ghost
# systemctl enable ghost

Now, with a web browser, go to URL https://myblog.com and you should see the Ghost Welcome Page.

Conclusion

Ghost is an easy to configure blogging and publishing platform, used by many companies worldwide, like Mozilla and SkyNews. It’s a free and open source software which makes it possible for anyone to easily create a new blog. In this tutorial, we have shown how a full installation and configuration can be completed in just a few steps, using Ubuntu 16.04 and Apache as your web server. Blog away!

Помог ли вам данный ответ? 17 Пользователи нашли это полезным (43 голосов)