How To Install NGINX On Ubuntu 22
How To Install NGINX On Ubuntu 22
04
In 3 Quick Steps
Installing NGINX on Ubuntu 22.04 is often a core requirement for SaaS applications and
internal projects that need a reverse proxy server.
NGINX is an open-source lightweight server that performs equally well as a web server and a
reverse proxy. It also manages email protocols (IMAP, POP3, and SMTP) and inbox
operations.
This versatile server package powers almost 35% of websites today. This gives you an idea of
NGINX’s flexibility and customizability to your projects.
In this article, we’ll discuss how to install NGINX on Ubuntu in simple steps that you can
easily follow on your Ubuntu 22.04 server.
But before that, we’ll briefly introduce NGINX so that you can understand the benefits
NGINX brings to your projects.
Contents
A Short Introduction to NGINX.......................................................................................................1
Prerequisites to Setting Up NGINX on Ubuntu 22.04....................................................................2
Install NGINX on Ubuntu 22.04.......................................................................................................2
Manage the NGINX Process.............................................................................................................5
Setting Up Server Blocks...................................................................................................................6
Step # 1: Create the Required Directories.......................................................................................6
Step # 2: Assign Proper Permissions................................................................................................6
Step # 3: Create Configuration Files................................................................................................7
Step # 4: Configure the Server Block...............................................................................................7
Important NGINX Files and Directories.........................................................................................8
NGINX has an event-driven architecture that can handle multiple requests in a single thread.
In contrast, Apache (its closest competitor) creates a thread for each request. As a result of
this multiple requests – single thread design, NGINX maintains a sustained response
performance as the volume of user requests increases. In some evaluations, NGINX
outperforms Apache by a factor of 2.5 with a similar resource consumption profile.
Now that you know that NGINX is a great choice for your projects let’s see the prerequisites
for installing NGINX on Ubuntu.
The good news is that NGINX is included in the default Ubuntu package index. So, you only
need to update the APT package index to get the latest NGINX package from Ubuntu’s
default repositories.
Start the process by updating the APT package index with the following command:
#sudo apt update
When the command finishes, we have the latest NGINX package on the server. Next, we’ll
install the NGINX server with the following command:
Now that you have installed NGINX on the Ubuntu server, the next step is configuring the
server firewall to allow traffic to the NGINX process.
Since NGINX is a popular server package, installation usually registers the NGINX service
with the system’s firewall.
However, you should always verify that the NGINX service is indeed registered with the
firewall and that it allows incoming traffic to the NGINX service.
Next, get the service status with the following command that highlights the NGINX service
status.
Ubuntu’s default behavior for starting services like NGINX during installation and server
boot is relatively consistent across versions. However, in most Ubuntu installations, the
NGINX web server doesn’t start automatically at the end of the installation process.
So, you need to start the NGINX service manually with the following command:
To check the status of a service managed by the systemd init system (including NGINX), use
the following command:
http://your_server_ip
If you can see something similar to the following, NGINX is properly installed on your
Ubuntu server.
You’ll notice that many of these commands use the systemctl command, which interacts with
the systemd init system.
Check the status of the NGINX service with the following command:
Use the following command to restart the NGINX process. Note that during restart, the
command reloads all configurations without dropping connections.
Use the following command to start the NGINX process when the server starts or reboots:
Use the following command to disable the NGINX service on server boot:
Note that whenever you change NGINX server config files, you should always reload rather
than restart the NGINX service. This refreshes the server configuration by preserving existing
connections without disrupting the user experience.
For example, the following commands create separate directories for two websites,
example.com and another-site.com:
cd /etc/nginx/sites-available
In this directory, create a new configuration file for each site you want to host. We
recommend using the site’s domain name as the filename.
For example:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
ssl_certificate "/usr/local/share/ca-certificates/example.crt";
ssl_certificate_key "/usr/local/share/ca-certificates/private.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
Use the following command to create a symbolic link to the sites-enabled directory to enable
the server block:
By default, NGINX comes with a default configuration file. If you don’t need it, we highly
recommend disabling it to avoid any conflicts among the config files:
Before using the new configuration file, it is critical that you test the NGINX configuration
file for syntax errors:
sudo nginx -t
Repeat steps 3 to 7 for each additional website you want to host on the same server.
Remember to update the DNS settings for each domain to point to your server’s IP address.
Once the server blocks are set up, and DNS is properly configured, NGINX will route
incoming requests to the appropriate server block based on the domain name in the HTTP
Host header.
Here are the key NGINX files and directories you should be aware of:
NGINX modules are installed and managed from this directory. Each module typically has its
configuration file in this directory.
NGINX stores the log files in this directory. It contains access and error logs, essential for
troubleshooting and monitoring the web server.
Conclusion
NGINX is a powerful and widely-used web server widely popular for its performance,
scalability, and versatility. Developers typically use it as a reverse proxy, load balancer, and
HTTP cache, in addition to its role as a web server. Its event-driven, asynchronous
architecture allows it to handle many concurrent connections efficiently, making it suitable
for high-traffic websites and applications.
FAQs
Q. What is NGINX?
NGINX is a popular and widely-used web server software that can also be used as a reverse proxy,
load balancer, and HTTP cache. It is known for its high performance and scalability.
NGINX runs on various operating systems, including Linux, Windows, macOS, and Unix variants. It
is most commonly used on Linux distributions for web hosting.
A: To use NGINX as a web server, you will need to configure it to serve your web content. This
involves creating configuration files and making necessary changes to them. You can find detailed
tutorials on how to configure NGINX as a web server on the official NGINX website.
To install NGINX on Ubuntu 22.04, you can follow the step-by-step guide above.
Yes, you can install NGINX on Ubuntu 20.04 using the same installation process as mentioned for
Ubuntu 22.04.
To manage NGINX on Ubuntu 22.04, you can use various commands like starting, stopping, and
restarting the service. For example, to start the NGINX service, you can run: sudo service nginx start
NGINX is versatile and can be used as a reverse proxy, load balancer, HTTP cache, and more. It is
often employed as a front-end proxy to distribute client requests across multiple back-end servers.
A server block, or a virtual host, is a configuration block in NGINX that defines settings for a specific
website or application. You can set up multiple server blocks to host numerous sites on a single
server, each with its domain or IP address.
To allow NGINX in the firewall, you need to configure the firewall rules. You can use the command:
sudo ufw allow ‘Nginx HTTP’