When you want to set up NGINX with Let's Encrypt, then you can do it automatically by using the application certbot.
To install certbot for nginx:
on Ubuntu/Debian:
sudo apt install python-certbot-nginx
on Arch linux:
sudo pacman -S certbot-nginx
on Centos:
sudo yum install epel-release
sudo yum install certbot-nginx
Then you need to make a very simple configuration file for your domain. The directory should be the same for all the mentioned operating systems
/etc/nginx/sites-available/example.com
In here you just add this information:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:5000 #Example
}
}
Then create the symlink to activate the domain
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Remember to change example.com with your domain, and switch proxy_pass to your service or directory of hosted files.
Now you should restart NGINX:
sudo nginx -t
This one will return an error if you have errors in your configuration.
If everything is ok then restart NGINX
sudo systemctl restart nginx.service
Now certbot comes into the picture:
sudo certbot --nginx -d example.com -d www.example.com
At this point Let's encrypt will try to reach your nginx server, and if everything is OK - this means:
- Firewall settings allow for port 80 and 443 to pass
- Portforwarding throug network for the 2 ports are allowed
Then you will get to pick easy or secure access. I recommend the secure option.
When you have clicked [enter] then the process will be finished and certbot will have generated all your certification files and added them to the correct path.
Your configuration file in /etc/nginx/sites-avalible/example.com will have been updated with all the correct settings.
You may be required to restart nginx once again.
I hope it was helpful. Good luck
[Sources]
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-centos-7
https://wiki.archlinux.org/index.php/Certbot#Nginx