glenpadua
By:
glenpadua

502 Bad Gateway error for Ghost after setting up server block for static site and restarting Nginx.

July 5, 2016 4k views
Ghost Nginx One-Click Install Apps Ubuntu

Hello there,

I recently installed Ghost via the One Click Application and tried to host it on a subdomain blog.my-domain.com. I created an Nginx server block for the static site to point to www.my-domain.com which is working fine. But when I try to access the Ghost Blog I get a 502 Bad Gateway error.

The two directories configured to serve the websites are:
1) /var/www/ghost for the blog which was there by default
2) /var/www/my-domain.com/html for the static website

The server block config file for ghost is at /etc/nginx/sites-enabled/ghost

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

    root /var/www/ghost;
    index index.html index.htm;

    server_name blog.my-domain.com www.blog.my-domain.com;

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.1:2368;
            proxy_redirect off;
     }
}

The server block config for the static site is at /etc/nginx/sites-enabled/my-domain.com

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

    root /var/www/my-domain.com/html;
    index index.html index.htm;
    server_name my-domain.com www.my-domain.com;

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

The Ghost config.js file which is at /var/www/ghost/ contains

var path = require('path'),
    config;

config = {
    // ### Production
    // When running Ghost in the wild, use the production environment.
    // Configure your URL and mail settings here
    production: {
        url: 'http://blog.my-domain.com',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

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

        // #### Database
        // Ghost supports sqlite3 (default), MySQL & PostgreSQL
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost-dev.db')
            },
            debug: false
        },
        // #### Server
        // Can be host & port (default), or socket
        server: {
            // Host to be passed to node's `net.Server#listen()`
            host: '127.0.0.1',
            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
            port: '2368'
        },
        // #### Paths
        // Specify where your content directory lives
        paths: {
            contentPath: path.join(__dirname, '/content/')
        }
    }
};

module.exports = config;

I also tried to recreate the symlink by removing the old one and creating it again using

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

I restarted the server using sudo service nginx restart and also ghost with sudo service ghost restart

When I checked it status of ghost with sudo service ghost status , it says it's running.

The Nginx Logs can be found here

Any help would be much appreciated!

1 comment
  • Also adding some details from the ghost.log

    > ghost@0.7.8 start /var/www/ghost
    > node index
    
    ERROR: Unable to access Ghost's content path:
      EACCES: permission denied, open '/var/www/ghost/content/apps/d38062335fdc1a76'
    
    Check that the content path exists and file system permissions are correct.
    Help and documentation can be found at http://support.ghost.org.
    
    npm ERR! Linux 3.13.0-71-generic
    npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "start" "--production"
    npm ERR! node v4.3.1
    npm ERR! npm  v2.14.12
    npm ERR! code ELIFECYCLE
    npm ERR! ghost@0.7.8 start: `node index`
    npm ERR! Exit status 235
    npm ERR! 
    npm ERR! Failed at the ghost@0.7.8 start script 'node index'.
    npm ERR! This is most likely a problem with the ghost package,
    npm ERR! not with npm itself.
    npm ERR! Tell the author that this fails on your system:
    npm ERR!     node index
    npm ERR! You can get their info via:
    npm ERR!     npm owner ls ghost
    npm ERR! There is likely additional logging output above.
    npm ERR! Linux 3.13.0-71-generic
    npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "start" "--production"
    npm ERR! node v4.3.1
    npm ERR! npm  v2.14.12
    npm ERR! path npm-debug.log.4460b6c3f5deaa775ee9bfa630bc9220
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall open
    
    npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.4460b6c3f5deaa775ee9bfa630bc9220'
    npm ERR!     at Error (native)
    npm ERR!  { [Error: EACCES: permission denied, open 'npm-debug.log.4460b6c3f5deaa775ee9bfa630bc9220']
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'open',
    npm ERR!   path: 'npm-debug.log.4460b6c3f5deaa775ee9bfa630bc9220' }
    npm ERR! 
    npm ERR! Please try running this command again as root/Administrator.
    
1 Answer
glenpadua July 6, 2016
Accepted Answer

Got it working again! Somehow the permissions had changed. (Realization after reading the Ghost log and doing some research) So running,

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

and then restarting the Ghost service got it to work again!

Have another answer? Share your knowledge.