Apache Virtual Hosts on Ubuntu 20.04

Step 1 — Creating the Directory Structure

sudo mkdir -p /var/www/your_domain_1/public_html
sudo mkdir -p /var/www/your_domain_2/public_html

Step 2 — Granting Permissions

sudo chown -R $USER:$USER /var/www/your_domain_1/public_html
sudo chown -R $USER:$USER /var/www/your_domain_2/public_html

หรือ

sudo chown -R www-data:www-data your_domain_1/public_html
sudo chown -R www-data:www-data your_domain_2/public_html
sudo chmod -R 755 /var/www

Step 3 — Creating Default Pages for Each Virtual Host

nano /var/www/your_domain_1/public_html/index.html
<html>
  <head>
    <title>Welcome to your_domain_1!</title>
  </head>
  <body>
    <h1>Success! The your_domain_1 virtual host is working!</h1>
  </body>
</html>
nano /var/www/your_domain_2/public_html/index.html
<html>
  <head>
    <title>Welcome to your_domain_2!</title>
  </head>
  <body>
    <h1>Success! The your_domain_2 virtual host is working!</h1>
  </body>
</html>

Step 4 — Creating New Virtual Host Files

sudo nano /etc/apache2/sites-available/your_domain_1.conf
<VirtualHost *:80>
        ServerAdmin admin@your_domain_1
        ServerName your_domain_1
        ServerAlias www.your_domain_1
        DocumentRoot /var/www/your_domain_1/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo nano /etc/apache2/sites-available/your_domain_2.conf
<VirtualHost *:80>
        ServerAdmin admin@your_domain_2
        ServerName your_domain_2
        ServerAlias www.your_domain_2
        DocumentRoot /var/www/your_domain_2/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 5 — Enabling the New Virtual Host Files

sudo a2ensite your_domain_1.conf
sudo a2ensite your_domain_2.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
sudo systemctl status apache2

Step 6 — (Optional) Setting Up Local Hosts File

sudo nano /etc/hosts
%windir%\system32\drivers\etc\hosts
127.0.0.1   localhost
127.0.1.1   guest-desktop
your_server_IP your_domain_1
your_server_IP your_domain_2

Step 7 — Testing Your Results

http://your_domain_1
http://your_domain_2

ติดตั้ง Apache บน Ubuntu 20.04

Step 1 — Installing Apache

sudo apt update
sudo apt install apache2

Step 2 — Adjusting the Firewall

อันนี้ข้ามได้

$ sudo ufw app list
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

As indicated by the output, there are three profiles available for Apache:

  • Apache: This profile opens only port 80 (normal, unencrypted web traffic)
  • Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic on port 80:

$ sudo ufw allow 'Apache'
Rules updated
Rules updated (v6)

You can verify the change by typing:

$ sudo ufw status
Status: inactive

ถ้าเป็น inactive ก็ทำการ enable ufwBasic UFW (Uncomplicated Firewall) commands – Serverspace.io

sudo ufw enable

แต่ถ้าใช้ Ubuntu บน WSL2 จะใช้ ufw ไม่ได้ – security – I can’t use ufw on WSL-Ubuntu – Ask Ubuntu

Step 3 — Checking your Web Server

$ systemctl status apache2

แต่ถ้ารันคำสั่งนี้ใน WSL2 จะไม่ได้

$ systemctl status apache2
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

ถ้าใช้ Ubuntu 20.04 ใน WSL2 ใช้คำสั่ง

$ service apache2 status
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-03-27 14:49:22 +07; 1min 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 4834 (apache2)
      Tasks: 55 (limit: 9444)
     Memory: 5.0M
     CGroup: /system.slice/apache2.service
             ├─4834 /usr/sbin/apache2 -k start
             ├─4835 /usr/sbin/apache2 -k start
             └─4836 /usr/sbin/apache2 -k start
$ sudo service apache2 reload
$ sudo service apache2 restart

ทดลองเข้าใช้งานที่ http://localhost/

หรือใช้คำสั่ง hostname เพื่อดู IP ของเครื่อง แล้วเข้าด้วย IP ได้

$ hostname -I

Step 4 — Other

ตรวจสอบไฟล์ /etc/apache2/sites-available/000-default.conf

$ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

APACHE_LOG_DIR ค่า default เป็น /var/log/apache2What is the Apache error log location on Linux (xmodulo.com)

$ ls -l /var/log/apache2/
total 36
-rw-r----- 1 root adm 16024 Mar 16 13:42 access.log
-rw-r----- 1 root adm 16683 Mar 16 13:42 error.log
-rw-r----- 1 root adm     0 Mar 14 16:42 other_vhosts_access.log