如何在 Rocky Linux 8 上安装 Moodle 电子学习平台
在此页
- 先决条件
- 第 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。
先决条件
-
A server running Rocky Linux 8.
-
A domain name pointing to the server. For our tutorial, we will use the
moodle.example.com
domain. -
A non-root user with sudo privileges.
-
Make sure everything is updated.
$ sudo dnf update
-
Install basic utility packages. Some of them may already be installed.
$ sudo dnf install wget curl nano unzip yum-utils -y
第 1 步 - 配置防火墙
第一步是配置防火墙。 Rocky Linux 使用 Firewalld 防火墙。检查防火墙状态。
$ sudo firewall-cmd --state
running
防火墙适用于不同的区域,公共区域是我们将使用的默认区域。列出防火墙上所有活动的服务和端口。
$ 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
我们需要为 Moodle 安装 PHP 8.0 才能为我们的教程工作。第一步是获取 Epel 存储库。
$ sudo dnf install epel-release
接下来,安装 Remi 存储库。
$ sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
检查可用的 PHP 流。
$ 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。启用 Remis 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
您将收到几个提示。第一个提示将询问您是否要安装验证密码插件。按 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 密码。确保您的密码符合验证插件的要求。
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;
退出外壳。
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 创建公共目录。
$ sudo mkdir /var/www/html/moodle
授予登录用户访问目录的权限。
$ sudo chown -R $USER:$USER /var/www/html/moodle
切换到公共目录。
$ 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 创建一个数据目录。
$ sudo mkdir /var/moodledata
给予 Moodle 数据目录适当的权限。
$ 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 域名和数据目录的位置。
$CFG->wwwroot = 'https://moodle.example.com';
$CFG->dataroot = '/var/moodledata';
通过按 Ctrl + X 并在出现提示时输入 Y 来保存文件。
第 8 步 - 安装 SSL
Certbot 工具使用 Lets Encrypt API 生成 SSL 证书。它需要 EPEL 存储库,但由于我们已经安装了它,所以我们可以直接进行。发出以下命令来安装它。
$ sudo dnf install certbot
生成 SSL 证书。
$ sudo certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m -d moodle.example.com
上面的命令会将证书下载到服务器上的 /etc/letsencrypt/live/moodle.example.com
目录。
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096
为 Lets Encrypt 自动更新创建一个挑战网站根目录。
$ sudo mkdir -p /var/lib/letsencrypt
创建 Cron 作业以更新 SSL。它将每天运行以检查证书并在需要时更新证书。为此,首先,创建文件 /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
更改 Moodles 网站和数据目录的文件安全上下文。
$ 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
,您将看到以下欢迎屏幕。

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

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

安装完成后单击继续按钮。接下来,通过按要求填写帐户详细信息来创建管理员帐户。

完成后,向下滚动到页面并单击更新配置文件以继续。

接下来,您将被要求配置 Moodles 主页设置。

根据要求配置它并单击保存更改以继续到 Moodle 仪表板。接下来,如果您不想在该公司注册您的 Moodle 站点,请单击底部的跳过链接。

现在,您可以开始使用学习平台了。
结论
我们关于在 Rocky Linux 8 服务器上安装 Moodle 学习平台的教程到此结束。如果您有任何问题,请在下面的评论中发表。