How to install Moodle on CentOS7
Moodle™ is a software platform built for dynamic content management that is geared towards educational usage, such as in creating e-learning portals for schools, universities, and workplaces. Moodle supports a high range of content that it can manage as well as an extensive range of community plugins that make it an easily-tailored solution to many educational challenges and in a variety of environments. Moodle can be run from a Linux web server that provides the basic stack for web applications, and can simplify planning and management for educators and non-educators alike.
In this tutorial, we will show you how you can install Moodle for yourself on a CentOS 7 server.
Getting Started
In order to perform this tutorial, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running a clean installation of CentOS 7.
• All commands must be entered as root
Please ensure that you have the proper access to root and are able to log in as the user or execute the sudo command. If your server does not yet have the LAMP platform, which refers to the specific stack of running Apache, MySQL, and PHP on Linux for web applications, we will cover how to install the necessary tools in the first part of the guide.
One last thing to note before getting started is the IP address and hostname we will be using for our server in this tutorial. Whenever you see references to these, please replace them with your own settings where appropriate. The tutorial hostname will be my.server.com, with an IP address of 10.10.0.35.
Tutorial
Before beginning the Moodle installation, it is recommended to update your server with information concerning the latest available packages and package versions. You can retrieve this information with the default package manager yum for CentOS 7. As the root user, run the below command in order to update your package repository information:
yum -y update
If you have SELinux running on your server for security, you will also need to disable it for the purposes of this guide. To do so, open the configuration file for SELinux in the text editor nano:
nano /etc/sysconfig/selinux
This file will have a line that looks like SELINUX=. This parameter can have one of three values: enforcing, permissive, or disabled. If this line says enforcing, as in SELINUX=enforcing, then you will need to change the value to either permissive or disabled.
After editing the configuration file, save and close. You must reboot your system for the new changes to take into effect:
reboot
You can verify the new status of SELinux by running the sestatus command. The output of this command should look as either of the two following lines:
SELinux status: disabled
SELinux status: permissive
Installing the LAMP Platform
As mentioned above, the LAMP platform refers to a specific setup of the Apache web server, MySQL database, and PHP running together on Linux. This setup is used when running web applications on your server as, when run in conjunction, provide the minimum support functionality necessary to run applications such as Moodle.
The EPEL repository is an additional package repository that is not included by default in your server installation. In this guide, we will be using some packages from this repository, and therefore, we must first add it to the server in order to be able to use it. Configure your CentOS 7 machine to use this additional package repository with the following two commands, which uses the package manager yum:
yum -y install epel-release
After adding the EPEL repository, we can start to install the basic packages needed for Moodle to run. The first package, httpd, will install the Apache web server. This is the world’s most popular software for hosting web services of any sort on your own machine. Install the Apache package using yum:
yum -y install httpd
You can start the Apache web server and enable it to start automatically on boot with these two quick commands:
systemctl start httpd
systemctl enable httpd
The next package required, the MySQL database, takes a little more work to install than Apache. First, you must add the necessary MySQL package repository for yum much like we added the EPEL repository. Since yum uses the package manager rpm, and what we want to do is add a new repository for yum, we can add it using rpm as such:
rpm -Uvh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
Now, we can simply use the yum install command again to install MySQL:
yum install -y mysql-server
While installing MySQL, the system will prompt you to provide a password for the MySQL root user. We recommend to set the password here to avoid having to manually create it further on, and you will need this password for later in the tutorial.
To start MySQL and enable it to startup on server boot, we now use the same two commands we used to start Apache for the mysqld service:
systemctl start mysqld
systemctl enable mysqld
Finally, to round out the LAMP stack, you will need to have PHP, which comes in the php package. It is this that enables your website to have dynamic content such as Moodle. Moodle furthermore will require a few more PHP-related extensions that are also installed with the main PHP installation using these commands. Note that we split the installation into several commands in order to make it easier to see what packages are required, but you can also execute their installation in one long command of the form yum -y install [package_1] [package_2] […] [package_n], keeping a single space as the separator between packages.
yum -y install php php-mysql php-xml php-xmlrpc php-soap php-pgsql php-gd php-iconv php-mbstring php-curl php-openssl php-tokenizer php-ldap php-pecl-apc php-ctype php-zip php-simplexml php-spl php-pcre php-dom php-xml php-intl php-json
To start using PHP on your web server, you will have to then restart Apache. Using the systemctl command, we can restart the service:
systemctl restart httpd
Due to the heavy reliance of Moodle on PHP for processing dynamic content, website performance can be negatively affected. However, a slow web server is oftentimes something that we precisely try to avoid. Luckily, there is a solution called PHP Zend OpCache that speeds up PHP execution by removing compilation and instead directly executing PHP code on the server. To install Zen OpCache on your server, get the package using yum:
yum -y install php-pecl-zendopcache
One benefit of Zen OpCache is how customizable it can be. You can configure it for your unique situation via its configuration file in /etc/php.d/opcache.ini.
After installing Zen OpCache, you must again reload the web server for the changes to take into effect:
systemctl restart httpd
Configuring MySQL
Moodle requires some tweaking of your MySQL configuration before it can be used. The changes you will have to make depend on the Moodle version you want to use, and you can find information concerning these changes in the documentation for your desired Moodle version. In this guide, we will install a stable version 3 Moodle release. For our example, we will have to modify the /etc/mysql/my.cnf configuration file for MySQL to change information concerning the default storage engine, file format, and file per table. We will do this by opening the configuration file in the vi text editor:
vi /etc/mysql/my.cnf
In this file, you will need to find the [mysqld] section, and locate Basic Settings. Here, insert the following lines after the last statement of this section:
default_storage_engine = innodb
innodb_file_per_table = 1
innodb_file_format = Barracuda
For the changes we just made to take into effect, we must now restart MySQL using this command:
systemctl restart mysql
With the MySQL database software installed on your CentOS 7 server, you are now able to create the database for Moodle to specifically use. Open up the MySQL prompt, which uses the language SQL (Structured Query Language) meant for database interactions. Replace the text PASSWORD in the line below and run:
mysql -u root -p PASSWORD
If the prompt now reads mysql>, you are now in the SQL terminal. From here, you can execute all sorts of commands that allow you to interact with MySQL. First, create the actual database for Moodle to store its data in with this command, which sets the database name to “moodle.” If you would like to use a different name, replace moodle in the line below to a name of your choice.
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
With the database created, you will also need to setup the username and password of the Moodle user. In the command below, replace USERNAME and PASSWORD with the respective Moodle user username and password in order to create the user:
create user 'USERNAME'@'localhost' IDENTIFIED BY 'PASSWORD';
While the user has been created, they do not yet have any permissions to interact with the Moodle database. Give the user read and write permissions by executing the following SQL statement, replacing the USERNAME and PASSWORD fields as necessary:
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO USERNAME@localhost IDENTIFIED BY 'PASSWORD';
If you have an error occur during user setup that states that you must use a hash value password (as will happen if using MySQL 5.6+), you can solve it by generating the hash and using that value instead of the password in the above commands. Obtain the hash by entering this in the SQL prompt (replacing PASSWORD with your password):
SELECT password('PASSWORD');
The above command will produce output that looks similar to this string, *AD51BAFB2GD003D3480BCED0DH81AB0BG1712535, and represents the hash value of the password. Replace your password with this hash in all sections that begin with IDENTIFIED BY ‘ in the failing code lines.
After creating the database and database user, exit the SQL prompt:
quit;
Restart MySQL again for the new changes to be verified:
service mysql restart
Installing Moodle
There are two possibilities for how to obtain Moodle on your CentOS 7 server. You can either clone the official Git repository (Git is a distributed version control system) for Moodle and then check out to a specific version branch (basically a code version), or you can get the code base for a specific version as a zip file. Both methods are fairly straight forward, but the Git method can be easier to maintain long term for keeping your Moodle setup up to date.
Installing with Git
If you choose to install Moodle via Git, thereby meaning that your local installation will be linked to the remote Moodle repository, then you will need the Git package. Install it with:
yum -y install git
To host the Moodle repository, we will create a new directory under /opt by cloning the remote repository. Cloning means that using Git, we will make an exact copy of the Moodle code base, with all versions available, in our local directory.
cd /opt
git clone git://git.moodle.org/moodle.git
Check that the directory for Moodle, /opt/moodle exists in the current directory by looking at the output of the ls command, which lists files and repositories in the current folder:
ls
Switch to the repository:
cd moodle
Here is the point at which you choose which Moodle version you want to use. The information for each version is stored inside the Git repository, with each “branch” representing a unique version. Find online the name of the branch you would like to use or use this command to list all branches available:
git branch -a
Once you have decided upon a branch, you can setup a local branch in your repository that will follow the remote version. Here, the branch we chose is MOODLE_30_STABLE for a stable Moodle version 3, but you should replace this with your own version.
git branch --track MOODLE_30_STABLE origin/MOODLE_30_STABLE
After setting up the branch for tracking, you are not currently using that version yet. To actually switch the code base in the Moodle repository to the corresponding version of the Moodle code, use the following (remembering to replace MOODLE_30_STABLE with your desired version):
git checkout MOODLE_30_STABLE
Installing from Zip File
If you instead choose to install Moodle from a zip file, you will require the download utility wget. Install this utility with:
yum -y install wget
Next, change directory to /opt just like you would if installing with Git. Find the Moodle version you would like to use here and replace stable31/moodle-3.1.2.zip in the line below to download the corresponding zip file:
wget https://download.moodle.org/download.php/stable31/moodle-3.1.2.zip
The file we just downloaded is compressed. To uncompress it and make its contents usable, use unzip the file into the new directory moodle:
unzip moodle-3.1.2.zip -d moodle
At this point, you have your desired Moodle version in either a regular or Git repository under /opt/moodle. However, for Moodle to work on your server, you must also have Moodle in the webroot. This webroot is, by default, located in /var/www and is the root point of your website. By choosing to have the main repository for Moodle outside the webroot, you are able to have more control over planned updates to the Moodle version you are using and for how you migrate versions. Copy the Moodle repository into the webroot as a normal, non-Git repository, using:
cp -R /opt/moodle /var/www/html/
Next, you must create an additional directory for Moodle to use to store its data, such as files, session information, and so on. This data directory must also be created in the webroot as such:
mkdir /var/www/html/moodledata
These repositories we created under the webroot will need to have their ownership and permissions modified for the Apache user to be able to access them. You can change the respective directory ownership and permissions with the commands chown and chmod:
chown -R apache.apache /var/moodledata
chmod -R 777 /var/moodledata
chmod -R 0755 /var/www/html/moodle
chown -R apache.apache /var/www/html/moodle
For the changes we just made to be implemented, the Apache web server must be restarted:
systemctl restart httpd
Setting Up Moodle Online
The remainder of the Moodle installation process will be done online. First, we will make the webroot temporarily writable so that this online setup can create a new configuration file config.php after completion. You can modify these permissions again with the chmod command:
chmod -R 777 /var/www/html/moodle
Next, open up your browser to the Moodle home page. Make sure to replace the tutorial IP address with your own. Go to:
http://10.10.0.35/moodle
On this page, follow the instructions to fully complete the Moodle setup. You will have to make changes that include the following:
1. Setting the language
2. Entering the data directory (moodledata) path, in our case it is /var/www/moodledata/
3. Choosing a database driver, Improved MySQL (native/mysqli) for our tutorial
4. Configuring the database settings
5. Setting mdl_ as the prefix for tables
The online installation will also helpfully provide an environment check to notify you if any of the required components to run Moodle are not installed, or not correctly installed, on your system. Go through all the prompts until the installation is complete.
It is vital to now change the permissions in the Moodle directory back again so it is not writable after completing the installation:
chmod -R 0755 /var/www/html/moodle
Conclusion
After you have completed the online setup, that’s it! You now have the learning-oriented content management platform Moodle installed and running on your CentOS 7 server. If this guide was helpful to you, kindly share it with others who may also be interested.