Step By Step Procedure To Set Up A Testing Site In Nginx

Arunkl
TheSecMaster
Published in
8 min readApr 6, 2023
NGINX and ubuntu logo on a green background.
Source:thesecmaster.com

In an ideal production environment, nothing will go to production without being tested. Applications, services, migrations, upgrades, patches, hot fixed, policies, new products, most likely everything will have to undergo testing before rolling out. As cybersecurity professionals, we need to have a site to conduct testing on vulnerable cipher suites using OpenSSL. We have decided to set up a testing site on the Nginx web server on Ubuntu. Please be with us to see how to set up a testing site on Ubuntu using the Nginx server.

Table of Contents

· What Is Nginx?
· Why Do We Use Nginx For Testing?
· How To Set Up A Testing Site In Nginx On Ubuntu?
· Some important configuration and log file locations in Nginx:

What Is Nginx?

Nginx is an open-source application primarily used as a feature-proof full-stack web server designed for maximum performance and stability. Initially, it has started out as a web server. Later it is loaded with many more features. In addition to its HTTP server capabilities, it can function as a proxy server for email (IMAP, POP3, and SMTP), a reverse proxy, and a load balancer for web services. Now, this open-source application is being used for web serving, reverse proxying, SSL/TLS intercepting, web accelerating, caching, load balancing, media streaming, and more. Visit this site to know everything about Nginx.

Why Do We Use Nginx For Testing?

There are many web server applications out there. But, we always prefer to use Nginx in all our testing for its array of features.

  1. Nginx is one of the fastest web servers around. Its benchmarks are the highest among others.
  2. It’s more than a webserver. We can use it as an all-in-one multifunction tool. Web server, API gateway, reverse proxy, SSL/TLS interceptor, web accelerator, caching, load balancer, media streaming, and more.
  3. NGINX has been at the forefront of development that fulfills the modern web requirements.

How To Set Up A Testing Site In Nginx On Ubuntu?

Setting up a testing site on Nginx is not that difficult as you think. You may need to set up Nginx on your Ubuntu, Configure some basic firewall settings to allow the service on the UFW firewall. That’s it. We have added some optional steps like configuring server blocks and adding host file entry which will make your test setup more flexible.

Time needed: 15 minutes.

Set Up a Testing Site in Nginx on Ubuntu:

  1. Update Software Repositories on Ubuntu

Updating the repositories is the best practice to start with any deployments on Linux. This helps to keep the system with the latest build, updates, and patches.

$ sudo apt update && sudo apt upgrade

Update Software Repositories on Ubuntu

2. Install Nginx on Ubuntu

Nginx is included in the default repositories from 20.04 and later versions. Use this command to install Nginx on Ubuntu.

$ sudo apt-get install nginx

Install Nginx on Ubuntu

3. Verify the Installation of Nginx on Ubuntu

You can verify the installation of Nginx just by using the version command.

$ nginx -v

Verify the Installation of Nginx on Ubuntu

4. Start Nginx service

Make sure the service of the Nginx is active and running. See the commands to start, stop, check the status of the Nginx service here below.

To check the Status:
$ sudo systemctl status nginx

To start Nginx:
$ sudo systemctl start nginx

To stop Nginx:
$ sudo systemctl stop nginx

Start Nginx service

5. Enable the Nginx at the boot time

The start and stop command shown in the previous step work for once. You may need to start the service at each reboot. You can set the Nginx service to either start or stop at the boot time. Run these commands to enable or disable the service at the boot time.

To enable Nginx at boot:
$ sudo systemctl enable nginx

To disable Nginx at boot:
$ sudo systemctl disable nginx

To reload the Nginx service (used to apply configuration changes):
$ sudo systemctl reload nginx

To hard restart of Nginx:
$ sudo systemctl restart nginx

Enable the Nginx at the boot time

6. Display the available Nginx profiles

Nginx installs few profiles for the UFW firewall to allow the Nginx traffic to pass through the firewall.

To display the available Nginx profiles:
$ sudo ufw app list

Display the available Nginx profilesDisplay the available Nginx profiles

7. Allow Nginx Traffic on UFW (Uncomplicated Firewall)

Nginx installs few profiles for the UFW firewall to allow the Nginx traffic to pass through the firewall.

To display the available Nginx profiles:
$ sudo ufw app list

To allow the Nginx traffic through the UFW firewall:
$ sudo ufw allow ‘nginx http’

To allow the encrypted Nginx traffic through the UFW firewall:
$ sudo ufw allow ‘nginx https’

To allow both HTTP and HTTPS:
$ sudo ufw allow ‘nginx full’

To reload the firewall rules:
$ sudo ufw reload

Allow Nginx Traffic on UFW (Uncomplicated Firewall)

8. Verify the Nginx service is running

To verify the Nginx service, open the web browser and type this URL http://127.0.0.1. You will see the Nginx page if it is running on your machine.

If you are in the CLI terminal use curl utility to load the page on CLI. Commands to install the curl utility and load the page on CLI is here.
$ sudo apt-get install curl
$ curl –i 127.0.0.1

The default Nginx html page is located in /var/www/html/index.nginx-debian.html. You can design this page by editing or replacing the html code of index.nginx-debian.html file.

Till now all the steps written are mandatory to set up a testing site on Nginx. The steps covered from here are optional. However, we urge you to complete the following steps too because we have covered configuring server blocks and host file editing which are required to host multiple sites on a single Nginx server.

Verify the Nginx service is running

9. Configure a Server Block on Nginx

Most of the times you may need to host multiple sites/domains on a single web server. It reduces time, hardware, and power costs. Most of the modern web servers accomplish this via virtual hosts. In Nginx those virtual machines are function as server blocks. Nginx has one default server block preconfigured. We are not going to tweak the default server block. We will create a new one for example site.

10. Create a directory for test site

Create a directory for your site under /var/www/.

$ sudo mkdir -p /var/www/exampledomain.com/html

Create a directory for test site

11. Set the permission and ownership

Run these commands to set the permission and ownership of exampledomain.com directory.

$ sudo chown $USER:$USER /var/www/exampledomain.com
$ sudo chmod 755 /var/www/exampledomain.com

Set the permission and ownership

12. Create an index.html file for the test site

Use any text editor to create index file. We have used nano editor in the demonstration. You can design this as per your need.

$ sudo nano /var/www/exampledomain.com/html/index.html

Press CTRL+o to save the file and Press CTRL+x to exit the file in nano.

Create an index.html file for the test site

13. Create the server block configuration file

Create a configuration file for your server block.

$ sudo nano /etc/nginx/sites-available/exampledomain.com

Create the server block configuration file

14. Code of Nginx server block configuration file

Write the below code inside the server block configuration file.

server {
listen 80;

root /var/www/exampledomain.com/html;
index index.html index.htm index.nginx.debian.html;

server_name exampledomain.com www.exampledomain.com;
location / {
try_files $uri $uri/ =404;
}
}

Press CTRL+o to save the file and Press CTRL+x to exit the file in nano.

Code of Nginx server block configuration file

15. Create symbolic link of the configuration file

Create symbolic link of the configuration file in startup directory.

To create symbolic link:
$ sudo ln -s /etc/nginx/sites-available/exampledomain.com /etc/nginx/sites-enabled

Create symbolic link of the configuration file

16. Restart the Nginx Service

Run this command to restart the Nginx service.

$ sudo systemctl restart nginx

Restart the Nginx Service

17. Test the server block configuration in Nginx

Issue this command to test the configurations.

$ sudo nginx –t

Test the server block configuration in Nginx

18. Add the host file entry

This step is again optional. However, we recommend to add host entry to map the ip address with the testing domain. This will allow you to use the domain name directly in the browser.

Use this command to check the IP address of your system.
$ hostname –i

Edit the file /etc/hosts in nano editor.
$ sudo nano /etc/hosts

Add the below line right below the localhost entry.
127.0.1.1 exampledomain.com www. exampledomain .com

Press CTRL+o to save the file and Press CTRL+x to exit the file in nano.

Add the host file entry

19. Restart Nginx service

Command to restart Nginx service.
$ sudo systemctl restart nginx

Restart Nginx service

20. Brows the test domain in a web browser

http://exampledomain.com

You should see the browser loading the index page that you created in step 12.

Brows the test domain in a web browser

This is how you can Set Up a Testing Site in Nginx on Ubuntu platform.

Some important configuration and log file locations in Nginx:

  • The Main website content: /var/www/html
  • The Main Nginx application files: /etc/nginx
  • The main Nginx configuration file: /etc/nginx/nginx.conf
  • Access logs which has every request to the server: /var/log/nginx/access.log
  • Error logs of Nginx: /var/log/ngins/error.log
  • List of all websites hosted on Nginx: /etc/nginx/sites-available
  • List of websites actively being served by Nginx: /etc/nginx/sites-enabled

It’s our pleasure to share such practical tutorials with you. Thanks for reading this post. Please let us know if you want to know more about this. We recommend to read the below post to know in detail. Please share this post if you find this interested. Visit our social media page on Facebook, LinkedIn, Twitter, Telegram, Tumblr, & Medium and subscribe to receive updates like this.

This post is originally published at thesecmaster.com

We thank everybody who has been supporting our work and requests you check out thesecmaster.com for more such articles.

--

--