How to Use Nginx on Linux Ubuntu

When deploying projects on a server, using Nginx as a proxy is a common method in Linux systems. If the project is deployed using Docker containers, it becomes even more convenient and is a common practice in modern development. Below is a summary of the basic operations.1. Set up the HTTP domainIn a production environment, it is recommended to create a separate nginx.conf configuration file for each project, making project management easier.

cd /etc/nginx/conf.d
nano your_domain.conf  # For example www.diemasoft.top.conf
# In the file, enter the following content
server {
    listen 80;  # Your server domain
    server_name your_domain;
    location / {
        # The port exposed by your Docker container
        proxy_pass http://localhost:5000;
    }
}
# Save   CRTL+O  enter  CRTL+X
# Before restarting Nginx, test if the Nginx configuration is correct
nginx -t    # Output syntax is ok and test is successful indicates the configuration format is correct
systemctl reload nginx  # Restart Nginx
# Verify in the browser if successful

2. Configure the HTTPS certificate

When costs are limited, you can use the free certificates provided by Let’s Encrypt and automate the application and configuration using the Certbot tool.

#1. Install Certbot and the Nginx plugin
sudo apt update
sudo apt install certbot python3-certbot-nginx -y

#2. Ensure Nginx configuration is correct (this step can refer to 1. Set up the HTTP domain)
# Nginx must have a configured HTTP server block for your domain,
# and the server_name directive must be set correctly.

#3. Run Certbot to obtain and install the certificate
sudo certbot --nginx -d www.yourdomain.com

#4. Follow the interactive prompts, enter your email address,
# for emergency security and renewal notifications.
# If everything goes smoothly, Certbot will output a congratulatory message, indicating that the certificate has been successfully issued and configured.

#5. Test automatic renewal
# Let's Encrypt certificates are only valid for 90 days,
# After installation, Certbot will automatically create a scheduled task for automatic renewal.
sudo certbot renew --dry-run  
# If this test passes, it indicates that your certificate can be automatically renewed without manual intervention.

Leave a Comment