Install let's Encrypt In On-Premise Server
Fega Suseno

Fega Suseno @seno21

About: I'm a DevOps Engineer, my passion is open source technology, Linux administration and programming. I currently work as an IT staff member at a hospital.

Joined:
Sep 19, 2023

Install let's Encrypt In On-Premise Server

Publish Date: Jan 13 '24
1 1

Postingan ini akan membahas secure http dengan free ssl let's encrypt. Kali ini kita akan bermain dengan study kasus sederhana terkait ssl le's encrypt.

Study Case:
Aplikasi web-based berjalan pada On Premise server dengan keperluan akses from anywhere. Akses via http, server on premise di forward menggunakan public IP, yang mana IP publicnya kita arahkan ke domain. Kita ambil contoh nama domainya seno.com. Jadi ketika kita ping domain tersebut akan muncul IP public saya. Akan tetapi untuk dst.port saya setting 2480 untuk port 80 dan 2443 untuk port 443. Sehingga ketika kita ingin akses http jadi seperti ini seno.com:2480 dan ketika ingin https kita akses seno.com:2443. Berikut topologinya

Image description

Lab Konfigurasi Let's Encrypt
Kita akan lakukan konfigurasi pada server dengan OS Ubuntu 20.04 LTS. Pertama yang harus kita lakukan adalah membuat config untuk web kita dan mematikan deafultnya confignya.

a2dissite 000-default
Enter fullscreen mode Exit fullscreen mode

lalu agar mudah salin saja dari configurasi defaultnya

cp 000-default seno.com
Enter fullscreen mode Exit fullscreen mode

Setelah itu modifikasi sedikit isinya seperti berikut, kita akan arahkan document root nya ke folder apps

<VirtualHost *:80>
        ServerName seno.com
        ServerAlias www.sno.com
        ServerAdmin admin@seno.com
        DocumentRoot /var/www/html/apps/
        DirectoryIndex index.php

        <Directory /var/www/html/apps>
                AllowOverride All
                Require all granted
        </Directory>


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Enter fullscreen mode Exit fullscreen mode

Setelah itu kita konfigurasi ssl dengan let's encrypt,kita perlu menginstall certbot

apt install certbot python3-certbot-apache -y
Enter fullscreen mode Exit fullscreen mode

Sesuai studi kasus maka kita pastikan port 80 dan 443 terbuka, namun sesuai study case diatas artinya kita perlu menambahkan port forwarding port 80 dan 443. Dalam kasus ini saya tambahkan port forwarding pada router mikrotik untuk port 80 dan 443. Untuk caranya saya tidak akan jelaskan disini, kepanjangan. Intinya kini ada 4 dst.port pada rule forwarding, yaitu port 2480,2443, 80, dan 443. Ini dilakukan karena let's encrypt membutuhkan open port 80 dan 443. Kita asumsikan sudah melakukan port forwarding 80 dan 443 pada router (dalam kasus ini menggunakan mikrotik).

Selanjutnya jalankan certbot untuk generate sertifikat let's encrypt bserta ssl config (file config ssl akan dibuat otomatis ex: 000-default-le-ssl.conf)

certbot --apache -d seno.com
Enter fullscreen mode Exit fullscreen mode

Karena kita akan samakan nama konfignya agar tidak bingung di kemudian hari maka kita hanya butuh sertifikatnya saja, tambahkan opsi certonly.

certbot certonly  --apache -d seno.com
Enter fullscreen mode Exit fullscreen mode

Maka akan muncul pertanyaan berikut:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) 
(Enter 'c' to cancel): your.email@mail.com
Enter fullscreen mode Exit fullscreen mode

Masukan email untuk notifikasi dari Let's Encrypt, setelah itu tekan enter

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. 
You must agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
(A)gree/(C)ancel: A
Enter fullscreen mode Exit fullscreen mode

Ketikan A untuk menyetejui semua term & service lalu tekan enter

Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
(Y)es/(N)o: N
Enter fullscreen mode Exit fullscreen mode

Pada bagian ini optional, intinya kita akan berlangganan news EFF atau tidak. EFF adalah organisasi yang mengembangkan certbot. Disini saya pilih N lalu tekan enter.

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Enter fullscreen mode Exit fullscreen mode

Pada bagian ini kita diminta untuk meredirect http ke https secara manual via browser atau redirect https secara otomatis. Rekomendasinya pilih no 2 agar ketika akses seno.com pada browser otomatis diarahkan ke https://seno.com. Settingan ini bisa di batalkan dari file config nantinya jika ingin di manualkan kembali.
Perlu diperhatikan jika ada kesalahan atau failed request sebanyak 3x maka akan perlu waktu 3 jam untuk kembali request. cek baik-baik konfigurasinya.

Biasanya let's encrypt akan menyimpan sertifikatnya di path berikut

  • etc/letsencrypt/live/seno.com/fullchain.pem
  • /etc/letsencrypt/live/seno.com/privkey.pem

Karena ini manual, maka kita perlu membuat config untuk ssl ny.Supaya mudah kita akan copy dari default config yg sudah ada

cp default-ssl.conf seno.com-ssl.conf
Enter fullscreen mode Exit fullscreen mode

lalu modifikasi isinya

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName seno.com
        ServerAlias www.seno.com
        ServerAdmin admin@seno.com
        DocumentRoot /var/www/html/apps/
        DirectoryIndex index.php

        <Directory /var/www/html/apps>
                AllowOverride All
                Require all granted
        </Directory>


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLCertificateFile /etc/letsencrypt/live/seno.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/seno.com/privkey.pem

</VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Enter fullscreen mode Exit fullscreen mode

setelah itu aktifkan module ssl

a2enmod ssl
Enter fullscreen mode Exit fullscreen mode

untuk mengeceknya bisa dengan perintah berikut

apachectl -M | grep ssl
Enter fullscreen mode Exit fullscreen mode

Jangan lupa juga untuk mengaktifkan sitenya

a2ensite seno.com.conf
Enter fullscreen mode Exit fullscreen mode
a2ensite seno.com-ssl.conf
Enter fullscreen mode Exit fullscreen mode

Setelah semua sudah selesai untuk mengecek sslnya bisa akses via browser

  • https://seno.com:2443

Untuk mengecek sslnya bisa via link berikut
https://www.ssllabs.com/ssltest/analyze.html?d=seno.com
atau bisa juga lewat www.sslshopper.com

Let’s Encrypt sudah setup cron atau systemd timer untuk perpanjangan otomatis. Kalau kamu mau uji manual bisa dengan perintah berikut

certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

Perlu di perhatikan adalah IP publicnya sesuai, misal ketika mensetting IP 123.102.20.1:80 maka ketika akan merenew let's encrypt harus dengan ip publik dan port yg sama juga.

Biasanya akan di buatkan cronjob untuk mendeteksi ketika sertifikat expired. Certbot otomatis menambahkan cronjob pada directory /etc/cron.d/certbot. Intinya certbot akan melakuan request sertifikat otomatis untuk mencegah expire sertifikat.

Catatan
Untuk versi Centos 7 ke atas untuk renew certbot tidak dibuatkan cron otomatis oleh certbot. Untuk mengatasi hal ini maka perlu dibuatkan cronjob secara manual, berikut petunjuknya

crontab -e
Enter fullscreen mode Exit fullscreen mode

Setlah itu masukan script cron berikut

0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew
Enter fullscreen mode Exit fullscreen mode

Tekan tombol esc lalu ketikan :wq untuk write and quit
Setelah selesai seharusnya certbot akan otomatis renew sertifikatnya.

Comments 1 total

Add comment