How to: Install Ghost with a subdomain on NGINX.

Moving Ghost to a subdomain? Here's a guide to help along the way.

Now, first off I'll admit, I'm pretty new to Ghost (even after backing it on Kickstarter) so think of this as a newbie guide.

I encountered your typical struggles along the way, so I hope I can help highlight where I went wrong.

This neat how-to may help : https://www.allaboutghost.com/how-to-install-ghost-in-a-subdirectory/

Prerequisites:

-Installation: Digital Ocean VPS, Ubuntu 14.04, Vanilla NGINX, Ghost v0.7.9. (how to manually install on D.Ocean - https://www.digitalocean.com/community/tutorials/how-to-host-ghost-with-nginx-on-digitalocean).

-Domains: Eg www.exampledomain.com. Installed on 1 domain. (make sure your NameServer etc is pointing to the correct location)

-Console Access: A non-root user with sudo privileges. Either through your VPS provider or sFTP but preferably SSH, and a text editor of your choice just incase.


1. Update where Ghost Lives

1.1) Open the Ghost config file at default location. - sudo nano /var/www/ghost/config.js

1.3) Change your EXAMPLEDOMAIN.co.uk to add /blog or whatever designation you choose for your Ghost URL.

    // Configure your URL and mail settings here
    production: {
        url: 'http://EXAMPLEDOMAIN.co.uk/blog/',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },
        server: {
            host: '127.0.0.1',
            port: '2368'
        }
    },

1.6) Once you have added /blog, hit CTRL+x and press Y and ENTER key to save changes to Ghost's config.js file.

So now www.exampledomain.co.uk/blog is the URL in which your Ghost install will be reached, but not until we finally configure NGINX to proxy requests to it.


2. Update NGINX Configuration

2.1) Open the NGINX server config file at default location. - sudo nano /etc/nginx/sites-enabled/default .

This is the default file although you can create others as per installation. (We can use this file later to enable PHP processing, and also to change the default index file on the default server/domain.)

2.3) Change your location { to include /blog. So location /blog { . Or whatever location you specified earlier.

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

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

         client_max_body_size 10G;

    location /blog {
        proxy_pass http://localhost:2368;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_redirect off;
    }

2.6) Once you have added /blog, hit CTRL+x and press Y and ENTER key to save changes to NGINX's server default file.


3. Restart NGINX

3.1) Restart NGINX for the applied config changes to take effect. - service nginx restart .


E voilĂ , You are done!

Your Ghost site should be reachable at your new specified URL and location defined with NGINX. For example www.exampledomain.co.uk/blog


TroubleShooting

Welcome to NGINX!

Reaching your default file server / single domain address.

Now at this point, I had nothing installed on www.exampledomain.co.uk but my Ghost blog was accessible at www.exampledomain.co.uk/blog

In this case trying to access your www.exampledomain.co.uk you may well be presented with...

I currently had a client's static HTML/php site that needed accessed. A simple matter of changing a config file.

Note: After changing the root directory for the default index file, NGINX was Downloading .PHP files rather than processing them, this is what we should hopefully remedy.


1. Install PHP

Skip to Step 2 if you have already installed PHP.

1.1) Install php5-fpm - sudo apt-get install php5-fpm

1.3) Open & Configure PHP.ini sudo nano /etc/php5/fpm/php.ini

1.5) Find and change cgi.fix_pathinfo=1, and change the 1 to 0, to process the exact file path. cgi.fix_pathinfo=0

1.9 ) Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.

listen = /var/run/php5-fpm.sock

CTRL+x press Y to save and exit.

1.95) Restart php5-fpm: sudo service php5-fpm restart


2. Update NGINX Configuration

2.1) Open the NGINX server config file again at default location. - sudo nano /etc/nginx/sites-enabled/default .

2.1) We're going to uncomment thus removing the # from the .php commands to match the code below.

location ~ \.php$ {  
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}
}

CTRL+x press Y to save and exit.

2.7) Further up towards the top of that file, we need to inspect where the root directory is. This will currently default to the 'welcome to nginx index file' aka root /usr/share/nginx/www

# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

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

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }

DigitalOcean droplet creates Ghost it's own directory and it is this which we need to edit to specify the default directory. Avoiding throwing up the "Welcome to NGINX" page.

Alternatively you can create your own root directory rather than modify the original. Copy this file and rename it to your own domain.


3. Update Ghost Configuration

3.1) Open the Ghost config file again at default location. - sudo nano /etc/nginx/sites-available/ghost

3.3) If not already done so, change the root to where your default files exist on the your domain. (NOT INCLUDING YOUR GHOST URL ie /blog)

3.5) Add the files to serve as index.php. So index index.php index.html index.htm;

3.7) Uncomment thus remove # lines, from the PHP section to match that of the code below.

You should end up looking a little like this

    root /var/www/EXAMPLEDOMAIN.co.uk/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name EXAMPLEDOMAIN.co.uk www.EXAMPLEDOMAIN.co.uk;

    client_max_body_size 10G;

    location /blog {
        proxy_pass http://localhost:2368;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_redirect off;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        # fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

CTRL+x press Y to save and exit.

3.7) Restart Nginx and php5-fpm service nginx restart && service php5-fpm restart


Access your domain

Hopefully, You are up and running!

Your Ghost site should be reachable at your new specified URL and location defined with NGINX. For example www.exampledomain.co.uk/blog

Your static HTML/php site should be reachable at your default URL and location defined with NGINX & Ghost config. For example www.exampledomain.co.uk


I apologise if I have led you further astray or have left gaps in the process. Like I said this is my first time setting up using Ghost/NGINX.

Please feel free to comments with how I can improve, and any problems you ran into, I will try to include later on.

Dont forget to like and share to help the community out.

All the Best- Peace Out

Jason