Install and Configure Nginx Web Server on Ubuntu 22.04

This tutorial provides a comprehensive guide to installing and configuring the Nginx web server on your Ubuntu 22.04 server. Learn how to set up a basic web server, configure virtual hosts, and implement SSL/TLS encryption for secure website hosting.

ubuntu nginx webserver ssl
Micro
VPS
1
vCPU
2 GB
Memory
20 GB
NVMe Disk
1024 GB
Traffic
4.90
/month
* Up to 6GB RAM, 60GB NVMe Disk Space and 1Gbit/s Network Speed
This tutorial guides you through the process of installing and configuring the Nginx web server on your Ubuntu 22.04 server. We'll cover setting up a basic web server, configuring virtual hosts, and enabling HTTPS for secure website hosting. ### Prerequisites * An Ubuntu 22.04 server with sudo non-root user access * A registered domain name (optional, but recommended for virtual hosts and HTTPS) ### Step 1: Update Your System Before starting, ensure your server is up-to-date: ```bash sudo apt update sudo apt upgrade -y ``` ### Step 2: Install Nginx Install Nginx using the package manager: ```bash sudo apt install nginx -y ``` ### Step 3: Verify Nginx Installation Once installed, verify that Nginx is running by accessing your server's IP address in a web browser. You should see the default Nginx welcome page. To find your server's IP address, use the following command: ```bash ip addr show eth0 | grep inet | awk '{ print $2; }' | cut -d/ -f1 ``` ### Step 4: Manage the Nginx Service Nginx runs as a service that can be managed with the following commands: * **Start Nginx:** `sudo systemctl start nginx` * **Stop Nginx:** `sudo systemctl stop nginx` * **Restart Nginx:** `sudo systemctl restart nginx` * **Reload Nginx:** `sudo systemctl reload nginx` (reloads configuration without restarting) * **Check Nginx Status:** `sudo systemctl status nginx` ### Step 5: Configure Virtual Hosts (Optional) Virtual hosts allow you to host multiple websites on a single server. Each website has its own configuration file. 1. **Create a directory for your website's files:** ```bash sudo mkdir -p /var/www/example.com/html ``` 2. **Create a virtual host configuration file:** ```bash sudo nano /etc/nginx/sites-available/example.com.conf ``` 3. **Paste the following configuration into the file (replace `example.com` with your domain):** ```nginx server { listen 80; listen [::]:80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } } ``` 4. **Enable the virtual host:** ```bash sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/ ``` 5. **Test your configuration and reload Nginx:** ```bash sudo nginx -t sudo systemctl reload nginx ``` ### Step 6: Enable HTTPS with Let's Encrypt (Optional) Secure your website with HTTPS using Let's Encrypt certificates. 1. **Install Certbot:** ```bash sudo apt install certbot python3-certbot-nginx -y ``` 2. **Obtain and install certificates:** ```bash sudo certbot --nginx -d example.com -d www.example.com ``` Follow the prompts to configure your certificates. Certbot will automatically update your Nginx configuration. 3. **Set up automatic renewals:** Certbot can automatically renew certificates. Add the following to your crontab: ```bash sudo crontab -e ``` Add the line below, then save and exit: ``` 0 0,12 * * * root certbot renew ``` ### Step 7: Further Configuration This tutorial covers basic Nginx installation and configuration. You can customize Nginx further by: * **Adding more virtual hosts** for additional websites. * **Configuring reverse proxies** to route traffic to other applications. * **Implementing security measures** such as rate limiting and IP whitelisting. * **Setting up caching** to improve website performance. ### Conclusion You have successfully installed and configured Nginx on your Ubuntu 22.04 server! Remember to tailor the configurations to your specific needs and refer to the official Nginx documentation for advanced customization options.


Created with ❤ at Estonia
EcoStack Technology OÜ