life, coding, and stuff

10 April 2013

Konfigurasi Web Server Apache


Apache adalah salah satu web server populer yang banyak dipakai. Kelebihannya adalah banyaknya kustomisasi yang bisa kita lakukan agar apache dapat bekerja sesuai yang kita inginkan. Kali saya akan menunjukkan beberapa konfigurasi pada apache yang dapat kita lakukan.

Membuat Virtual Direktori

Biasanya, kita harus meletakkan halaman-halaman web kita di folder /var/www (jika di linux) atau /htdocs (jika di windows). Nah, ada trik dimana jika kita ingin meletakkan halaman web di luar folder-folder ini, atau istilahnya Virtual directory.

Pertama kita harus meng-enable modul mod_alias milik apache
LoadModule alias_module modules/mod_alias.so



Kemudian tambahkan konfigurasi berikut di .httpd.conf


# ADD THIS LINES FOR YOUR VIRTUAL DIRECTORIES Alias /project1 /home/me/web/project1 Alias /project2 /home/me/web/project2 Alias /project3 /home/me/web/project3

dalam contoh, saya membuat virtual directory bernama /project1 dimana sebenarnya letak project1 ini ada di /home/me/project1

Lalu tambahkan baris berikut untuk setiap virtual direktori:

# SETTING DIRECTORY FOR /home/me/web/project1

Options None
AllowOverride All
Order allow,deny
Allow from all


Kita harus membuat lebih dari satu konfigurasi untuk setiap virtual direktori yang kita buat seperti di atas. Kemudian Restart Apache.

Hosting lebih dari satu website

Apache dapat meng-host lebih dari satu website. Metode ini bisa dilakukan dengan melakukan setting yang disebut "virtual host" di Apache.

di folder /etc/apache2/sites-available/ ada sebuah file bernama "default". Gunakan file ini sebagai template untuk file virtual host. Untuk melakukannya cukup copy file-nya, dan rename dengan nama lain. contoh :
cp default djmadjack.com
kemudian edit file yang baru kta copy tersebut menggunakan text editor


nano djmadjack.com
kemudian tambahkan baris berikut :


      ServerAdmin eric@djmadjack.com
      ServerName www.djmadjack.com
      DocumentRoot /var/www/djmadjack.com/
      
              Options FollowSymLinks
              AllowOverride None
      
      
              Options Indexes FollowSymLinks MultiViews
              AllowOverride None
              Order allow,deny
              allow from all
      

 (...)

Baris DocumentRoot adalah letak di mana halaman web kita akan disimpan.

Kemudian kita perlu meng-enable website baru kita.

a2ensite djmadjack.com
Jika sewaktu-waktu kita perlu men-disable, gunakan

a2dissite djmadjack.com

Terakhir, restart apache.

Direktori Halaman Web per User

Secara default, letak halaman web apache adalh di /var/www. Ada kalanya kuta ingin membuat agar setiap user di komputer memiliki folder web nya sendiri-sendiri, jadi nantinya website kita bisa diakses lewat url : http://example.com/~namauser/
Pertama, kita perlu meng-enable modul mod_userdir di apache

sudo a2enmod userdir


Lalu lakukan konfigurasi di file userdir.conf

gksudo gedit /etc/apache2/mods-enabled/userdir.conf

Kemudian ganti dengan baris berikut :

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root
 
        <Directory /home/*/public_html>
  AllowOverride All
  Options MultiViews Indexes SymLinksIfOwnerMatch
  <Limit GET POST OPTIONS>
          Order allow,deny
          Allow from all
  </Limit>
  <LimitExcept GET POST OPTIONS>
          Order deny,allow
          Deny from all
  </LimitExcept>
        </Directory>
</IfModule>


Restart apache.

Dengan ini, setiap user dapat memiliki folder web nya di folder public_html di home foldernya masing-masing. Url nya dapat diakses melalui : http://127.0.0.1/~namauser/.

Disable Directory Listing

Secara default, apache akan memperlihatkan direktori jika tidak dapat menemukan file index.html atau index.php. Untuk alasan keamanan, kita mungkin ingin mendisable hal ini.

Pertama, buka file .httpd.conf

Cari baris berikut

Options Includes Indexes FollowSymLinks MultiViews

Kemudian hapus tulisan Indexes menjadi :

Options Includes FollowSymLinks MultiViews

Restart apache.

Melindungi Direktori dengan Password

Kita dapat melindungi sebuah direktori agar tidak dapat diakses orang yang tidak berhak. Caranya dengan menggunakan file .htaccess

Pertama buat file bernama .htacess di direktori yang ingin kita lindungi. Isi dengan baris berikut :


AuthName "Section Name"
    AuthType Basic
    AuthUserFile /home/username/.htpasswds
    Require valid-user 


baris /home/username/,htpasswds merupakan lokasi file yang berisi username dan password kita.
Buatlah file tersebut.

Kemudian buka situs berikut, isi username dan password anda pada form yang sesuai, kemudian klik create password. maka password akan dienkripsi.

kemudian isi file .htpasswds kita dengan username dan password yang kita dapat dari situs barusan.


username:password


Restart apache.

Nah sekarang, direktori kita sudah terlindungi. Jika user membuka direktori ini lewat web browser, sebuah dialog box akan muncul dan menanyakan username dan password sebelum kita dapat mengaksesnya. Atau alternatifya kita bisa membukanya lewat :

http://username:password@www.website.com/directory/ 
    
Konfigueasi SSL

Ada kalanya kita ingin melindungi website kita dengan SSL untuk alasan keamanan.  Untuk kasus self-signed certificate, kita bisa melakukan langkah berikut :

Pertama, enable SSL di Apache 

sudo a2enmod ssl

Generate file CSR

sudo openssl req -new > new.ssl.csr

Masukkan passphrase, ingat baik-baik, karen akita akan ditanya lagi di langkah berikutnya.

Kemudian kita akan diberikan serangkaian pertanyaan :

Generating a 1024 bit RSA private key
................++++++
........................++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:Masukkan kode Negara
State or Province Name (full name) [Some-State]:Masukkan nama Provinsi
Locality Name (eg, city) []:Masukkan nama kota
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Masukkan nama instansi 
Organizational Unit Name (eg, section) []:Masukkan nama unit di institusi anda
Common Name (eg, YOUR name) []:Nama lengkap anda
Email Address []:Email anda

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:Kosongi
An optional company name []:Opsional

Kemudian generate certificate nya

sudo openssl rsa -in privkey.pem -out new.cert.key
sudo openssl x509 -in new.ssl.csr -out new.cert.cert -req -signkey new.cert.key -days NNN
sudo cp new.cert.cert /etc/ssl/certs/server.crt
sudo cp new.cert.key /etc/ssl/private/server.key

Ganti NNN dengan jumlah hari certificate ini akan berlaku sebelum expired.

Kemudian lakukan konfigurasi di file situs anda yang ada di sites-available. Contohnya, saya di : /etc/apache2/sites-available/mydomain.net:

NameVirtualHost *:443
NameVirtualHost *:80


    ServerAdmin email address here
    ServerName mydomain.net
    ServerAlias www.mydomain.net
    DocumentRoot /srv/www/mydomain.net/public_html/
    ErrorLog /srv/www/mydomain.net/logs/error.log
    CustomLog /srv/www/mydomain.net/logs/access.log combined



    ServerAdmin 
 jzb@zonker.net
    ServerName mydomain.net
    ServerAlias www.mydomain.net
    DocumentRoot /srv/www/mydomain.net/public_html/

    ErrorLog /srv/www/mydomain.net/logs/error.log
    CustomLog /srv/www/mydomain.net/logs/access.log combined

    SSLEngine on
    SSLOptions +StrictRequire
    SSLCertificateFile /etc/ssl/certs/server.crt
    SSLCertificateKeyFile /etc/ssl/private/server.key

Enable situs anda 

sudo a2ensite mydomain.net

Restart apache.

Nah, sekarang kita bisa membuka situs kita dengan protokol https://www.example.com

1 comment:

  1. WynnBET typically posts them around 2-3pm, making them by far the quickest e-book to release strains. If this continues, there is a clear benefit to half in} with WynnBET. In the following section, we’ll go through each of our picks for one of the best sports activities betting apps individually, the identical as we did for our rundown of one of the best scores and odds apps to download. Jackpot Cash on line casino cellular offers wonderful cellular gaming services 먹튀검증 abc-1111 with a formidable app. Such sites will provide a cushty recreation and guarantee a fast withdrawal of rewards. Gambling in the on line casino, as well as|in addition to} different options, are tailored for cellular devices.

    ReplyDelete

Popular Posts