Tips to Configure and Run Nginx on Ubuntu

Tips to Configure and Run Nginx on Ubuntu

Hello Geeky, so today we are focusing on How to Configure and Run Nginx on Ubuntu. So please read this tutorial carefully so you may comprehend it in a better helpful way.

Guide: How to Configure and Run Nginx on Ubuntu

Nginx is one of the most used web servers on the planet. It has turned into a backbone for perhaps the biggest sites on the planet, including Netflix, WordPress.org, and that’s all tip of ice. In fact, we even used Nginx to run our site.

Nginx has proven itself to be one of the fastest and lightest web servers. This discrepancy is limited in part due to their inconsistent incident that is in the process of being addressed with the care of petitioners. This differs from Apache’s short-term behavior which uses phrases to address questions.

Just like browsing as a web server, you can also sign up as an alternate proxy, load balancer, intermediate mail, or even an HTTP store. In any case, we can talk about using Nginx as a web server.

System Requirements

For this tutorial, we will use the following:

  • Hybrid ServerMania or dedicated server
  • Ubuntu 20.04 operating system
  • 512MB RAM
  • 2GB Disk space

These queries change based on the expected traffic flow to your website. For upgrades, check ServerMania’s range of offerings. For Ubuntu 20.04 hosting on the ServerMania server, see Ubuntu Hosting on ServerMania.

Tunto Ubuntu 20.04

  • Log in using your root password you set while browsing up Ubuntu example using the following command:

root @ localhost: ~ # ssh root @ servermania_server_ip

  • Alternatively, you can use SSH keys to access without a password. As a good practice, do not use the root user for running administrative tasks.
  • Instead, create a new user and give useful admin benefits to that user. Use a strong password for the new user.

root @ localhost: ~ # adduser smuser

  • To give user benefits, you need to add this user to the sudo group. Use the following command:

root @ localhost: ~ # usermod -aG sudo smuser

  • Log in using a new user using the following command:

ssh smuser @ servermania_server_ip

Install Nginx (Ubuntu 20.04)

  • Every operating system always comes with a package manager. With Ubuntu 20.04, you can use apt or apt-get. As this is a new server, you first need to update the package manager.
  • Update package manager to a new compatible version:
  • You can use apt or apt-get package manager to install Nginx.

sudo apt update sudo apt-get update

  • Now, you are ready to install Ngnix. As you are logging in with a non-rooted user with sudo benefits, you will need to prioritize every command that requires admin privileges with sudo.
  • Install Ngnix web server using apt:

sudo apt install nginx sudo apt-get install nginx

  • Although the service starts automatically after installing Nginx, before testing whether Nginx is working properly or not, you will need to configure the OS firewall to access Nginx.

Configure the firewall for Nginx

  • The most common tool used to describe ingress and egress rules is iptables. Ubuntu 20.04 has a user-friendly interface on iptables, which is called uncomplicated firewall or ufw.
  • Although it comes with Ubuntu 20.04 but to make sure it is installed properly, install it manually. Follow the steps mentioned below to set up up firewall after installing Nginx:
  • Install ufw:

sudo apt install ufw

  • Check if ufw is working properly:

sudo ufw ipo

  • Allow traffic over HTTP and check the status again:

sudo ufw allows ‘Nginx HTTP’

  • Allow traffic on TCP (OpenSSH) even if you want to log in again:

sudo ufw download ‘OpenSSH’

  • If the status does not work when you previously checked, enable:

sudo ufw works

  • You will be prompted to decide whether you want to use the changes or not.
  • Be careful that if you remove the TCP report or add the OpenSSH command in the firewall, you will not be able to log in again.
  • Let us now discuss some important Nginx commands for control and administration.

Nginx Installation & Management

  • Check the Nginx server status:

sudo systemctl ipo nginx

  • You can also check if Nginx is working by accessing your website using a browser. Visit the following link:

http: // servermania_server_ip

  • Use systemctl to start, pause, or restart the Nginx server:

sudo systemctl da nginx sudo systemctl start nginx sudo systemctl restart nginx

  • The Nginx configuration file is located at /etc/nginx/nginx.conf. If you make changes to the configuration, you can restart the Nginx server or restart the configuration file.

systemctl also nginx # reconfigure Nginx

  • Whenever possible, use the replay command instead of the reboot command as it does not affect existing links to Nginx.

Configure Server Blocks

  • The server blocks provide performance similar to Apache server host hosts. Apache web server is the most popular server used in LAMP Summary. To install the LAMP stack on ServerMania servers, see How to Quickly Set up your own server.
  • Nginx server blocks let you use more than one site with your Nginx server. If you want to set up two domains, domainone.com, and domaintwo.com on the same Nginx server, you need to go through the following steps:
  • Create a directory, one for each domain: By default, Nginx creates a single server block that points to the / var / www / html directory. This will work well if you only have one site.
  • The html background framework contains all the content hosted on the website. Create html documents for both your domains.

sudo mkdir -p /var/www/domainone.com/html sudo mkdir -p /var/www/domaintwo.com/html

  • Change ownership process: Doing this will allow the user to create and edit the contents of these new processes.

sudo chown -R $ user: $ user /var/www/domainone.com/html sudo chown -R $ user: $ user /var/www/domaintwo.com/html

  • Adjust permissions for both local processes: This step is important to ensure that you do not need root opportunities to make changes to any of those fields.

sudo chmod -R 755 / var / www

  • Create an index.html landing page for both domains. Create a basic HTML snippet for an acceptable page for both sites.

nano /var/www/domainone.com/html/index.html

  • Once the editor opens, paste the following into the editor:

Welcome to ServerMania’s Domain One!

You have reached Nginx via Dina server # 1

body>

  • Save the index.html file and create a type for the second area. Make sure you change the welcome message.
  • Create server blocks: As previously mentioned, by default, Nginx has only one server block. You can find it here – / etc / nginx / sites-available / default. Right now, because we have two sites to cater to, we will need two server blocks. Server blocks allow traffic on port 80. Let us copy the default server blocks for our new server blocks.

sudo cp / etc / nginx / sites -is / default /etc/nginx/sites-available/domainone.com

  • Similarly, copy the default file and create a second server block too.
  • Mark One of the Two Servers as Default:
  • You can have only one default server with the default_server option enabled. Please remove one of the server blocks.
  • Activate Server Blocks & Restart Nginx:
  • You can activate server blocks by creating iconic links from configuration files in the server block to the directory running the sites.

sudo ln -s /etc/nginx/sites-available/domainone.com / etc / nginx / sites-enabled /

  • Once you create identical links for both domains and restart Nginx, it will start running queries for both domains from their respective blocks received on port 80.
  • To test the changes, you can try to visit both domains and see if the content inside index.html is correct. You are ready to install Nginx on Ubuntu 20.04!

Faq

Guide about How to Configure and Run Nginx on Ubuntu


In this guide, we told you about the How to Configure and Run Nginx on Ubuntu; please read all steps above so that you understand How to Configure and Run Nginx on Ubuntu in case if you need any assistance from us, then contact us.

How this tutorial or guide assisting you?


So in this guide, we discuss the How to Configure and Run Nginx on Ubuntu, which undoubtedly benefits you.



Final note

I hope you like the guide How to Configure and Run Nginx on Ubuntu. In case if you have any queries regards this article/tutorial you may ask us. Also, please share your love by sharing this article with your friends and family.

Trickbugs: Get the latest Trick and Tips
Logo
Enable registration in settings - general