Being able to install and configure a web server is a valuable skill to have both as a web developer and software engineer. A web server is a program responsible for delivering your web content to clients over the internet or a network.
Let’s take a look at how to install and configure the Nginx web server on Ubuntu.
Why Use Nginx?
Nginx is a lightweight and high-performance web server that can run on both Windows and Linux. Apart from being a web server, Nginx is also extensively used as a reverse proxy server and mail proxy server. Note that Nginx is pronounced as “engine x.”
As a reverse proxy server, you configure Nginx to route traffic from a specified port to some application running on the server. The Nginx reverse proxy provides an extra layer of security as it checks and filters the traffic before hitting the server.
Nginx runs as a service or daemon on a server and can be easily managed by service managers such as systemctl on Linux. Last but not least, Nginx is well documented and enjoys enormous community support.
How to Install Nginx Server on Ubuntu
For the purpose of this guide, we’ll demonstrate how you can install the Nginx server on a Linux machine running Ubuntu.
Step 1: Installing Security Prerequisites
If you are installing Nginx on a secure server then it is important that you install these prerequisites to ensure that your installation is secure and you are getting stable versions of the programs.
First, update your system’s package list.
Install gnupg2 for a secure communication channel when downloading the application.
Next, you should get the official signing key from Nginx to verify the authenticity of the package.
Now, verify that the imported file contains the proper key by running:
The output should contain the fingerprint as shown below.
Register and set up the Nginx repository on your system to get stable packages, which is important for production and secure environments.
Step 2: Installing Nginx
The next step is to install the Nginx server. Remember to update your package sources before downloading new packages.
Installing the Nginx server is pretty straightforward. Simply run the following command:
Learn More: The Difference Between APT and dpkg on Ubuntu
Nginx in Action
The default page served by Nginx is stored in the directory /usr/share/nginx/html. You can place your static web pages either in this location or another location of your choice and configure a virtual host.
A virtual host allows you to serve multiple web pages on the same server hardware or infrastructure.
You can check the status of the Nginx server using the following command:
The output here shows that the Nginx daemon is active and running. If it is not active, start it by issuing the following command:
If you make some configuration changes to Nginx, you can restart the server using the systemctl command as follows:
To serve the default Nginx webpage, open the URL http://localhost:80 in a web browser. You will be greeted by a page similar to the one below.
Learn How to Develop Web Apps
Congratulations, you’ve successfully installed and configured Nginx. This guide has looked at how to install and configure a web server on Ubuntu. In addition, we discussed how you can manage the Nginx server daemon using systemctl.
The demand for web developers has never been this huge, and it shows no signs of slowing down anytime soon. Start your web development journey by learning the basics of HTML today.