Install Nginx with Server Blocks
One very important thing with the droplet I setup on DigitalOcean is to be able to host multiple sites. Hence, we're going to use Server Blocks (the equivalent of Virtual Hosts on Apache).
I found and tried several tutorials on how to create Server Blocks and I ended up creating my own "method" based on all the resources I found.
Ghost is the first thing I will host and in most tutorials I've been told to setup Node.js, Ghost and then Nginx. However, I found it better to start with Nginx and then do the rest.
Setup your Domain Hosts
First things first, make sure your domain is setup correctly. Wether you use the root domain or a subdomain, make sure the A record points to your server's IP address.
Create a New Directory
We now need to create the directory where the site will be hosted. In this example, I'll use domain.com
as the domain.
Install Nginx
In your SSH terminal, type the following line to install Nginx
Now let's delete the default configuration file, then we will create our own.
Sources
Create a Server Blocks File
For the configuration file, I mostly based my settings on the Nginx documentation.
We can now create our configuration file.
I called it blocks
in this example, but you can of course call it whatever you want.
This line will open the text editor in which we will input the custom configuration.
Sources
Create the Blocks
There are many ways of configuring a block. As of now I have only done it for Ghost, so here is what you need for it. However, if you refer to the Nginx documentation you will find all the basic elements for a block.
Note: When editing a file with nano, you can save the changes by pressing Ctrl+O (Ctrl right). Press Ctrl+X to exit.
Ghost
Next step is to create a "symlink" to the directory sites-enabled
. Indeed, sites-available
only contains the available sites, it doesn't mean it's enabled.
Finally, let's restart Nginx.
Sources
- https://www.digitalocean.com/community/articles/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3
- http://wiki.nginx.org/ServerBlockExample
- http://gerardmcgarry.com/2010/setting-up-a-virtual-host-in-nginx/
That's it. If everything went well, you should see the Nginx welcome page when browsing to domain.com.