How to configure Digital Ocean VPS to host multiple Ghost blogs

Hosting multiple ghost blogs on Digital Ocean.

Ghost is a free and open source blogging platform written in node.js (JavaScript) and it's designed for publishing. Moving from Wordpress to Ghost was a good idea, the site speed increased the dashboard is cleaner. But, Node.js is new compared to PHP, so when I needed to host multiple ghost blogs on the same VPS I've found the first downside of the platform. But after some research turned out that it's not so complicated after all.

This tutorial assumes you have created the one-click Ghost droplet on Digital Ocean and you have at least two domain names, which are pointing at your droplet.

So, let's start.
In your console, stop ghost to avoid problems:

service ghost stop

and nginx also:

service nginx stop

Now, we need to modify the nginx configuration. We need to define different paths in order to be able to handle requests separately. So, navigate to:

cd /etc/nginx/sites-enabled

Now, rename the ghost configuration file according to our first domain and then copy that to our second domain configuration file. We will change it later.

mv ghost myfirstdomain.conf
cp myfirstdomain.conf myseconddomain.conf

Then we have to modify our configuration files. So open the first one using nano or vim:

nano myfirstdomain.conf

and change the server_name to myfirstdomain.com

server_name myfirstdomain.com  

in the server section we have the following code:

server {  
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

We have to edit our second configuration file, the server_name will be myseconddomain.com and in addition we have to change the proxy_pass value, in order to be able to split the traffic between the two sites. It can be any port number that is not used by another process, so let's say 2378.

server_name myseconddomain.com  
proxy_pass http://localhost:2777  

We also need to change the server section of our second .conf file. It cannot be default server as well, so it will look like this:

server {  
    listen 80;
    listen [::]:80;

Then save the file and restart nginx.

service nginx restart

Now, we need to change the Ghost configuration. Go to the web root directory and create a directory for each Ghost blog:

cd /var/www
mkdir myfirstdomain.com
mkdir myseconddomain.com

We need to copy the Ghost directory into our new folders:

cp -r ghost myfirstdomain.com
mv ghost myseconddomain.com

Now, we can open our first Ghost configuration file and change the production url.

nano /var/www/myfirstdomain.com/ghost/config.js

Change the value of url in the production section to the first domain's url:

production: {  
    url: 'http://myfirstdomain.com'

Save and close the file and then we have to do the same thing for the second Ghost site.

nano /var/www/myseconddomain.com/ghost/config.js

Of course, in the production section the value of the url will be corresponding to our second domain and in addition you have to scroll down to the server section and change the port:'2378'

production: {  
    url: 'http://myseconddomain.com'
port: '2378'  

To be able to easily manage our Ghost blogs separately we have to create Upstart Scripts, which will allow us to start, stop, restart and check status of each ghost site. So first we have to delete the old System V Script

rm /etc/init.d/ghost

Now we can create the new configuration for our sites. Go to the init directory and create the first configuration file:

cd /etc/init
nano ghost-myfirstdomain.conf

and paste the following code:

# ghost-myfirstdomain

start on startup

script  
    cd /var/www/myfirstdomain.com/ghost
    npm start --production
end script  

save and close this file, then copy it to the second site's configuration file, and change accordingly

cp ghost-myfirstdomain.conf ghost-myseconddomain.conf

In our case the second configuration file will look like this:

# ghost-myseconddomain

start on startup

script  
    cd /var/www/myseconddomain.com/ghost
    npm start --production
end script  

The last step is to start the Ghost blogs by running:

service ghost-myfirstdomain start
service ghost-myseconddomain start

If you visit each of your domains, you should see the Ghost blogging landing page.