ติดตั้ง Java JDK บน Ubuntu 20.04

Installing Java

update packages ก่อน

$ sudo apt update

ติดตั้ง Java JDK 11 (openjdk)

$ sudo apt install default-jdk

แต่ถ้าจะติดตั้ง Java 8 ใช้คำสั่ง

sudo apt install openjdk-8-jdk

โปรแกรมจะติดตั้งอยู่ที่ /usr/lib/jvm/java-11-openjdk-amd64/bin/

    $ ls -l /usr/lib/jvm/java-11-openjdk-amd64/bin/java*
    -rwxr-xr-x 1 root root 14560 ม.ค.  20 16:07 /usr/lib/jvm/java-11-openjdk-amd64/bin/java
    -rwxr-xr-x 1 root root 14608 ม.ค.  20 16:07 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac
    -rwxr-xr-x 1 root root 14608 ม.ค.  20 16:07 /usr/lib/jvm/java-11-openjdk-amd64/bin/javadoc
    -rwxr-xr-x 1 root root 14576 ม.ค.  20 16:07 /usr/lib/jvm/java-11-openjdk-amd64/bin/javap

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

    $ java --version
    openjdk 11.0.18 2023-01-17
    OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1)
    OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1, mixed mode, sharing)
    $ javac --version
    javac 11.0.18

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

    $ java --version
    openjdk 11.0.18 2023-01-17
    OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu122.04)
    OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
    $ javac --version
    javac 11.0.18

    Managing Java

    ใช้คำสั่ง update-alternatives

    $ update-alternatives --help
    Usage: update-alternatives [<option> ...] <command>
    
    Commands:
      --install <link> <name> <path> <priority>
        [--slave <link> <name> <path>] ...
                               add a group of alternatives to the system.
      --remove <name> <path>   remove <path> from the <name> group alternative.
      --remove-all <name>      remove <name> group from the alternatives system.
      --auto <name>            switch the master link <name> to automatic mode.
      --display <name>         display information about the <name> group.
      --query <name>           machine parseable version of --display <name>.
      --list <name>            display all targets of the <name> group.
      --get-selections         list master alternative names and their status.
      --set-selections         read alternative status from standard input.
      --config <name>          show alternatives for the <name> group and ask the
                               user to select which one to use.
      --set <name> <path>      set <path> as alternative for <name>.
      --all                    call --config on all alternatives.
    
    <link> is the symlink pointing to /etc/alternatives/<name>.
      (e.g. /usr/bin/pager)
    <name> is the master name for this link group.
      (e.g. pager)
    <path> is the location of one of the alternative target files.
      (e.g. /usr/bin/less)
    <priority> is an integer; options with higher numbers have higher priority in
      automatic mode.
    
    Options:
      --altdir <directory>     change the alternatives directory.
      --admindir <directory>   change the administrative directory.
      --log <file>             change the log file.
      --force                  allow replacing files with alternative links.
      --skip-auto              skip prompt for alternatives correctly configured
                               in automatic mode (relevant for --config only)
      --quiet                  quiet operation, minimal output.
      --verbose                verbose operation, more output.
      --debug                  debug output, way more output.
      --help                   show this help message.
      --version                show the version.

    You can have multiple Java installations on one server. You can configure which version is the default for use on the command line by using the update-alternatives command.

    $ sudo update-alternatives --config java

    ถ้ามี java ตัวเดียวก็จะขึ้นประมาณนี้

    $ sudo update-alternatives --config java
    There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
    Nothing to configure.

    แต่ถ้ามี java หลายตัว ก็จะแสดงให้เราเลือก

    javac ก็เหมือนกัน ใช้คำสั่ง

    $ sudo update-alternatives --config javac

    Setting the JAVA_HOME

    $ sudo nano /etc/environment

    At the end of this file, add the following line, and to not include the bin/ portion of the path: (หา path ได้ด้วยคำสั่ง update-alternatives)

    JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
    JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

    Modifying this file will set the JAVA_HOME path for all users on your system.

    Save the file and exit the editor.

    Now reload this file to apply the changes to your current session:

    $ source /etc/environment

    Verify that the environment variable is set:

    $ echo $JAVA_HOME

    Link

    Self-Signed SSL Certificate for Apache in Ubuntu 20.04

    ติดตั้ง Apache

    sudo apt update
    sudo apt install apache2

    Enabling mod_ssl

    Before we can use any SSL certificates, we first have to enable mod_ssl, an Apache module that provides support for SSL encryption.

    Enable mod_ssl with the a2enmod command:

    $ sudo a2enmod ssl
    [sudo] password for jack:
    Considering dependency setenvif for ssl:
    Module setenvif already enabled
    Considering dependency mime for ssl:
    Module mime already enabled
    Considering dependency socache_shmcb for ssl:
    Enabling module socache_shmcb.
    Enabling module ssl.
    See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.
    To activate the new configuration, you need to run:
      systemctl restart apache2

    Restart Apache to activate the module:

    sudo systemctl restart apache2

    Creating the SSL Certificate

    Now that Apache is ready to use encryption, we can move on to generating a new SSL certificate. The certificate will store some basic information about your site, and will be accompanied by a key file that allows the server to securely handle encrypted data.

    We can create the SSL key and certificate files with the openssl command:

    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
         -keyout /etc/ssl/private/apache-selfsigned.key \
         -out /etc/ssl/certs/apache-selfsigned.crt

    private key ที่ได้จะอยู่ที่ /etc/ssl/private/apache-selfsigned.key

    Configuring Apache to Use SSL

    ที่ไฟล์ 000-default.conf เพิ่ม VirtualHost *:443

    $ sudo nano /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>
    
    <VirtualHost *:443>
       DocumentRoot /var/www/html
    
       SSLEngine on
       SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
       SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
    </VirtualHost>

    Next, let’s test for configuration errors:

    sudo apache2ctl configtest

    Let reload apache2

    sudo systemctl reload apache2

    ทดลองเรียกไปที่ https://localhost

    Redirecting HTTP to HTTPS

    $ sudo nano /etc/apache2/sites-available/000-default.conf

    To use Redirect to match any requests and send them to the SSL VirtualHost. Make sure to include the trailing slash:

    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            Redirect / https://jack5.com/
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
    <VirtualHost *:443>
       DocumentRoot /var/www/html
    
       SSLEngine on
       SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
       SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
    </VirtualHost>

    ตอนนี้พอเรียกไปที่ http://localhost ก็จะ redirect ไปที่ https://localhost ละ

    Install Mautic on Ubuntu 20.04

    ก่อนติดตั้ง Mautic ต้องมี

    • Apache2
    • Mysql

    Step 1: Download Mautic onto Your Ubuntu 20.04 Server

    ดู release ของ Mautic ที่ github.com/mautic/mautic/releases/

    Download the latest stable version by executing the following command on your server.

    wget https://github.com/mautic/mautic/releases/download/4.2.1/4.2.1-update.zip
    wget https://github.com/mautic/mautic/releases/download/4.4.7/4.4.7-update.zip

    Install the unzip utility and unzip it to /var/www/mautic/ directory.

    sudo apt install unzip
    
    sudo mkdir -p /var/www/mautic/
    
    sudo unzip 4.4.7-update.zip -d /var/www/mautic/

    Then make the web server user (www-data) as the owner of this directory.

    sudo chown -R www-data:www-data /var/www/mautic/

    Step 2: Create a MariaDB Database and User for Mautic

    Log in to MariaDB console.

    sudo mysql

    Next, create a new database for Mautic using the following command. This tutorial names it mautic, you can use whatever name you like for the database.

    CREATE DATABASE mautic DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

    จะมี warning ประมาณนี้

    CREATE DATABASE mautic DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci	1 row(s) affected, 2 warning(s):
     3719 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
     3778 'utf8mb3_unicode_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.

    ให้เปลี่ยนเป็นใช้คำสั่งนี้แทน

    CREATE DATABASE mautic DEFAULT CHARACTER SET UTF8MB4 COLLATE utf8mb4_unicode_ci;

    The following command will create a database user and password, and at the same time grant all permission of the new database to the new user so later on Mautic can write to the database. Replace red texts with your preferred database name, username and password.

    CREATE USER 'jack'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL ON mautic.* TO 'jack'@'localhost' IDENTIFIED BY 'password';

    Flush privileges table and exit MariaDB console.

    FLUSH PRIVILEGES;
    
    EXIT;

    Step 3: Install Required and Recommended PHP Modules.

    Run the following command to install PHP modules required or recommended by Mautic

    sudo apt install php-imagick php7.4-fpm php7.4-mysql php7.4-common php7.4-gd php7.4-imap php7.4-imap php7.4-json php7.4-curl php7.4-zip php7.4-xml php7.4-mbstring php7.4-bz2 php7.4-intl php7.4-gmp

    เราใช้ PHP-FPM แทน PHP – PHP: FastCGI Process Manager (FPM) – Manual

    If you use Apache web server, then you need to disable the PHP module for Apache.

    sudo a2dismod php7.4

    You also need to disable the prefork MPM module in Apache.

    sudo a2dismod mpm_prefork

    Now you need to run the following command to enable three modules in order to use PHP-FPM in Apache, regardless of whether mod_php is installed on your server.

    sudo a2enmod mpm_event proxy_fcgi setenvif

    Then restart Apache.

    sudo systemctl restart apache2

    Step 4: Create Apache Virtual Host for Mautic

    If you use Apache web server, create a virtual host for Mautic.

    sudo nano /etc/apache2/sites-available/mautic.conf

    Put the following text into the file. Replace mautic.example.com with your real domain name and don’t forget to set DNS A record for it.

    <VirtualHost *:80>
      ServerName mautic.example.com
      DocumentRoot /var/www/mautic/
    
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
    
      <Directory />
        Options FollowSymLinks
        AllowOverride All
      </Directory>
    
      <Directory /var/www/mautic/>
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
      </Directory>
    
    </VirtualHost>

    Save and close the file. Then enable this virtual host with:

    sudo a2ensite mautic.conf

    enable PHP-FPM – How to Install PHP-FPM with Apache on Ubuntu 20.04 (cloudbooklet.com)

    sudo a2enconf php7.4-fpm

    สามารถทดสอบ PHP-FPM ได้ด้วย

    <?php phpinfo(); ?>

    Reload Apache for the changes to take effect.

    sudo systemctl reload apache2

    แก้ไฟล์ /etc/hosts

    sudo nano /etc/hosts
    127.0.0.1       localhost
    127.0.1.1       jack5.com       jack5
    192.168.1.142   mautic.example.com
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters

    ถ้าจะเรียกจาก Windows ให้แก้ไขไฟล์ C:\Windows\System32\drivers\etc\hosts

    192.168.1.142        jack5.com
    192.168.1.142        mautic.example.com

    Now you should be able to see the Mautic web-based install wizard at http://mautic.example.com/installer.

    แก้ไขไฟล์ php.ini ของ fpm ที่ /etc/php/7.4/fpm/php.ini
    (ของ php ที่ /etc/php/7.4/apache2/php.ini ไม่ต้องแก้)

    $ sudo nano /etc/php/7.4/fpm/php.ini
    date.timezone = Asia/Bangkok
    ...
    memory_limit = 512M

    เสร็จแล้ว reload php7.4-fpm

    sudo systemctl reload php7.4-fpm

    Step 5: Enabling HTTPS

    sudo a2enmod ssl
    sudo systemctl restart apache2
    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
    sudo nano /etc/apache2/sites-available/mautic.conf
    <VirtualHost *:443>
      ServerName mautic.example.com
      DocumentRoot /var/www/mautic/
    
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
    
      SSLEngine on
      SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
      SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
    
      <Directory />
        Options FollowSymLinks
        AllowOverride All
      </Directory>
    
      <Directory /var/www/mautic/>
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
      </Directory>
    
    </VirtualHost>
    
    <VirtualHost *:80>
       ServerName mautic.example.com
       Redirect / https://mautic.example.com
    </VirtualHost>
    sudo a2ensite mautic.conf
    sudo apache2ctl configtest
    sudo systemctl reload apache2

    Step 6: Finish Mautic Installation in Web Browser

    1. ใส่ข้อมูลดาต้าเบส
    2. ใส่ข้อมูล admin
    3. ใส่ข้อมูล SMTP Server , เราใช้ sendgrid.com แบบ API Key

    ติดตั้ง เสร็จแล้ว login ไม่ได้ ฟ้องว่า

    Unable to resolve binding type, invalid or unsupported http request

    ให้แก้ไขด้วยคำสั่ง – Mautic 3. Can not login for first time – Support / Mautic 3 – Install/Upgrade Support – Mautic Community Forums

    sudo a2enmod rewrite
    sudo systemctl reload apache2

    เมื่อ login สำเร็จจะได้หน้านี้

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

    ติดตั้ง MySQL

    sudo apt update
    sudo apt install mysql-server

    ตรวจสอบสถานะ

    systemctl status mysql

    ลองใช้งาน

    $ sudo mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)
    
    Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>

    สร้าง user

    CREATE USER 'jack'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON * . * TO 'jack'@'localhost';
    FLUSH PRIVILEGES;
    SELECT user,authentication_string,plugin,host FROM mysql.user;

    ทดสอบที่ local จะเข้าได้ละ

    $ sudo mysql -u jack  -p

    เข้าจากเครื่องอื่น

    1 Access mysqld.cnf File

    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

    2 Change Bind-Address IP

    You now have access to the MySQL server configuration file. Scroll down to the bind-address line and change the IP address. The current default IP is set to 127.0.0.1. This IP limits MySQL connections to the local machine.

    The new IP should match the address of the machine that needs to access the MySQL server remotely. For example, if you bind MySQL to 0.0.0.0, then any machine that reaches the MySQL server can also connect with it.

    bind-address            = 0.0.0.0
    sudo systemctl restart mysql

    สร้าง remote user โดย remote ไปจากเครื่อง 192.168.1.124

    CREATE USER 'jack'@'192.168.1.124' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON * . * TO 'jack'@'192.168.1.124';
    FLUSH PRIVILEGES;

    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

    Hostname (computer name) ของ Ubuntu 20.04

    ดู Hostname

    ดู Hostname ด้วยคำสั่ง hostname

    $ hostname
    jack5

    ดู Hostname ด้วยคำสั่ง hostnamectl

    $ hostnamectl
       Static hostname: jack5
             Icon name: computer-vm
               Chassis: vm
            Machine ID: d38e601ed3104707bc3b33c7058533e0
               Boot ID: afa54862c8be4726a7b75fc1e24ec4d1
        Virtualization: oracle
      Operating System: Ubuntu 20.04.6 LTS
                Kernel: Linux 5.15.0-67-generic
          Architecture: x86-64

    ดู Hostname ที่ไฟล์ /etc/hostname

    $ cat /etc/hostname
    jack5

    ดู Hostname ที่ไฟล์ /etc/hosts

    $ cat /etc/hosts
    127.0.0.1       localhost
    127.0.1.1       jack5.com       jack5
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters

    ลอง ping jack5 จะได้ IP 127.0.1.1

    $ ping jack5
    PING jack5.com (127.0.1.1) 56(84) bytes of data.
    64 bytes from jack5.com (127.0.1.1): icmp_seq=1 ttl=64 time=0.018 ms
    64 bytes from jack5.com (127.0.1.1): icmp_seq=2 ttl=64 time=0.028 ms
    64 bytes from jack5.com (127.0.1.1): icmp_seq=3 ttl=64 time=0.031 ms
    $ ping jack5.com
    PING jack5.com (127.0.1.1) 56(84) bytes of data.
    64 bytes from jack5.com (127.0.1.1): icmp_seq=1 ttl=64 time=0.019 ms
    64 bytes from jack5.com (127.0.1.1): icmp_seq=2 ttl=64 time=0.027 ms
    64 bytes from jack5.com (127.0.1.1): icmp_seq=3 ttl=64 time=0.027 ms

    ลอง ping localhost จะได้ IP 127.0.0.1

    $ ping localhost
    PING localhost (127.0.0.1) 56(84) bytes of data.
    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.587 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.025 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.025 ms

    เปลี่ยน Hostname

    แก้ไขไฟล์ /etc/hostname และไฟล์ /etc/hosts

    sudo nano /etc/hostname
    sudo nano /etc/hosts

    reboot เครื่อง

    sudo reboot

    command hostname

    ดูชื่อเครื่องด้วย hostname

    $ hostname

    ดู help

    $ hostname --help
    Usage: hostname [-b] {hostname|-F file}         set host name (from file)
           hostname [-a|-A|-d|-f|-i|-I|-s|-y]       display formatted name
           hostname                                 display host name
    
           {yp,nis,}domainname {nisdomain|-F file}  set NIS domain name (from file)
           {yp,nis,}domainname                      display NIS domain name
    
           dnsdomainname                            display dns domain name
    
           hostname -V|--version|-h|--help          print info and exit
    
    Program name:
           {yp,nis,}domainname=hostname -y
           dnsdomainname=hostname -d
    
    Program options:
        -a, --alias            alias names
        -A, --all-fqdns        all long host names (FQDNs)
        -b, --boot             set default hostname if none available
        -d, --domain           DNS domain name
        -f, --fqdn, --long     long host name (FQDN)
        -F, --file             read host name or NIS domain name from given file
        -i, --ip-address       addresses for the host name
        -I, --all-ip-addresses all addresses for the host
        -s, --short            short host name
        -y, --yp, --nis        NIS/YP domain name
    
    Description:
       This command can get or set the host name or the NIS domain name. You can
       also get the DNS domain or the FQDN (fully qualified domain name).
       Unless you are using bind or NIS for host lookups you can change the
       FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
       part of the FQDN) in the /etc/hosts file.

    set hostname เป็น example.org

    $ sudo hostname example.org
    $ hostname
    example.org

    ดู IP ของเครื่องด้วย hostname -I

    $ hostname -I
    10.0.2.15 192.168.1.142

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

    Ubuntu Package Management

    Apt

    The apt command is a powerful command-line tool, which works with Ubuntu’s Advanced Packaging Tool (APT) performing such functions as installation of new software packages, upgrade of existing software packages, updating of the package list index, and even upgrading the entire Ubuntu system.

    Some examples of popular uses for the apt utility:

    Install a Package: Installation of packages using the apt tool is quite simple. For example, to install the nmap network scanner, type the following:

    sudo apt install nmap

    Remove a Package: Removal of a package (or packages) is also straightforward. To remove the package installed in the previous example, type the following:

    sudo apt remove nmap

    auto remove package

    sudo apt autoremove

    คำสั่งพื้นฐาน Ubuntu 20.04

    ที่ command line เลือก default editor ใช้คำสั่ง

    $ select-editor
    
    Select an editor.  To change later, run 'select-editor'.
      1. /bin/nano        <---- easiest
      2. /usr/bin/vim.tiny
      3. /bin/ed
    
    Choose 1-3 [1]:

    คำสั่ง crontab

    • crontab filename การนำเอาคำสั่ง crontab เข้ามาจากไฟล์อื่น
    • crontab -l ดูคำสั่ง crontab ทั้งหมดที่มีอยู่
    • crontab -e แก้ไข crontab ปัจจุบัน
    • crontab -r ลบคำสั่ง crontab ที่มีทั้งหมด

      รูปแบบของคำสั่ง crontab มีทั้งหมด 6 fields

      • 1 = minute มีค่า 0 – 59 เวลาเป็นนาที จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงนาทีที่กำหนด
      • 2 = hour มีค่า 0 – 23 เวลาเป็นชั่วโมง จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงชั่วโมงที่กำหนด
      • 3 = day มีค่า 1 – 31 เวลาเป็นวัน จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงวันที่กำหนด
      • 4 = month มีค่า 1 – 12 เวลาเป็นเดือน จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงเดือนที่กำหนด
      • 5 = weekday มีค่า 0 – 6 วันขะงแต่ละสัปดาห์ มีค่าดังนี้ (อาทิตย์ = 0, จันทร์ = 1, อังคาร = 2, พุธ = 3, พฤหัส = 4, ศุกร์ = 5 ,เสาร์ = 6 )
      • 6 = command คำสั่ง เราสามารถกำหนดคำสั่งได้มากมาย รวมทั้ง script ต่าง ๆ ตามที่เราต้องการ
      * * * * * command to be executed
      - - - - -
      | | | | |
      | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
      | | | ------- Month (1 - 12)
      | | --------- Day of month (1 - 31)
      | ----------- Hour (0 - 23)
      ------------- Minute (0 - 59)

      เสร็จแล้วก็ restart service

      $ systemctl status cron
      $ sudo systemctl stop cron
      $ sudo systemctl start cron
      $ sudo service cron status

      Package

      sudo apt install unzip