{"id":1143,"date":"2016-03-14T19:20:30","date_gmt":"2016-03-14T18:20:30","guid":{"rendered":"http:\/\/monodes.com\/predaelli\/?p=1143"},"modified":"2016-03-14T09:36:20","modified_gmt":"2016-03-14T08:36:20","slug":"how-to-secure-nginx-with-lets-encrypt-on-ubuntu-14-04-digitalocean","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2016\/03\/14\/how-to-secure-nginx-with-lets-encrypt-on-ubuntu-14-04-digitalocean\/","title":{"rendered":"How To Secure Nginx with Let&#8217;s Encrypt on Ubuntu 14.04 | DigitalOcean"},"content":{"rendered":"<p>Following <a href=\"https:\/\/letsencrypt.readthedocs.org\/en\/latest\/using.html#installation\">https:\/\/letsencrypt.readthedocs.org\/en\/latest\/using.html#installation<br \/>\n<\/a><\/p>\n<p>you can obtain a nice SSL certificate for your own webservers; yet for those who likes NGinx like me this guide <em><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04\">How To Secure Nginx with Let&#8217;s Encrypt on Ubuntu 14.04 | DigitalOcean<\/a> <\/em>is also useful<\/p>\n<blockquote><p>In this tutorial, we will show you how to use Let&#8217;s Encrypt to obtain a free SSL certificate and use it with Nginx on Ubuntu 14.04. We will also show you how to automatically renew your SSL certificate. If you&#8217;re running a different web server, simply<\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>Since it&#8217;s based on US of America your can&#8217;t really trust them for &#8220;national security&#8221; issues or even when there&#8217;s business involved or you strive for real privacy. For that level of trustness the only way are self-signed certificates distributed with a one-to-one physical channel.<!--nextpage--><\/p>\n<blockquote>\n<div class=\"container\">\n<h1 class=\"content-title\">How To Secure Nginx with Let&#8217;s Encrypt on Ubuntu 14.04<\/h1>\n<p><span class=\"meta-section tags\"> <span class=\"meta-value\"><span class=\"tutorial-date\">Dec 17, 2015<\/span> <a class=\"\" href=\"https:\/\/www.digitalocean.com\/community\/tags\/nginx?type=tutorials\">Nginx<\/a>, <a class=\"\" href=\"https:\/\/www.digitalocean.com\/community\/tags\/security?type=tutorials\">Security<\/a>, <a class=\"\" href=\"https:\/\/www.digitalocean.com\/community\/tags\/let-s-encrypt?type=tutorials\">Let&#8217;s Encrypt<\/a> <a class=\"\" href=\"https:\/\/www.digitalocean.com\/community\/tags\/ubuntu?type=tutorials\">Ubuntu<\/a><\/span> <\/span><\/p>\n<\/div>\n<div class=\"container\"><\/div>\n<div class=\"content-body tutorial-content\" data-growable-markdown=\"\">\n<h3 id=\"introduction\">Introduction<\/h3>\n<p>Let&#8217;s Encrypt is a new Certificate Authority (CA) that provides an easy way to obtain and install free TLS\/SSL certificates, thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, <code class=\"\" data-line=\"\">letsencrypt<\/code>, that attempts to automate most (if not all) of the required steps. Currently, as Let&#8217;s Encrypt is still in open beta, the entire process of obtaining and installing a certificate is fully automated only on Apache web servers. However, Let&#8217;s Encrypt can be used to easily obtain a free SSL certificate, which can be installed manually, regardless of your choice of web server software.<\/p>\n<p>In this tutorial, we will show you how to use Let&#8217;s Encrypt to obtain a free SSL certificate and use it with Nginx on Ubuntu 14.04. We will also show you how to automatically renew your SSL certificate. If you&#8217;re running a different web server, simply follow your web server&#8217;s documentation to learn how to use the certificate with your setup.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/nginx-letsencrypt.png?w=910\" alt=\"Nginx with Let's Encrypt TLS\/SSL Certificate and Auto-renewal\" \/><\/p>\n<div data-unique=\"prerequisites\"><\/div>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>Before following this tutorial, you&#8217;ll need a few things.<\/p>\n<p>You should have an Ubuntu 14.04 server with a non-root user who has <code class=\"\" data-line=\"\">sudo<\/code> privileges. You can learn how to set up such a user account by following steps 1-3 in our <a href=\"https:\/\/www.digitalocean.com\/community\/articles\/initial-server-setup-with-ubuntu-14-04\">initial server setup for Ubuntu 14.04 tutorial<\/a>.<\/p>\n<p>You must own or control the registered domain name that you wish to use the certificate with. If you do not already have a registered domain name, you may register one with one of the many domain name registrars out there (e.g. Namecheap, GoDaddy, etc.).<\/p>\n<p>If you haven&#8217;t already, be sure to create an <strong>A Record<\/strong> that points your domain to the public IP address of your server. This is required because of how Let&#8217;s Encrypt validates that you own the domain it is issuing a certificate for. For example, if you want to obtain a certificate for <code class=\"\" data-line=\"\">example.com<\/code>, that domain must resolve to your server for the validation process to work. Our setup will use <code class=\"\" data-line=\"\">example.com<\/code> and <code class=\"\" data-line=\"\">www.example.com<\/code> as the domain names, so <strong>both DNS records are required<\/strong>.<\/p>\n<p>Once you have all of the prerequisites out of the way, let&#8217;s move on to installing the Let&#8217;s Encrypt client software.<\/p>\n<div data-unique=\"step-1-\u2014-install-let's-encrypt-client\"><\/div>\n<h2 id=\"step-1-\u2014-install-let-39-s-encrypt-client\">Step 1 \u2014 Install Let&#8217;s Encrypt Client<\/h2>\n<p>The first step to using Let&#8217;s Encrypt to obtain an SSL certificate is to install the <code class=\"\" data-line=\"\">letsencrypt<\/code> software on your server. Currently, the best way to install Let&#8217;s Encrypt is to simply clone it from the official GitHub repository. In the future, it will likely be available via a package manager.<\/p>\n<h3 id=\"install-git-and-bc\">Install Git and bc<\/h3>\n<p>Let&#8217;s install Git and bc now, so we can clone the Let&#8217;s Encrypt repository.<\/p>\n<p>Update your server&#8217;s package manager with this command:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo apt-get update<\/li>\n<\/ul>\n<p>Then install the <code class=\"\" data-line=\"\">git<\/code> and <code class=\"\" data-line=\"\">bc<\/code> packages with apt-get:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo apt-get -y install git bc<\/li>\n<\/ul>\n<p>With <code class=\"\" data-line=\"\">git<\/code> and <code class=\"\" data-line=\"\">bc<\/code> installed, we can easily download <code class=\"\" data-line=\"\">letsencrypt<\/code> by cloning the repository from GitHub.<\/p>\n<h3 id=\"clone-let-39-s-encrypt\">Clone Let&#8217;s Encrypt<\/h3>\n<p>We can now clone the Let\u2019s Encrypt repository in <code class=\"\" data-line=\"\">\/opt<\/code> with this command:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo git clone https:\/\/github.com\/letsencrypt\/letsencrypt \/opt\/letsencrypt<\/li>\n<\/ul>\n<p>You should now have a copy of the <code class=\"\" data-line=\"\">letsencrypt<\/code> repository in the <code class=\"\" data-line=\"\">\/opt\/letsencrypt<\/code> directory.<\/p>\n<div data-unique=\"step-2-\u2014-obtain-a-certificate\"><\/div>\n<h2 id=\"step-2-\u2014-obtain-a-certificate\">Step 2 \u2014 Obtain a Certificate<\/h2>\n<p>Let&#8217;s Encrypt provides a variety of ways to obtain SSL certificates, through various plugins. Unlike the Apache plugin, which is covered in <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-14-04\">a different tutorial<\/a>, most of the plugins will only help you with obtaining a certificate which you must manually configure your web server to use. Plugins that only obtain certificates, and don&#8217;t install them, are referred to as &#8220;authenticators&#8221; because they are used to authenticate whether a server should be issued a certificate.<\/p>\n<p>We&#8217;ll show you how to use the <strong>Standalone<\/strong> plugin to obtain an SSL certificate.<\/p>\n<h3 id=\"verify-port-80-is-open\">Verify Port 80 is Open<\/h3>\n<p>The Standalone plugin provides a very simple way to obtain SSL certificates. It works by temporarily running a small web server, on port <code class=\"\" data-line=\"\">80<\/code>, on your server, to which the Let&#8217;s Encrypt CA can connect and validate your server&#8217;s identity before issuing a certificate. As such, this method requires that port <code class=\"\" data-line=\"\">80<\/code> is not in use. That is, be sure to stop your normal web server, if it&#8217;s using port <code class=\"\" data-line=\"\">80<\/code> (i.e. <code class=\"\" data-line=\"\">http<\/code>), before attempting to use this plugin.<\/p>\n<p>For example, if you&#8217;re using Nginx, you can stop it by running this command:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo service nginx stop<\/li>\n<\/ul>\n<p>If you&#8217;re not sure if port <code class=\"\" data-line=\"\">80<\/code> is in use, you can run this command:<\/p>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">netstat -na | grep &#039;:80.*LISTEN&#039;\n<\/code><\/pre>\n<p>If there is no output when you run this command, you can use the Standalone plugin.<\/p>\n<h3 id=\"run-let-39-s-encrypt\">Run Let&#8217;s Encrypt<\/h3>\n<p>Before using Let&#8217;s Encrypt, change to the <code class=\"\" data-line=\"\">letsencrypt<\/code> directory:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">cd \/opt\/letsencrypt<\/li>\n<\/ul>\n<p>Now use the Standalone plugin by running this command:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">.\/letsencrypt-auto certonly &#8211;standalone<\/li>\n<\/ul>\n<p><span class=\"note\"><strong>Note:<\/strong> The Let&#8217;s Encrypt software requires superuser privileges, so you will be required to enter your password if you haven&#8217;t used <code class=\"\" data-line=\"\">sudo<\/code> recently.<br \/>\n<\/span><\/p>\n<p>After <code class=\"\" data-line=\"\">letsencrypt<\/code> initializes, you will be prompted for some information. This exact prompts may vary depending on if you&#8217;ve used Let&#8217;s Encrypt before, but we&#8217;ll step you through the first time.<\/p>\n<p>At the prompt, enter an email address that will be used for notices and lost key recovery:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/le-email.png?w=910\" alt=\"Email prompt\" \/><\/p>\n<p>Then you must agree to the Let&#8217;s Encrypt Subscribe Agreement. Select Agree:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/le-agreement.png?w=910\" alt=\"Let's Encrypt Subscriber's Agreement\" \/><\/p>\n<p>Then enter your domain name(s). Note that if you want a single cert to work with multiple domain names (e.g. <code class=\"\" data-line=\"\">example.com<\/code> and <code class=\"\" data-line=\"\">www.example.com<\/code>), be sure to include all of them:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2016\/03\/le-domain.png?w=910\" alt=\"Domain name prompt\" \/><\/p>\n<p>If everything was successful, you should see an output message that looks something like this:<\/p>\n<div class=\"secondary-code-label \" title=\"Output:\">Output:<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">IMPORTANT NOTES:\n - If you lose your account credentials, you can recover through\n   e-mails sent to sammy@digitalocean.com\n - Congratulations! Your certificate and chain have been saved at\n   &lt;span class=&quot;highlight&quot;&gt;\/etc\/letsencrypt\/live\/example.com\/&lt;\/span&gt;fullchain.pem. Your\n   cert will expire on &lt;span class=&quot;highlight&quot;&gt;2016-03-15&lt;\/span&gt;. To obtain a new version of the\n   certificate in the future, simply run Let&#039;s Encrypt again.\n - Your account credentials have been saved in your Let&#039;s Encrypt\n   configuration directory at \/etc\/letsencrypt. You should make a\n   secure backup of this folder now. This configuration directory will\n   also contain certificates and private keys obtained by Let&#039;s\n   Encrypt so making regular backups of this folder is ideal.\n - If like Let&#039;s Encrypt, please consider supporting our work by:\n\n   Donating to ISRG \/ Let&#039;s Encrypt:   https:\/\/letsencrypt.org\/donate\n   Donating to EFF:                    https:\/\/eff.org\/donate-le\n<\/code><\/pre>\n<p>You will want to note the path and expiration date of your certificate, which was highlighted in the example output.<\/p>\n<p><strong>Firewall Note:<\/strong> If you receive an error like <code class=\"\" data-line=\"\">Failed to connect to host for DVSNI challenge<\/code>, your server&#8217;s firewall may need to be configured to allow TCP traffic on port <code class=\"\" data-line=\"\">443<\/code>.<\/p>\n<p><strong>Note:<\/strong> If your domain is routing through a DNS service like CloudFlare, you will need to temporarily disable it until you have obtained the certificate.<\/p>\n<h3 id=\"certificate-files\">Certificate Files<\/h3>\n<p>After obtaining the cert, you will have the following PEM-encoded files:<\/p>\n<ul>\n<li><strong>cert.pem:<\/strong> Your domain&#8217;s certificate<\/li>\n<li><strong>chain.pem:<\/strong> The Let&#8217;s Encrypt chain certificate<\/li>\n<li><strong>fullchain.pem:<\/strong> <code class=\"\" data-line=\"\">cert.pem<\/code> and <code class=\"\" data-line=\"\">chain.pem<\/code> combined<\/li>\n<li><strong>privkey.pem:<\/strong> Your certificate&#8217;s private key<\/li>\n<\/ul>\n<p>It&#8217;s important that you are aware of the location of the certificate files that were just created, so you can use them in your web server configuration. The files themselves are placed in a subdirectory in <code class=\"\" data-line=\"\">\/etc\/letsencrypt\/archive<\/code>. However, Let&#8217;s Encrypt creates symbolic links to the most recent certificate files in the <code class=\"\" data-line=\"\">\/etc\/letsencrypt\/live\/&lt;span class=&quot;highlight&quot;&gt;your_domain_name&lt;\/span&gt;<\/code> directory. Because the links will always point to the most recent certificate files, this is the path that you should use to refer to your certificate files.<\/p>\n<p>You can check that the files exist by running this command (substituting in your domain name):<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo ls \/etc\/letsencrypt\/live\/<span class=\"highlight\">your_domain_name<\/span><\/li>\n<\/ul>\n<p>The output should be the four previously mentioned certificate files. You will most likely configure your web server to use <code class=\"\" data-line=\"\">fullchain.pem<\/code> as the certificate file, and <code class=\"\" data-line=\"\">privkey.pem<\/code> as the certificate key file.<\/p>\n<div data-unique=\"step-3-\u2014-configure-tls\/ssl-on-web-server-(nginx)\"><\/div>\n<h2 id=\"step-3-\u2014-configure-tls-ssl-on-web-server-nginx\">Step 3 \u2014 Configure TLS\/SSL on Web Server (Nginx)<\/h2>\n<p>Now that you have an SSL certificate, you need to configure your web server to use it.<\/p>\n<p>We&#8217;ll demonstrate how to configure the Nginx web server to use the certificate. If you use different web server software, you will need to reference how to set up SSL certificates with that particular software.<\/p>\n<p>If you haven&#8217;t installed Nginx yet, you can just type:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo apt-get install nginx<\/li>\n<\/ul>\n<p>Now you must edit the Nginx configuration that contains your server block. By default, it&#8217;s located at <code class=\"\" data-line=\"\">\/etc\/nginx\/sites-available\/default<\/code>. We&#8217;ll use <code class=\"\" data-line=\"\">nano<\/code> to edit it:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/nginx\/sites-available\/default<\/li>\n<\/ul>\n<p>Find the <code class=\"\" data-line=\"\">server<\/code> block. It might look something like this:<\/p>\n<div class=\"code-label \" title=\"Nginx configuration\">Nginx configuration<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">server {\n        listen 80 default_server;\n        listen [::]:80 default_server ipv6only=on;\n\n        root \/usr\/share\/nginx\/html;\n        index index.html index.htm;\n\n        location \/ {\n                try_files $uri $uri\/ =404;\n        }\n}\n<\/code><\/pre>\n<p><strong>Comment out<\/strong> or <strong>delete<\/strong> the lines that configure this server block to listen on port 80. In the example configuration, these two lines should be deleted:<\/p>\n<div class=\"code-label \" title=\"Nginx configuration deletions\">Nginx configuration deletions<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">        listen 80 default_server;\n        listen [::]:80 default_server ipv6only=on;\n<\/code><\/pre>\n<p>We are going to configure this server block to listen on port 443 with SSL enabled instead.<\/p>\n<p>Within your <code class=\"\" data-line=\"\">server {<\/code> block, add the following lines but replace all of the instances of <code class=\"\" data-line=\"\">example.com<\/code> with your own domain:<\/p>\n<div class=\"code-label \" title=\"Nginx configuration additions \u2014 1 of 3\">Nginx configuration additions \u2014 1 of 3<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">        listen 443 ssl;\n\n        server_name &lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt; www.&lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt;;\n\n        ssl_certificate \/etc\/letsencrypt\/live\/&lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt;\/fullchain.pem;\n        ssl_certificate_key \/etc\/letsencrypt\/live\/&lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt;\/privkey.pem;\n<\/code><\/pre>\n<p>This enables your server to use SSL, and tells it to use the Let&#8217;s Encrypt SSL certificate that we obtained earlier.<\/p>\n<p>To allow only the most secure SSL protocols and ciphers, add the following lines to the same server block:<\/p>\n<div class=\"code-label \" title=\"Nginx configuration additions \u2014 2 of 3\">Nginx configuration additions \u2014 2 of 3<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n        ssl_prefer_server_ciphers on;\n        ssl_ciphers &#039;EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH&#039;;\n<\/code><\/pre>\n<p>Lastly, outside of the original server block (that is listening on HTTPS, port 443), add this server block to redirect HTTP (port 80) to HTTPS. Be sure to replace the highlighted part with your own domain name:<\/p>\n<div class=\"code-label \" title=\"Nginx configuration additions \u2014 3 of 3\">Nginx configuration additions \u2014 3 of 3<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">server {\n    listen 80;\n    server_name &lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt;;\n    return 301 https:\/\/$host$request_uri;\n}\n<\/code><\/pre>\n<p>Save and exit.<\/p>\n<p>Now put the changes into effect by restarting Nginx:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo service nginx restart<\/li>\n<\/ul>\n<p>The Let&#8217;s Encrypt TLS\/SSL certificate is now in place. At this point, you should test that the TLS\/SSL certificate works by visiting your domain via HTTPS in a web browser.<\/p>\n<div data-unique=\"step-4-\u2014-set-up-auto-renewal\"><\/div>\n<h2 id=\"step-4-\u2014-set-up-auto-renewal\">Step 4 \u2014 Set Up Auto Renewal<\/h2>\n<p>Let\u2019s Encrypt certificates are valid for 90 days, but it\u2019s recommended that you renew the certificates every 60 days to allow a margin of error. At the time of this writing, automatic renewal is still not available as a feature of the client itself, but you can manually renew your certificates by running the Let\u2019s Encrypt client again.<\/p>\n<p>A practical way to ensure your certificates won\u2019t get outdated is to create a cron job that will automatically handle the renewal process for you. In order to avoid the interactive, menu-driven process that we used earlier, we will use different parameters when calling the Let\u2019s Encrypt client in the cron job.<\/p>\n<p>We will use Webroot plugin, instead of the Standalone plugin used earlier, because it allows your server to validate your domain without stopping your web server. The Webroot plugin adds a hidden file to your web server&#8217;s document root, which the Let&#8217;s Encrypt CA can read to verify your domain.<\/p>\n<h3 id=\"how-to-use-the-webroot-plugin\">How To Use the Webroot Plugin<\/h3>\n<p>The Webroot plugin works by placing a special file in the <code class=\"\" data-line=\"\">\/.well-known<\/code> directory within your document root, which can be opened (through your web server) by the Let&#8217;s Encrypt service for validation. Depending on your configuration, you may need to explicitly allow access to the <code class=\"\" data-line=\"\">\/.well-known<\/code> directory. To ensure that the directory is accessible to Let&#8217;s Encrypt for validation, let&#8217;s make a quick change to our Nginx configuration. Open it for editing:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/nginx\/sites-enabled\/default<\/li>\n<\/ul>\n<p>Inside the <code class=\"\" data-line=\"\">ssl<\/code> server block, add this location block:<\/p>\n<div class=\"code-label \" title=\"Add to SSL server block\">Add to SSL server block<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">        location ~ \/.well-known {\n                allow all;\n        }\n<\/code><\/pre>\n<p>You will also want look up what your document root is set to by searching for the <code class=\"\" data-line=\"\">root<\/code> directive, as the path is required to use the Webroot plugin. If you&#8217;re using the default configuration file, the root will be <code class=\"\" data-line=\"\">\/usr\/share\/nginx\/html<\/code>.<\/p>\n<p>Save and exit.<\/p>\n<p>Now that we know our <code class=\"\" data-line=\"\">webroot-path<\/code>, we can use the Webroot plugin to renew our certificate with this commands. Here, we are also specifying our domain names with the <code class=\"\" data-line=\"\">-d<\/code> option. Note that you should replace the highlighted parts with the appropriate webroot path and domain names:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">cd \/opt\/letsencrypt<\/li>\n<li class=\"line\">.\/letsencrypt-auto certonly -a webroot &#8211;agree-tos &#8211;renew-by-default &#8211;webroot-path=<span class=\"highlight\">\/usr\/share\/nginx\/html<\/span> -d <span class=\"highlight\">example.com<\/span> -d <span class=\"highlight\">www.example.com<\/span><\/li>\n<\/ul>\n<p>Once that succeeds, you will need to reload your Nginx service to use the renewed certificate:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo service nginx reload<\/li>\n<\/ul>\n<p>Now that we know the commands that we need to renew our certificate, we can automate this process using scripts and a cron job.<\/p>\n<h3 id=\"create-a-let-39-s-encrypt-configuration-file\">Create a Let&#8217;s Encrypt Configuration File<\/h3>\n<p>Before moving on, let&#8217;s simplify our renewal process by creating a Let&#8217;s Encrypt configuration file at <code class=\"\" data-line=\"\">\/usr\/local\/etc\/le-renew-webroot.ini<\/code>.<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo cp \/opt\/letsencrypt\/examples\/cli.ini \/usr\/local\/etc\/le-renew-webroot.ini<\/li>\n<\/ul>\n<p>Now open the file for editing;<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/usr\/local\/etc\/le-renew-webroot.ini<\/li>\n<\/ul>\n<p>Next, uncomment the <code class=\"\" data-line=\"\">email<\/code>, <code class=\"\" data-line=\"\">domains<\/code>, and <code class=\"\" data-line=\"\">webroot-path<\/code> lines, and update them with your own information. When you are done, the file (with comments removed) should look something like this:<\/p>\n<div class=\"code-label \" title=\"le-cli-example.com.ini excerpt\">le-cli-example.com.ini excerpt<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">rsa-key-size = 4096\n\nemail = &lt;span class=&quot;highlight&quot;&gt;you@example.com&lt;\/span&gt;\n\ndomains = &lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt;, &lt;span class=&quot;highlight&quot;&gt;www.example.com&lt;\/span&gt;\n\nwebroot-path = &lt;span class=&quot;highlight&quot;&gt;\/usr\/share\/nginx\/html&lt;\/span&gt;\n<\/code><\/pre>\n<p>Now, instead of specifying the webroot path and domain names in the command, we can use the Let&#8217;s Encrypt configuration file to fill in the blanks. Assuming your configuration file is correct, this command can be used to renew your certificate:<\/p>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">cd \/opt\/letsencrypt\n.\/letsencrypt-auto certonly -a webroot --renew-by-default --config \/usr\/local\/etc\/le-renew-webroot.ini\n<\/code><\/pre>\n<p>Now let&#8217;s create a script that we can use to renew our certificate.<\/p>\n<h3 id=\"create-a-renewal-script\">Create a Renewal Script<\/h3>\n<p>To automate the renewal process, we will use a shell script that will verify the certificate expiration date for the provided domain and request a renewal when the expiration is less than 30 days away. This script will be scheduled to run once a week. This way, even if a cron job fails, there\u2019s a 30-day window to try again every week.<\/p>\n<p>First, download the script and make it executable. Feel free to review the contents of the script before downloading it.<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo curl -L -o \/usr\/local\/sbin\/le-renew-webroot https:\/\/gist.githubusercontent.com\/thisismitch\/e1b603165523df66d5cc\/raw\/fbffbf358e96110d5566f13677d9bd5f4f65794c\/le-renew-webroot<\/li>\n<li class=\"line\">sudo chmod +x \/usr\/local\/sbin\/le-renew-webroot<\/li>\n<\/ul>\n<p>The <code class=\"\" data-line=\"\">le-renew-webroot<\/code> script takes as argument the domain name whose certificate you want to check for renewal. When the renewal is not yet necessary, it will simply output how many days are left until the given certificate expiration.<\/p>\n<p><span class=\"note\"><strong>Note:<\/strong> The script will not run if the <code class=\"\" data-line=\"\">\/usr\/local\/etc\/le-renew-webroot.ini<\/code> file does not exist. Also, be sure that the first domain that is specified in the configuration file is the same as the first domain you specified when you originally created the certificate.<br \/>\n<\/span><\/p>\n<p>If you run the script now, you will be able to see how many days are left for this certificate to expire:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo le-renew-webroot<\/li>\n<\/ul>\n<div class=\"secondary-code-label \" title=\"output\">output<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">Checking expiration date for &lt;span class=&quot;highlight&quot;&gt;example.com&lt;\/span&gt;...\nThe certificate is up to date, no need for renewal (89 days left).\n<\/code><\/pre>\n<p>Next, we will edit the crontab to create a new job that will run this command every week. To edit the crontab for the root user, run:<\/p>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo crontab -e<\/li>\n<\/ul>\n<p>Include the following content, all in one line:<\/p>\n<div class=\"code-label \" title=\"crontab entry\">crontab entry<\/div>\n<pre class=\"code-pre \"><code class=\"\" data-line=\"\">30 2 * * 1 \/usr\/local\/sbin\/le-renew-webroot &gt;&gt; \/var\/log\/le-renewal.log\n<\/code><\/pre>\n<p>Save and exit. This will create a new cron job that will execute the <code class=\"\" data-line=\"\">le-renew-webroot<\/code> command every Monday at 2:30 am. The output produced by the command will be piped to a log file located at <code class=\"\" data-line=\"\">\/var\/log\/le-renewal.log<\/code>.<\/p>\n<div data-unique=\"conclusion\"><\/div>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>That&#8217;s it! Your web server is now using a free Let&#8217;s Encrypt TLS\/SSL certificate to securely serve HTTPS content.<\/p>\n<\/div>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Following https:\/\/letsencrypt.readthedocs.org\/en\/latest\/using.html#installation you can obtain a nice SSL certificate for your own webservers; yet for those who likes NGinx like me this guide How To Secure Nginx with Let&#8217;s Encrypt on Ubuntu 14.04 | DigitalOcean is also useful In this tutorial, we will show you how to use Let&#8217;s Encrypt to obtain a free SSL&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2016\/03\/14\/how-to-secure-nginx-with-lets-encrypt-on-ubuntu-14-04-digitalocean\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14,46],"tags":[102,58],"class_list":["post-1143","post","type-post","status-publish","format-standard","hentry","category-software-libero","category-web","tag-letsencrypt","tag-nginx"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-ir","jetpack-related-posts":[{"id":12725,"url":"https:\/\/monodes.com\/predaelli\/2025\/02\/09\/nginx-proxy-manager\/","url_meta":{"origin":1143,"position":0},"title":"Nginx Proxy Manager","author":"Paolo Redaelli","date":"2025-02-09","format":"link","excerpt":"Nginx Proxy Manager comes as a pre-built docker image that enables you to easily forward to your websites running at home or otherwise, including free SSL, without having to know too much about Nginx or Letsencrypt. \u00a0","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9614,"url":"https:\/\/monodes.com\/predaelli\/2022\/09\/18\/self-hosted-streaming-server-nginx-rtmp-youtube-facebook-live\/","url_meta":{"origin":1143,"position":1},"title":"Self-Hosted Streaming Server \u2013 NGINX + RTMP \u2013 YouTube + Facebook Live","author":"Paolo Redaelli","date":"2022-09-18","format":false,"excerpt":"Copying this here in case the original goes offline Self-Hosted Streaming Server \u2013 NGINX + RTMP \u2013 YouTube + Facebook Live Published by Anthony on 14th April 2020 I\u2019m writing these notes up, mainly as a reference for me in the future \u2013 but they might just help others looking\u2026","rel":"","context":"In &quot;Basilica's website&quot;","block_context":{"text":"Basilica's website","link":"https:\/\/monodes.com\/predaelli\/category\/basilicas-website\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/screenshot-2015-06-10-16-01-521.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/screenshot-2015-06-10-16-01-521.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/screenshot-2015-06-10-16-01-521.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":522,"url":"https:\/\/monodes.com\/predaelli\/2015\/07\/06\/top-20-nginx-webserver-best-security-practices\/","url_meta":{"origin":1143,"position":2},"title":"Top 20 Nginx WebServer Best Security Practices","author":"Paolo Redaelli","date":"2015-07-06","format":false,"excerpt":"Some of those Top 20 Nginx WebServer Best Security Practices are meant for high-volume, highly exposed to attacks site, but they are nevertheless worth noticing....","rel":"","context":"In &quot;Web&quot;","block_context":{"text":"Web","link":"https:\/\/monodes.com\/predaelli\/category\/web\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11351,"url":"https:\/\/monodes.com\/predaelli\/2024\/02\/18\/about-an-nginx-fork\/","url_meta":{"origin":1143,"position":3},"title":"About \u2014 an nginx fork","author":"Paolo Redaelli","date":"2024-02-18","format":false,"excerpt":"Angie, an efficient, powerful, and scalable web server,forked from nginx by some of its former devsto extend the functionality far beyond the original.","rel":"","context":"In &quot;Software&quot;","block_context":{"text":"Software","link":"https:\/\/monodes.com\/predaelli\/category\/software\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5044,"url":"https:\/\/monodes.com\/predaelli\/2018\/12\/10\/dehydrated-a-bash-client-for-lets-encrypt-antoine-aflalo\/","url_meta":{"origin":1143,"position":4},"title":"Dehydrated: a bash client for Let&#8217;s Encrypt &#8211; Antoine Aflalo","author":"Paolo Redaelli","date":"2018-12-10","format":false,"excerpt":"Tutorial on how to use Dehydrated, a bash client for Let's Encrypt ACME Protocol. Dehydrated helps you take care of your SSL certificates. Source: Dehydrated: a bash client for Let's Encrypt - Antoine Aflalo Dehydrated: a bash client for Let\u2019s Encrypt Dehydrated was firstly known as letsencrypt.sh but because letsencrypt\u2026","rel":"","context":"In &quot;Web&quot;","block_context":{"text":"Web","link":"https:\/\/monodes.com\/predaelli\/category\/web\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2018\/12\/Acme_Dehydrated-1.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2018\/12\/Acme_Dehydrated-1.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2018\/12\/Acme_Dehydrated-1.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2018\/12\/Acme_Dehydrated-1.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":7056,"url":"https:\/\/monodes.com\/predaelli\/2020\/04\/03\/openlitespeed-troppo-buono-per-essere-vero\/","url_meta":{"origin":1143,"position":5},"title":"(open)LiteSpeed: troppo buono per essere vero?","author":"Paolo Redaelli","date":"2020-04-03","format":false,"excerpt":"Leggendo la chat di IoRestoACasa.work mi \u00e8 caduto l'occhio su un frammento di conversazione che decantava le qualit\u00e0 del server web LiteSpeed, sostenendo che fosse\u00a0molto pi\u00f9 veloce di Nginx. Tralasciamo che in prima battuta credevo fosse lighttp. Poi ho storto il naso scoprendo che era proprietario ma aveva una versione\u2026","rel":"","context":"In &quot;Proprietary software&quot;","block_context":{"text":"Proprietary software","link":"https:\/\/monodes.com\/predaelli\/category\/software\/proprietary-software\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2020\/04\/2631_chart_02.webp?resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/1143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=1143"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/1143\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=1143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=1143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=1143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}