diyhosting/certbot.html

146 lines
5.9 KiB
HTML

<!DOCTYPE html>
<html lang=en>
<head>
<title>Certbot and HTTPS &ndash; diyhosting.bhh.sh</title>
<!--# include file=".nav.html" -->
</head>
<body>
<header><h1>Certbot and HTTPS</h1></header>
<nav></nav>
<main>
<p>
Once you have a website, it is extremely important to enable encrypted connections over HTTPS/SSL.
You might have no idea what that means, but it's easy to do now that we've <a href="nginx.html">set our web server up</a>.
</p>
<p>
Certbot is a program that automatically creates and deploys the certificates that allow encrypted connections.
It used to be painful (and often expensive) to do this, but now it's all free and automatic.
</p>
<h2>Why is encryption important?</h2>
<ul>
<li>With HTTPS, users' ISPs cannot snoop on what they are looking at on your website.
They know that they have connected, but the particular pages they visit are private as everything is encrypted. HTTPS increases user privacy.</li>
<li>If you later create usernames and passwords for any service on your site, lack of encryption can compromise that private data! Most well-designed software will automatically <em>prevent</em> any unencrypted connections over the internet.</li>
<li>Search engines like Google favor pages with HTTPS over unencrypted HTTP.</li>
<li>You get the official-looking green 🔒 symbol in the URL bar in most browsers which makes normies subtly trust your site more.</li>
</ul>
<h2>Let's do it!</h2>
<img src=pix/nginx-website.png>
<p>Note in this picture that a browser accessing your site will say "Not secure" or something else to notify you that we are using and unencrypted HTTP connection rather than an encrypted HTTPS one.</p>
<H2>Installation</h2>
<p>Just run:</p>
<pre><code>apt install python3-certbot-nginx</code></pre>
<p>And this will install <code>certbot</code> and its module for <code>nginx</code>.</p>
<h2>Run</h2>
<p>
As I mentioned in the previous article, firewalls might interfere with certbot, so you will want to either disable your firewall or at least ensure that it allows connections on ports 80 and 443:
</p>
<pre><code>ufw allow 80
ufw allow 443</code></pre>
<p>
Now let's run certbot:
</p>
<pre><code>certbot --nginx</code></pre>
<p>
The command will ask you for your email.
This is so when the certificates need to be renewed in three months, you will get an email about it.
You can set the certificates to renew automatically, but it's a good idea to check it the first time to ensure it renewed properly.
You can avoid giving your email by running the command with the <code>--register-unsafely-without-email</code> option as well.
</p>
<p>Agree to the terms, and optionally consent to give your email to the EFF (I recommend against this obviously).
</p>
<p>
Once all that is done, it will ask you what domains you want a certificate for. You can just press enter to select all.
</p>
<img src=pix/certbot-01.png>
<p>
It will take a moment to create the certificate, but afterwards, you will be asked if you want to automatically redirect all connections to be encrypted.
Since this is preferable, choose 2 to Redirect.
</p>
<img src=pix/certbot-02.png>
<h3>Checking for success</h3>
<p>You should now be able to go to your website and see that there is a lock icon or some other notification that you are now on an encrypted connection.</p>
<img src=pix/certbot-03.png>
<h2>Setting up certificate renewal</h2>
<p>
As I mentioned in passing, the Certbot certificates last for 3 months.
To renew certificates, you just have to run <code>certbot --nginx renew</code> and it will renew any certificates close to expiry.
</p>
<p>
Of course, you don't want to have to remember to log in to renew them every three months, so it's easy to tell the server to automatically run this command.
We will use a <a href="cron.html">cronjob</a> for this. Run the following command:
</p>
<pre><code>crontab -e</code></pre>
<aside>
<p>
There might be a little menu that pops up asking what text editor you prefer when you run this command.
If you don't know how to use vim, choose <code>nano</code>, the first option.
</p>
</aside>
<p>
This <code>crontab</code> command will open up a file for editing.
A crontab is a list of commands that your operating system will run automatically at certain times.
We are going to tell it to automatically try to renew our certificates every month so we never have to.
</p>
<p>
Create a new line at the end of the file and add this content:
</p>
<pre><code>0 0 1 * * certbot --nginx renew</code></pre>
<p>
Save the file and exit to activate this cronjob.
</p>
<p>
For more on cron and crontabs please <a href="cron.html">click here!</a>
</p>
<span class=prev><a href="nginx.html">Previous: Set up a webserver.</a></span>
<!-- <span class=next><a href="html.html">Next: Use HTML to Make Simple Webpages</a></span> -->
<span class=next>You've reached the end of the basic course!</span>
<p>
You now have a live website on the internet.
You can add to it what you wish.
</p>
<p>
As you add content to your site, there are many other things you can also install linked on <a href="index.html">the main page</a>,
and many more improvements, tweaks and bonuses.
</p>
</main>
<!--# include file=".footer.html" -->
</body>
</html>