Nginx is an open source web server loved for its performance and low resource utilization. It is created to be highly versatile, scalable, and powerful reverse proxy server with load balancing and caching capabilities. Nginx is definitely a popular choice if you are looking to host any web application with high amount of traffic.

Here are some key use cases of Nginx:

  • As a Web Server: Nginx will perform the role of serving your website content (html, css, images) to users accessing from web browsers.
  • As caching server: Nginx can also be used to cache static website content to reduce loading time.
  • As reverse proxy: When used as a reverse proxy server, it will forward clients request to a backend service such as PHP, Python, Node.js
  • Using Nginx as a load balancer: In this context, nginx will do incoming traffic distribution to multiple backend services

If your goal is to have a web server with low memory footprint, free to use, offer high performance, can scale horizontally, customizable, and offers a modular architecture, then Nginx is the right solution for you. In this article we look at the process used in the installation, configuration, and general usage of Nginx web server on Ubuntu 24.04 Linux machine.

1. Setup Pre-requisites

Before we proceed, you will need the following prerequisites.

  • An Ubuntu 24.04 – Can be Server or Desktop edition
  • SSH access or direct GUI with terminal
  • Login user account with sudo privileges, or else the root user.
  • Internet access from your Ubuntu system.
  • Optional: Domain used to host your website, this is required for apps not accessible through an IP address

Login to your Ubuntu Linux system and make sure the packages index is updated.

sudo apt update

If the server is new and doesn’t contain applications running in production, you can as well perform packages upgrade.

sudo apt upgrade -y

2. Install Nginx Web Server Package

Next we look at the available options of installing Nginx on Ubuntu 24.04.

Option 1) Install from OS default repository

Ubuntu APT sources contain the packages on Nginx web application.

$ sudo apt list -a nginx
Listing... Done
nginx/noble 1.24.0-2ubuntu7 amd64

From the output we can see the version number. To install Nginx package execute the commands below in the terminal.

sudo apt install nginx

Agree to the prompt to install it.

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  nginx-common
Suggested packages:
  fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 552 kB of archives.
After this operation, 1,596 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

The installer will create a systemd service unit for the management of Nginx web server. To check service status run:

systemctl status nginx.service

2) Install from Nginx official APT repository

If your preference is using the developer upstream release, then using Nginx APT repository will be ideal. Install dependencies.

sudo apt install gnupg2 curl ca-certificates lsb-release debian-archive-keyring

Import Nginx repository signing key before adding the repository. This will ensure packages integrity and authenticity on the system.

curl -fsSL https://nginx.org/keys/nginx_signing.key|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/nginx.gpg

Next, create the repository file in your system. At the time of writing this article, noble repository is not available, we will use jammy.

echo "deb http://nginx.org/packages/ubuntu jammy nginx"| sudo tee /etc/apt/sources.list.d/nginx.list

Update your package lists after adding the repo.

$ sudo apt update
Hit:1 http://ke.archive.ubuntu.com/ubuntu noble InRelease
Hit:2 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:3 http://ke.archive.ubuntu.com/ubuntu noble-updates InRelease
Get:4 http://nginx.org/packages/ubuntu jammy InRelease [3,587 B]
Get:5 http://ke.archive.ubuntu.com/ubuntu noble-backports InRelease [90.8 kB]
Get:6 http://nginx.org/packages/ubuntu jammy/nginx amd64 Packages [14.4 kB]
Fetched 109 kB in 2s (65.3 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

Check package in the repo and its version.

$ sudo apt list -a nginx
Listing... Done
nginx/noble,now 1.24.0-2ubuntu7 amd64 [installed]
nginx/stable 1.24.0-1~jammy amd64
nginx/stable 1.22.1-1~jammy amd64
nginx/stable 1.22.0-1~jammy amd64
nginx/stable 1.20.2-1~jammy amd64

Install the package by using the following commands.

sudo apt install nginx

3. Hosting PHP application using Nginx

There is a basic welcome page in the web root of Nginx web server. Access the page on your browser through server’s IP address.

install nginx web server ubuntu 01

For users with an active UFW firewall, enable the service on port 80 and 443 for https.

sudo ufw allow http
sudo ufw allow https

PHP is a server-side scripting language that’s used by developers in creating the dynamic website content. Nginx does not execute PHP backend code and it will need FastCGI process manager (such as PHP-FPM) to process the request. The splitting of the roles between nginx and php-fpm will result in faster loading responsive user experience.

Install PHP and PHP-FPM

To host PHP application and serve using Nginx, install PHP an PHP-FPM process handler.

sudo apt install php  php-fpm

PHP-FPM will manage a pool of PHP worker processes used in executing PHP scripts efficiently. It’s a bridge between Nginx and PHP code.

Disable apache service that’s installed automatically with php as dependency.

sudo systemctl disable --now apache2

If relational database such as MySQL/MariaDB is used, also install the mysql PHP extension.

sudo apt install php-mysql

If you’re not using Virtual Hosting, open the default configuration file and adjust code snippet under the server section.

$ sudo vim /etc/nginx/sites-available/default
       location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php-fpm.sock;
       }

Attached screenshot is for reference:

install nginx web server ubuntu 02 1

Restart nginx.

sudo systemctl restart nginx

Host test PHP script on the server to test this works.

echo '<?php phpinfo(); ?>' |sudo tee /var/www/html/test.php

To verify it is working, access the test page on your browser – http://ServerIP/test.php

install nginx web server ubuntu 03

4. Configuring Virtual Hosting in Nginx

You can take advantage of the virtual hosting technique if you want to host multiple websites on a single Nginx web server. Each domain is identified by a different domain name, but using a single IP address configured on the server. With this you will better resource utilization and cheap hosting.

We can create a sample virtual host.

sudo vim /etc/nginx/sites-available/sample-virtual.conf

Edit and paste the following contents.

server {
    listen       80;
    server_name  myvhost.com;

    location / {
        root   /var/www/myvhost;
        index  index.html index.htm;
    }
}

Let’s create missing directory set as root

sudo mkdir /var/www/myvhost

Create a basic html index page.

sudo tee -a /var/www/myvhost/index.html<<EOF
<html>
<body>
Hello World from Nginx Virtual Host
</body>
</html>
EOF

Enable the site.

sudo  ln -s /etc/nginx/sites-available/sample-virtual.conf /etc/nginx/sites-enabled/
sudo systemctl reload nginx

If the domain used myvhost.com has an A record in DNS, or local mapping under /etc/hosts, then you can access the site.

# On your local workstation
$ sudo vim /etc/hosts
192.168.1.201 myvhost.com 

Access the site using set domain name.

install nginx web server ubuntu 04

Nginx is indeed a performance oriented web server created to be highly customizable, secure, and stable. It can be used to host web applications that handles thousands of transactions per second. Nginx being an open-source has made it a default choice for individuals and businesses alike. In this article we’ve been able to install Nginx on Ubuntu 24.04, host a PHP application, and additionally configure Virtual Hosting. We hope this article was informative.

LEAVE A REPLY

Please enter your comment!
Please enter your name here