Run Ghost and other websites using Nginx Webserver on Ubuntu 16.04

Aug 8, 2018 2 min read
Run Ghost and other websites using Nginx Webserver on Ubuntu 16.04
Prerequisites

I will start by assuming that you have a server running and did all the prerequisites. If you don't, you can check this guide that teaches you how to setup a ghost blog

Getting Started

The basic idea of using multiple ghost blogs or multiple webpages is to have "a space" assigned to Nginx, so it can process your requests. Nginx has one server block called default, this means that whatever request is not included in a server block in Nginx, it will go there. You can set your main page as default or any other website (server block)

For instance, let's say you have 2 websites:

site3.com
site2.com

They could be both blogs, or static pages or something else, the point is the same, which is to assign it to Nginx.

Let's say that site2.com will be our 2nd web and site3.com is the 3rd

For site2.com you would do as follow:

Assuming you have ghost installed, you will have a .conf created, you can check, if not it's very simple to create it, type: cd /etc/nginx/sites-available/ and check for the file. It should end in .conf.

Open it sudo vim nameOfTheFile.conf and check what's inside, assuming you don't have ssl, it should be simple like this:

Here you are listening on port 80, for ghost you have to find which port node.js is listening and assign it, we'll go over it for the next domain. But this one is great for static pages, it's the basic example.

Snippet:

server {
     listen 80;
     listen [::]:80;
     server_name site2.com www.site2.com;

     root /var/www/site2_web;

     index index.html index.htm;

     location / {
          try_files $uri $uri/ =404;
     }
}

After modifying the file in /etc/nginx/sites-available, we need to create a link to this file to /etc/nginx/sites-enabled, otherwise nothing of this would work. Go to: cd /etc/nginx/sites-enabled and type:

sudo ln -s /etc/nginx/sites-available/example.com.conf

Now let's say you want to add your other domain: site3.com, which is a ghost blog, the snippet would be like this:

server {

    server_name site3.com www.site3.com;
    root /var/www/site3_web;

    location / {
        proxy_set_header X-Forwarded-For 
        $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2369;
    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;

}

Notice the difference between the two. Also, where the app is listening.
To make sure that your ghost blog is listening on the same port as node.js, type: sudo netstat -peanut.

Also make sure your folders have the right permissions:
sudo chmod -R 755 /var/www/site2_web
sudo chmod -R 755 /var/www/site3_web

After that restart Nginx: sudo systemctl restart nginx

Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.