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

WSL 2

WSL 2 ติดตั้งได้บน Windows 11 หรือ Windows 10 version 2004+ (Build 19041 and higher)

การติดตั้ง WSL 2

  1. Open PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting “Run as administrator
  2. Enter the command: wsl --install
  3. You’ll need to restart your machine following this installation process to begin using WSL.

คำสั่ง wsl --install จะติดตั้ง Ubuntu distribution เป็นค่า default แต่เราสามารถติดตั้ง Linux distributions อื่นได้

ดู Linux distribution ที่มีใน Microsoft Store ใช้คำสั่ง wsl --list --online

> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_8_5                        Oracle Linux 8.5
OracleLinux_7_9                        Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4                     openSUSE Leap 15.4
openSUSE-Tumbleweed                    openSUSE Tumbleweed

เลือก Linux distribution ที่จะติดตั้งด้วยคำสั่ง wsl --install --distribution <Distribution Name>

> wsl --install Ubuntu-22.04
Installing: Ubuntu 22.04 LTS
[======                    11.0%                           ]

ถ้าจะ uninstall ใช้คำสั่ง

> wsl --unregister Ubuntu-22.04

หลังจาก WSL ติดตั้ง Linux distribution เสร็จ ให้ restart เครื่อง

เรียกใช้งาน installed Linux distribution ได้จาก Windows search แล้วค้นหาคำว่า Ubuntu หรือเปิด Windows Terminal แล้วเลือก Ubuntu

เมื่อเรียกใช้งานครั้งแรก จะให้ตั้ง username และ password.

เมื่อตั้งเสร็จ สามารถดูข้อมูลของ Linux distribution ที่ติดตั้งได้ด้วยคำสั่ง cat /etc/os-release

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.5 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.5 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Update และ upgrade packages ของ Ubuntu ด้วยคำสั่ง sudo apt update && upgrade

Visual Studio Code

ติดตั้ง WSL Extension ให้กับ Visual Studio Code

เรียก Visual Studio Code จาก command line ของ Linux (Bash) ด้วยคำสั่ง code .

File system

ถ้าอยู่ที่ Bash ใน Linux สามารถอ้างถึง drive c บน Windows ได้ที่ /mnt/c/ (mounted C drive)

ถ้าจะกลับไปที่ home directory บน Linux ได้ด้วยคำสั่ง cd ~

ถ้าอยู่ที่ PowerShell สามารถอ้างถึง home directory บน Linux ได้ด้วยคำสั่ง cd \\wsl.localhost\Ubuntu\home\<username>

ถ้าอยู่ที่ Windows File Explorer (explorer.exe) สามารถดูได้ทั้ง Windows file system (c:\) และ Linux file system (\\wsl.localhost\Ubuntu)

WSL commands

ถ้ารันจาก PowerShell หรือ Windows Command Prompt (CMD) พิมพ์คำสั่ง wsl แต่ถ้ารันจาก Bash พิมพ์คำสั่ง wsl.exe

ดูเวอร์ชันของ WSL ด้วย wsl --version

  • WSL
  • the Linux Kernel used by WSL
  • the version for WSLg, which runs Linux GUI apps
  • the version for MSRDC, which represents the Microsoft Remote Desktop Client supporting WSL
  • the versions of Direct3D and DXCore, used for graphics rendering
  • the version of Windows that you’re running
> wsl --version
WSL version: 1.1.3.0
Kernel version: 5.15.90.1
WSLg version: 1.0.49
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.1344

อัพเดท WSL ด้วย wsl --update

ดูรายการ Linux distributions ที่มีการิดตั้งด้วย wsl --list --verbose หรือ wsl -l -v

> wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         2

เปลี่ยน default Linux distribution ด้วย wsl --set-default <Distribution Name>

ที่ PowerShell เปิด Bash command line ไปที่ home directory ด้วย wsl ~ หรือ เปิด Bash command line โดยยังอยู่ที่พาทเดิมด้วย wsl และออกจาก Bash ด้วย exit

ที่ Bash เรียก Windows File Explorer ให้ชี้ไปที่ current directory path ด้วย explorer.exe . (อย่าลืมพิมพ์จุดด้วย)

คำสั่งของ Ubuntu ใน WSL2

คำสั่ง systemctl ใช้ไม่ได้

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

ให้ใช้คำสั่ง service แทน

$ sudo service apache2 status

ติดตั้ง Docker Desktop WSL 2

Prerequisites

  • อัพเดท Windows 10 ให้เป็นเวอร์ชัน 1909 (build 18363)
  • เปิด Windows Insider Program Settings แล้วเลือก Fast (Windows 10 Insider Preview build 19018 or higher).
  • ติดตั้ง WSL 2

Download

Install

  • ติดตั้ง Docker Desktop Installer.exe ที่ download มา
  • ที่ System tray ด้านล่างขวาของหน้าจอ ให้คลิกขวาที่ Docker เลือก Settings > General.
  • เลือก Enable the experimental WSL 2 based engine
  • กด Apply & Restart
  • เมื่อ Docker Desktop รีสตาร์ทเสร็จ, เลือก Settings > Resources > WSL Integration แล้วเลือก WSL 2 distributions (เช่น Ubuntu-18.04)
  • กด Apply & Restart

ตรวจสอบการติดตั้ง

ดูเวอร์ชัน

> docker --version
Docker version 19.03.5, build 633a0ea

ดูเวอร์ชันแบบละเอียด

> docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:22:37 2019
 OS/Arch:           windows/amd64
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:29:19 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Link

ติดตั้ง WSL 2

การใช้งานเบื้องต้น

Lists distributions

> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

Show detailed information about all distributions

> wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Stopped         2

> wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         2

Prerequisites

You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11.

Install WSL command

You can now install everything you need to run Windows Subsystem for Linux (WSL) by entering this command in an administrator PowerShell or Windows Command Prompt and then restarting your machine.

wsl --install

This command will enable the required optional components, download the latest Linux kernel, set WSL 2 as your default, and install a Linux distribution for you (Ubuntu by default, see below to change this).

The first time you launch a newly installed Linux distribution, a console window will open and you’ll be asked to wait for files to de-compress and be stored on your machine. All future launches should take less than a second.

Basic commands for WSL

Install a specific Linux distribution

wsl --install --distribution <Distribution Name>

Designate a distribution of Linux for installation besides the default (Ubuntu) by replacing <Distribution Name> with the name of the distribution. This command can also be entered as: wsl -d <Distribution Name>.

List available Linux distributions

> wsl --list --online
The following is a list of valid distributions that can be installed.
The default distribution is denoted by '*'.
Install using 'wsl --install -d <Distro>'.

  NAME            FRIENDLY NAME
* Ubuntu          Ubuntu
  Debian          Debian GNU/Linux
  kali-linux      Kali Linux Rolling
  openSUSE-42     openSUSE Leap 42
  SLES-12         SUSE Linux Enterprise Server v12
  Ubuntu-16.04    Ubuntu 16.04 LTS
  Ubuntu-18.04    Ubuntu 18.04 LTS
  Ubuntu-20.04    Ubuntu 20.04 LTS

See a list of the Linux distributions available through the online store. This command can also be entered as: wsl -l -o.

List installed Linux distributions

> wsl --list --verbose
  NAME      STATE           VERSION
* Ubuntu    Running         2

See a list of the Linux distributions installed on your Windows machine, including the state (whether the distribution is running or stopped) and the version of WSL running the distribution (WSL 1 or WSL 2). Comparing WSL 1 and WSL 2. This command can also be entered as: wsl -l -v. Additional options that can be used with the list command include: --all to list all distributions, --running to list only distributions that are currently running, or --quiet to only show distribution names.

Check WSL status

> wsl --status
Default Distribution: Ubuntu
Default Version: 2

Windows Subsystem for Linux was last updated on 15-Jul-22
The Windows Subsystem for Linux kernel can be manually updated with 'wsl --update', but automatic updates cannot occur due to your system settings.
To receive automatic kernel updates, please enable the Windows Update setting: 'Receive updates for other Microsoft products when you update Windows'.
For more information please visit https://aka.ms/wsl2kernel.

Kernel version: 5.10.16

See general information about your WSL configuration, such as default distribution type, default distribution, and kernel version.

Update WSL

wsl --update

Manually update the version of your WSL Linux kernel. You can also use the command: wsl --update rollback to rollback to a previous version of the WSL Linux kernel.

Shutdown

wsl --shutdown

Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine. This command may be necessary in instances that require you to restart the WSL 2 virtual machine environment, such as changing memory usage limits or making a change to your .wslconfig file.

Terminate

wsl --terminate <Distribution Name>

To terminate the specified distribution, or stop it from running, replace <Distribution Name> with the name of the targeted distribution.

Help command

> wsl --help
Copyright (c) Microsoft Corporation. All rights reserved.

Usage: wsl.exe [Argument] [Options...] [CommandLine]

Arguments for running Linux binaries:

    If no command line is provided, wsl.exe launches the default shell.

    --exec, -e <CommandLine>
        Execute the specified command without using the default Linux shell.

    --
        Pass the remaining command line as is.

Options:
    --cd <Directory>
        Sets the specified directory as the current working directory.
        If ~ is used the Linux user's home path will be used. If the path begins
        with a / character, it will be interpreted as an absolute Linux path.
        Otherwise, the value must be an absolute Windows path.

    --distribution, -d <Distro>
        Run the specified distribution.

    --user, -u <UserName>
        Run as the specified user.

    --system
        Launches a shell for the system distribution.

Arguments for managing Windows Subsystem for Linux:

    --help
        Display usage information.

    --install [Options]
        Install additional Windows Subsystem for Linux distributions.
        For a list of valid distributions, use 'wsl --list --online'.

        Options:
            --distribution, -d [Argument]
                Downloads and installs a distribution by name.

                Arguments:
                    A valid distribution name (not case sensitive).

                Examples:
                    wsl --install -d Ubuntu
                    wsl --install --distribution Debian

    --set-default-version <Version>
        Changes the default install version for new distributions.

    --shutdown
        Immediately terminates all running distributions and the WSL 2
        lightweight utility virtual machine.

    --status
        Show the status of Windows Subsystem for Linux.

    --update [Options]
        If no options are specified, the WSL 2 kernel will be updated
        to the latest version.

        Options:
            --rollback
                Revert to the previous version of the WSL 2 kernel.

Arguments for managing distributions in Windows Subsystem for Linux:

    --export <Distro> <FileName>
        Exports the distribution to a tar file.
        The filename can be - for standard output.

    --import <Distro> <InstallLocation> <FileName> [Options]
        Imports the specified tar file as a new distribution.
        The filename can be - for standard input.

        Options:
            --version <Version>
                Specifies the version to use for the new distribution.

    --list, -l [Options]
        Lists distributions.

        Options:
            --all
                List all distributions, including distributions that are
                currently being installed or uninstalled.

            --running
                List only distributions that are currently running.

            --quiet, -q
                Only show distribution names.

            --verbose, -v
                Show detailed information about all distributions.

            --online, -o
                Displays a list of available distributions for install with 'wsl --install'.

    --set-default, -s <Distro>
        Sets the distribution as the default.

    --set-version <Distro> <Version>
        Changes the version of the specified distribution.

    --terminate, -t <Distro>
        Terminates the specified distribution.

    --unregister <Distro>
        Unregisters the distribution and deletes the root filesystem.

    --mount <Disk>
        Attaches and mounts a physical disk in all WSL2 distributions.

        Options:
            --bare
                Attach the disk to WSL2, but don't mount it.

            --type <Type>
                Filesystem to use when mounting a disk, if not specified defaults to ext4.

            --options <Options>
                Additional mount options.

            --partition <Index>
                Index of the partition to mount, if not specified defaults to the whole disk.

    --unmount [Disk]
        Unmounts and detaches a disk from all WSL2 distributions.
        Unmounts and detaches all disks if called without argument.

ขั้นตอนการติดตั้ง WSL 2 สำหรับ Windows 10 ที่ Build ต่ำกว่า 19041

1.update Windows ให้เป็นเวอร์ชันล่าสุด (1909) , ตรวจสอบเวอร์ชันด้วยคำสั่ง winver

2.เปิด Windows Insider Program Settings แล้วเลือก Fast

3.เปิด Turn Windows features on or off เลือก Windows Subsystem for Linux และ Virtual Machine Platform

4.เปิด Microsoft Store เลือกติดตั้ง Ubuntu 18.04

หลังจากติดตั้งเสร็จ จะให้เราตั้ง username และ password

5.เปิด Power Shell

list ดู Distro ที่ติดตั้ง

> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu-18.04 (Default)

กำหนดให้ Ubuntu-18.04 ที่พึ่งติดตั้ง ใช้ WSL 2

> wsl --set-version Ubuntu-18.04 2

กำหนดให้ default เป็นเวอร์ชัน 2 เลย

> wsl --set-default-version 2

ตรวจสอบดูว่าเป็น version 2 แล้วหรือยัง

> wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-18.04    Stopped         2

ถ้ารันแล้วไม่เจอคำสั่ง -v แสดงว่ายังเป็น WSL 1

6.กลับมาที่ app Ubuntu18.04 แล้วลองเรียก VS Code

> wsl -d Ubuntu-18.04

หรือ

$ code .