Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Ghost in a subdirectory of my main Node.js application

I am trying to run Ghost on a subdirectory of my main Node.js project. It is currently hosted in azure websites.
Something like: http://randomurlforpost.azurewebsites.net/blog

I followed the instructions here: https://github.com/TryGhost/Ghost/wiki/Using-Ghost-as-an-NPM-module

With the new addition of using Ghost as a npm module do I still need Nginx or Apache?. As of now I have my main site running on localhost:3000 and the Ghost instance running on localhost:2368.

I have tried doing all kinds of modifications to the part of the code stated on the instructions however I have not succeeded.

//app.js, is there a specific place to put this?

var ghost = require('ghost');
ghost().then(function (ghostServer) {
    ghostServer.start();
});

//config.js
    development: {
        url: 'http://localhost:3000/blog',
        database: {
            client: 'sqlite3',
            connection: {
            filename: path.join(__dirname, '/content/data/ghostdev.db')
            },
            debug: false
        },
        server: {
            host: '127.0.0.1',
            port: '2368'
        },
        paths: {
            contentPath: path.join(__dirname, '/content/'),
        }
    },
//index.js
ghost().then(function (ghostServer) {

    parentApp.use(ghostServer.config.paths.subdir,ghostServer.rootApp);

    // Let ghost handle starting our server instance.
    ghostServer.start(parentApp);
}).catch(function (err) {
    errors.logErrorAndExit(err, err.context, err.help);
});

EDIT: I was able to route traffic with http-proxy however it is routing it to localhost:2368/blog (which doesn't exist) any ideas on how to prevent this?

var httpProxy = require('http-proxy');
var blogProxy = httpProxy.createProxyServer();
var ghost     = require('ghost');
var path      = require('path');

// Route /blog* to Ghost
router.get("/blog*", function(req, res, next){ 
    blogProxy.ws(req, res, { target: 'http://localhost:2368' });
});
like image 560
AndresGalaviz Avatar asked Jan 23 '15 04:01

AndresGalaviz


1 Answers

I've been having the same issue and first tried using Apache's ProxyPass to redirect /blog to port 2368 but found other issues doing this.

Before trying my suggestions you should undo any changes made using httpproxy.

What seems to have worked for me is placing the code you have in index.js directly into your app.js file instead of what you already have in there. You will need to add the ghost errors variable and rename parentApp to the name of your app, I'll call this yourAppName so it's clear but mine is just app. So inside app.js you can put:

var yourAppName = express();
var ghost = require('ghost');
var ghosterrors = require('ghost/core/server/errors')

ghost().then(function(ghostServer) {
  yourAppName.use(ghostServer.config.paths.subdir, ghostServer.rootApp);

  ghostServer.start(yourAppName);
}).catch(function(err) {
  errors.logErrorAndExit(err, err.context, err.help);
});

You probably already have the ghost and express variables declared in app.js so you won't need to add these lines.

The blog should now be available at the URL specified in config.js.

like image 96
mykfmn Avatar answered Nov 16 '22 04:11

mykfmn