Learn How To Install Moodle on CentOS 7

November 23, 2019

Table of Contents

If you are using a different system, please check our other tutorials.

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 Moodle 3.2.x on a CentOS 7 server.


  • 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

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.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.1.

3.1 Build the MariaDB 10.1 YUM repo

In order to create the MariaDB 10.1 YUM repo file, copy the below code segment to your SSH terminal console and then press the Enter button:

cat <<EOF | sudo tee -a /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.1 CentOS repository list - created 2017-01-14 03:11 UTC
# http://downloads.mariadb.org/mariadb/repositories/
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64

3.2 Install MariaDB 10.1 using YUM

sudo yum install MariaDB-server MariaDB-client -y

3.3 Start the MariaDB service and set it as running at system startup

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

3.4 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.5 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 USER 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON moodle.* TO 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;

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.2.1

5.1 Prepare the Moodle program files

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

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

wget https://download.moodle.org/download.php/direct/stable32/moodle-3.2.1.tgz
sudo tar -zxvf moodle-3.2.1.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/www/moodledata
sudo chown -R apache:apache /var/www/moodledata
sudo chmod -R 755 /var/www/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
ErrorLog /var/log/httpd/moodle.example.com-error_log
CustomLog /var/log/httpd/moodle.example.com-access_log common

5.4 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
== Data directory ==
type value, press Enter to use default value (/var/www/html/moodledata)
: /var/www/moodledata
== Choose database driver ==
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.5 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.6 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:


5.7 Restart Apache

Restart Apache in order to apply all your modifications:

sudo systemctl restart httpd.service

5.8 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 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.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!