ติดตั้ง 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