如何在 Ubuntu 22.04 上使用 Nginx 和 Free Lets Encrypt SSL 安装 Moodle
本教程适用于这些操作系统版本
- Ubuntu 22.04(Jammy Jellyfish)
- Ubuntu 16.04(Xenial Xerus)
在此页
- 先决条件
- 开始
- 安装 Nginx、MariaDB 和 PHP
- 为 Moodle 创建数据库
- 在 Ubuntu 22.04 上安装 Moodle
- 为 Moodle 配置 Nginx
- 访问 Moodle 网络界面
- 使用 Lets Encrypt SSL 保护 Moodle
- 结论
Moodle 是一个免费的开源学习管理系统和 CMS,用 PHP 编写。它允许导师和教师为他们的学生创建课程,并提供远程教育和其他更容易获得的在线学习计划。 Moodle 提供了一个简单、用户友好的自定义仪表板,可帮助用户访问当前、过去或未来的课程并查看待处理的工作。它专为旨在提供精通技术的教育的教师和教育工作者而设计。
本教程将向您展示如何在 Ubuntu 22.04 上安装带有 Nginx 的 Moodle 和 Lets Encrypt SSL。
先决条件
- 一台运行 Ubuntu 22.04 的服务器。
- 用您的服务器 IP 指向的有效域名。
- 在服务器上配置了根密码。
入门
首先,您必须将系统包更新到最新版本。您可以通过运行以下命令来更新所有这些:
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 网络界面
现在,打开您的网络浏览器并使用 URL http://moodle.example.com 访问 Moodle 网络界面。您应该看到 Moodle 安装页面:

选择您的语言,然后单击下一步。您应该会看到以下页面:

提供您的 Moodle 网址、目录路径、数据目录路径,然后单击下一步。您应该会看到以下页面:

选择您的数据库驱动程序类型,然后单击下一步。您应该会看到以下页面:

提供您的数据库主机、数据库名称、用户名、密码,然后单击“下一步”。您应该会看到以下页面:

单击继续以确认所有条件。您应该会看到以下页面:

确保安装了所有必需的 PHP 扩展,然后单击继续。您应该会看到以下页面:

单击继续。您应该会看到以下页面:


提供您的管理员用户名、密码、电子邮件、国家/地区、时区,然后单击“更新配置文件”。您应该会看到以下页面:


提供您的首页设置并单击“保存更改”按钮以保存更改。
使用 Lets Encrypt SSL 保护 Moodle
接下来,您需要安装 Certbot 工具以下载 Lets Encrypt SSL 并配置 Nginx 以使用此 SSL。
首先,使用以下命令安装 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):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 和 Lets Encrypt SSL 的 Moodle。您现在可以探索 Moodle 功能并轻松创建您自己的在线学习管理系统。如果您有任何问题,请随时问我