1. Home
  2. Linux
  3. How to install nginx on ubuntu server

How to install Nginx on Ubuntu Server

Nginx is an HTTP web server for Linux and other server platforms. It is considered the most complete alternative to the Apache web server. This guide will show you how to set up Nginx in Ubuntu Server.

What to do before installing Nginx on Ubuntu

Before installing Nginx on Ubuntu Server, it is important that all packages are updated, and all security updates are installed. While it is possible to use the Nginx web server on Ubuntu without updating, it’s not a great idea, as you could be vulnerable to attacks.

Thankfully, updating Ubuntu, even on the server, is incredibly easy. To start, run the apt update command. This command will refresh your software sources and check for new updates if they are available.

sudo apt update

Upon running the apt update command, it is time to run the apt upgrade command. This command will take all software updates and install them to your Ubuntu system. The update process can take time, especially if you have many updates to install, so be patient.

sudo apt upgrade

Upon installing all of the upgrades, you’ll need to reboot Ubuntu. Why? Sometimes, when Ubuntu is updated, a new kernel is installed. A new kernel cannot be used until a system restart occurs.

To quickly reboot Ubuntu, run the sudo reboot command.

sudo reboot

Once you log back in, Ubuntu will be up to date and ready for Nginx!

How to install Nginx on Ubuntu

The Nginx web server is pretty easy to get going on Ubuntu because it is in the main software repositories. To install Nginx, run the following apt install command and set up the “nginx” package.

sudo apt install nginx

Ubuntu will ask you for a password when you write the command above. Using the keyboard, enter your user account password. If your account doesn’t have sudo access, you can log into root and install the package instead.

Note: you must have the Ubuntu root account unlocked to log in with su.

su -
apt install nginx

Upon entering the apt install command and entering your user account password, Ubuntu will collect all dependencies required to set up Nginx. It will then ask you to press the Y key to confirm you wish to install the software. Press Y to confirm.

When you press the Y key on the keyboard, the Ubuntu package manager will set up Nginx and get it working on your Ubuntu server system.

How to allow Nginx through the Ubuntu firewall

If you use the UFW firewall on the Ubuntu server, you will need to allow Nginx through it to get full use. If you do not do this, Nginx may not work properly. To allow UFW through the firewall, do the following.

Use the ufw app list command. Note that you will need to access the root account to run the ufw app list command on your Ubuntu server. If you cannot access the root account with su run the sudo -s command.

su -
ufw app list

When you run the ufw app list command, the Ubuntu firewall will show you “available applications.” These “available applications,” should be “Nginx Full,” “Nginx HTTP,” “Nginx HTTPS,” etc.

To enable port traffic with Nginx only through port 443 (HTTPS), you can use the ufw allow “Nginx HTTPS” command. This command is great for those that don’t use HTTP, and run everything through HTTPS.

ufw allow 'Nginx HTTPS'

If, however, you do rely on HTTP (port 80,) you can enable that port through the Ubuntu firewall using the ufw allow command, but this time replacing ‘Nginx HTTPS’ with ‘Nginx HTTP’.

ufw allow 'Nginx HTTP'

If you rely on both HTTP and HTTPS, there is a third profile in the Ubuntu firewall available to enable. This profile is called “Nginx Full.” Enabling this will allow traffic on port 80 (HTTP,) as well as port 443 (HTTPS). Use the ufw allow ‘Nginx Full’ command to enable the profile.

ufw allow 'Nginx Full'

How to shut off the ufw firewall

While it is a good idea to keep the Ubuntu firewall in place and allow Nginx through it, it isn’t a requirement. If you aren’t exposing your Nginx web server to the greater internet, and it is just for LAN, you may not need it. Here’s how to shut off the firewall.

To disable the Ubuntu firewall, you can simply run the ufw disable. This command will turn off the firewall, but not uninstall it.

ufw disable

Alternatively, if you want to rid the firewall entirely, you can run the apt remove uninstall command. This command will remove the firewall entirely.

apt remove ufw