Nginx is a high-performance web server and reverse proxy. It is also used as a load balancer, HTTP cache, and mail proxy. Compared to Apache, Nginx is known for its high performance and low memory usage which makes it ideal for high traffic websites.
This tutorial will guide you through the process of installing and configuring Nginx on an Ubuntu 18.04 server. By the end of this guide, you will have a running Nginx server that you can use to host your websites.
Before we start, it’s important to note that you should have a non-root user account with sudo privileges set up on your server. Also, ensure that your server is updated with the latest security patches and updates.
Step 1: Update Your System
The first step in installing any package on your Ubuntu system is to ensure that the system’s package manager, apt, is up to date. Run the following command to update the package list:
sudo apt update
This command will fetch the latest version of all packages currently installed on your system from the repositories.
Step 2: Install Nginx
With your system up to date, you can proceed to install Nginx. Nginx is included in the Ubuntu 18.04 default repositories, so the installation is straightforward. Run the following command:
sudo apt install nginx
During the installation, Ubuntu will ask you for your password. Enter it to proceed with the installation.
Step 3: Adjust the Firewall
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw, Ubuntu’s firewall, upon installation. This makes it rather easy to allow Nginx access.
You can list the applications configurations that ufw knows how to work with by typing:
sudo ufw app list
You should see a list of all the application profiles, including Nginx. To allow incoming HTTP traffic, you can use the following command:
sudo ufw allow 'Nginx HTTP'
You can verify the change by typing:
sudo ufw status
You should see HTTP traffic allowed in the displayed output.
Step 4: Check your Web Server
At the end of the installation process, Ubuntu 18.04 starts Nginx. The web server should already be up and running.
You can check with the systemd init system to make sure the service is running by typing:
systemctl status nginx
If everything was done correctly, you should see an active (running) status.
Step 5: Manage the Nginx Process
Now that you have your web server up and running, let’s go over some basic management commands.
To stop your web server, you can type:
sudo systemctl stop nginx
To start the web server when it is stopped, type:
sudo systemctl start nginx
To stop and then start the service again, type:
sudo systemctl restart nginx
If you are simply making configuration changes, Nginx can often reload without dropping connections. To do this, type:
sudo systemctl reload nginx
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:
sudo systemctl disable nginx
To re-enable the service to start up at boot, youcan type:
sudo systemctl enable nginx
Step 6: Set Up Server Blocks (Virtual Hosts)
If you plan on hosting multiple domains on your server, you can set up Nginx server blocks, similar to Apache’s virtual hosts. This allows you to host more than one website on a single machine.
First, create the directory for your domain:
sudo mkdir -p /var/www/your_domain/html
Next, assign ownership of the directory with the $USER environment variable:
sudo chown -R $USER:$USER /var/www/your_domain/html
The permissions of your web roots should be correct if you haven’t modified your umask value, but you can make sure by typing:
sudo chmod -R 755 /var/www/your_domain
Next, create a sample index.html page using nano or your favorite text editor:
nano /var/www/your_domain/html/index.html
Inside, add the following sample HTML:
<html> <head> <title>Welcome to Your_domain!</title> </head> <body> <h1>Success! The your_domain server block is working!</h1> </body> </html>
Save and close the file when you are finished.
Step 7: Create Server Block Files for Each Domain
Next, you will need to create a new server block configuration file for each domain you are hosting. Start by creating a new file in the sites-available directory:
sudo nano /etc/nginx/sites-available/your_domain
Inside, paste the following configuration:
server { listen 80; listen [::]:80; root /var/www/your_domain/html; index index.html index.htm index.nginx-debian.html; server_name your_domain www.your_domain; location / { try_files $uri $uri/ =404; } }
Save and close the file when you are finished. Then, enable the file by creating a link from it to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Finally, test for syntax errors:
sudo nginx -t
If no problems were found, restart Nginx to enable your changes:
sudo systemctl restart nginx
Step 8: Test your Results
Now that you have your server blocks set up, you can test your setup by navigating to the domains in your web browser:
http://your_domain
You should see your web page displaying the text “Success! The your_domain server block is working!”. If you see this page, then your server block is working correctly.
Conclusion
In this tutorial, we have walked through the process of installing and configuring Nginx on an Ubuntu 18.04 server. We have covered updating your system, installing Nginx, adjusting the firewall, checking your web server, managing the Nginx process, setting up server blocks (virtual hosts), creating server block files for each domain, and testing your results.
By following these steps, you should now have a fully functioning Nginx server on your Ubuntu 18.04 system. This setup is a good starting point for hosting your websites and exploring more advanced Nginx configurations. Remember, Nginx is a powerful and flexible web server capable of handling many simultaneous connections, and as such, it is ideal for serving static content, acting as a reverse proxy, and for load balancing.
If you’re interested in exploring other web server options, our guide on the best web servers can provide you with a comparison of the most popular servers available today.
Remember, the choice of web server largely depends on the requirements of your website and your technical expertise. Whether you’re looking for a dedicated server, a VPS server, cloud hosting, or shared hosting, we’ve got you covered.
Happy hosting!
FAQs
-
What is Nginx?
Nginx is a high-performance HTTP server and reverse proxy. It is also used as a load balancer, HTTP cache, and mail proxy. Compared to Apache, Nginx is known for its high performance and low memory usage.
-
Why use Nginx over Apache?
Nginx is known for its high performance and low memory usage, which makes it ideal for high traffic websites. It also has a more straightforward configuration than Apache.
-
What is a server block in Nginx?
A server block in Nginx is equivalent to a virtual host in Apache. It allows you to host more than one website on a single machine.
-
How do I start and stop Nginx?
You can start Nginx with the command ‘sudo systemctl start nginx’ and stop it with ‘sudo systemctl stop nginx’. To restart Nginx, use ‘sudo systemctl restart nginx’.
-
How do I test my Nginx configuration?
You can test your Nginx configuration with the command ‘sudo nginx -t’. This will check for syntax errors and report any issues.
Commands Mentioned
- sudo apt update – Updates the package list for the system.
- sudo apt install nginx – Installs Nginx on the system.
- sudo ufw app list – Lists the application profiles known to ufw.
- sudo ufw allow ‘Nginx HTTP’ – Allows incoming HTTP traffic on Nginx.
- sudo ufw status – Checks the status of ufw.
- systemctl status nginx – Checks the status of the Nginx service.
- sudo systemctl stop nginx – Stops the Nginx service.
- sudo systemctl start nginx – Starts the Nginx service.
- sudo systemctl restart nginx – Restarts the Nginx service.
- sudo systemctl reload nginx – Reloads the Nginx service without dropping connections.
- sudo systemctl disable nginx – Disables the Nginx service from starting at boot.
- sudo systemctl enable nginx – Enables the Nginx service to start at boot.
- sudo mkdir -p /var/www/your_domain/html – Creates the directory for your domain.
- sudo chown -R $USER:$USER /var/www/your_domain/html – Assigns ownership of the directory.
- sudo chmod -R 755 /var/www/your_domain – Sets the correct permissions for your web roots.
- sudo nano /etc/nginx/sites-available/your_domain – Opens a new server block configuration file for editing.
- sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/ – Creates a link from the sites-available directory to the sites-enabled directory.
- sudo nginx -t – Tests for syntax errors in the Nginx configuration files.
- sudo systemctl restart nginx – Restarts the Nginx service to enable changes.