Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Setting up Let's Encrypt with NGINX on Ubuntu
Alex Bouma
Alex Bouma

Posted on • Originally published atalex.bouma.blog on

     

Setting up Let's Encrypt with NGINX on Ubuntu

This guide is more a reference to myself how to setup a fresh auto-renewing certificatie on a Ubuntu (any Linux distro supported bycertbot should work though) box.

_Note: All commands should run as the root user so switch to the root user before starting (by runningsudo su orsu - depending on your setup)._

0. Setup dependencies

We needgit to downloadcertbot from GitHub.

# Update the package repositories to install the latestapt-get update# Install gitapt-getinstallgit
Enter fullscreen modeExit fullscreen mode

1. Setup Let's Encrypt' certbot

# Switch to a working directorycd /opt# Clone the certbot repository into the certbot foldergit clone https://github.com/certbot/certbot# Create a directory to hold our configuration file(s)mkdir /etc/certbot# Create a directory to hold certbot validation filesmkdir-p /var/www/letsencrypt
Enter fullscreen modeExit fullscreen mode

3. Setup a certbot configuration file

# Start a new file in the configuration directory we just creatednano /etc/certbot/domain.com.conf
Enter fullscreen modeExit fullscreen mode

Add the following contents to this file:

# Use the webroot authenticator.authenticator= webroot# Use the following path for the webroot authenticator to usewebroot-path= /var/www/letsencrypt# Generate certificates for the specified domains, add multiple domains by seperating them with a commadomains= domain.com, www.domain.com# Register certs with the following email addressemail= your@email.com# Use a 4096 bit RSA key instead of 2048rsa-key-size= 4096
Enter fullscreen modeExit fullscreen mode

Replace the domain and email value with your own ofcourse :)

4. Edit your domains NGINX config

Open up your nginx vhost configuration file and add the following location block:

server{listen80;server_namedomain.com;location^~/.well-known{allowall;alias/var/www/letsencrypt/.well-known;}# ... snip ... #}
Enter fullscreen modeExit fullscreen mode

Add the location block in the http server block, or if you already have valid certificate you can also place it in your https server block.

Apply the changes by restarting NGINX:service nginx restart (ofcourse after you checked if your configuration is valid by runningservice nginx configtest).

5. Request the certificate

Execute the following command and follow the onscreen instructions to have the certificate being issued.

/opt/certbot/letsencrypt-auto certonly-c /etc/certbot/domain.com.conf
Enter fullscreen modeExit fullscreen mode

6. Configure NGINX to use the certificate

Change the vhost configuration to something like the following:

# Redirect to HTTPSserver{    listen 80;    server_name domain.com    location ^~ /.well-known{        allow all;alias /var/www/letsencrypt/.well-known;}return301 https://domain.com$request_uri;}# HTTPS server blockserver{    listen 443 ssl;    server_name domain.com;    ssl on;    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;    location ^~ /.well-known{        allow all;alias /var/www/letsencrypt/.well-known;}# ... snip ... #}
Enter fullscreen modeExit fullscreen mode

Apply the changes by restarting NGINX:service nginx restart (ofcourse after you checked if your configuration is valid by runningservice nginx configtest).

7. Check if everything is working

Visit your domain to see if the browser adds the green padlock and run your site throughSSL Labs to validate all is correct and secure.

Note:I am focussing on the minimal changes to make the certificate work and enable HTTPS. However there are a lot of settings and considerations to make it actually secure and recieve the A+ rating onSSL Labs. For more info consult:https://raymii.org/strong-ssl-security-on-nginx

8. Auto renew the certificate

For this we are going to use a cron script that runs each month and updates our certificate and restarts NGINX.

# Create a new monthly cron filenano /etc/cron.monthly/renew-ssl-certificates
Enter fullscreen modeExit fullscreen mode

Add the following contents to that file:

#!/bin/bash/opt/certbot/letsencrypt-auto certonly-c /etc/certbot/domain.com.conf--renew-by-defaultservice nginx restart
Enter fullscreen modeExit fullscreen mode

Don't forget to change/etc/certbot/domain.com.conf to whatever your own config file is called.

The last step is to make the renew cron executable, for that run:

chmod +x /etc/cron.monthly/renew-ssl-certificates
Enter fullscreen modeExit fullscreen mode

9. Profit!

You now have afree Let's Encrypt certificate up and running that auto renews without you having to lift a finger :) Great!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
dineshrathee12 profile image
Dinesh Rathee
Dinesh Rathee , Young IT Professional 💻 Working in Dublin (Ireland) 💚Enthusiast, Innovative, Loves Travelling #Fan of @elonmusk 🚀🛰, Proud Son👨‍🎓with Big Dreams.📝Opinions are my own 🏁
  • Location
    Dublin (Ireland)
  • Education
    Masters (Systems Engineering) - Birla Institute of Technology and Science, Pilani
  • Work
    Cloud Support Engineer I (Linux) at Amazon Web Services
  • Joined

LetsEncrypt have revoked around 3 million certs last night due to a bug that they found. Are you impacted by this, Check out ?

DevTo
[+]dev.to/dineshrathee12/letsencrypt-...

GitHub
[+]github.com/dineshrathee12/Let-s-En...

LetsEncryptCommunity
[+]community.letsencrypt.org/t/letsen...

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

PHP artisan, full stack dev and server wrangler | Building http://chief.app
  • Location
    The Netherlands
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp