How to Install NGINX on Ubuntu 22.04

NGINX, which is known for its great performance, scalability, and flexibility, has grown as a popular alternative for powering websites and applications. Its capacity to manage heavy traffic loads, serve static material quickly, and function as a configurable reverse proxy established its place as a key component of modern online infrastructure.

Ubuntu a popular and user-friendly Linux distribution provides an ideal environment for deploying NGINX. In this post, we will cover the process of install NGINX on Ubuntu 22.04 from basic installation, firewall configuration, testing, and essential management tasks.

Prerequisites

Before starting NGINX install Ubuntu ensure you have the following:

  • An Ubuntu 22.04 server
  • SSH access to your server
  • A non-root user with sudo privileges

Installing NGINX on Ubuntu

Following are steps described below to install NGINX on Linux based Ubuntu 22.04 operating system:

Step 1: Update and Upgrade System

It is important to have an up-to-date system before installing any new software. Open your terminal and run the following commands:

sudo apt update && sudo apt upgrade -y
update upgrade

These commands will refresh the package lists and install any available updates.

Step 2: Install NGINX

NGINX is readily available in the Ubuntu repositories. Install it using the following command:

sudo apt install nginx
install nginx

This will install NGINX and its dependencies on your Ubuntu system.

Step 3: Configure Firewall (UFW)

If you have install UFW on Ubuntu system you will need to allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx Full'

This command opens the necessary ports for NGINX.

Step 4: Verify NGINX Installation

To check if NGINX is running and accessible, open a web browser and navigate to your server’s IP address. You should see the default NGINX welcome page.

nginx browser

Step 5: Manage NGINX Process

To start, stop, restart, or reload NGINX, use one of the following commands:

sudo systemctl start nginx

To check your NGINX status type the following command:

sudo systemctl status nginx
nginx status

Step 6: Setting Up Server Blocks

To host multiple websites on a single server you will need to create server blocks. These configurations define how NGINX should handle requests for different domains.

1. To create directories for your websites use the following command:

sudo mkdir -p /var/www/example1 /var/www/example2

Replace example1 and example2 with your desired domain names which is useful to manage multiple sites.

2. To assign ownership and permissions use the following command:

sudo chown -R $USER:$USER /var/www

3. Create basic HTML files in the respective directories to test your configuration.

4. You can also create files in the server block in the /etc/nginx/sites-available directory for each website. For example:

sudo nano /etc/nginx/sites-available/example1

Paste the following configuration replacing placeholders with your domain, document root, and other settings:

server {
    listen 80;
    server_name example1.com;

    root /var/www/example1;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

5. Create a symbolic link in Linux to the configuration file in the /etc/nginx/sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example1 /etc/nginx/sites-enabled/

6. To test and restart NGINX use the following command:

sudo nginx -t && sudo systemctl restart nginx

Important NGINX Files and Directories

Following are important NGINX files and directories described below:

  1. /etc/nginx/nginx.conf: Main NGINX configuration file
  2. /etc/nginx/sites-available: Directory for server block configuration files
  3. /etc/nginx/sites-enabled: Directory for enabled server block configuration files
  4. /var/log/nginx/access.log: Access log file
  5. /var/log/nginx/error.log: Error log file

NGINX Optimization

NGINX offers extensive configuration options to increase performance and security. Some common configurations include:

  • SSL/TLS certificates: For secure communication (HTTPS)
  • Load balancing: Distributing traffic across multiple servers
  • Caching: Improving performance by storing static content
  • Proxy server: Forwarding requests to other servers
  • Custom error pages: Providing informative error messages

Conclusion

By successfully installing and configuring NGINX on Ubuntu 22.04 you have a solid foundation for a high-performance and scalable web infrastructure. NGINX’s versatility as a web server reverse proxy and load balancer empowers you to optimize website delivery enhance security and improve overall user experience.

Ultimately, the best web server for you depends on your unique project requirements. Try our free VPS hosting that is reliable to you with scalability that adapts your resources to meet your growing needs. Explore Ultahost VPS plans and find the perfect fit.

FAQ

What is NGINX?
How do I install NGINX on Ubuntu 22.04?
Do I need to configure NGINX after installation?
How do I start NGINX on Ubuntu 22.04?
How do I check if NGINX is running?
Can I use NGINX as a reverse proxy on Ubuntu 22.04?
How do I uninstall NGINX from Ubuntu 22.04?

Related Post

How to Install Fail2ban on Ubuntu 22.04

Fail2Ban provides a protective shield for Ubuntu 22.04 ...

How to Install Magento on Ubuntu

Magento is a powerful open-source e-commerce platform t...

How to Install ClickHouse on Ubuntu

ClickHouse is a high-performance, open-source database ...

How to Install phpBB on Ubuntu 24.04

phpBB is a free and open-source forum software that is ...

How to Install Steam in Linux for Gaming

Linux gaming is exploding in popularity, and Steam is l...

How to List Linux Users on Ubuntu

Among the complexity of Linux systems user accounts ser...

Leave a Comment