Apache: Virtual Host 2

From OnnoWiki
Jump to navigation Jump to search

Install

sudo apt update
sudo apt install apache2

Buat Directory

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.com/public_html
sudo chmod -R 755 /var/www

Buat Demo Page

Edit

nano /var/www/example.com/public_html/index.html

Isi dengan

<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>

Success! The example.com virtual host is working!

  </body>
</html>

Copy

cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

Modify jika di perlukan

nano /var/www/test.com/public_html/index.html


<html>
  <head>
    <title>Welcome to Test.com!</title>
  </head>

<body>

Success! The test.com virtual host is working!

  </body>
</html>

Buat Virtual Host

Buat Contekan

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Edit

sudo nano /etc/apache2/sites-available/example.com.conf

Aslinya

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Untuk virtualhost

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Konfigurasi Apache2

Edit

sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf
sudo nano /etc/apache2/sites-available/test.com.conf

Akan tampak

<VirtualHost *:80>
    ServerAdmin admin@test.com
    ServerName test.com
    ServerAlias www.test.com
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable Virtual Host

Enable

sudo a2ensite example.com.conf
sudo a2ensite test.com.conf

Disable

sudo a2dissite 000-default.conf

Restart

sudo systemctl restart apache2
sudo service apache2 restart

Client

Edit

sudo nano /etc/hosts

Isi dengan

127.0.0.1   localhost
127.0.1.1   guest-desktop
111.111.111.111 example.com
111.111.111.111 test.com 


Pranala Menarik