Configuring NGINX and Installing SSL with Certbot.
When it comes to configuring Nginx on your server and installing an SSL certificate with Certbot, this article provides a perfect guide.
Prerequisites:
Before you begin, ensure that you have the following:
- A Server Running: Access to a server(e.g., Ubuntu).
- Application Running on a Specific Port: Your application should be running on a specific port (e.g., port 3000).
- Open HTTP and HTTPS Ports: Ensure that ports 80 (HTTP) and 443 (HTTPS) are open in your server’s firewall.
- Domain Name: A registered domain name that points to your server’s IP address.
Step 1:- Installation
First, install NGINX using the following command:
sudo apt install nginxStpe 2:- Delete the Default NGINX Configuration and Set Up Your Own.
- To delete the default configuration, run the following command:
sudo rm /etc/nginx/nginx.conf- Next, create a new configuration file and edit it:
sudo vi /etc/nginx/nginx.confAfter running the command above, an editing terminal will open. Press i to enter insert mode, then paste the following configuration.
Note: Replace <your_domain_name> with your actual domain name (e.g., example.com) (if you don’t have any domain then write server_name _; for localhost).
Ensure that the localhost port matches your server's port if not matches then edit the port in this configuration.
events {
# Event directives...
}
http {
server {
listen 80;
server_name <your_domain_name>;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}After making these changes, press Esc to exit insert mode, then type :wq (write and quit) and press Enter.
Step 3:- Reload NGINX to Apply the New Configuration
To apply the new configuration, run:
sudo nginx -s reloadCongratulations! Your domain is now configured.
step 4:- Install Certbot
Now, to secure your domain, you need to install Certbot, which provides free SSL certificates. Run:
sudo apt install certbot python3-certbot-nginxstep 5:- Obtain Your Certificate
Replace <your_domain_name> with your actual domain name. When prompted, provide your email address, and Certbot will generate your certificate in a few seconds:
sudo certbot --nginx -d <your_domain_name>🔥 Your domain is now secure!
Additional Commands
To check if your certificate is close to expiring, run:
sudo certbot renew --dry-runTo renew your Certbot certificate, execute:
sudo certbot renewThank you for reading!