Table of Contents
  • Home
  • /
  • Blog
  • /
  • Step By Step Procedure To Set Up A Testing Site In Nginx
December 14, 2023
|
8m

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


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

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.

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 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 web server. 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 modern web requirements.

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

Setting up a testing site on Nginx is not as 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.

Set Up a Testing Site in Nginx on Ubuntu:

Step 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

Step 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

Step 3: Verify the Installation of Nginx on Ubuntu

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

$ nginx -v

Step 4: Start the 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

Step 5: Enable the Nginx at the boot time

The start and stop commands 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

Step 6: Display the available Nginx profiles

Nginx installs a 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

Step 7: Allow Nginx Traffic on UFW (Uncomplicated Firewall)

Nginx installs a 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

Step 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 the curl utility to load the page on CLI. Commands to install the curl utility and load the page on CLI are 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 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.

Step 9: Configure a Server Block on Nginx

Most of the time 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 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.

  1. Create a directory for test site

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

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

Step 10: 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

Step 11: Create an index.html file for the test site

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

$ 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.

Step 12: Create the server block configuration file

Create a configuration file for your server block.

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


Step 13: 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.


Step 14: Create a symbolic link to the configuration file

Create a symbolic link to the configuration file in the startup directory.
To create symbolic link:
$ sudo ln -s etc/nginx/sites-available/exampledomain.com etc/nginx/sites-enabled


Step 15: Restart the Nginx Service

Run this command to restart the Nginx service.

$ sudo systemctl restart nginx

Step 16: Test the server block configuration in Nginx

Issue this command to test the configurations.

$ sudo nginx –t

Step 17: Add the host file entry

This step is again optional. However, we recommend adding a 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 the 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.

Step 18: Restart Nginx service


Command to restart Nginx service.
$ sudo systemctl restart nginx

Step 19: Brose 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.

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 don’t forget to share this with others.

Arun KL

Arun KL is a cybersecurity professional with 15+ years of experience in IT infrastructure, cloud security, vulnerability management, Penetration Testing, security operations, and incident response. He is adept at designing and implementing robust security solutions to safeguard systems and data. Arun holds multiple industry certifications including CCNA, CCNA Security, RHCE, CEH, and AWS Security.

Recently added

Application Security

View All

Learn More About Cyber Security Security & Technology

“Knowledge Arsenal: Empowering Your Security Journey through Continuous Learning”

Cybersecurity All-in-One For Dummies - 1st Edition

"Cybersecurity All-in-One For Dummies" offers a comprehensive guide to securing personal and business digital assets from cyber threats, with actionable insights from industry experts.

Tools

Featured

View All

Learn Something New with Free Email subscription

Subscribe

Subscribe