Week6 - Selesfia/ComputerNetwork GitHub Wiki

Add Subtitle to a Video

  1. Go to Chrome Setting -> Accessibility
  2. Turn ON the live caption setting and choose your preferred language

Install MariaDB Repository on Ubuntu 22.04

Reference

MariaDB Installation Process

  1. Create 2 VM instances : "mywww", and "mymariadb". Setting for "mywww":
  • Switch to N1 and the boot disk to Ubuntu, lastly tick the option for allow HTTP.
  • Paste the following text to automation:
#! /bin/bash
 apt update
 apt -y install apache2
 cat <<EOF > /var/www/html/index.html
 <html><body><p>Linux startup script added directly. $(hostname -I) </p></body></html>

Setting for "mymariadb": Switch to N1, change the boot disk to Ubuntu 22.04

  1. Connect to SSH -> On "mymariadb"
$ sudo mkdir -p /etc/apt/keyrings
$ sudo curl -o /etc/apt/keyrings/mariadb-keyring.pgp 'https://mariadb.org/mariadb_release_signing_key.pgp'
$ sudo vim /etc/apt/sources.list.d/mariadb.sources

and paste the following text

# MariaDB 11.1 repository list - created 2023-11-08 06:16 UTC
# https://mariadb.org/download/
X-Repolib-Name: MariaDB
Types: deb
# deb.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# URIs: https://deb.mariadb.org/11.1/ubuntu
URIs: https://ftp.ubuntu-tw.org/mirror/mariadb/repo/11.1/ubuntu
Suites: jammy
Components: main main/debug
Signed-By: /etc/apt/keyrings/mariadb-keyring.pgp

esc :wq to save and exit vim

  1. Still on "mymariadb"
$ sudo apt-get update                   # 更新
$ sudo apt install mariadb-server       # 安裝 MariaDB
$ sudo systemctl status mariadb         # 安裝完服務就已經啟動了,可以再檢查一下
$ mysql_secure_installation             # 初始化 MariaDB

Initializing MariaDB will require you to answer several questions, as follows:

  • Enter current password for root (enter for none) Enter the root password. MariaDB does not have a password by default. Just press enter.
  • Switch to unix_socket authentication [Y/n] Switch to unix_socket authentication, which is a new authentication method in version 10.4.3 and is not mandatory. Enter n.
  • Change the root password? [Y/n] Modify the database root account password and enter y.
  • New password Enter the root account password to be set.
  • Re-enter new password Enter the root account password you want to set again.
  • Remove anonymous users? [Y/n] To remove an anonymous account, enter y.
  • Disallow root login remotely? [Y/n] Remote login using the root account is not allowed. Here I enter n, but for security reasons you should enter y.
  • Remove test database and access to it? [Y/n] To remove the test database and account, enter y.
  • Reload privilege tables now? [Y/n] To reload permission settings, enter y. The installation has been completed here. You can use the following command to log in and give it a try.
  1. $ sudo mysql -u root -p

Common commands

$ sudo systemctl enable mariadb         # Automatically start service at boot
$ sudo systemctl start mariadb          # Start service
$ sudo systemctl stop mariadb           # Stop service
$ sudo systemctl restart mariadb        # Restart service
$ sudo systemctl status mariadb         # Check service status

Allow External Connections

  1. Edit /etc/mysql/mariadb.conf.d/50-server.cnf, find the line bind-address = 127.0.0.1, and change it to 0.0.0.0. After modification, restart the MariaDB service (sudo systemctl restart mariadb).

  1. Allowing connections from any host(%) and the account is set to root and the password is 123456. All databases can be managed. Please refer to the following instructions to set it up.
-- 建立帳號,並授與使用所有資料庫的所有權限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

-- 更新系統權限表
FLUSH PRIVILEGES;

Create Simple Database

  1. Enter your MarisDB server
$ create database testdb;
$ use testdb;
$ create table addrbook(name varchar(50) not null, phone char(10));
$ insert into addrbook(name, phone) value ("tom", "0912123456");
$ insert into addrbook(name, phone) value ("mary", "0911234567"); 
$ select name, phone from addrbook;

Install PHP version 8.1 on Ubuntu

Reference

Add new version of PHP package library source

  1. Back to "mywww" VM
$ sudo apt install apache2-utils -y
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php

This line of message will appear in the middle, please press the "Enter" key to continue running.
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Update apt Source

$ sudo apt update

Install PHP 8.1 Related Packages

$ sudo apt install php8.1 libapache2-mod-php8.1 php8.1-gd php8.1-mysql php8.1-curl php8.1-mbstring php8.1-intl -y
$ sudo apt install php8.1-gmp php8.1-bcmath php8.1-imagick php8.1-xml php8.1-zip -y

Enable PHP Modules for Apache

$ sudo a2enmod php8.1
$ sudo systemctl restart apache2

Test PHP Version Information

  1. $ sudo vim /var/www/html/info.php
  2. Paste the following text: (esc :wq to quit and save vim)
<?php

phpinfo();

?>

  1. Open "mywww" website using the external ip and add /info.php

Connect to a MySQL database

  1. $ sudo vim /var/www/html/test.php
  2. Paste the following text:
<?php
$servername="[10.140.0.10](https://10.140.0.13/)";
$username="root";    
$password="123456";
$dbname="testdb";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error){
    die("connection failed: " . $conn->connect_error);
}
else{
    echo "connect OK!" . "<br>";
}

$sql="select name,phone from addrbook";
$result=$conn->query($sql);

if($result->num_rows>0){
    while($row=$result->fetch_assoc()){
        echo "name: " . $row["name"] . "\tphone: " . $row["phone"] . "<br>";
    }
} else {
    echo "0 record";
}
?>

  1. Open "mywww" website using the external ip and add /test.php

PS : Remember to delete the resources that you created if you don't use it anymore.

15/10/2024

⚠️ **GitHub.com Fallback** ⚠️