In this tutorial, we will show you how to install Nginx on your Ubuntu 18.04 LTS. For those of you who didn’t know, Nginx pronounced “engine x” is an open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It can be used as a standalone web server, load balancer, content cache, and reverse proxy for HTTP and non-HTTP servers.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Nginx on a Ubuntu 18.04 (Bionic Beaver) server.
Prerequisites
- A server running one of the following operating systems: Ubuntu 18.04 (Bionic Beaver).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Nginx on Ubuntu 18.04 LTS Bionic Beaver
Step 1. First, make sure that all your system packages are up-to-date by running the following apt-get
commands in the terminal.
sudo apt-get update sudo apt-get upgrade
Step 2. Installing Nginx.
Installing Nginx package on Ubuntu 18.04 Bionic Beaver) is as easy as running:
apt-get install nginx
After that, run the commands to enable Nginx to automatically startup when your server starts:
sudo systemctl stop nginx.service sudo systemctl start nginx.service sudo systemctl enable nginx.service
To test the Nginx setup, open your browser and browse to the server hostname or IP address and you should see the Nginx default test page as shown below:
Step 3. Configure Nginx.
Nginx site-specific configuration files are kept in /etc/nginx/conf.d/
. Generally, you will want a separate file in this directory for each domain or subdomain you will be hosting.
- Copy the default configuration file. Replace idroot.us with your website’s domain name or your public IP address.
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/idroot.us.conf
- Open your site’s configuration file in a nano text editor. Replace example.com in the server_name directive with your site’s domain name or IP address. If you already have content ready to serve (such as a WordPress installation) replace the path in the root directive with the path to your site’s content:
server { listen 80; server_name idroot.us; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
Test your configuration for errors:
nginx -t
Step 4. Configure the Firewall for Nginx.
Firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with UFW upon installation, making it straightforward to allow Nginx access:
sudo ufw allow 'Nginx HTTP'
Congratulations! You have successfully installed Nginx. Thanks for using this tutorial for installing the Nginx web server in Ubuntu 18.04 LTS (Bionic Beaver) system. For additional help or useful information, we recommend you check the official Nginx website.