How to install Moodle on Ubuntu 14

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Moodle, which stands for “modular object-oriented dynamic learning environment,” is a type of software that is known as a learning management system. It is free, open-source, and written in PHP. Moodle is most commonly used in e-learning projects for schools, universities, and even workplaces and allows a mixture of interactive and informative content to be stored primarily for learning purposes and be accessed from the internet. Thanks to its immense variety of customizable management features and community-sourced plugins, Moodle is a popular way to create websites for private use tailored towards learning to suit any environment.

To install Moodle on your Ubuntu 14 server, follow this guide.

Getting Started

You will need the following in order to complete this tutorial:
• 1 Node (Cloud Server or Dedicated Server) running a clean installation of CentOS 7.
• All commands must be entered as root

A web application like Moodle also requires that your Ubuntu 14 server contains a particular set of tools, known as LAMP. This term is an acronym that refers to Linux, Apache, MySQL, and PHP all installed on a single server to be used together. This particular stack is notable for enabling the server to host dynamic websites and web applications by running the Apache web server on the system, storing website data in the MySQL database, and using PHP for dynamic content processing. Do not worry if your server does not have these tools yet, we will cover it in the first part of the Moodle installation guide.

Tutorial

It is always good practice to update your local package repository before downloading any new software. This allows for you to have the most recent information about which packages and package versions are available. As root, run the apt-get utility with the option update. This utility is the built-in package installation and management tool for Ubuntu and can also be used to fetch new package information, as we will do in this step:

apt-get update

If you already have the packages specified in the LAMP acronym running on your Linux system, you can proceed to the following section concerning further prerequisites. However, if you do not have Apache, MySQL, and/or PHP, then you must install these prerequisite packages.

Installing Other Moodle Prerequisites

Beyond LAMP, your Ubuntu server will also need further packages to be able to run Moodle. These packages include more advanced utilities for PHP and running PHP with the MySQL database. A notable and very important package out of these remaining prerequisites is git-core. This package will install the distributed version control system Git on your Ubuntu server. Git is vital as it will be used to install and/or update the Moodle Core Application on your server. Install the PHP packages and Git with the apt-get utility:

apt-get -y install graphviz aspell php5-pspell php5-curl php5-gd php5-intl php5-mysql php5-xmlrpc php5-ldap git-core

Downloading Moodle and Configuring the Server

With the prerequisites setup for Moodle, we can now proceed to actually downloading Moodle and making some configurations for it to work. For the sake of this guide, we will use the /opt repository as our installation location, however you are free to choose your own and thus just replace all references to this repository with your own. First, change to the repository /opt:

cd /opt

Next, fetch the Moodle repository from Git. This step is why it is highly important that Git is installed on your system. If you are behind a corporate firewall, you may need to play around with your proxy settings if you encounter any issues fetching from Git. To get Moodle using Git, we will perform the action clone, which will make a copy of the main Moodle code from the Git repository hosted on their website. This local repository is linked to the main repository using Git upon creation:

git clone git://git.moodle.org/moodle.git

The previous step only made a copy of Moodle, but did not yet install it. Change to the newly created Moodle repository that will appear after the cloning process in the main directory /opt:

cd moodle

The different versions of Moodle are stored in branches within this Git repository. This means that multiple versions of the Moodle code exist, with each version being known as a Git “branch.” To retrieve a list of all branches that are available on the remote code source, use:

git branch -a

After fetching the branch list, setup your local repository to track the specific branch you want on the remote repository, in this case MOODLE_30_STABLE. What this does is link the two branches and tell Git which one to use specifically for tracking. This branch we track will be the version of Moodle we will then install. To select a different version, for example version 2.7, you can find online which version is available and its corresponding branch name, and then replace MOODLE_30_STABLE with the version you want.

git branch --track MOODLE_30_STABLE origin/MOODLE_30_STABLE

With the branch we want being tracked, we can now switch to it in our local Moodle repository to be able to start using that version of Moodle code. Again, replace MOODLE_30_STABLE with the version that you want to use instead if it differs.

git checkout MOODLE_30_STABLE

Now that we have the code for our desired Moodle version, this code must be copied into the document root directory, also known as the webroot, found in /var/www/. This directory is the location of the Apache web server installation for hosting your website. Use the command cp to copy the Moodle directory with the flag -R to ensure that we copy everything recursively. We intentionally setup the local Moodle Git repository outside the Apache webroot so that it will be easier to plan and stage upgrades in the future for Moodle to customize its plugins to work for you.

cp -R /opt/moodle /var/www/html/

Setting up Moodle will also require that you create another directory after the previous one that will host the actual data for Moodle. It will also be created in the /var location using the command mkdir:

mkdir /var/moodledata

These two directories we created will need some changes to their permissions and ownership before they can be used for Moodle. Make these changes using the chown and chmod commands to respectively change the ownership and permissions for these new directories with the following commands:

chown -R www-data /var/moodledata
chmod -R 777 /var/moodledata
chmod -R 0755 /var/www/html/moodle

Configuring MySQL

Your MySQL configuration must be tweaked in order to use Moodle. You can always find the latest information for how these tweaks must be made for each Moodle version online, as it can differ. For the version we have installed, for example, we will need to change the default storage engine to innodb, modify the file format to Barracude specifically for this version, and set the storage engine’s innodb_file_per_table in order for the file format to work properly. Note that while this version should automatically select innodb as default during installation, this may not be true for the Moodle version you chose, and thus it is always good practice to set it to default anyways. Open the configuration file with the editor vi in order to make the required changes:

vi /etc/mysql/my.cnf

With the file open, locate the [mysqld] section and find “Basic Settings.” Under the last statement, add the following lines before saving and closing the file:

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:

service mysql restart

Following this, you will need to actually create the database for Moodle and the Moodle MySQL user with the correct permissions. For this step, you absolutely need the password you set above for the MySQL user during installation, replacing the PASSWORD in the line below with your own, in order to open the MySQL prompt.

mysql -u root -p PASSWORD

With the terminal having changed to MySQL (the line should now read mysql>), you can now configure and communicate with the database using SQL, short for Structured Query Language. SQL is a language written specifically for interacting with databases. Run the following line to create the database for Moodle. Replace the text moodle with a different database name if you wish, otherwise the database will be named moodle:

CREATE DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

In the line below, replace USERNAME and PASSWORD with the username and password of the Moodle user and keep the single quotation marks. This will create that user:

create user 'USERNAME'@'localhost' IDENTIFIED BY 'PASSWORD';

With the Moodle user created, you will need to give the user permissions to actually interact with Moodle. Again, replace USERNAME and PASSWORD with your own information.

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO USERNAME@localhost IDENTIFIED BY 'PASSWORD';

There is a possibility that you will have an error occur during the user creation that involves a password hash. This will happen if you use MySQL 5.6+. To solve it, you must use the hash value of the password instead. You can obtain this value by entering the following in the SQL prompt, replacing PASSWORD with your password:

SELECT password('PASSWORD');

The output of the above command should look something like *AD51BAFB2GD003D3480BCED0DH81AB0BG1712535. This is the hash of your password, and you must use this in the sections in the above commands that begin by IDENTIFIED BY ‘ instead of your actual password.

When you are finished, use this command to exit the SQL prompt:

quit;

Installing Moodle

With the server setup and ready for Moodle, we finally get to the actual installation. The webroot (/var/www) will need to be made temporarily writable in order for the config.php file to be created correctly during the online setup. Change the webroot permissions using the command chmod:

chmod -R 777 /var/www/html/moodle

Open your browser to the following page, replacing IP.ADDRESS.OF.YOUR.SERVER with the IP address of your Ubuntu 14 node:

http://IP.ADDRESS.OF.YOUR.SERVER/moodle

The prompts will guide you during the Moodle installation. These prompts will include the following:
1. Changing the path for moodledata to /var/moodledata
2. Selecting mysqli was the database type
3. Setting the host server to localhost under database settings
4. Entering the name of the Moodle database (“moodle” in our guide)
5. Entering the username and password of the Moodle user that you created
6. Setting the tables prefix to mdl_
7. Creating a site administrator account and password

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

At this step, we are almost done with the installation and configuration of Moodle! The last step is to set the system paths, as it will provide better Moodle performance. To do this, open up the Moodle webpage again, and navigate to:

Site Administration > Server > System Paths

On this page, input the following information exactly as it is written here:
Path to Du: /usr/bin/du
Path to Apsell: /usr/bin/aspell
Path to dot: /usr/bin/dot

Finally, click on Save Changes to finish path addition.

Conclusion

Congratulations! After completing all of the above steps to install Moodle’s prerequisites, configure your system, and do the actual installation of Moodle, you should now have a working setup that you can access at http://IP.ADDRESS.OF.YOUR.SERVER/moodle! If this tutorial was of use to you, feel free to share it.