最後更新日期:2024 年 10 月 21 日

跟大家分享我的操作步驟。

Linux 伺服器基本資料設定

時區:Asia/Taipei

主機名稱:oak

操作人員帳號:david

apt update && apt upgrade

timedatectl list-timezones

timedatectl set-timezone 'Asia/Taipei'

date

hostnamectl set-hostname oak

adduser david

adduser david sudo

Apache 安裝設定

sudo apt update

sudo apt install apache2

sudo systemctl status apache2

hostname -I

curl http://<IP_ADDR>

// stop your web server
sudo systemctl stop apache2

sudo systemctl start apache2

sudo systemctl restart apache2

// If you are simply making configuration changes, Apache can often reload without dropping connections.
sudo systemctl reload apache2

//By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by running:
sudo systemctl disable apache2

// To re-enable the service to start up at boot, run:
sudo systemctl enable apache2


預設的網頁檔案根目錄是 /var/www/html

MySQL 安裝設定

sudo apt install mysql-server

sudo mysql -u root

CREATE DATABASE webdata;
CREATE USER 'webuser' IDENTIFIED BY 'password';
GRANT ALL ON webdata.* TO 'webuser';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'password';

PHP 安裝設定

sudo apt install php libapache2-mod-php php-mysql

sudo apt install php-curl php-json php-cgi

sudo apt install php-gd php-mbstring php-xml php-xmlrpc

sudo apt install php-bcmath php-zip php-pdo php-common php-tokenizer 
# composer create-project 需要 zip

驗證 LAMP 是否成功運作

/var/www/php.index

<html>
<head>
    <title>PHP Test</title>
</head>
    <body>
    <?php echo '<p>Welcome to the Site!</p>';

    // When running this script on a local database, the servername must be 'localhost'. Use the name and password of the web user account created earlier. Do not use the root password.
    $servername = "localhost";
    $username = "webuser";
    $password = "password";

    // Create MySQL connection
    $conn = mysqli_connect($servername, $username, $password);

    // If the conn variable is empty, the connection has failed. The output for the failure case includes the error message
    if (!$conn) {
        die('<p>Connection failed: </p>' . mysqli_connect_error());
    }
    echo '<p>Connected successfully</p>';
    ?>
</body>
</html>

安裝 composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"


sudo mv composer.phar /usr/local/bin/composer

參考資料

Guides – Setting Up and Securing a Compute Instance

How To Install the Apache Web Server on Ubuntu 22.04

Install a LAMP Stack on Ubuntu 22.04

Download Composer

Last modified: 2024-10-21

Author

Comments

Write a Reply or Comment

Your email address will not be published.