Ghost comes configured by default to use SQLite, though, and of course I had to run it on Postgres. Also, I needed to set up Apache vhosts to redirect to the various Ghost instances, since one Node instance only runs one Ghost instance, and needed supervisord for autostart. At some point later, I'll repackage all this to use docker containers for each Ghost instance, but these instructions don't cover that.
First, install Node.js. OS packages are fine for Ubuntu 14.04 or Fedora; for other OSes you may need to get funky with source installs. I'm not going to cover that here. You'll also need to install the Node PostgreSQL driver, and the postgresql-server-dev package.
Second, "git clone" the stable branch of the Ghost code into an appropriate directory. This can be directly your web directory for Ghost, or you can download, build, and then copy. If you do the latter, be aware that Ghost has several "dot" files it needs which a regular "cp -r" won't pick up.
Now, before you build Ghost, you're going to need to make some changes to support PostgreSQL. Edit "package.json" in the main directory, and add PostgreSQL into the "optional dependencies" section like so:
"optionalDependencies": {
"mysql": "2.1.1",
"pg" : "latest"
},
Now, build Ghost by running the build commands from the home directory:
npm install -g grunt-cli
npm install
grunt init
grunt prod
Make sure you scroll through the output of the above commands and look for errors; they don't always fail on error.
As a note, Ghost, like Rails, has "dev", "stage", and "production" modes. Each of these modes can use a different database, even a different DBMS. Or you can have Ghost connect to the same database for all three modes. By default, Ghost starts in dev mode.
You'll need to configure your database connection. Before editing config.js, make sure that you've set up PostgreSQL to accept local connections using an md5 password. I additionally route Ghost through pgBouncer in case of getting slammed by connections; this requires you to configure pgBouncer as well:
- add Ghost user to the database
- create a database for the Ghost
- configure pg_hba.conf so the new user can connect with an md5 password
- if using pgBouncer, configure pgbouncer.ini and users.txt with the database and the user/password you're using for Ghost.
database: {
client: 'pg',
connection: {
host : '127.0.0.1',
port : '6432',
user : 'ghostblog',
password : 'supercalifragalistic',
database : 'ghostblog',
charset : 'utf8'
}
},
Then save. As you can see, that's a configuration for pgBouncer. If you're not using pgBouncer, then you can omit the host and port configuration. If you're using Heroku, you'll need to point the Host at the public address for your Heroku Postgres instance. Restart Ghost at this point, or start it if you haven't already:
npm start
or:
npm start --production
After this, you should be able to connect to Ghost on port 2368 of the defined interface (127.0.0.1 if you haven't configured anything). However, that doesn't actually get a site up. For one, Ghost may have to share the server with non-Ghost sites and other Ghost instances. Second, users aren't generally accustomed to looking for sites on port 2638. Also, I'm not all that confident about Node.js security, given how new the platform is.
Enter Apache vhosts with proxying.
First, configure Ghost to listen only an alternate port and the internal IP address:
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()`
port: '4444'
}
Note that if you have several Ghost instances, you'll need a different port for each. If you're running Ghost under Docker, then redirect the Ghost port to a unique high port on the host.
Next, create a new vhost file (in /etc/apache2/servers-available on Ubuntu) pointing to Ghost:
<VirtualHost *:80>
ServerName sfpostgres.org
ServerAlias www.sfpostgres.org
ProxyRequests off
ProxyPass / http://127.0.0.1:4444/
ProxyPassReverse / http:/127.0.0.1:4444/
CustomLog /var/log/apache2/www.sfpostgres.org-access.log combined
ErrorLog /var/log/apache2/www.sfpostgres.org-error.log
</VirtualHost>
Those ProxyPass lines allow bi-directional pass-through from Apache to Ghost. Note that this prevents you from putting any other Apache directives on the Vhost other than logging, such as an authentication config; they will get ignored, because the request gets passed to Ghost first.
Now, link that to servers-enabled, and restart Ghost and Apache. You should now be able to connect to Ghost using the URL of the vhost.
But wait ... what's keeping Ghost running? npm will quit on error, and has no ability to restart with a server restart. You could craft your own init file, but and alternative is to use supervisord.
First, install supervisor from packages. Then add a config file to /etc/supervisor/conf.d:
[program:sfpostgres]
command = node /var/www/sfpostgres.org/index.js
directory = /var/www/sfpostgres.org
user = ghost
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/sfpostgres.log
stderr_logfile = /var/log/supervisor/sfpostgres_err.log
environment = NODE_ENV="production"
Then add the service to supervisor and enable it:
supervisorctl add sfpostgres
supervisorctl start sfpostgres
.... and that's it! You should now have a Ghost instance on a custom domain with autorestart able to share a server with other websites. And most importantly, running on PostgreSQL.