Skip to content

Commit fa768fb

Browse files
committed
Doc: Show how to run certbot commands
1 parent 53e9382 commit fa768fb

File tree

2 files changed

+234
-1
lines changed

2 files changed

+234
-1
lines changed

Diff for: images/linode/ssl/ssl_enabled.png

108 KB
Loading

Diff for: linode/secure_domain_with_ssl.md

+234-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,242 @@ Click the "Get Started" button to learn how you can get a free SSL certificate.
4040

4141
![Cerbot commands](/images/linode/ssl/certbot_commands.gif)
4242

43-
Once on https://certbot.eff.org/, I will identify that my HTTP website is running Nginx on Ubuntu 20. Filling this will provide me several commands that I need to run to get a free SSL certificate. To use certbot, I will need these things:
43+
Once on https://certbot.eff.org/, I will identify that my HTTP website is running `Nginx` on `Ubuntu 20`. Filling this will provide me several commands that I need to run to get a free SSL certificate. To use certbot, I will need these things:
4444

4545
- Comfort with the command line interface
4646
- A HTTP website what is already online with an open port (80)
4747
- Application hosted on a server that can be accessed via SSH with the ability to run sudo commands
4848

49+
50+
## Certbot Commands
51+
52+
These are the commands I need to get a free SSL certificate:
53+
54+
1. `sudo snap install core; sudo snap refresh core`
55+
2. `sudo apt-get remove certbot`
56+
3. `sudo snap install --classic certbot`
57+
4. `sudo ln -s /snap/bin/certbot /usr/bin/certbot`
58+
5. `sudo certbot --nginx`
59+
6. `sudo certbot renew --dry-run`
60+
<br>
61+
62+
To understand what each command does, I have provided brief descriptions below.
63+
64+
1. To run the Certbot commands, I will need to SSH into my server:
65+
66+
```python
67+
$ ssh gitauharrison@212.71.247.241
68+
```
69+
70+
Ubuntu 20.04 comes with `snapd` pre-installed, so I do not have to worry about this. If your machine does not have it, make sure you install it. You can check [Conanical documentation](https://snapcraft.io/docs/installing-snapd) to find out how you can work with `snapd`.
71+
<br>
72+
73+
74+
2. Once I am in the server, I will need to update my version of `snapd` to the latest version:
75+
76+
```python
77+
gitauharrison@bolderlearner:~$ sudo snap install core; sudo snap refresh core
78+
```
79+
80+
3. Next, I need to remove any OS package manager like `apt` before installing `certbot`. This is to ensure that when I run the command `certbot`, the snap is used and not the OS package manager.
81+
82+
```python
83+
gitauharrison@bolderlearner:~$ sudo apt-get remove certbot
84+
```
85+
86+
4. Now, I can install Certbot in my server:
87+
88+
89+
```python
90+
gitauharrison@bolderlearner:~$ sudo snap install --classic certbot
91+
```
92+
93+
94+
5. To ensure that Certbot can be run, I need to prepare my server to run the command `certbot`:
95+
96+
97+
```python
98+
gitauharrison@bolderlearner:~$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
99+
```
100+
101+
### Editing Nginx Configuration File
102+
103+
Now, I am ready to run the command `certbot`. But before I do that, I need to make some changes to my Nginx configuration. I will open the Nginx configuration file in the nano editor:
104+
105+
106+
```python
107+
gitauharrison@bolderlearner:~$ sudo nano /etc/nginx/sites-enabled/somasoma_elearning
108+
```
109+
110+
Currently, I have the server name set to 212.71.247.241. What I am interested in doing is to change this to my domain name.
111+
112+
`/etc/nginx/sites-enabled/somasoma_elearning`: Update server_name to domain name
113+
```python
114+
{
115+
server_name www.bolderlearner.com;
116+
}
117+
```
118+
119+
To save this change, I will press `ctrl + X`, type "y" for "yes", and hit `enter`.
120+
121+
6. Now my server is ready to run the command `certbot`:
122+
123+
124+
```python
125+
gitauharrison@bolderlearner:~$ sudo certbot --nginx
126+
```
127+
128+
I will be asked a few questions such as what my email address is, if I would like to share my email with the foundation and to select a name I would like to activate HTTPS for. Everything should work fine and a certificate successfully delivered and received. Reading the messages carefully in my terminal, I realized that the certificate is to be deployed for www.bolderlearner.com to `/etc/nginx/sites-enabled/somasoma_elearning`.
129+
130+
I will open this file in `nano` to see these changes:
131+
132+
133+
```python
134+
gitauharrison@bolderlearner:~$ sudo nano /etc/nginx/sites-enabled/somasoma_elearning
135+
```
136+
137+
True to there word, my Nginx configuratin file has been automatically updated.
138+
139+
140+
`/etc/nginx/sites-enabled/somasoma_elearning`: Changes made by certbot
141+
142+
```python
143+
server {
144+
server_name www.bolderlearner.com;
145+
location /static {
146+
alias /home/gitauharrison/somasoma_elearning_app/app/static;
147+
}
148+
location / {
149+
proxy_pass http://localhost:8000;
150+
include /etc/nginx/proxy_params;
151+
proxy_redirect off;
152+
}
153+
154+
155+
listen 443 ssl; # managed by Certbot
156+
ssl_certificate /etc/letsencrypt/live/www.bolderlearner.com/fullchain.pem; # managed by Certbot
157+
ssl_certificate_key /etc/letsencrypt/live/www.bolderlearner.com/privkey.pem; # managed by Certbot
158+
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
159+
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
160+
161+
}
162+
server {
163+
if ($host = www.bolderlearner.com) {
164+
return 301 https://$host$request_uri;
165+
} # managed by Certbot
166+
167+
168+
169+
listen 80;
170+
server_name www.bolderlearner.com;
171+
return 404; # managed by Certbot
172+
173+
174+
}
175+
```
176+
177+
I said that I wanted the HTTP traffic to be redirected to HTTPS. The second `server` block captures this directive. If the host is www.bolderlearner.com, then all redirects (denoted by 301) will be through HTTPS.
178+
179+
180+
### Testing NGinx Configuration
181+
182+
183+
To test my Nginx configuration, I will run the command `nginx -t` in the terminal:
184+
185+
186+
```python
187+
gitauharrison@bolderlearner:~$ nginx -t
188+
189+
190+
# Output
191+
192+
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
193+
2022/02/10 05:36:26 [warn] 105538#105538: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
194+
2022/02/10 05:36:26 [emerg] 105538#105538: cannot load certificate "/etc/letsencrypt/live/www.bolderlearner.com/fullchain.pem": BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen('/etc/letsencrypt/live/www.bolderlearner.com/fullchain.pem','r') error:2006D002:BIO routines:BIO_new_file:system lib)
195+
nginx: configuration file /etc/nginx/nginx.conf test failed
196+
```
197+
198+
It seems like I have an error, but the "error" in question is actually a permission issue. To fix this, I need to prefix the command above with `sudo`.
199+
200+
201+
```python
202+
gitauharrison@bolderlearner:~$ sudo nginx -t
203+
204+
205+
# Output
206+
207+
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
208+
nginx: configuration file /etc/nginx/nginx.conf test is successful
209+
```
210+
211+
Originally, when setting up my firewall, I allowed HTTP traffic. I need to update this to allow HTTPS traffic. On the terminal, I will run:
212+
213+
```python
214+
gitauharrison@bolderlearner:~$ sudo ufw allow https
215+
```
216+
217+
I can optionally disallow HTTP traffic, but I will not do that because my configuration is already set to redirect all HTTP traffic to HTTPS. Before I can test my website to see if all these changes have taken effect, I will restart my Nginx server:
218+
219+
220+
```python
221+
gitauharrison@bolderlearner:~$ sudo systemctl restart nginx
222+
```
223+
224+
If I go back to my browser, and reload my site, I should be able to see that HTTPS has taken effect.
225+
226+
227+
![SSl enabled](/images/linode/ssl/ssl_enabled.png)
228+
229+
### Certificate Auto-renewal
230+
231+
This certificate typically lasts for around 90 days and after that I need to renew it. This can be really cumbersome, and I might even forget about it. I would want that just before the expiration date, the application is able to auto-renew this certificate for me.
232+
233+
7. This is how auto-renewal looks like.
234+
235+
```python
236+
gitauharrison@bolderlearner:~$ sudo certbot renew --dry-run
237+
238+
239+
# Output
240+
241+
Saving debug log to /var/log/letsencrypt/letsencrypt.log
242+
243+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
244+
Processing /etc/letsencrypt/renewal/www.bolderlearner.com.conf
245+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246+
Account registered.
247+
Simulating renewal of an existing certificate for www.bolderlearner.com
248+
249+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
250+
Congratulations, all simulated renewals succeeded:
251+
/etc/letsencrypt/live/www.bolderlearner.com/fullchain.pem (success)
252+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
253+
```
254+
255+
This is was a simulated renewal. To automate this renewal process, I will run a cronjob. I will open my crontab file using nano:
256+
257+
258+
```python
259+
gitauharrison@bolderlearner:~$ sudo crontab -e
260+
261+
262+
# Output
263+
264+
no crontab for root - using an empty one
265+
266+
Select an editor. To change later, run 'select-editor'.
267+
1. /bin/nano <---- easiest
268+
2. /usr/bin/vim.basic
269+
3. /usr/bin/vim.tiny
270+
4. /bin/ed
271+
272+
Choose 1-4 [1]: 1 # I have selected nano
273+
```
274+
275+
Scroll to the bottom of the file and add the following line:
276+
277+
`/tmp/crontab.SYULN5/crontab`: Auto-renewal
278+
```python
279+
30 4 1 * * sudo certbot renw --quiet
280+
```
281+
To save, I will press `ctrl + X`, type `y` and hit `enter`. This basically autorenews the certificate at 4.30 am on the 1st of every month. The `--quiet` option tells the program to not print out any messages or feedback.

0 commit comments

Comments
 (0)