Homework for students – How to install Nginx web server on Ubuntu?
As we mentioned in your previous tutorial, Apache2 is the most popular web server but not the only one in used today. Nginx web server is rapidly becoming the second most popular web server powering many of the websites online.
Some of the many reasons Nginx is rapidly catching up to Apache2 are the ways its manages resources, speed, simplicity and can be used as a reverse-proxy. It’s pretty good!
This brief tutorial is going to show you how to install Nginx web server on Ubuntu.
Installing Nginx on Ubuntu is simple. In fact, it only takes a single command line to accomplished. Configuring Nginx is where it becomes a bit difficult for new users and students.
The post below shows you how to install Apache2, and alternative to Nginx on Ubuntu
Step 1: Updating Ubuntu Server
One of the things to do before installing Nginx web server is to update Ubuntu. This can be done by running the commands below
sudo apt-get update
Step 2: Installing Nginx on Ubuntu Server
After updating your server, run the commands below to install Nginx
sudo apt-get -y install nginx
The above commands tell Ubuntu to fetch Nginx’s packages from its repositories and install.
Step 3: Managing Nginx Web Server
After you install Nginx server, it might be better to learn some of the basic commands used to manage it. The lines below show you how to stop, start, restart and reload Nginx
— Stops the server:
sudo systemctl stop nginx
— Start the server:
sudo systemctl start nginx
— Restarts the server:
sudo systemctl restart nginx
— Reload config changes no stops:
sudo systemctl reload nginx
Step 4: Configure Nginx Server
When Nginx is installed on Ubuntu servers, the default root directory is located at /var/www/html. Just as with Apache2 server on Ubuntu.
Any file you place in the root directory ending in .html will be served to web clients from that directory. This is the default location the server looks to server documents saved in .html/.htm
The majority of Nginx configurations files are stored in /etc/nginx directory. In this directory is where you’ll find the server configuration setting files, root directory controls and many of the different configuration settings files to control the server.
Two important locations you’ll spend most of your time are /etc/nginx/sites-available and /etc/nginx/sites-enabled
The sites-available directory contains all the available sites. This is the location you create new sites, and sites-enabled directory contains all sites that are enabled. By default, sites are not enabled until you run a commands to enable them.
The first default available site that’s also enabled is /etc/nginx/sites-available/default
This file contains the basic Nginx test settings configurations. You can keep the default site and continue to configure it or copy the default site into something new and work from there. Make sure to enable the new site though.
Step 5: Enabling and Disabling Nginx Sites
After creating Nginx sites on Ubuntu something to remember is to always enable the sites. With Apache2, we showed you how to do that. Sites can be enabled by running the a2ensite and disable by running the a2dissite commands.
Nginx does not have these available. So to enable a site named example.com on Ubuntu for Nginx, just create a symbolic link from the sites-available to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
To disable the example.com site, just delete it from the sites-enabled directory.
sudo rm /etc/nginx/sites-enabled/example.com
After enabling or disabling a site, restart Nginx server.
sudo systemctl restart nginx
To test if Nginx is installed and working, open your browser and browse to the server IP or hostname and you should see Nginx default page if it was installed properly.