本页内容

  • 前提条件
  • 第 1 步 - 配置防火墙
  • 第 2 步 - 安装 Git
  • 第 3 步 - 安装并配置 PHP
  • 第 4 步 - 安装并配置 MySQL
  • 第 5 步 - 安装 Nginx
  • 第 6 步 - 安装 Moodle
  • 第 7 步 - 配置 Moodle
  • 第 8 步 - 安装 SSL
  • 第 9 步 - 配置 Nginx
  • 第 10 步 - 配置 SELinux
  • 第 11 步 - 完成 Moodle 安装
  • 结论

Moodle 是一款免费、开源的在线学习管理系统(LMS)。它使教育工作者能够为教育课程创建功能齐全的网站,并具备完整的在线课堂体验。它使用 PHP 编写。它提供丰富的功能集,包括 wiki、评分、作业提交、在线测验、讨论板等。

本指南介绍如何在 Rocky Linux 8 服务器上安装 Moodle。

前提条件

  • 一台运行 Rocky Linux 8 的服务器。
  • 一个指向该服务器的域名。在本教程中,我们将使用 moodle.example.com 域名。
  • 一个具有 sudo 权限的非 root 用户。
  • 确保所有内容都已更新。$ sudo dnf update
  • 安装基础实用工具包。其中一些可能已经安装。$ sudo dnf install wget curl nano unzip yum-utils -y

一台运行 Rocky Linux 8 的服务器。

一个指向该服务器的域名。在本教程中,我们将使用 moodle.example.com 域名。

一个具有 sudo 权限的非 root 用户。

确保所有内容都已更新。

$ sudo dnf update

安装基础实用工具包。其中一些可能已经安装。

$ sudo dnf install wget curl nano unzip yum-utils -y

第 1 步 - 配置防火墙

第一步是配置防火墙。Rocky Linux 使用 Firewalld 防火墙。检查防火墙的状态。

$ sudo firewall-cmd --state
running

防火墙使用不同的区域(zone)工作,public 区域是我们将使用的默认区域。列出防火墙上所有处于活动状态的服务和端口。

$ sudo firewall-cmd --permanent --list-services

它应该显示以下输出。

cockpit dhcpv6-client ssh

Moodle 需要 HTTP 和 HTTPS 端口才能运行。开放它们。

$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --add-service=https --permanent

重新加载防火墙以使更改生效。

$ sudo firewall-cmd --reload

第 2 步 - 安装 Git

Moodle 需要 Git 来获取应用程序文件。安装 Git。

$ sudo dnf install git

第 3 步 - 安装并配置 PHP

在本教程中,我们需要安装 PHP 8.0 才能让 Moodle 正常运行。第一步是获取 Epel 仓库。

$ sudo dnf install epel-release

接下来,安装 Remi 仓库。

$ sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

检查可用的 PHP 流(stream)。

$ dnf module list php -y
Rocky Linux 8 - AppStream
Name                Stream                 Profiles                                 Summary
php                 7.2 [d]                common [d], devel, minimal               PHP scripting language
php                 7.3                    common [d], devel, minimal               PHP scripting language
php                 7.4                    common [d], devel, minimal               PHP scripting language
php                 8.0                    common [d], devel, minimal               PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                Stream                 Profiles                                 Summary
php                 remi-7.2               common [d], devel, minimal               PHP scripting language
php                 remi-7.3               common [d], devel, minimal               PHP scripting language
php                 remi-7.4               common [d], devel, minimal               PHP scripting language
php                 remi-8.0               common [d], devel, minimal               PHP scripting language
php                 remi-8.1               common [d], devel, minimal               PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

默认版本是 7.2。启用 Remi 的 PHP 8.0 仓库。

$ sudo dnf module reset php -y
$ sudo dnf module enable php:remi-8.0

安装 PHP 以及 Moodle 所需的扩展。

$ sudo dnf install graphviz aspell ghostscript clamav php-fpm php-iconv php-curl php-mysqlnd php-cli php-mbstring php-xmlrpc php-soap php-zip php-gd php-xml php-intl php-json php-sodium php-opcache

验证安装。

$ php --version
PHP 8.0.21 (cli) (built: Jul  6 2022 10:13:53) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.21, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.21, Copyright (c), by Zend Technologies

打开 php.ini 文件进行编辑。

$ sudo nano /etc/php.ini

修改以下变量的值,将邮件附件大小设置为 25MB。

upload_max_filesize = 25M
post_max_size = 25M

接下来,去掉变量 max_input_vars 前面的分号来取消注释,并将其值改为 5000。

max_input_vars = 5000

按 Ctrl + X,并在提示时输入 Y 来保存文件。

打开文件 /etc/php-fpm.d/www.conf。

$ sudo nano /etc/php-fpm.d/www.conf

在文件中找到 user=apache 和 group=apache 这两行,并按如下方式修改它们。

...
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...

接下来,取消注释套接字文件的所有者、组和默认权限行,并按如下所示修改它们。

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

接下来,在以下行前面加上分号,按所示将其注释掉。

;listen.acl_users = apache,nginx

按 Ctrl + X,并在提示时输入 Y 来保存文件。

为 PHP 会话目录设置适当的权限。

$ chown -R nginx:nginx /var/lib/php/session/

启用并启动 PHP-FPM 服务。

$ sudo systemctl enable php-fpm --now

第 4 步 - 安装并配置 MySQL

安装 MySQL 服务器。

$ sudo dnf install mysql-server

通过检查版本来确认安装。

$ mysql --version
mysql  Ver 8.0.26 for Linux on x86_64 (Source distribution)

启用并启动 MySQL 服务。

$ sudo systemctl enable mysqld --now

运行安全安装脚本。

$ sudo mysql_secure_installation

你会收到若干提示。第一个提示会询问你是否要安装 Validate Password Plugin(密码验证插件)。按 Y 安装该插件。选择 2 作为其安全级别,这将要求你的密码至少 8 个字符长,并包含大写字母、小写字母、数字和特殊字符的组合。

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: (Press Y)

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: (Type 2)

接下来,系统会要求你创建一个强 root 密码。请确保你的密码符合 Validate 插件的要求。

Please set the password for root here.

New password:

Re-enter new password:

接下来,你会收到若干个与增强数据库安全性相关的提示。在每个提示中都按 Y。

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : (Press Y)
Remove anonymous users? (Press y|Y for Yes, any other key for No) : (Press Y)
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : (Press Y)
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : (Press Y)
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : (Press Y)
Success.

All done!

登录到 MariaDB shell。

$ sudo mysql

为 Moodle 创建一个数据库。

mysql > CREATE DATABASE moodledb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

创建一个用于访问该数据库的 SQL 用户。将 YourPassword23! 替换为你自己选择的密码。

mysql > create user 'moodleuser'@'localhost' IDENTIFIED BY 'YourPassword23!';

授予 moodleuser 对该数据库的访问权限。

mysql > GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodledb.* TO 'moodleuser'@'localhost';

重新加载权限表。

mysql > FLUSH PRIVILEGES;

退出 shell。

mysql > exit

第 5 步 - 安装 Nginx

Rocky Linux 自带的是较旧版本的 Nginx。你需要下载官方 Nginx 仓库来安装最新版本。

创建并打开 /etc/yum.repos.d/nginx.repo 文件,用于创建官方 Nginx 仓库。

$ sudo nano /etc/yum.repos.d/nginx.repo

在其中粘贴以下代码。

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

按 Ctrl + X,并在提示时输入 Y 来保存文件。

安装 Nginx 服务器。

$ sudo dnf install nginx

验证安装。

$ nginx -v
nginx version: nginx/1.22.0

第 6 步 - 安装 Moodle

为 Moodle 创建 public 目录。

$ sudo mkdir /var/www/html/moodle

授予已登录用户访问该目录的权限。

$ sudo chown -R $USER:$USER /var/www/html/moodle

切换到 public 目录。

$ cd /var/www/html/moodle

克隆 Moodle 的 Github 仓库。

$ git clone https://github.com/moodle/moodle.git .

查看可用分支列表。

$ git branch -a

目前,MOODLE_400_STABLE 是最新的可用版本。创建一个名为 MOODLE_400_STABLE 的本地分支,并将其设置为跟踪远程分支。

$ git branch --track MOODLE_400_STABLE origin/MOODLE_400_STABLE

切换到新创建的本地分支。

$ git checkout MOODLE_400_STABLE

为 Moodle 创建一个 data 目录。

$ sudo mkdir /var/moodledata

为 Moodle data 目录设置适当的权限。

$ sudo chown -R nginx /var/moodledata
$ sudo chmod -R 775 /var/moodledata

为 Moodle 目录授予写权限。

$ sudo chmod -R 755 /var/www/html/moodle

第 7 步 - 配置 Moodle

切换到 Moodle 目录。

$ cd /var/www/html/moodle

复制示例配置文件以创建 Moodle 配置文件。

$ cp config-dist.php config.php

打开配置文件进行编辑。

$ nano config.php

找到数据库配置部分,然后按如下所示配置用于存储所有 Moodle 数据的数据库。

$CFG->dbtype    = 'mysqli';      // 'pgsql', 'mariadb', 'mysqli', 'auroramysql', 'sqlsrv' or 'oci'
$CFG->dblibrary = 'native';     // 'native' only at the moment
$CFG->dbhost    = 'localhost';  // eg 'localhost' or 'db.isp.com' or IP
$CFG->dbname    = 'moodledb';     // database name, eg moodle
$CFG->dbuser    = 'moodleuser';   // your database username
$CFG->dbpass    = 'YourPassword23!';   // your database password
$CFG->prefix    = 'mdl_';       // prefix to use for all table names

同时,配置 Moodle 域名和 data 目录的位置。

$CFG->wwwroot   = 'https://moodle.example.com';
$CFG->dataroot  = '/var/moodledata';

按 Ctrl + X,并在提示时输入 Y 来保存文件。

第 8 步 - 安装 SSL

Certbot 工具使用 Let's Encrypt API 生成 SSL 证书。它需要 EPEL 仓库,但由于我们已经安装了该仓库,因此可以直接继续。执行以下命令来安装它。

$ sudo dnf install certbot

生成 SSL 证书。

$ sudo certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m 
[email protected]
 -d moodle.example.com

上述命令会将证书下载到服务器上的 /etc/letsencrypt/live/moodle.example.com 目录。

生成 Diffie-Hellman 组证书。

$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096

为 Let's Encrypt 自动续期创建一个 challenge web 根目录。

$ sudo mkdir -p /var/lib/letsencrypt

创建一个用于续期 SSL 的 Cron 任务。它将每天运行以检查证书,并在需要时进行续期。为此,首先创建文件 /etc/cron.daily/certbot-renew 并打开它进行编辑。

$ sudo nano /etc/cron.daily/certbot-renew

粘贴以下代码。

#!/bin/sh
certbot renew --cert-name moodle.example.com --webroot -w /var/lib/letsencrypt/ --post-hook "systemctl reload nginx"

按 Ctrl + X,并在提示时输入 Y 来保存文件。

修改该任务文件的权限,使其可执行。

$ sudo chmod +x /etc/cron.daily/certbot-renew

第 9 步 - 配置 Nginx

创建并打开文件 /etc/nginx/conf.d/moodle.conf 进行编辑。

$ sudo nano /etc/nginx/conf.d/moodle.conf

在其中粘贴以下代码。

# Redirect all non-encrypted to encrypted
server {
    listen 80;
    listen [::]:80;
    server_name moodle.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    server_name moodle.example.com;
    root   /var/www/html/moodle;
    index  index.php;

    ssl_certificate     /etc/letsencrypt/live/moodle.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/moodle.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/moodle.example.com/chain.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    access_log /var/log/nginx/moodle.access.log main;
    error_log  /var/log/nginx/moodle.error.log;
    
    client_max_body_size 25M;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ ^(.+\.php)(.*)$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_index index.php;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        include /etc/nginx/mime.types;
        include fastcgi_params;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    # Hide all dot files but allow "Well-Known URIs" as per RFC 5785
	location ~ /\.(?!well-known).* {
    	return 404;
	}
 
	# This should be after the php fpm rule and very close to the last nginx ruleset.
	# Don't allow direct access to various internal files. See MDL-69333
	location ~ (/vendor/|/node_modules/|composer\.json|/readme|/README|readme\.txt|/upgrade\.txt|db/install\.xml|/fixtures/|/behat/|phpunit\.xml|\.lock|environment\.xml) {
     	deny all;
	    return 404;
	}
}

完成后,按 Ctrl + X,并在提示时输入 Y 来保存文件。

打开文件 /etc/nginx/nginx.conf 进行编辑。

$ sudo nano /etc/nginx/nginx.conf

在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下行。

server_names_hash_bucket_size  64;

按 Ctrl + X,并在提示时输入 Y 来保存文件。

验证 Nginx 配置文件的语法。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

启用并启动 Nginx 服务以启用新配置。

$ sudo systemctl enable nginx --now

如果你遇到以下错误,那么很可能是由于 SELinux 限制所致。

nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)

要修复该错误,请运行以下命令。

$ sudo ausearch -c 'nginx' --raw | audit2allow -M my-nginx
$ sudo semodule -X 300 -i my-nginx.pp

再次启动 Nginx 服务。

$ sudo systemctl start nginx

第 10 步 - 配置 SELinux

修改 Moodle 的 web 目录和 data 目录的文件安全上下文。

$ sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/moodle'
$ sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/moodledata'
$ sudo restorecon -Rv '/var/www/html/moodle'
$ sudo restorecon -Rv '/var/moodledata'

配置 SELinux 以允许来自 Moodle 的网络连接。

$ sudo setsebool -P httpd_can_network_connect on

第 11 步 - 完成 Moodle 安装

在浏览器中打开 URL https://moodle.example.com,你将看到以下欢迎界面。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

按 Continue 按钮继续。接下来,安装程序将检查系统要求。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

如果一切正常,则向下滚动并点击 Continue 按钮,继续进行文件安装和数据库设置。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

安装完成后,点击 Continue 按钮。接下来,按要求填写账户详细信息以创建管理员账户。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

完成后,向下滚动到页面并点击 Update profile 继续。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

接下来,系统会要求你配置 Moodle 的首页设置。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

根据需求进行配置,并点击 Save changes 进入 Moodle 仪表板。接下来,如果你不想向该公司注册你的 Moodle 站点,请点击底部的 Skip 链接。

在 Rocky Linux 8 系统上完整安装 Moodle 在线学习平台教程

现在,你可以开始使用这个学习平台了。

结论

关于在 Rocky Linux 8 服务器上安装 Moodle 学习平台的教程到此结束。如果你有任何问题,请在下方评论区留言。