本教程适用于以下操作系统版本
- Ubuntu 24.04 (Noble Numbat)
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 16.04 (Xenial Xerus)
本页目录
- 先决条件
- 准备工作
- 安装 Nginx、MariaDB 和 PHP
- 为 Moodle 创建数据库
- 在 Ubuntu 22.04 上安装 Moodle
- 为 Moodle 配置 Nginx
- 访问 Moodle Web 界面
- 使用 Let's Encrypt SSL 保护 Moodle
- 总结
Moodle 是一个用 PHP 编写的免费开源学习管理系统与内容管理系统(CMS)。它允许导师和教师为学生创建课程,并使远程教育及其他在线学习项目更易于开展。Moodle 提供简洁、易用且可自定义的控制面板,帮助用户访问当前、过去或未来的课程并查看待办作业。它专为希望借助技术手段开展教学的教师和教育工作者而设计。
本教程将演示如何在 Ubuntu 22.04 上使用 Nginx 和 Let's Encrypt SSL 安装 Moodle。
先决条件
- 一台运行 Ubuntu 22.04 的服务器。
- 一个已解析到服务器 IP 的有效域名。
- 服务器上已配置 root 密码。
准备工作
首先,您必须将系统软件包更新到最新版本。可以运行以下命令更新所有软件包:
apt-get update -y
服务器更新完成后,即可继续下一步。
安装 Nginx、MariaDB 和 PHP
开始之前,您需要在系统上安装 Apache、MariaDB、PHP 及其他 PHP 库。首先,使用以下命令安装 Apache 和 MariaDB 服务器:
apt-get install nginx mariadb-server -y
默认情况下,Ubuntu 22.04 自带 PHP 8.1 版本,而 Moodle 不支持该 PHP 版本。因此您需要在服务器上安装 PHP 7.4。
首先,使用以下命令安装所有必需的依赖项:
apt install software-properties-common ca-certificates lsb-release apt-transport-https -y
接下来,使用以下命令将 PHP 软件源添加到服务器:
add-apt-repository ppa:ondrej/php
接下来,使用以下命令更新软件源:
apt update
软件源更新完成后,使用以下命令安装 PHP 及其他必需的扩展:
apt install php7.4 php7.4-fpm php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-soap php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip unzip git curl -y
所有软件包安装完成后,编辑 php.ini 文件并修改部分设置:
nano /etc/php/7.4/fpm/php.ini
修改以下几行:
memory_limit = 256M max_input_vars = 6000 cgi.fix_pathinfo = 0 upload_max_filesize = 100M max_execution_time = 360 date.timezone = UTC
保存并关闭文件,然后重启 PHP-FPM 服务以使更改生效:
systemctl restart php7.4-fpm
完成后,即可继续下一步。
为 Moodle 创建数据库
Moodle 使用 MySQL 或 MariaDB 作为数据库后端,因此您需要为 Moodle 创建一个数据库和用户。
首先,使用以下命令连接到 MySQL shell:
mysql
登录后,使用以下命令创建数据库和用户:
CREATE DATABASE moodledb;
CREATE USER 'moodle'@'localhost' IDENTIFIED BY 'password';
接下来,使用以下命令为 Moodle 数据库授予所有权限:
GRANT ALL ON moodledb.* TO 'moodle'@'localhost' WITH GRANT OPTION;
接下来,使用以下命令刷新权限并退出 MySQL:
FLUSH PRIVILEGES;
EXIT;
接下来,编辑 MariaDB 默认配置文件并定义 innodb_file_format:
nano /etc/mysql/mariadb.conf.d/50-server.cnf
在 [mysqld] 段中添加以下几行:
[mysqld] innodb_file_format = Barracuda innodb_file_per_table = 1 innodb_large_prefix = ON
保存文件,然后重启 MariaDB 服务以使更改生效:
systemctl restart mariadb
在 Ubuntu 22.04 上安装 Moodle
首先,切换到 Apache 根目录,并使用以下命令下载最新版本的 Moodle:
cd /var/www/html
git clone -b MOODLE_400_STABLE git://git.moodle.org/moodle.git moodle
接下来,为 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
完成后,即可继续下一步。
为 Moodle 配置 Nginx
接下来,您需要创建一个 Nginx 虚拟主机配置文件来承载 Moodle:
nano /etc/nginx/conf.d/moodle.conf
添加以下几行:
server {
listen 80;
root /var/www/html/moodle;
index index.php index.html index.htm;
server_name moodle.example.com;
client_max_body_size 100M;
autoindex off;
location / {
try_files $uri $uri/ =404;
}
location /dataroot/ {
internal;
alias /var/www/html/moodledata/;
}
location ~ [^/].php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存并关闭文件,然后使用以下命令检查 Nginx 是否存在语法错误:
nginx -t
您将看到以下输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
最后,重启 Nginx 服务以使更改生效:
systemctl restart nginx
也可以使用以下命令查看 Nginx 服务的状态:
systemctl status nginx
您应该会看到以下输出:
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-07-18 07:08:50 UTC; 26s ago
Docs: man:nginx(8)
Process: 51379 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 51382 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 51383 (nginx)
Tasks: 3 (limit: 4579)
Memory: 3.6M
CPU: 64ms
CGroup: /system.slice/nginx.service
??51383 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
??51384 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
??51385 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Jul 18 07:08:50 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 18 07:08:50 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
至此,Nginx 已配置为承载 Moodle。现在可以继续下一步了。
访问 Moodle Web 界面
现在,打开 Web 浏览器,通过 URL http://moodle.example.com 访问 Moodle Web 界面。您将看到 Moodle 安装页面:

选择您的语言并点击 Next。您将看到以下页面:

填写 Moodle 网址、目录路径和数据目录路径,然后点击 Next。您将看到以下页面:

选择数据库驱动类型并点击 Next。您将看到以下页面:

填写数据库主机、数据库名、用户名和密码,然后点击 Next。您将看到以下页面:

点击 Continue 确认所有条件。您将看到以下页面:

确认已安装所有必需的 PHP 扩展,然后点击 Continue。您将看到以下页面:

点击 Continue。您将看到以下页面:


填写管理员用户名、密码、邮箱、国家/地区和时区,然后点击 Update profile。您将看到以下页面:


填写首页设置,然后点击 Save changes 按钮保存更改。
使用 Let's Encrypt SSL 保护 Moodle
接下来,您需要安装 Certbot 工具以获取 Let's Encrypt SSL 证书,并配置 Nginx 使用该证书。
首先,使用以下命令安装 Certbot:
apt-get install python3-certbot-nginx -y
安装完成后,运行以下命令获取 SSL 证书并配置 Nginx 使用它:
certbot --nginx -d moodle.example.com
系统将提示您提供有效的邮箱地址并接受服务条款,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y Obtaining a new certificate Performing the following challenges: http-01 challenge for moodle.example.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/nginx/conf.d/moodle.conf
接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
输入 2 并按 Enter 键继续。您将看到以下输出:
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/moodle.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://moodle.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=moodle.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/moodle.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/moodle.example.com/privkey.pem Your cert will expire on 2022-10-19. 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" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - 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 - We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.
现在可以通过 URL http://moodle.example.com 访问 Moodle 网站
总结
恭喜!您已成功在 Ubuntu 22.04 上使用 Nginx 和 Let's Encrypt SSL 安装了 Moodle。现在您可以探索 Moodle 的各项功能,轻松创建属于自己的在线学习管理系统。