How To Set Up Nginx Server Blocks (Virtual Hosts) On Ubuntu Server
Introduction
Server Blocks is the terminology used in Nginx. They are configuration files with server { }
encapsulation, Hence the name Server Blocks. It is similar to Virtual Hosts for people coming from Apache background. It is the Nginx way of configuring several websites within a single instance of Nginx installation.
There are two prerequisites you need to consider before proceeding
Prerequisites
- Ubuntu Server 20.04+.
- Secure shell access to your server with super user (sudo) permission.
- (optional) Need to have purchased a Domain name & VPS
- (optional) You Domain’s NameServer should be pointing to VPS service provider (ex: DigitalOcean, AWS, Linode…)
Step 1: Install Nginx
Nginx is available in Ubuntu package repository and can be installed by running
sudo apt-get install nginx
You can verify the status of Nginx server by running
sudo service nginx status
If the status is inactive then you may start Nginx server by running
sudo service nginx start
Once the Nginx server has started you may visit http://example.com and be greeted with Welcome to nginx! webpage.
Step 2: Understanding Nginx directory structure
Nginx has predefined directories for us to work with. Change your working directory to /etc/nginx/sites-available
. This is where all server block files exists in our server. When you ls
in this directory, We see a file named default in this directory. This is the default configuration file generated by Nginx during installation. If you read the contents of this file you may see a directive root with value /var/www/html
. This is our document root directory which Nginx has configured it for us. But we will be creating separate directories for each server block, As it enhances our website organisation and ease of use when dealing with several websites.
Step 3: Create Server Block (Virtual Host)
Let us create a file and name it ‘example.com’ in the following location /etc/nginx/sites-available
sudo touch /etc/nginx/sites-available/example.com
Use a text editor of choice vim
or nano
to edit the file we created. I would be using ‘vim’ as my choice of editor through out this tutorial.
sudo vim /etc/nginx/sites-available/example.com
Copy paste the below code for server block configuration.
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Server directive server { }
encapsulates the configuration needed for a Server Block. Where, root is the default directory used to serve web pages and ‘index.html’ is the default file to be served when the domain url gets a HTTP GET request. The default location ‘/‘ is the entry point of the website which in our case is root directory. If a URL request fails, Nginx will look for a file named 404.html in the root directory.
Step 4: Create Document Root Directory
Let’s create the document root directory for our server block and populate ’index.html’ with the html content given below.
sudo mkdir /var/www/example.com
sudo touch /var/www/example.com/index.html
Copy paste the below html code into /var/www/example.com/index.html
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to Nginx</h1>
</body>
</html>
There is one step pending, I.e., to enable the server block. Nginx by default looks for configuration files in ‘sites-enabled’ directory to start the server. We create a symbolic link from /etc/nginx/sites-available/example.com
to /etc/nginx/sites-enabled/example.com
to enable our server block.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Let us restart the Nginx server to load the configurations we created.
sudo service nginx restart
Find your IP address
With this your website can be accessed on example.com or the IP address of your server.
You can find IP address of your server by running
ip addr | grep inet
Conclusion
Additional domains could be enabled by creating server block files for each domain in /etc/nginx/sites-available/
directory. Ensure you have directories created for them in /var/www/