
Nginx is an open-source web server that facilitates the delivery of static and dynamic web applications or services. It serves as a web server, load balancer, reverse proxy, or HTTP cache, seamlessly integrating with existing applications to form a stack or deliver web applications through your server's IP address or domain.
This article explains how to install Nginx on Ubuntu 22.04 and set up sample web applications to run efficiently on your server.
Before you begin:
Have an Ubuntu 22.04 server.
Create a domain name A record pointing to the server IP address.
Access the server using SSH as a non-root user with sudo privileges.
The latest version of Nginx is available in the default APT repositories for Ubuntu 22.04. Follow the steps below to update your server packages and install the Nginx web server.
Update the server package index.
Install Nginx.
View the installed Nginx version on your server.
Your output should be similar to the one below:
Nginx utilizes the nginx systemd service profile to manage the web server's runtime and processes on your server. Follow the steps below to enable the Nginx system service and control the web server processes on your system.
Enable the Nginx web server to start automatically at boot time.
Output:
Start the Nginx service.
Stop the Nginx service.
Restart the Nginx service.
Verify the Nginx service status.
Output:
Nginx virtual hosts include specific configurations that allow the web server to serve web application files from a designated directory using a specified domain. Follow the steps below to create a sample Nginx virtual host configuration for securely serving web application files on your server.
Create a new Nginx virtual host configuration in the /etc/nginx/sites-available directory. For example, app.example.com.conf.
Add the following configurations to the file.
Save and close the file.
The above Nginx virtual host configuration listens for incoming connections using your app.example.com domain and serves your web application files from the /var/www/app.example.com web root directory on your server.
Test the Nginx configuration for errors.
Link the configuration to the /etc/nginx/sites-enabled directory to activate the virtual host on your server.
Create a new web root directory /var/www/app.example.com to store your web application files.
Create a new HTML application file within the directory. For example, index.html.
Add the following HTML contents to the file.
Save and close the file.
The above HTML application displays a Greetings from Vultr heading when accessed in a web browser.
Restart Nginx to apply the new virtual host configurations on your server.
Access your domain to ensure that Nginx is correctly delivering the web application files from your virtual host configuration. You can use the curl utility to test access to your domain. For example:
Output:
SSL certificates enable secure communication between a user's browser and the Nginx web server using HTTPS. By default, Nginx listens for incoming connections on the insecure HTTP port 80. Follow the steps below to generate trusted Let's Encrypt SSL certificates and configure Nginx to securely handle HTTPS connection requests.
Install the Certbot Let's Encrypt client package using Snap.
View the installed Certbot version on your server.
Output:
Allow network connections on the HTTP port 80 for Certbot ACME verification.
Generate a new SSL certificate for your domain. Replace app.example.com with the actual domain in your Nginx virtual host configurations.
Uncomplicated Firewall (UFW) is enabled by default on Ubuntu 22.04. Follow the steps below to configure the firewall with rules that allow Nginx to listen for incoming HTTP and HTTPS connections on your server.
Allow connections on the HTTPS port 443.
View the UFW table and verify that the new connection rules are active.
Output:
In this article, you installed Nginx on your Ubuntu 22.04 server and configured it to serve web applications. Nginx supports multiple virtual hosts for secure application delivery and can be integrated with other applications like MySQL and PHP to enable dynamic web services.
0 Comments
Be the first to comment and share your perspective with the community.