How to Install Moodle 3.3.x on CentOS 7

Updated on September 22, 2017
How to Install Moodle 3.3.x on CentOS 7 header image

Moodle is an open-source Learning Platform or course management system (CMS) - a free Open Source software package designed to help educators create effective online courses.

This tutorial will cover the process of installing the latest stable release of Moodle, Moodle 3.3.2+, on a CentOS 7 server.

Prerequisites

  • A CentOS 7 x64 server instance with at least 2GB of RAM (4GB or more recommended).
  • A sudo user.
  • The EPEL yum repository.

Step 1: Update the system

Log in to your server via SSH using the sudo user to install epel, update the system, and restart to apply the updates.

sudo yum install epel-release -y
sudo yum update -y && sudo shutdown -r now

Step 2: Install Apache 2.4.x

sudo yum install httpd -y

In production, you should remove the pre-set Apache welcome page:

sudo sed -i 's/^/#&/g' /etc/httpd/conf.d/welcome.conf

Prevent Apache from listing web directory files to visitors:

sudo sed -i "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" /etc/httpd/conf/httpd.conf

Start the Apache service and enable it to auto-start on boot

sudo systemctl start httpd.service
sudo systemctl enable httpd.service

Step 3: Install MariaDB 10.2.x

Moodle stores all its data to a MySQL Database. MariaDB is a drop-in replacement for MySQL and we will be installing the latest stable version, MariaDB 10.2.x.

3.1 Install and start MariaDB 10.2.x

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
sudo yum install MariaDB-server MariaDB-client -y
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

3.2 Secure the installation of MariaDB

sudo /usr/bin/mysql_secure_installation

Answer questions as below, and make sure to use a strong MariaDB root password instead of the sample one showed beneath:

  • Enter current password for root (enter for none): Just press the Enter button
  • Set root password? [Y/n]: Y
  • New password: your-root-password
  • Re-enter new password: your-root-password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

3.3 Create a MariaDB database for Moodle

Log into the MySQL shell as root:

mysql -u root -p

Type the MariaDB root password you set earlier when prompted.

In the MySQL shell, create a database moodle, a database user moodleuser, the database user's password yourpassword as follows.

Note: For security purposes, you should replace the sample password yourpassword mentioned above with your own ones.

CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON moodle.* TO 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

Step 4: Install PHP 7.1 and necessary PHP 7.1 extensions

PHP is also required by Moodle. In order to achieve better performance, you can install PHP 7.1 and several PHP 7.1 extensions as follows:

sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install mod_php71w php71w-common php71w-mbstring php71w-xmlrpc php71w-soap php71w-gd php71w-xml php71w-intl php71w-mysqlnd php71w-cli php71w-mcrypt php71w-ldap -y

Step 5: Install Moodle 3.3.2+

5.1 Prepare the Moodle program files

At the time of writing, the latest stable release of Moodle is Moodle 3.3.2+. Download and unzip the Moodle 3.3.2+ archive as follows:

Note: You can always get the download URL of the latest stable release of Moodle from its official download page.

cd
yum install -y wget
wget https://download.moodle.org/download.php/direct/stable33/moodle-latest-33.tgz
sudo tar -zxvf moodle-latest-33.tgz -C /var/www/html
sudo chown -R root:root /var/www/html/moodle

5.2 Setup a dedicated data directory for Moodle

For security purposes, this data directory should be outside of the web root directory:

sudo mkdir /var/moodledata
sudo chown -R apache:apache /var/moodledata
sudo chmod -R 755 /var/moodledata

5.3 Setup a virtual host for Moodle

Note: Remember to replace the values of ServerAdmin, ServerName, ServerAlias, Errorlog, and CustomLog with your own ones.

cat <<EOF | sudo tee -a /etc/httpd/conf.d/moodle.conf
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/moodle/
ServerName moodle.example.com
ServerAlias www.moodle.example.com
<Directory /var/www/html/moodle/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/moodle.example.com-error_log
CustomLog /var/log/httpd/moodle.example.com-access_log common
</VirtualHost>
EOF

5.4 Setup SELinux

On the Vultr CentOS 7 server instance I'm using, SELinux is disabled by default. It is a probably less secure but easier security policy. If you are in the same condition and would like to keep it that way, just ignore the instructions in this section and go to the next section.

If you are using a server instance with SELinux enabled in the enforcing mode, you need to setup SELinux as follows.

Show the status of SELinux:

sestatus

On a fresh CentOS 7 Minimal x64 1708 server instance, the output is:

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

Install required SELinux management tools:

sudo yum install -y policycoreutils policycoreutils-python 

Setup Moodle files' SELinux contexts as below:

sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/moodle(/.*)?'
sudo restorecon -Rv '/var/www/html/moodle/'
sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/moodledata(/.*)?'
sudo restorecon -Rv '/var/moodledata/'

5.5 Install Moodle from CLI

sudo /usr/bin/php /var/www/html/moodle/admin/cli/install.php

When prompted, provide necessary information according to the specific settings to your setup. Summary information are listed below:

== Choose a language ==
en - English (en)
? - Available language packs
type value, press Enter to use default value (en)
: en
-------------------------------------------------------------------------------
== Data directories permission ==
type value, press Enter to use default value (2777)
: 2777
-------------------------------------------------------------------------------
== Web address ==
type value
: http://203.0.113.1
-------------------------------------------------------------------------------
== Data directory ==
type value, press Enter to use default value (/var/www/html/moodledata)
: /var/moodledata
-------------------------------------------------------------------------------
== Choose database driver ==
 mysqli
 mariadb
type value, press Enter to use default value (mysqli)
: mariadb
-------------------------------------------------------------------------------
== Database host ==
type value, press Enter to use default value (localhost)
: localhost
-------------------------------------------------------------------------------
== Database name ==
type value, press Enter to use default value (moodle)
: moodle
-------------------------------------------------------------------------------
== Tables prefix ==
type value, press Enter to use default value (mdl_)
: mdl_
-------------------------------------------------------------------------------
== Database port ==
type value, press Enter to use default value ()
:
-------------------------------------------------------------------------------
== Unix socket ==
type value, press Enter to use default value ()
:
-------------------------------------------------------------------------------
== Database user ==
type value, press Enter to use default value (root)
: moodleuser
-------------------------------------------------------------------------------
== Database password ==
type value
: yourpassword
-------------------------------------------------------------------------------
== Full site name ==
type value
: My Moodle Site
-------------------------------------------------------------------------------
== Short name for site (eg single word) ==
type value
: moodle
-------------------------------------------------------------------------------
== Admin account username ==
type value, press Enter to use default value (admin)
: admin
-------------------------------------------------------------------------------
== New admin user password ==
type value
: your-admin-password
-------------------------------------------------------------------------------
== New admin user email address ==
type value, press Enter to use default value ()
: admin@example.com
-------------------------------------------------------------------------------
== Upgrade key (leave empty to not set it) ==
type value
:
-------------------------------------------------------------------------------
Have you read these conditions and understood them?
type y (means yes) or n (means no)
: y

5.6 Modify permissions to /var/www/html/config.php

Having Moodle successfully installed, you need to allow the apache user to read Moodle configurations by modifying the permissions to /var/www/html/config.php as below:

sudo chmod o+r /var/www/html/moodle/config.php

5.7 Setup a cron job

Additionally, you need to setup a cron job in order to keep Moodle running correctly:

sudo crontab -u apache -e

Populate the cron file with:

* * * * *    /usr/bin/php /var/www/html/moodle/admin/cli/cron.php >/dev/null

Save and quit:

:wq!

5.8 Restart Apache

Restart Apache in order to apply all your modifications:

sudo systemctl restart httpd.service

5.9 Modify firewall rules in order to allow HTTP access

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload

Finally, point your web browser to http://203.0.113.1 to visit the Moodle website. Use the admin's username and password you setup earlier to log in.

This concludes our tutorial. Thanks for reading.