Setup Nginx Server Blocks In Ubuntu
Setup Nginx Server Blocks In Ubuntu 18.04 LTS
Make sure you have updated your Ubuntu system to most recent version.
$ sudo apt-get update
1. Install Nginx webserver
To install Nginx webserver on Ubuntu, run:
$ sudo apt-get install nginx
Once Nginx installed, test whether it is working or not by browsing the nginx test page in the browser.
Open your web browser and point it to http://IP_Address or http://localhost. You should see a page like below.
Good! Nginx webserver is up and working!!
2. Create web directory for each host
I am going to create two Server blocks, namely ostechnix1.lan and ostechnix2.lan.
Let us create a directory for first server block ostechnix1.lan. This directory is required for storing the data of our server blocks.
To do so, enter:
$ sudo mkdir -p /var/www/html/ostechnix1.lan/public_html
Likewise, create a directory for second server block ostechnix2.lan as shown below.
$ sudo mkdir -p /var/www/html/ostechnix2.lan/public_html
The above two directories are owned by root user. We need to change the ownership to the regular user.
To do so, run:
$ sudo chown -R $USER:$USER /var/www/html/ostechnix1.lan/public_html
$ sudo chown -R $USER:$USER /var/www/html/ostechnix2.lan/public_html
Here, $USER refers the currently logged-in user.
Next, set read permissions to the Nginx root directory i.e /var/www/html/ using command:
$ sudo chmod -R 755 /var/www/html/
We do this because we already created a separate directory for each server block for storing their data. So we made the Nginx root directory as read only for all users except the root user.
We have created required directories for storing data of each server block, setup proper permissions. Now, it is time to create some sample pages which will be served from each server block.
3. Create demo web pages for each host
Let us create a sample page for ostechnix1.lan site. To do so, run:
$ sudo vi /var/www/html/ostechnix1.lan/public_html/index.html
Add the following lines in it:
<html> <head> <title>www.ostechnix.lan</title> </head> <body> <h1>Hello, This is a test page for ostechnix1.lan website</h1> </body> </html>
Save and close the file.
Likewise, create a sample page for ostechnix2.lan site:
$ sudo vi /var/www/html/ostechnix2.lan/public_html/index.html
Add the following lines in it:
<html> <head> <title>www.ostechnix.lan</title> </head> <body> <h1>Hello, This is a test page for ostechnix2.lan website</h1> </body> </html>
Save and close the file.
4. Create configuration file for each host
Next, we need to create configuration files for each server block. First, let us do this for ostechnix1.lan site.
Copy the default server block config file’s contents to the new server block files like below.
$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/ostechnix1.lan.conf
$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/ostechnix2.lan.conf
Next, edit ostechnix.lan1.conf file:
$ sudo vi /etc/nginx/sites-available/ostechnix1.lan.conf
Make the necessary changes in the server_name and root directives to match with the first domain name. All changes are shown in bold letters below.
# Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html/ostechnix1.lan/public_html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name ostechnix1.lan www.ostechnix1.lan; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; }
Save and close the file.
Next, edit ostechnix2.lan.conf file:
$ sudo vi /etc/nginx/sites-available/ostechnix2.lan.conf
Make the necessary changes in the server_name and root directives to reflect to the second domain name. All changes are shown on bold letters.
# Default server configuration # server { listen 80; listen [::]:80; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html/ostechnix2.lan/public_html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name ostechnix2.lan www.ostechnix2.lan; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; }
As you may noticed in the above configuration, I have made one more change. I removed the default_server line in the listen 80 ; and listen [::]:80 ; directives in the second server block(ostechnix2.lan) file. Because, we already used default_server line in first server block, hence we removed it from the second file to avoid conflict.
Save/close the file.
5. Enable Nginx server blocks
After making the necessary changes, disable the default server block config file, and enable all newly created server block config files as shown below.
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo ln -s /etc/nginx/sites-available/ostechnix1.lan.conf /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/ostechnix2.lan.conf /etc/nginx/sites-enabled/
Restart Nginx service to take effect the changes.
$ sudo systemctl restart nginx
That’s it. We have successfully configured server blocks in Nginx. Let us go ahead and check whether they are working or not.
6. Test Nginx Server blocks
Open /etc/hosts file in any editor:
$ sudo vi /etc/hosts
Add all your virtual websites/domains one by one like below.
[...] 192.168.225.24 ostechnix1.lan 192.168.225.24 ostechnix2.lan [...]
Please note that if you want to access the server blocks from any remote systems, you must add the above lines in each remote system’s /etc/hosts file. Save and close the file.
Open up your web browser and point it to http://ostechnix1.lan or http://ostechnix2.lan.
ostechnix1.lan test page:
ostechnix2.lan test page:
Congratulations! You can now be able to access your all websites/domains. From now on, you can upload the data and serve them from different websites.
And, that’s all for now. Hope this was useful. More good stuffs to come. Stay tuned!
Cheers!
source : OSTECHNIX