Guide to Install and Configure Nginx on Ubuntu 22.04
Step 1: Install Nginx
Update your package index and install Nginx:
sudo apt update
sudo apt install nginxStep 2: Start and enable the Nginx service:
sudo systemctl start nginx
sudo systemctl enable nginxStep 3: Check Nginx Status
Check the status of the Nginx service:
sudo systemctl status nginx
sudo systemctl stop nginxYou should see output indicating that Nginx is active and running.
Step 4: Configure Nginx
Create a new configuration file for your website:
sudo vim /etc/nginx/sites-available/your_domainAdd the following configuration:
server {
listen 80;
server_name your_domain_or_IP;
location / {
root /var/www/html;
index index.html;
}
}Enable the configuration:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxStep 5: Adjust Firewall Settings
If you have UFW enabled, allow HTTP traffic:
sudo ufw allow 'Nginx HTTP'You can now access your website by entering your server's IP address or domain name in a web browser.
Step 6: Allow Nginx HTTPS Traffic
If you plan to use SSL/TLS certificates for your website, you will need to allow HTTPS traffic through the firewall:
sudo ufw allow 'Nginx Full'You can now proceed to set up SSL certificates for your Nginx server.
Step 7: Set Up SSL Certificates
Using Let's Encrypt
Install Certbot:
sudo apt install certbot python3-certbot-nginxObtain an SSL certificate:
sudo certbot --nginx -d your_domain -d www.your_domainFollow the prompts to complete the installation. Certbot will automatically configure SSL for your Nginx configuration.
Renewing SSL Certificates
Certbot automatically renews certificates. To test the renewal process, you can run:
sudo certbot renew --dry-runCongratulations! You have successfully installed and configured Nginx on your Ubuntu 22.04 server. You can now serve web content securely using HTTPS.