How to Install Nginx with HTTP/2 Support on Ubuntu
Nginx is a powerful web server known for its performance and efficiency. Enabling HTTP/2 support can enhance website performance by allowing multiplexing of requests, reducing latency, and improving loading times. This guide will walk you through the steps to install Nginx with HTTP/2 support on Ubuntu 18.04/20.04.
1. Prerequisites
Before starting, ensure you have the following:
- A server running Ubuntu 18.04 or Ubuntu 20.04 .
- Sudo privileges to install packages.
2. Updating the Package Index
Start by updating the package index to ensure you have the latest information on available packages:
sudo apt update
3. Installing Nginx
Step 1: Install Nginx
To install Nginx, run the following command:
sudo apt install nginx
Step 2: Start Nginx
Once installed, start the Nginx service:
sudo systemctl start nginx
Step 3: Enable Nginx to Start on Boot
To ensure Nginx starts automatically at boot, run:
sudo systemctl enable nginx
4. Installing OpenSSL
To use HTTP/2, you need to have SSL/TLS enabled. You can install OpenSSL using:
sudo apt install openssl
5. Obtaining an SSL Certificate
You can obtain a free SSL certificate from Let’s Encrypt. First, install Certbot, which helps in obtaining and managing SSL certificates:
sudo apt install certbot python3-certbot-nginx
Step 1: Obtain the SSL Certificate
Run the following command to obtain a certificate for your domain:
sudo certbot --nginx
Follow the prompts to set up your SSL certificate. Certbot will automatically configure Nginx to use the certificate and enable HTTPS.
6. Configuring Nginx for HTTP/2
Once you have an SSL certificate, you can enable HTTP/2 in the Nginx configuration.
Step 1: Edit the Nginx Configuration File
Open the default Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Step 2: Update the Server Block
Find the server block for your HTTPS configuration (usually defined with listen 443 ssl;). Update it to include http2 like this:
server {
listen 443 ssl http2;
server_name your_domain.com; # Replace with your domain
ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem; # Adjust as necessary
ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem; # Adjust as necessary
location / {
# Your site configuration
}
}
Step 3: Save and Exit
Save the changes and exit the editor (CTRL + X, then Y, then Enter).
7. Testing Nginx Configuration
Before restarting Nginx, check for syntax errors in the configuration:
If everything is okay, you will see a confirmation message.
8. Restarting Nginx
To apply the changes, restart the Nginx service:
sudo systemctl restart nginx
9. Verifying HTTP/2 Support
To check if HTTP/2 is enabled, you can use an online tool like tools.keycdn.com/http2-test or use a browser with developer tools.
Step 1: Using Browser Developer Tools
- Open your website in a web browser.
- Right-click and select Inspect or press F12 to open Developer Tools.
- Go to the Network tab and reload the page.
- Click on the requests and check the Protocol column to see if HTTP/2 is being used.
10. Conclusion
Installing Nginx with HTTP/2 support on Ubuntu 18.04/20.04 is a great way to improve your website’s performance. By following this guide, you can enable HTTP/2, which enhances loading times and user experience. Regularly monitor your server and keep your software updated to maintain optimal performance and security.