Install Ghost blog on a subdomain using Nginx server on Ubuntu 18.04
Recently I had a client who had VPS with Ubuntu 18.04 and had to quickly install the Ghost blog platform in a subdomain, however, his primary domain or the root folder was already populated by WordPress. I took immediate action and this is how I did it:
Prerequisites
- Nginx server installed
- Node.js installed
- MySQL installed
1. Create subdomain and test
Make sure that via your domain provider you have created a subdomain, usually, this can be done by only adding a new A record for the subdomain, it must point to the server IP address, for example, this is how is done via the Hostinger panel, here is a screenshot as an example:
However, if you bought your domain from Namecheap or Godaddy or some other provider then the approach should be pretty similar. Just in case, after you add the subdomain, check if the subdomain has properly propagated via this tool: https://dnschecker.org/#A and if all good continue reading below.
First SSH into your server as root, create a new directory for the subdomain where the ghost blog will be installed, usually should be inside the /var/www/subdomain.example.com/html directory.
> mkdir /var/www/suddomain.example.com/html
That’s it for now, please leave this here and let’s continue with the subdomain configuration later on when we will be installing ghost.
2. Create a new database
Access the MySQL client:
> mysql -u root -p
if prompted for a password simply add your root password.
Write the following to create a new database:
mysql> CREATE DATABASE yourDatabaseName CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
Note: replace “yourDatabaseName” with another name you wish to for the database.
Let’s create a new user for this database and grant full privileges for that user:
mysql> GRANT ALL ON yourDatabaseName.* TO yourUserName @’localhost’ IDENTIFIED BY ‘password’;
Note: replace “yourUserName” with the username per your wish, between the apostrophes change the “password” with your own password.
In the end, just in case, let’s do a flush:
mysql> FLUSH PRIVILEGES;
3. Create a new substitute (sudo) user
Run the command:
> adduser username
Note: be sure to replace username with the user that you want to create.
Note: using the user name ghost causes conflicts with the Ghost-CLI, so it’s important to use an alternative name.
4. Install Ghost via Ghost-CLI
The main aim of the Ghost-CLI is to make it possible to install or in future time to update your Ghost blog in a single command.
First, let’s install the CLI, run the following command:
> sudo npm install -g ghost-cli@latest
This will install the Ghost-CLI globally on your VPS which is required for running the installation.
Make the sudo user as a non-root user for the subdomain:
> sudo chown username:username /var/www/subdomain.example.com/html
Provide directory access to the subdomain:
> sudo chmod 775 /var/www/subdomain.example.com/html
Now navigate to your subdomain:
> cd /var/www/subdomain.example.com/html
Switch to the sudo user you previously created:
> su — username
Finally, run the command to install ghost:
ghost install
Now simply follow all the prompts that it gives you:
- Ghost Blog URL: “http://subdomain.example.com” (specify the full URL path for the subdomain).
- Host: “localhost” (Just press enter).
- MySQL Username: “yourUserName” (Write the database user that you previously created).
- Password: “yourPassword” (Write the password that you set during the MySQL server installation)
- Ghost User: “Y” (It will set up a default ghost user)
- Ghost Database: “yourDatabaseName” (Write the database name that you already created).
- Let’s Encrypt SSL: If you want to set up an SSL for your blog then just press “y” and enter your email. It will set up an SSL for your blog.
- Setup Nginx: “Y” (This is important because it will generate a correct server block, please read below).
- Setup Systemd: “Y” (Do not skip it or press “n” because it runs ghost or if you press “n” for now then you have to set up it manually. So just press “y” and let it install automatically).
- Start Ghost: Definitely choose and enter “yes”.
- That’s it!
5. Completing the subdomain configuration
Switchback again to the root user:
> su — root
You must rename and check the subdomain.example.com.conf config file that was created by the ghost-CLI:
> sudo nano /etc/nginx/sites-enabled/subdomain.example.com.conf
To avoid further conflicts, we have only to rename the file from subdomain.example.com.conf to subdomain.example.com:
> mv /etc/nginx/subdomain.example.com.conf /etc/nginx/subdomain.example.com
now let’s copy and symlink that file to a file in Nginx’s sites-enabled directory:
> sudo ln -s /etc/nginx/sites-available/subdomain.example.com /etc/nginx/sites-enabled/subdomain.example.com
open the file:
> nano subdomain.example.com
you should have this server block inside the file:
server {
listen 80;
listen [::]:80; server_name subdomain.example.com;
root /var/www/subdomain.example.com/html/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh) 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:2368;
} location ~ /.well-known {
allow all;
} client_max_body_size 50m;
}
if in the server block there is a different URL for the subdomain then make sure to replace it with the actual subdomain URL.
*Important:
Before you save and exit the file, check the location block for correct proxy, take a look on this line:
proxy_pass http://127.0.0.1:2368;
you must make sure that the IP and the proxy match the same as from the /var/www/subdomain.example.com/config.production.json file, for example:
“url”: “https://subdomain.example.com",
“server”: {
“port”: 2368,
“host”: “127.0.0.1”
},
6. Test and finish
Just in case check the error log:
> sudo nano /var/log/nginx/error.log
Check if there is an error in any conf file:
> nginx -t
If all passes without any errors and all is good then do not forget to restart the Nginx server:
> sudo service nginx restart
In the end, navigate to your subdomain URL address and your new Ghost blog should load, navigate to the admin panel to configure your new Ghost blog installation, the URL needs to be ending with /ghost so the full path needs to be like this: “subdomain.example.com/ghost”.
Follow the steps on the screen and complete the installation.
I really love working with the Ghost blog platform, I already developed and delivered a couple of themes for this platform, it runs very fast and it is minimalistic, I wish you happy blogging.
Let’s connect via Twitter: https://twitter.com/ifkas