一、安装PHP7.4 on CentOS7

1yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
3yum -y install yum-utils
4yum-config-manager --enable remi-php74
5yum update
6yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt \
7 php-mbstring php-curl php-xml php-pear php-bcmath php-json  \
8 php-mysqlnd php-xml php-xmlrpc php-pdo  php-pecl-zip php-intl php-common

安装opcache

Opcache 最初被称为 Zend Optimizer+,Opcache(在推出PHP 5.5.0以后)是被作为内置扩展以提高性 PHP 的性能。通过 PECL 它也可以用于 5.2,5.3和5.4 版本的 PHP。它的工作原理是将预编译的脚本字节码存储在共享内存中,从而 PHP 无需为每个请求加载和解析脚本。Opcache 最初被称为 Zend Optimizer+,Opcache(在推出PHP 5.5.0以后)是被作为内置扩展以提高性 PHP 的性能。通过 PECL 它也可以用于 5.2,5.3和5.4 版本的 PHP。它的工作原理是将预编译的脚本字节码存储在共享内存中,从而 PHP 无需为每个请求加载和解析脚本。

1# yum install php-opcache
2# vim /etc/php.d/10-opcache.ini
3opcache.enable_cli=1
4opcache.memory_consumption=128
5opcache.interned_strings_buffer=8
6opcache.max_accelerated_files=4000
7opcache.revalidate_freq=60
8opcache.fast_shutdown=1

二、安装 MariaDB 10 in CentOS7

最新版本是10.9.1,这里向下移几个小版本,保证稳定性

 1// configuration yum repo
 2# vi /etc/yum.repos.d/MariaDB.repo
 3[mariadb]
 4name = MariaDB
 5baseurl = http://yum.mariadb.org/10.5/centos7-amd64
 6gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
 7gpgcheck=1
 8
 9// install the mariadb and configuration for safe
10# yum install MariaDB-server MariaDB-client -y
11# systemctl start mariadb
12# systemctl enable mariadb
13# systemctl status mariadb
14# mysql_secure_installation
15# mysql -V
16# mysqld --print-defaults
17# mysql -u root -p

配置数据库my.cnf

 1# cat /etc/my.cnf|grep -v ^#
 2[mysqld]
 3datadir=/var/lib/mysql
 4socket=/var/lib/mysql/mysql.sock
 5symbolic-links=0
 6innodb_file_format = Barracuda
 7innodb_file_per_table = 1
 8innodb_large_prefix = true
 9
10character-set-server = utf8mb4
11collation-server = utf8mb4_unicode_ci
12skip-character-set-client-handshake
13
14[mysqld_safe]
15log-error=/var/log/mariadb/mariadb.log
16pid-file=/var/run/mariadb/mariadb.pid
17
18!includedir /etc/my.cnf.d
19
20
21[client]
22default-character-set = utf8mb4
23
24[mysql]
25default-character-set = utf8mb4

创建用户

1CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword';

三、安装moodle

1yum -y install nginx
2wget https://download.moodle.org/stable400/moodle-4.0.1.zip
3unzip moodle-4.0.1.zip
4mv moodle /var/www/html/
5chown -R nginx:nginx /var/www/html/moodle
6mkdir -p /var/www/html/moodledata/
7chown nginx:nginx /var/www/html/moodle

配置nginx:

 1server {
 2    listen       80;
 3    listen       [::]:80;
 4    server_name  _;
 5    #root         /usr/share/nginx/html;
 6    index  index.php index.html index.htm;
 7    root         /var/www/html/moodle;
 8
 9    location / {
10    try_files $uri $uri/ =404;
11    }
12
13    location /dataroot/ {
14    internal;
15    alias /var/www/html/moodledata/;
16    }
17
18    location ~ ^(.+\.php)(.*)$ {
19        root /var/www/html/moodle/;
20        fastcgi_split_path_info  ^(.+\.php)(.*)$;
21        fastcgi_index            index.php;
22        fastcgi_pass             127.0.0.1:9000;
23        include /etc/nginx/mime.types;
24        include                  fastcgi_params;
25        fastcgi_param   PATH_INFO       $fastcgi_path_info;
26        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
27    }
28
29    # Hiding internal files
30    location ~ /\.(?!well-known).* {
31        return 404;
32    }
33
34    # This should be after the php fpm rule and very close to the last nginx ruleset.
35    # Don't allow direct access to various internal files. See MDL-69333
36    location ~ (/vendor/|/node_modules/|composer\.json|/readme|/README|readme\.txt|/upgrade\.txt|db/install\.xml|/fixtures/|/behat/|phpunit\.xml|\.lock|environment\.xml) {
37        deny all;
38        return 404;
39    }
40
41    error_page 404 /error/index.php;
42    error_page 403 =404 /error/index.php;
43
44    error_page 500 502 503 504 /50x.html;
45    location = /50x.html {
46    }
47}

#### 修改php.ini文件

1file_uploads = On
2allow_url_fopen = On
3memory_limit = 256M
4upload_max_filesize = 64M
5max_execution_time = 360
6cgi.fix_pathinfo = 0
7date.timezone = Asia/Shanghai

启用xsendfile

Enable xsendfile for Nginx in Moodles config.php, this is documented in the config-dist.php, a minimal configuration look like this

1$CFG->xsendfile = 'X-Accel-Redirect';
2$CFG->xsendfilealiases = array(
3    '/dataroot/' => $CFG->dataroot
4);

php-fpm的安全配置,限定只能运行php结尾的文件:

1# cat /etc/php*/fpm/pool.d/www.conf
2security.limit_extensions = .php

四、其他

如何禁用https

In config.php, adding the following line should prevent https from being forced:

1$CFG->overridetossl = false;

一键安装

1php admin/cli/install.php --lang=en --chmod=2775 --wwwroot=http://localhost:8080 --dataroot=/var/www/html/moodledata/ --adminuser=user --adminpass=adminpass --adminemail=user@example.com --fullname="New Site" --shortname="New Site" --non-interactive --allow-unstable --agree-license --dbtype=mariadb --dbhost=localhost --dbport=3306 --dbname=moodle --dbuser=root --dbpass=mypassword

上面的步骤执行完成后,最好再通过chown -R nginx:nginx /var/www/html/moodle/ /var/www/html/moodledata/指令确保nginx对所属的文件有相应的权限。

moodle-install
moodle-install

cron清理配置:

1* * * * *    /usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null

参考文档:
https://docs.moodle.org/400/en/Nginx
https://docs.moodle.org/400/en/Installation_quick_guide
https://docs.moodle.org/400/en/Caching