Cara Menerapkan HTTPS di Localhost

cara menerapkan https di localhost

jika kita hanya menggunakan http maka trafik atau lalulintas data antara server dan client akan terlihat secara telanjang, dengan menggunakan wireshark ataupun tools yang sejenis dengan sangat mudah trafik lalulintas data akan dengan sangat mudah terbaca. Untuk menutupi kelemahan tersebut lahirlah teknologi untuk mengatasinya yaitu https atau http over tls atau http over ssl, yaitu lalulintas data yang akan di encrypsi terlebihdahulu sebelum data dikirimkan.

dalam artikel ini saya akan mencoba menerapkan https ini di system operasi lubuntu, untuk kebutuhan oprek-oprek wordpress agar lebih secure walaupun di localhost :)

sekarang kita fokus ke webserver nya yaitu menjadikan webserver yang ada di lubuntu ini menjadi lebih secure dan aman.

pertama-tama install segala kebutuhan softwarenya yaitu :

1. saya menggunakan Lubuntu versi 20.04
2. brew
3. mkcert
4. apache (sudah terinstall sebelumnya)
5. tentu saja browser kesayangan kita yaitu mozilla firefox

Install brew

Homebrew (brew) yaitu manager package yang dibuat untuk memudahkan installasi software di MacOS. Brew sekarang tersedia di Linux karena sifatnya yang opensource jadi banyak kontributor yang ikut mengembangkan tools ini.

dependencies
1. gcc +4.4
2. linux +2.6.32
3. glibc +2.12
4. 64-bitx86 CPU


install paket yang dibutuhkan
$ sudo apt-get install build-essential curl file git


download dan install brew
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

setelah itu jalankan perintah dibawah :

$ test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
$ test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
$ test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
$ echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile    

jalankan perintah install aplikasi
$ brew install hello

selanjutnya jalankan aplikasi yang sudah terinstall
hello

jika lancar akan muncul

Hello, World!


selanjutnya

Install mkcert

install paket yang dibutuhkan

$ sudo apt-get install libnss3-tools

install mkcert via brew
$ brew install mkcert

hasilnya adalah sbb:
Updating Homebrew...
==> Downloading https://linuxbrew.bintray.com/bottles/mkcert-1.2.0.x86_64_linux.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mkcert-1.2.0.x86_64_linux.bottle.tar.gz
/home/linuxbrew/.linuxbrew/Cellar/mkcert/1.2.0: 6 files, 4.3MB


install Certificat Authority (CA)

$ mkcert -install

maka hasilnya sbb;
==================
Created a new local CA 💥
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox and/or Chrome/Chromium trust store (requires browser restart)! 🦊


selanjutnya membuat certificate untuk localhost

$ mkcert localhost

Created a new certificate valid for the following names 📜
 - "localhost"

The certificate is at "./localhost.pem" and the key at "./localhost-key.pem" ✅

It will expire on 16 October 2024 🗓

terdapat dua file yang dihasilkan, certificate localhost.pem dan key localhost-key.pem. Semuanya berada di folder tempat menjalankan perintah.


sekarang kita siapkan webserver apache untuk menerapkan HTTPS

$ install apache
$ sudo apt-get install apache2

 selanjutnya memindahkan certificate dan key
 
$ sudo mv localhost.pem /etc/ssl/certs
$ sudo mv localhost-key.pem /etc/ssl/private
 
 Konfigurasi virtualhost localhost untuk port 443 (HTTPS)
 
 $ sudo nano /etc/apache2/sites-available/default-ssl.conf
 
 Cari opsi certificate dan ubah seperti dibawah
 
 SSLCertificateFile /etc/ssl/certs/localhost.pem
 SSLCertificateKeyFile /etc/ssl/private/localhost-key.pem
 
 
selanjutnya aktifkan modeul SSL untuk Apache dan virtualhost default-ssl, yang sudah kita siapkan tadi, dengan perintah berikut :

$ sudo a2enmode ssl
$ sudo a2ensite default-ssl.conf
$ sudo systemctl restart apache2
$ sudo systemctl status apache2

buka browser mozilla firefox dan akses alamat
https://localhost maka hasilnya seperti terlihat dalam gambar




HTTPS untuk Custom VirtualHost
membuat certificate untuk virtualhost contoh.bro

$ mkcert contoh.bro

haslinya :

Created a new certificate valid for the following names 📜
 - "contoh.bro"

The certificate is at "./contoh.bro.pem" and the key at "./contoh.bro-key.pem" ✅

It will expire on 16 October 2024 🗓


memindahkan certificat :

$ sudo mv contoh.bro.pem /etc/ssl/certs
$ sudo mv contoh.bro-key.pem /etc/ssl/private


membuat folder untuk virtualhost contoh.bro

sudo mkdir /var/www/html/contoh.bro

lalu buat file html sederhana atau boleh pake template boostrap bila perlu

$ sudo featherpad /var/www/html/contoh.bro/index.html

isinya :

ini adalah Website Contoh yang menerapkan https

membuat configurasi virtualhost di apache untuk contoh.bro

$ sudo featherpad /etc/apache2/sites-available/contoh.bro.conf

isinya seperti berikut :

<VirtualHost *:80>
    ServerName contoh.bro
    ServerAlias www.contoh.bro

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.contoh.bro [OR]
    RewriteCond %{SERVER_NAME} =contoh.bro
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName contoh.bro
    ServerAlias www.contoh.bro
    DocumentRoot /var/www/html/contoh.bro

    ErrorLog /var/log/apache2/contoh.bro_error.log
    CustomLog /var/log/apache2/contoh.bro_access.log combined

    SSLCertificateFile /etc/ssl/certs/contoh.bro.pem
    SSLCertificateKeyFile /etc/ssl/private/contoh.bro-key.pem
</VirtualHost>
</IfModule>    


setelah itu aktifkan virtualhost contoh.bro

$ sudo a2ensite contoh.bro.conf

restart apache

$ sudo service apache2 restart
$ sudo systemctl status apache2



memasukan host contoh.bro ke dalam konfigurasi hosts

$ sudo featherpad /etc/hosts

tambahkan

127.0.0.1    contoh.bro

silahkan uji di browser firefox dengan mengakses address https://contoh.bro sesuai dengan konfigurasi virtualhost apache

virtualhost contoh.bro yang sudah berubah menjadi https ditandai dengan adanya gambar gembok disebelah address https://contoh.bro. berikut web contoh.bro yang sudah saya isi template bootstrap




Komentar