在本教程中,我们将向您展示如何在 Ubuntu 22.04 LTS 上使用 LEMP Stack 安装 Moodle。 对于那些不知道的人,Moodle 是一个免费的开源学习管理系统和用 PHP 编写的 CMS。 使用 Moodle,您可以创建一个功能齐全的教育和培训课程网站,适合完全在线、混合和面对面的课堂体验。 Moodle 平台是高度可定制的,并采用模块化的功能方法,因此它可以扩展并适应您的需求。 Moodle 派上用场,特别是对于全球各地的远程教育机构,为他们的学习者提供培训材料。
本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo
‘ 到命令以获取 root 权限。 我将向您展示在 Ubuntu 22.04 (Jammy Jellyfish) 上使用 LEMP Stack 逐步安装 Moodle。 对于 Ubuntu 22.04 和任何其他基于 Debian 的发行版,如 Linux Mint、Elementary OS、Pop!_OS 等,您可以按照相同的说明进行操作。
先决条件
- 运行以下操作系统之一的服务器:Ubuntu 22.04、20.04 和任何其他基于 Debian 的发行版,如 Linux Mint。
- 建议您使用全新的操作系统安装来防止任何潜在问题。
- 对服务器的 SSH 访问(或者如果您在桌面上,则只需打开终端)。
- 一个
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 Stack,您可以在此处按照我们的指南进行操作。
步骤 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
这将提示您输入密码,因此输入您的 MariaDB 根密码并点击 Enter. 登录到数据库服务器后,您需要为 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; } }
Save 和 close 然后,重新启动 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
. 您将被重定向到以下页面:
恭喜! 您已成功安装 Moodle。 感谢您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系统上安装带有 LEMP 的 Moodle。 如需更多帮助或有用信息,我们建议您查看 Moodle官方网站.