Setting up Nginx on Ubuntu
Ubuntu Desktop/Server Versions: Installing Nginx
Nginx
nginx.org (engine x) is a versatile server, serving as a reverse and proxy server, mail server, as well as a generic TCP/UDP proxy server. It is both free and open-source, known for its high performance. Administrators often choose it for its resource efficiency and responsiveness under heavy loads. Its lightweight resource utilization, coupled with the capability to scale easily on minimal hardware and its asynchronous, events-driven architecture, contributes to its popularity among administrators, developers, and engineers.
This tutorial will be divided into three sections:
Section 1: Covering Nginx installation using apt-get (note that the version may be older than the latest available).
Section 2: Explaining the installation of Nginx with Pre-Built Packages.
Section 3: Covering the installation of the latest version of Nginx from source.
Requirements
What you'll need to keep up with:
Assuming you have a minimal Ubuntu 12.10 (Quantal Quetzal) installed on your Blend Hosting from blendhosting.com[2], this tutorial is based on Ubuntu 16.04.1 LTS (Xenial Xerus). If you don’t have it, you can purchase one starting from $5.59/month by following this link[3].
To connect to Blend Hosting, you’ll need any SSH Client. For Windows, you can use PuTTY (http://www.putty.org/[4]). For Unix systems, you can use the SSH Tool in your terminal.
Additionally, it is assumed that you possess the knowledge to execute basic bash commands in Linux/MacOSX.
Ubuntu Package Manager installation (apt-get)
Installing Nginx on Ubuntu is a straightforward process, especially when utilizing the built-in package manager tool, apt-get.
As a good practice before performing any apt-get actions, it is recommended to update the repository information. Let’s proceed with that step:
sudo apt-get update
After updating the repositories, the process boils down to executing this simple command:
sudo apt-get install nginx
This straightforward command will check the apt-get repository for the package named “nginx” and install it with the appropriate permissions.
Before proceeding with the installation, you will be prompted to confirm whether you want to continue or not.
Once the installation is complete, there’s only one task left: verify that the Nginx package was successfully installed.
nginx -v
If the process executed successfully, you should receive the feedback: nginx version: nginx/1.10.0 (Ubuntu), indicating that everything is in order.
To complete the setup, connect your web browser to the IP address you used to SSH into your VPS. You should encounter a welcome page, confirming that Nginx has been successfully installed and is operational.
Reminder
If you recall, the command nginx -v indicated that the installed version was nginx/1.10.0 (Ubuntu). It’s important to note that the version obtained through apt-get may not always be the latest.
I recommend cross-referencing with the version documentation on nginx.org to understand the specifics of the installed version. If you discover that a newer version exists with features you require, continue reading for instructions on installing the latest version.
Using pre-built packages for installation
Opting for the Mainline Pre-Built packages presents a significant advantage over the “standard” apt-get installation. This approach binds your apt-get nginx repository to the latest version package, continually refreshed by the nginx team.
You also have the option to bind to stable Pre-Built packages, a decision I’ll guide you through setting up.
The initial step involves adding the repository links to your `/etc/apt/sources.plist` file. For more detailed information, refer to the documentation here: http://nginx.org/en/linux_packages.html#mainline[5].
echo "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx" >> /etc/apt/sources.list
echo "deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" >> /etc/apt/sources.list
The following two commands append the specified string inside “” to your /etc/apt/sources.list.
The next crucial step is to download their signing key to authenticate the nginx repository.
wget http://nginx.org/keys/nginx_signing.key
The command will download the key into the current directory.
To proceed with the installation, it’s necessary to add the downloaded key to the apt program keyring:
sudo apt-key add nginx_signing.key
Upon successful execution, your command line should display “OK,” indicating that you are ready to proceed with the installation.
To kickstart the installation process, issue the following two commands (note that apt-get update is crucial as it reloads the repositories and fetches the new Nginx version from the repository):
sudo apt-get update
sudo apt-get install nginx
After completing the installation, verify if Nginx was successfully installed by running the following command:
nginx version: nginx/1.11.4
Congratulations! You are now ready to start using your Nginx installation, which will stay up-to-date with the new versions pushed on the Nginx mainline. Enjoy using Nginx for your web server needs!
Setting up from source
This method is recommended only for experienced users who desire total control over the installation process and the methods available in Nginx.
Requirements
- Build tools have to be installed:
sudo apt-get install build-essential - PCRE must be installed:
sudo apt-get install libpcre3 libpcre3-dev - zlib installed:
sudo apt-get install zlib1g-dev
Let’s kick off the process with the classic download phase:
wget http://nginx.org/download/nginx-1.11.4.tar.gz
Once the download is complete, we need to unpack it. This will extract the contents to the current directory, creating a folder named “nginx-1.11.4,” and navigate into it:
tar -zxvf nginx-1.11.4.tar.gz && cd nginx-1.11.4
The next step involves running the configure script, which examines the available methods Nginx can use and creates a Makefile upon completion. I’ll be using the standard execution, but for more detailed information on what you can control, refer to: http://nginx.org/en/docs/configure.html[6].
./configure && make && make install
The installation of Nginx will be carried out in: /usr/local/nginx
To ensure you can execute the `nginx` command from any location in your terminal, you need to add the PATH variable of the nginx binary to the bash PATH. The following one-liner takes care of this by adding the PATH to .bashrc, reloading it, checking the nginx version, and displaying which nginx binary is currently in use:
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> ~/.bashrc && source .bashrc && nginx -v && which nginx
I’m grateful that you looked over my guide; do get in touch with me if you have any corrections or additions!
[1]: http://nginx.org/
[2]: https://www.blendhosting.com/
[3]: https://www.blendhosting.com/vps/
[4]: http://www.putty.org/
[5]: http://nginx.org/en/linux_packages.html#mainline
[6]: http://nginx.org/en/docs/configure.html
