Table of Contents
- Introduction
- What is NGINX?
- Why Choose NGINX for Your Server?
- Prerequisites
- Requirements Before Installing NGINX
- Checking System Compatibility
- Step 1: Update Your System
- Why System Updates Are Crucial
- How to Update Your System
- Step 2: Install NGINX
- Installing NGINX Using APT
- Verifying the Installation
- Step 3: Adjusting Firewall Settings
- Configuring Firewall for NGINX
- Allowing NGINX Full Access
- Step 4: Managing NGINX Service
- Starting and Stopping NGINX
- Enabling NGINX to Start at Boot
- Step 5: Setting Up NGINX Server Blocks
- Understanding Server Blocks
- Creating Your First Server Block
- Step 6: Testing NGINX Configuration
- Testing Your Configuration for Errors
- Reloading NGINX to Apply Changes
- Step 7: Securing NGINX with SSL
- Importance of SSL Certificates
- Setting Up SSL with Let’s Encrypt
- Conclusion
- Summary of What You’ve Learned
- Next Steps in Your NGINX Journey
1. Introduction
NGINX is a powerful web server that also functions as a reverse proxy, load balancer, and HTTP cache. It’s known for its high performance and ability to handle large numbers of concurrent connections. Whether you’re running a simple static website or a complex application, NGINX is an excellent choice.
In this guide, you’ll learn how to install and set up NGINX on an Ubuntu server, tailored for both beginners and advanced users.
2. Prerequisites
Before we dive into the installation process, ensure your server meets the following requirements:
- Ubuntu Server: Make sure you have an Ubuntu server (18.04, 20.04, or 22.04).
- Root Access: You’ll need sudo or root access to execute commands.
- Internet Connection: Required for downloading and installing packages.
3. Step 1: Update Your System
First, update your package lists to ensure you have the latest versions available.
sudo apt update
sudo apt upgrade
4. Step 2: Install NGINX
Installing NGINX is straightforward with APT, Ubuntu’s package manager.
sudo apt install nginx
Once the installation is complete, you can check the status of NGINX:
sudo systemctl status nginx
5. Step 3: Adjusting Firewall Settings
To allow web traffic to reach your server, you need to adjust your firewall settings. If you have UFW (Uncomplicated Firewall) enabled, use the following commands:
sudo ufw allow ‘Nginx HTTP’
For HTTPS traffic:
sudo ufw allow ‘Nginx HTTPS’
6. Step 4: Managing NGINX Service
To start NGINX:
sudo systemctl start nginx
To stop NGINX:
sudo systemctl stop nginx
To enable NGINX to start on boot:
sudo systemctl enable nginx
7. Step 5: Setting Up NGINX Server Blocks
Server Blocks allow you to host multiple domains on a single server. Let’s set up a basic server block.
Create a directory for your website:
sudo mkdir -p /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
Create a new server block configuration file:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following content:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable the server block by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
8. Step 6: Testing NGINX Configuration
Before reloading NGINX, test your configuration for syntax errors:
sudo nginx -t
If the test is successful, reload NGINX:
sudo systemctl reload nginx
9. Step 7: Securing NGINX with SSL
Secure your site with SSL using Let’s Encrypt.
Ensure Snap is Installed:
Snap is usually pre-installed on Ubuntu. To ensure Snap is installed, run:
sudo apt update
sudo apt install snapd
Snap is usually pre-installed on Ubuntu. To ensure Snap is installed, run:
sudo snap install –classic certbot
Create a Symlink for the Certbot Command:
To ensure the certbot command works correctly, you may need to create a symlink:
sudo ln -s /snap/bin/certbot /usr/bin/certbot
After installation, verify Certbot is installed correctly by checking its version:
certbot –version
You can now use Certbot to obtain or renew SSL certificates. For example, to use Certbot with Nginx:
sudo certbot –nginx -d yourdomain.com
10. Conclusion
Congratulations! You’ve successfully installed and configured NGINX on your Ubuntu server. With NGINX up and running, you’re now ready to explore more advanced configurations and optimizations.