Lewati ke isi

Introduction

The Apache HTTP server is a popular open-source web server that offers flexibility, power, and widespread support for developers. Apache server configuration does not take place in a single monolithic file, but instead happens through a modular design where new files can be added and modified as needed. Within this modular design, you can create an individual site or domain called a virtual host.

Using virtual hosts, one Apache instance can serve multiple websites. Each domain or individual site that is configured using Apache will direct the visitor to a specific directory holding that site’s information. This is done without indicating to the visitor that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load.

In this guide, you will set up Apache virtual hosts on an Ubuntu 20.04 server. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting by creating two virtual host sites.

Creating the Web Directory Structure

sudo mkdir -p /var/www/api.agungsurya.my.id/api-server/
sudo mkdir -p /var/www/docs.agungsurya.my.id/

Creating New Virtual Host Files

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/api.agungsurya.my.id.conf
sudo nano /etc/apache2/sites-available/api.agungsurya.my.id.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 api.agungsurya.my.id

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/api.agungsurya.my.id/api-server

        # 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

        SecRuleEngine On #ini sebenarnya tambahan modsecurity kalau tidak punya modsecurity comment saja
#       SSLEngine on#
        #SSLCertificateFile /etc/apache2/ssl/example/api/cert.pem
        #SSLCertificateKeyFile /etc/apache2/ssl/example/api/privkey.pem

       # SSLEngine on

#        SSLCertificateFile     /etc/apache2/ssl/example/cert.pem
#        SSLCertificateKeyFile /etc/apache2/ssl/example/key.pem

#                SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
#                SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

#   SSLEngine on
#   SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
#   SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        #LimitRequestLine 100000000
        #LimitRequestFieldSize 100000


<Directory /var/www/api.agungsurya.my.id/api-server>
                Options FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
               # Order allow,deny
                #allow from all
</Directory>

        # 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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Enabling the New Virtual Host Files

sudo a2ensite api.agungsurya.my.id.conf
sudo a2ensite docs.agungsurya.my.id.conf
The should receive the following output:

Output
Enabling site example.com.
To activate the new configuration, you need to run:
  systemctl reload apache2
sudo systemctl restart apache2