{"id":555,"date":"2020-05-08T22:04:10","date_gmt":"2020-05-08T14:04:10","guid":{"rendered":"http:\/\/www.sysadmindayph.com\/blog\/?p=555"},"modified":"2020-05-08T22:04:13","modified_gmt":"2020-05-08T14:04:13","slug":"fast-in-a-hurry-step-by-step-how-to-set-up-certbot-for-nginx","status":"publish","type":"post","link":"https:\/\/www.sysadmindayph.com\/blog\/fast-in-a-hurry-step-by-step-how-to-set-up-certbot-for-nginx\/","title":{"rendered":"[Fast, In a Hurry] Step-by-Step: How to Set Up Certbot for Nginx"},"content":{"rendered":"\n<p>I know you&#8217;re in a hurry. Sysadmin&#8217;s time is so precious because we have to deal with the next incident or the next request or the next to-do.<\/p>\n\n\n\n<p>So let&#8217;s get into it&#8230;. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/www.google.com\/search?q=certbot+for+nginx&amp;sxsrf=ALeKk02A4IOGc4f5Qsu_D5D3rotPlk6zmA:1588946170560&amp;tbm=isch&amp;source=iu&amp;ictx=1&amp;fir=4t5rRhtvnSEO1M%253A%252C6OR9DkKj6349NM%252C_&amp;vet=1&amp;usg=AI4_-kTjE5K8XhL4hXAZIxN2rVkNZ9yU4Q&amp;sa=X&amp;ved=2ahUKEwiMw8DgtaTpAhWKuJQKHWo-DgYQ9QEwAHoECAoQAw#imgrc=4t5rRhtvnSEO1M:\"><\/a><strong>How to Set Up an\u00a0Nginx Certbot<\/strong><\/h2>\n\n\n\n<p>This is specific to nginx. BUT it is almost the same as if you are doing it for Apache. You just have to use it&#8217;s plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What We Will Do. &#8230;<\/h2>\n\n\n\n<p>&nbsp;install Certbot, and then use it to get a free SSL certificate for Nginx on Ubuntu 18.04. We will also set up this certificate to renew automatically, so you don\u2019t have to spend hours maintaining it. We will then test the system to make sure that everything is running smoothly.<\/p>\n\n\n\n<p>The best practice for doing this is to use a separate Nginx server block file, rather than the default Nginx file. <\/p>\n\n\n\n<p>In other words, virtualhosting <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites. &#8230;<\/h2>\n\n\n\n<p>efore starting this tutorial, you are going to need a few things. To my mind, the best solution to using your Ubuntu setup as a HTTPS server is to use a LEMP stack, and this is what I\u2019m using in the following tutorial. I know that some people prefer a different setup, however, so I\u2019ll list the absolute minimum requirements here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, an Ubuntu 18.04 server set up and running. You will also need this server to have a sudo non-root user, and an operational firewall.<\/li><li>A registered, working, and tested domain name. In this tutorial, I\u2019m going to use example.com. There are plenty of ways to get a domain name cheaply, or even for free, or you can just use your existing registrar.<\/li><li>For your domain name, you are going to need to have set up&nbsp;<strong>two&nbsp;<\/strong>DNS records. One should be an A record with example.com pointing to your server\u2019s public IP, and the other another A record with example.com pointing to the same place.<\/li><li>Once you have all this, go ahead and install Nginx on your server. This is pretty straightforward, but there are plenty of&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-nginx-on-ubuntu-18-04\">guides<\/a>&nbsp;available if you get stuck. Make sure, when you install Nginx, that you also have a server block for your domain. In this tutorial, I will use \/etc\/nginx\/sites-available\/example.com as my example, but install the server block wherever is easiest for you.<\/li><\/ul>\n\n\n\n<p>Keep in mind that Nginx can also be&nbsp;<a href=\"https:\/\/docs.nginx.com\/nginx\/admin-guide\/web-server\/reverse-proxy\/\" target=\"_blank\" rel=\"noreferrer noopener\">used as a proxy server<\/a>&nbsp;within your network environment. If you are using a proxy configuration, it complicates the SSL setup process but can still be done with Let\u2019s Encrypt. To secure external traffic from end to end, you will need to actually obtain two separate SSL certificates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Install\u00a0<strong>Certbot<\/strong>. &#8230;<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo add-apt-repository ppa:certbot\/certbot<\/em><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo apt update<\/em><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install python-certbot-nginx<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Configure and Confirm\u00a0<strong>Nginx<\/strong>. &#8230;<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo nano \/etc\/nginx\/sites-available\/example.com<\/em><\/pre>\n\n\n\n<p>With the file open, look or search for the server_name line. It should look like this:<\/p>\n\n\n\n<p>\u2026<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>server_name example.com www.example.com;<\/em><\/pre>\n\n\n\n<p>\u2026<\/p>\n\n\n\n<p>If it does, good.<\/p>\n\n\n\n<p>If it doesn\u2019t, you are going to have to tell Nginx where to look. To do so, update the line to point it to the right domain. Then save and close the document. This should be all you have to do, but it is worth checking at this point that everything is well. You can check that your edits make sense to Nginx by running it from the command line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo nginx -t<\/em><\/pre>\n\n\n\n<p>If you get an error, something went wrong. The most likely source is a typo in your own edits, so go back and check the file for those. Once you\u2019ve got this command running with no errors, you can move on.<\/p>\n\n\n\n<p>The next step is to re-start Nginx so it will use the correct server block. To do this, you will need to make a system call, but don\u2019t worry. Run this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo systemctl reload nginx<\/em><\/pre>\n\n\n\n<p>At this point, Nginx should report that it found the correct server block.<\/p>\n\n\n\n<p>So far so good.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Allow HTTPS Traffic Through your Firewall. &#8230;<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo ufw status<\/em><\/pre>\n\n\n\n<p>The output should look something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.haydenjames.io\/wp-content\/uploads\/2019\/09\/ubuntu_firewall_ufw.png\" alt=\"Ubuntu firewall ufw\" class=\"wp-image-10219\" title=\"Ubuntu firewall ufw\"\/><\/figure>\n\n\n\n<p>As you can see, at the moment only HTTP traffic is allowed through your server, so you need to tell ufw to allow HTTPS through. Nginx already comes with a profile that will allow this, so all you need to do is enable it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo ufw allow 'Nginx Full'<\/em><\/pre>\n\n\n\n<p>Then disable the obsolete Nginx HTTP profile by deleting it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo ufw delete allow 'Nginx HTTP'<\/em><\/pre>\n\n\n\n<p>To check that these commands worked, you can verify the configuration settings for ufw in the same way as before. Run the same status command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo ufw status<\/em><\/pre>\n\n\n\n<p>And you should see that the output has changed, so now HTTPs is permitted:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Status: active\nTo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Action&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; From\n--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ------&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ----\nOpenSSH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ALLOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Anywhere\nNginx Full&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ALLOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Anywhere\nOpenSSH (v6)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ALLOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Anywhere (v6)\nNginx Full (v6)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ALLOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Anywhere (v6)<\/pre>\n\n\n\n<p>So you should now have Nginx installed, with a ufw set up that will allow HTTPs traffic through.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Get an SSL Certificate. &#8230;<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo certbot --nginx -d example.com -d www.example.com<\/em><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Verifying Auto-Renewal for\u00a0<strong>Certbot<\/strong>. &#8230;<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>$ sudo certbot renew --dry-run<\/em><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Next Steps and Extra Security<\/h2>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know you&#8217;re in a hurry. Sysadmin&#8217;s time is so precious because we have to deal with the next incident or the next request or the next to-do. So let&#8217;s get into it&#8230;. How to &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,7],"tags":[403,404],"class_list":["post-555","post","type-post","status-publish","format-standard","hentry","category-installation","category-operating-system","tag-linux","tag-nginx"],"_links":{"self":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":1,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/555\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}