本教程將向您展示如何在 Ubuntu 22.04 LTS 上使用 LEMP 堆棧安裝 Moodle。對於那些不知道的人,Moodle 是一個用 PHP 編寫的免費開源學習管理系統和 CMS。 Moodle 允許您創建功能齊全的教育和培訓課程網站。這是一種完全在線的、混合的、面對面的課堂體驗。 Moodle 平台是高度可定制的,並採用模塊化的功能方法,因此可以擴展和適應您的需求。 Moodle 對於世界各地的遠程學習機構向其學習者提供培訓材料特別有用。

本文假定您至少具備 Linux 的基本知識,知道如何使用 shell,最重要的是,您將網站託管在自己的 VPS 上。 假設您在 root 帳戶下運行,安裝非常簡單。sudo‘ 到獲得 root 權限的命令。 我將逐步向您展示如何在 Ubuntu 22.04 (Jammy Jellyfish) 上使用 LEMP 堆棧安裝 Moodle。 Ubuntu 22.04 和其他基於 Debian 的發行版(例如 Linux Mint、Elementary OS、Pop!_OS)可以遵循相同的步驟。

先決條件

  • 運行以下任何操作系統的服務器:Ubuntu 22.04、20.04 和其他基於 Debian 的發行版,例如 Linux Mint。
  • 我們建議使用全新的操作系統安裝來防止潛在問題。
  • 通過 SSH 訪問您的服務器(如果您使用的是桌面,則打開一個終端)。
  • 1個 non-root sudo user或訪問 root user我們建議您充當 non-root sudo user但是,如果您在以 root 身份運行時不小心,可能會損壞您的系統。

在 Ubuntu 22.04 LTS Jammy Jellyfish 上使用 LEMP 堆棧安裝 Moodle

步驟 1. 首先,運行命令以確保所有系統包都是最新的。 apt 終端中的命令。

sudo apt update
sudo apt upgrade
sudo apt install software-properties-common dirmngr

步驟 2. 安裝 LEMP 堆棧。

在開始本教程之前,您需要在您的服務器上安裝 LEMP 服務器。 如果您沒有安裝 LEMP 堆棧,請按照本指南進行操作。

步驟 3. 在 Ubuntu 22.04 上安裝 Moodle。

Moodle 在 Ubuntu 22.04 基本存儲庫中默認不可用。通過運行以下命令將最新穩定版本的 Moodle 下載到您的 Ubuntu 系統:

cd /var/www/html
git clone -b MOODLE_400_STABLE git://git.moodle.org/moodle.git moodle

我需要更改一些文件夾權限。

mkdir -p /var/www/html/moodledata
chown -R www-data:www-data /var/www/html/moodle
chmod -R 755 /var/www/html/*
chown www-data:www-data /var/www/html/moodledata

第 4 步。為 Moodle 配置 MariaDB

MariaDB 默認沒有加固。你可以使用它 mysql_secure_installation 腳本。 您應該仔細閱讀以下每個步驟以設置 root 密碼、刪除匿名用戶、禁止遠程 root 登錄、刪除測試數據庫以及訪問安全的 MariaDB。

mysql_secure_installation

配置如下:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

接下來,我們需要登錄到 MariaDB 控制台並為 Moodle 創建一個數據庫。運行以下命令:

mysql -u root -p

登錄到您的數據庫服務器後,您需要為您的 Moodle 安裝創建一個數據庫。

MariaDB [(none)]> CREATE DATABASE moodle_db;
MariaDB [(none)]> CREATE USER 'moodle_user'@'localhost' IDENTIFIED BY 'your-strong-password';
MariaDB [(none)]> GRANT ALL ON moodle_db.* TO 'moodle_user'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

步驟 6. 為 Moodle 配置 Nginx 虛擬主機。

接下來,創建一個 Nginx 虛擬主機配置文件。

nano /etc/nginx/conf.d/wordpress.conf

添加以下文件。

server {
    listen 80;
    root /var/www/html/moodle;
    index  index.php index.html index.htm;
    server_name  your-domian.com;

    client_max_body_size 500M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
	
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }	

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }	

    location ~ .php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

保存並關閉 現在重新啟動 Nginx Web 服務器以使更改生效。

nginx -t
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

第 6 步。在 Moodle 中啟用安全 HTTPS。

一、安裝 證書機器人 在 Ubuntu 22.04 上使用以下命令:

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

安裝 Certbot 後,使用以下命令為 Nginx 設置 Certbot:

sudo apt install python3-certbot-nginx

然後運行以下命令開始創建證書:

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d www.your-domain.com

輸出:

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://domain.com and https://www.your-domain.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=your-domain.com
https://www.ssllabs.com/ssltest/analyze.html?d=www.your-domain.com
-------------------------------------------------------------------------------

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/your-domain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/your-domain.comm/privkey.pem
Your cert will expire on 2022-07-20. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

Let’s Encrypt 證書的有效期為 90 天,我們強烈建議您在證書過期之前續訂證書。要測試此更新過程是否有效,您可以運行:

sudo certbot renew --dry-run

輸出:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your-domain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for your-domain.com and www.your-domain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/domain.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

步驟 7. 配置防火牆。

Ubuntu 22.04 或更新版本 ufw 防火牆默認運行。通過端口啟用連接 80 HTTP 和 443 HTTPS:

sudo ufw allow 'Nginx FULL'
sudo ufw enable
sudo ufw status

步驟 8. 訪問 Moodle 網絡界面。

安裝成功完成後,打開 Web 瀏覽器並使用 URL 訪問 Moodle 安裝嚮導。 https://your-domain.com您將被重定向到以下頁面。

在 Ubuntu 22.04 上使用 LEMP 堆棧安裝 Moodle

恭喜! 您已成功安裝 Moodle。 感謝您使用本教程在您的 Ubuntu 22.04 LTS Jammy Jellyfish 系統上使用 LEMP 安裝 Moodle。如需更多幫助和有用信息, Moodle官網.