0

Set Up Apache Virtual Hosts on Ubuntu 16.04

Virtual Hosts berguna untuk membuat banyak domain/website dalam satu alamat ip. Pada tutorial ini akan dibahas setup Virtual Hosts pada ubuntu 16.04.

Install Apache Webserver

Install apahe webserver terlebih dahulu. Disini menggunakan Lamp.

sudo apt-get install lamp-server^
sudo service mysql start
sudo service apache2 start

Setelah selesai install lamp, test apakah web server yang dibuat sudah aktif atau belum dengan cara ketik ip server pada browser. Jika sudah jalan, akan muncul tampilan seperti berikut ini:

Setup Apache Virtual Hosts

Misal domain yang akan dibuat adalah sub1.akh.al dan sub2.akh.al. Pada contoh ini digunakan sub domain dari akh.al.

  1. Buat Virtual Directory
    sudo mkdir -p /var/www/html/sub1.akh.al/public_html
    sudo mkdir -p /var/www/html/sub2.akh.al/public_html
  2. Setting Ownership dan Permissions
    sudo chown -R $USER:$USER /var/www/html/sub1.akh.al/public_html
    sudo chown -R $USER:$USER /var/www/html/sub2.akh.al/public_html

    $USER merukan user yang digunakan untuk login.
    Set permission untuk apache web root (/var/www/html/), sehingga semua user dapat membaca file yang ada di dalam directory.

    sudo chmod -R 755 /var/www/html/
  3. Buat Virtual Hosts Files
    Secara default, virtual hosts files mempunya nama 000-default.conf berlokasi di /etc/apache2/sites-available. Copy file tersebut dengan nama sub1.akh.al.conf.

    cd /etc/apache2/sites-available
    cp 00-default.conf sub1.akh.al.conf

    Ganti isi file sesua dengan domain yang digunakan.

    nano sub1.akh.al.conf
    <VirtualHost *:80>
            # The ServerName directive sets the request scheme, hostname and port that
            # the server uses to identify itself. This is used when creating
            # redirection URLs. In the context of virtual hosts, the ServerName
            # specifies what hostname must appear in the request's Host: header to
            # match this virtual host. For the default virtual host (this file) this
            # value is not decisive as it is used as a last resort host regardless.
            # However, you must set it for any further virtual host explicitly.
            #ServerName www.example.com
    
            ServerAdmin webmaster@akh.al
            ServerName sub1.akh.al
            ServerAlias www.sub1.akh.al
            DocumentRoot /var/www/html/sub1.akh.al/public_html
            <Directory /var/www/html/sub1.akh.al/public_html>
                AllowOverride all
            </Directory>
    
            # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
            # error, crit, alert, emerg.
            # It is also possible to configure the loglevel for particular
            # modules, e.g.
            #LogLevel info ssl:warn
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            # For most configuration files from conf-available/, which are
            # enabled or disabled at a global level, it is possible to
            # include a line for only one particular virtual host. For example the
            # following line enables the CGI configuration for this host only
            # after it has been globally disabled with "a2disconf".
            #Include conf-available/serve-cgi-bin.conf
    </VirtualHost>

    Buat virtual hosts file yang kedua dengan nama sub2.akh.al.conf, dan ganti isinya juga sesuai dengan domain yang akan digunakan.

    cp sub1.akh.al.conf sub2.akh.al.conf
    <VirtualHost *:80>
            # The ServerName directive sets the request scheme, hostname and port that
            # the server uses to identify itself. This is used when creating
            # redirection URLs. In the context of virtual hosts, the ServerName
            # specifies what hostname must appear in the request's Host: header to
            # match this virtual host. For the default virtual host (this file) this
            # value is not decisive as it is used as a last resort host regardless.
            # However, you must set it for any further virtual host explicitly.
            #ServerName www.example.com
    
            ServerAdmin webmaster@akh.al
            ServerName sub2.akh.al
            ServerAlias www.sub2.akh.al
            DocumentRoot /var/www/html/sub2.akh.al/public_html
            <Directory /var/www/html/sub2.akh.al/public_html>
                AllowOverride all       
            </Directory>
    
            # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
            # error, crit, alert, emerg.
            # It is also possible to configure the loglevel for particular
            # modules, e.g.
            #LogLevel info ssl:warn
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            # For most configuration files from conf-available/, which are
            # enabled or disabled at a global level, it is possible to
            # include a line for only one particular virtual host. For example the
            # following line enables the CGI configuration for this host only
            # after it has been globally disabled with "a2disconf".
            #Include conf-available/serve-cgi-bin.conf
    </VirtualHost>

    Setelah membuat virtual hosts files yang baru, non aktifkan virtual hosts files default dan aktifkan virtual hosts files yang baru.

    sudo a2dissite 000-default.conf
    sudo a2ensite sub1.akh.al.conf
    sudo a2ensite sub2.akh.al.conf

    Kemudian restart apache service

    sudo systemctl restart apache2
  4. Testing Virtual Hosts
    Untuk mencobanya, buka domain yang sudah ditambahkan dengan browser. Jika berhasil akan nampak seperti gambar dibawah ini: