Cara Mengamankan Apache dengan SSL dan Mari Enkripsi di FreeBSD


Dalam tutorial ini kita akan mempelajari cara mengamankan server Apache HTTP dengan sertifikat TLS/SSL yang ditawarkan oleh Let's Encrypt di FreeBSD 11. x. Kami juga akan membahas cara mengotomatiskan proses perpanjangan sertifikat untuk Lets’ Encrypt.

Baca Juga: Instal Let's Encrypt untuk Nginx di FreeBSD

Sertifikat TLS/SSL digunakan oleh server web Apache untuk mengenkripsi komunikasi antara node akhir, atau yang lebih umum antara server dan klien untuk memberikan keamanan. Let's Encrypt menyediakan utilitas baris perintah certbot, yang merupakan aplikasi yang dapat memfasilitasi cara Anda mendapatkan sertifikat tepercaya secara gratis.

Persyaratan:

  1. Instalasi FreeBSD 11.x
  2. 10 Hal yang Harus Dilakukan Setelah Instalasi FreeBSD
  3. Cara Install Apache, MariaDB dan PHP di FreeBSD

Langkah 1: Konfigurasikan Apache SSL di FreeBSD

1. Sebelum mulai menginstal utilitas certbot dan membuat file konfigurasi TSL untuk Apache, pertama-tama buatlah dua direktori berbeda bernama sites-available dan sites-enabled di direktori konfigurasi root Apache dengan mengeluarkan perintah di bawah ini.

Tujuan dari kedua direktori ini adalah untuk memudahkan manajemen konfigurasi virtual hosting dalam sistem, tanpa memodifikasi file konfigurasi utama Apache httpd.conf setiap kali kita menambahkan virtual host baru.

mkdir /usr/local/etc/apache24/sites-available
mkdir /usr/local/etc/apache24/sites-enabled

2. Setelah Anda membuat kedua direktori, buka file httpd.conf Apache dengan editor teks dan tambahkan baris berikut di dekat akhir file seperti yang diilustrasikan di bawah ini.

nano /usr/local/etc/apache24/httpd.conf

Tambahkan baris berikut:

IncludeOptional etc/apache24/sites-enabled/*.conf

3. Selanjutnya, aktifkan modul TLS untuk Apache dengan membuat file baru berikut bernama 020_mod_ssl.conf di modules.d< direktori dengan konten berikut.

nano /usr/local/etc/apache24/modules.d/020_mod_ssl.conf

Tambahkan baris berikut ke file 020_mod_ssl.conf.

Listen 443
SSLProtocol ALL -SSLv2 -SSLv3
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLPassPhraseDialog  builtin
SSLSessionCacheTimeout  300

4. Sekarang, hapus komentar pada modul SSL dari file /usr/local/etc/apache24/httpd.conf dengan menghapus hashtag dari awal dari baris berikut seperti yang diilustrasikan di bawah ini:

LoadModule ssl_module libexec/apache24/mod_ssl.so

5. Selanjutnya, buat file konfigurasi TLS untuk domain Anda di direktori sites-available, sebaiknya dengan nama domain Anda, seperti yang disajikan di kutipan di bawah ini:

nano /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf

Tambahkan konfigurasi virtualhost berikut ke file bsd.lan-ssl.conf.

<VirtualHost *:443>
    ServerName www.yourdomain.com
	ServerAlias yourdomain.com
                DocumentRoot "/usr/local/www/apache24/data/"
	SSLEngine on

	SSLCertificateFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem"
	SSLCertificateKeyFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem"
	SSLCertificateChainFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem"

<FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>

<Directory "/usr/local/www/apache24/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

	BrowserMatch "MSIE [2-5]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0

	CustomLog "/var/log/apache/httpd-ssl_request.log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

	<Directory "/usr/local/www/apache24/data/">
            Options Indexes FollowSymLinks MultiViews
        #AllowOverride controls what directives may be placed in .htaccess files.       
                        AllowOverride All
        #Controls who can get stuff from this server file
                        Require all granted
        </Directory>
       
    ErrorLog "/var/log/apache/yourdomain.ssl-error.log"
    CustomLog "/var/log/apache/yourdomain.ssl-access_log" combined

</VirtualHost>

Pastikan Anda mengganti variabel nama domain dari pernyataan ServerName, ServerAlias, ErrorLog, CustomLog dengan tepat.

Langkah 2: Instal Lets'Encrypt di FreeBSD

6. Pada langkah berikutnya, jalankan perintah berikut untuk menginstal utilitas certbot yang disediakan oleh Let's Encrypt, yang akan digunakan untuk mendapatkan < sertifikat gratis kuat>Apache TSL untuk domain Anda.

Saat memasang certbot serangkaian perintah akan ditampilkan di layar Anda. Gunakan tangkapan layar di bawah untuk mengonfigurasi utilitas certbot. Selain itu, mengkompilasi dan menginstal utilitas certbot mungkin memerlukan waktu, tergantung pada sumber daya mesin Anda.

cd /usr/ports/security/py-certbot
make install clean

7. Setelah proses kompilasi selesai, jalankan perintah di bawah ini untuk memperbarui utilitas certbot dan dependensi certbot yang diperlukan.

pkg install py27-certbot
pkg install py27-acme

8. Untuk menghasilkan sertifikat untuk domain Anda, jalankan perintah seperti yang diilustrasikan di bawah. Pastikan Anda memberikan lokasi webroot yang benar di mana file situs web Anda disimpan dalam sistem file (perintah DocumentRoot dari file konfigurasi domain Anda) menggunakan tanda -w. Jika Anda memiliki beberapa subdomain, tambahkan semuanya dengan tanda -d.

certbot certonly --webroot -w /usr/local/www/apache24/data/ -d yourdomain.com -d www.yourdomain.com

Saat memperoleh sertifikat, berikan alamat email untuk perpanjangan sertifikat, tekan a untuk menyetujui syarat dan ketentuan Let's Encrypt dan n untuk tidak membagikan alamat email mitra Let's Encrypt.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):[email 
There seem to be problems with that address. Enter email address (used for
urgent renewal and security notices)  If you really want to skip this, you can
run the client with --register-unsafely-without-email but make sure you then
backup your account key from /etc/letsencrypt/accounts   (Enter 'c' to cancel):[email 

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: a ------------------------------------------------------------------------------- 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 EFF and our work to encrypt the web, protect its users and defend digital rights. ------------------------------------------------------------------------------- (Y)es/(N)o: n Obtaining a new certificate Performing the following challenges: http-01 challenge for www.domain.com Using the webroot path /usr/local/www/apache24/data for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem. Your cert will expire on 2017-11-15. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /usr/local/etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

9. Setelah Anda memperoleh sertifikat untuk domain Anda, Anda dapat menjalankan perintah ls untuk mencantumkan semua komponen sertifikat (rantai, kunci pribadi, sertifikat) seperti yang ditunjukkan pada contoh di bawah.

ls -al /usr/local/etc/letsencrypt/live/www.yourdomain.com/

Langkah 3: Perbarui Sertifikat TLS Apache di FreeBSD

10. Untuk menambahkan sertifikat Let's Encrypt ke situs web Anda, buka file konfigurasi apache untuk domain Anda dan perbarui baris berikut untuk mencerminkan jalur sertifikat yang diterbitkan.

nano /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf

Tambahkan baris sertifikat TLS ini:

SSLCertificateFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem"
	SSLCertificateKeyFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem"
	SSLCertificateChainFile "/usr/local/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem"

11. Terakhir, aktifkan file konfigurasi TLS, dengan membuat symlink untuk file konfigurasi TLS domain Anda ke direktori sites-enabled, periksa konfigurasi Apache untuk kemungkinan kesalahan sintaksis dan, jika sintaksnya OK, restart daemon Apache dengan mengeluarkan perintah di bawah ini.

ln -sf /usr/local/etc/apache24/sites-available/bsd.lan-ssl.conf /usr/local/etc/apache24/sites-enabled/
apachectl -t
service apache24 restart

12. Untuk memeriksa apakah layanan Apache mendengarkan pada port HTTPS 443, jalankan perintah berikut untuk mencantumkan soket jaringan httpd.

sockstat -4 | grep httpd

13. Anda dapat menavigasi ke alamat domain Anda dari browser melalui protokol HTTPS untuk mengonfirmasi bahwa sertifikat Let's Encrypt berhasil diterapkan.

https://www.yourdomain.com

14. Untuk mendapatkan informasi tambahan tentang sertifikat Let's Encrypt yang diterbitkan dari baris perintah, gunakan perintah openssl sebagai berikut.

openssl s_client -connect www.yourdomain.com:443

15. Anda juga dapat memverifikasi apakah lalu lintas dienkripsi dengan sertifikat valid yang diberikan oleh Let's Encrypt CA dari perangkat seluler seperti yang diilustrasikan dalam tangkapan layar seluler di bawah.

Itu saja! Klien kini dapat mengunjungi situs web Anda dengan aman, karena lalu lintas yang mengalir antara server dan browser klien dienkripsi. Untuk tugas yang lebih kompleks mengenai utilitas certbot, kunjungi tautan berikut: https://certbot.eff.org/