內容選單標籤

2026年4月24日 星期五

Ubuntu24.04 練習

  date
Fri Apr 24 08:13:46 AM UTC 2026

sudo timedatectl set-timezone Asia/Taipei

 date
Fri Apr 24 04:16:53 PM CST 2026

--------------------------------------------------------------------

 


Ubuntu24.04 for Apache2+php+MariaDB

 Windows11 + VMware +Ubuntu24.04 Server(192.168.209.131/24)

-------------------------------------------------------------------

#系統安裝後,更新
sudo apt update
sudo apt upgrade

#安裝ssh
sudo systemctl status ssh
apt list --installed | grep ssh
sudo apt install openssh-server
sudo systemctl status ssh
sudo systemctl start ssh
sudo systemctl enable ssh
ss -tunlp

#防火牆放行ssh
sudo ufw status
sudo ufw enable
sudo ufw allow ssh
sudo ufw status

#PuTTY安裝設定
左上圖示  Change Setting  Window  Apperance  Font Setting  Change  大小:18
選取視窗中文字,滑鼠左鍵一下,即將內容複製到剪貼簿中

#安裝Apache
sudo apt install apache2
apt list --installed | grep apache2
systemctl status apache2
sudo ufw allow "Apache Full"   &&HTTP + HTTPS
sudo ufw status
ss -tunpl
http://192.168.209.131/

#安裝php
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml -y
apt list --installed | grep php
php -v
sudo systemctl restart apache2
#測試php環境
echo "<?php phpinfo();?>" | sudo tee /var/www/html/info.php
cat /var/www/html/info.php
http://192.168.209.131/info.php

#安裝MariaDB
sudo apt install mariadb-server -y
sudo systemctl status mariadb
#執行安全安裝腳本
sudo mysql_secure_installation

Change the root password? [Y/n] y
New password:db123456
Re-enter new password:db123456

#建立資料庫與使用者
sudo mariadb
MariaDB [(none)]> create database Dbkk;
MariaDB [(none)]> create user "kk"@"localhost" identified by "db123456";
MariaDB [Dbkk]> grant all privileges on Dbkk.* to "kk"@"localhost";
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit
#建立資料表
sudo mariadb -u kk -p
Enter password:db123456
MariaDB [(none)]> show databases;
MariaDB [(none)]> use Dbkk;
MariaDB [Dbkk]> create table TabCla(Idno int auto_increment primary key,Na varchar(100),Acnt varchar(100));
MariaDB [Dbkk]> show tables;
MariaDB [Dbkk]> describe TabCla;
#新增一筆資料
MariaDB [Dbkk]> insert into TabCla(Na,Acnt) values("測試員01","test01");
MariaDB [Dbkk]> select * from TabCla;
#使用者家目錄建立web資料夾,軟連結到Apache目錄
mkdir -p  /home/kk/web
sudo ln -s /home/kk/web /var/www/html/web
ls -al /home/kk

drwxrwxr-x 2 kk   kk   4096 Apr 24 06:11 web

ls -al /var/www/html

lrwxrwxrwx 1 root root    12 Apr 24 06:12 web -> /home/kk/web

#確保Apache有權限讀取家目錄
chmod 755 /home/kk
chmod 755 /home/kk/web

ls -al /home/kk

drwxr-xr-x 2 kk   kk   4096 Apr 24 06:11 web

ls -al /var/www/html

lrwxrwxrwx 1 root root    12 Apr 24 06:12 web -> /home/kk/web

#測試
nano web/01.html
<H1>TEST!</H1>
http://192.168.209.131/web/01.html

#建立PHP

#連線資料庫
nano web/db.php
<?php
$host = 'localhost';
$dbname = 'Dbkk';
$user = 'kk'; // 建議使用您之前建立的特定帳號
$pass = 'db123456';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("連線失敗: " . $e->getMessage());
}
?>


#瀏覽所有資料
nano web/ListCla.php
<?php
include 'db.php';
$stmt = $pdo->query("SELECT * FROM TabCla");
echo "<h1>資料列表</h1><table border='1'><tr><th>ID</th><th>姓名</th><th>帳號</th><th>操作</th></tr>";
while ($row = $stmt->fetch()) {
    echo "<tr>
            <td>{$row['Idno']}</td>
            <td>{$row['Na']}</td>
            <td>{$row['Acnt']}</td>
            <td>
                <a href='ModiCla.php?id={$row['Idno']}'>修改</a> |
                <a href='delCla.php?id={$row['Idno']}'>刪除</a>
            </td>
          </tr>";
}
echo "</table><br><a href='addCla.php'>新增資料</a>";
?>

 

#新增功能
nano web/addCla.php
<?php
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $stmt = $pdo->prepare("INSERT INTO TabCla (Na, Acnt) VALUES (?, ?)");
    $stmt->execute([$_POST['Na'], $_POST['Acnt']]);
    header("Location: ListCla.php");
}
?>
<form method="post">
    姓名: <input type="text" name="Na" required><br>
    帳號: <input type="text" name="Acnt" required><br>
    <button type="submit">送出新增</button>
</form>

  

#刪除功能
nano web/delCla.php
<?php
include 'db.php';
if (isset($_GET['id'])) {
    $stmt = $pdo->prepare("DELETE FROM TabCla WHERE Idno = ?");
    $stmt->execute([$_GET['id']]);
}
header("Location: ListCla.php");
?>

  

#查詢與修改功能 
nano web/ModiCla.php
<?php
include 'db.php';
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM TabCla WHERE Idno = ?");
$stmt->execute([$id]);
$row = $stmt->fetch();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $upd = $pdo->prepare("UPDATE TabCla SET Na = ?, Acnt = ? WHERE Idno = ?");
    $upd->execute([$_POST['Na'], $_POST['Acnt'], $id]);
    header("Location: ListCla.php");
}
?>
<form method="post">
    姓名: <input type="text" name="Na" value="<?=$row['Na']?>"><br>
    帳號: <input type="text" name="Acnt" value="<?=$row['Acnt']?>"><br>
    <button type="submit">確認修改</button>
</form>


http://192.168.209.131/web/ListCla.php