Setup Moodle CMS on Ubuntu 18.04 LTS with Apache2, MariaDB and PHP 7.1 Support

I recently got Ubuntu 18.04 LTS Server installed on my VMware Workstation lab machine and began testing different applications and settings… and the steps below is how I got Moodle CMS installed on Ubuntu 18.04 LTS server with Apache2, MariaDB and PHP 7.1 support.

Moodle is an open source course manage system (CMS) written and based on PHP… it allows learning institutions and organizations create powerful courses for students and other users… Moodle is used by many distance learning institutions around the world to provide courses for their students… it’s free and released under the GNU general public license.

We’re going to show you how to install and start using Moodle CMS on your Ubuntu 18.04 LTS Server

To get started with installing Moodle follow the steps below:

Step 1: Install Apache2 HTTP Server

Moodle needs a web server and the most popular webserver in use today is Apache2. So, go and install Apache2 on Ubuntu by running the commands below:

sudo apt update
sudo apt-get install apache2

After installing Apache2, run the commands below to disable directory listing.

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

Next, run the commands below to stop, start and enable Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 2: Install MariaDB Database Server

Moodle needs a databse server.. and MariaDB database server is a great place to start. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

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

After that, run the commands below to secure MariaDB server.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat 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

After that, open MariaDB default configuration file by running the commands below:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Then add the below lines just below [mysqld] section.

default_storage_engine = innodb
innodb_file_per_table = 1
innodb_file_format = Barracuda
innodb_large_prefix = 1

Save the file and exit.

Restart MariaDB server

sudo systemctl restart mysql.service

Step 3: Install PHP 7.1 and Related Modules

PHP 7.1 may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1

sudo apt update

Finally, run the commands below to install PHP 7.1 and related modules..

sudo apt install php7.1 libapache2-mod-php7.1 php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-ldap php7.1-zip php7.1-curl

After install PHP, run the commands below to open PHP-FPM default file.

sudo nano /etc/php/7.1/apache2/php.ini

Then change the following lines below in the file and save. You may increase the value to suite your environment.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
cgi.fix_pathinfo = 0
date.timezone = America/Chicago

Step 4: Create Moodle Database

Now that you’ve installed all the packages that are required, continue below to start configuring the servers. First run the commands below to create a blank Moodle database.

Run the commands below to logon to MariaDB database server

sudo mysql -u root -p

Then create a database called moodle

CREATE DATABASE moodle;

Create a database user called moodleuser with new password

CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON moodle.* TO 'moodleuser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Step 5: Download Moodle Latest Release

Next, run the commands below to download Moodle latest release. The commands below to download Moodle archive package.

cd /tmp && wget https://download.moodle.org/download.php/direct/stable33/moodle-latest-33.tgz

Then run the commands below to extract the downloaded file to Apache2 default root.

tar -zxvf moodle-latest-33.tgz
sudo mv moodle /var/www/html/moodle
sudo mkdir /var/www/html/moodledata

Change modify the directory permission.

sudo chown -R www-data:www-data /var/www/html/moodle/
sudo chmod -R 755 /var/www/html/moodle/
sudo chown www-data /var/www/html/moodledata

Step 6: Configure Apache2

Finally, configure Apahce2 site configuration file for Moodle. This file will control how users access Moodle content. Run the commands below to create a new configuration file called moodle.conf

sudo nano /etc/apache2/sites-available/moodle.conf

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

<VirtualHost *:80>
     ServerAdmin admin@example.com
     DocumentRoot /var/www/html/moodle/
     ServerName example.com
     ServerAlias www.example.com
 
     <Directory /var/www/html/moodle/>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save the file and exit.

Step 7: Enable the Moodle site and Enable ReWtire Module

After configuring the VirtualHost above, enable it by running the commands below

sudo a2enmod rewrite
sudo a2ensite moodle.conf

Step 8 : Restart Apache2

To load all the settings above, restart Apache2 by running the commands below.

sudo systemctl restart apache2.service

After that, open your browser and browse to the server IP address or hostname and you should see Moodle default setup wizard page.

http://example.com

Verify that the path and info are correct.. then click Next.

Choose the database type and click Next.

Type the database name, username and the password for the user and continue.

After that Moodle should install and complete. If you run into trouble, go back and look at the tutorial to make sure you follow all the steps.

 

Complete all the info and save.. This is how to install Moodle on Ubuntu server.

Enjoy!

You may also like the post below:

Install Moodle CMS on Ubuntu 18.04 LTS with Nginx, MariaDB and PHP 7.1 Support

13 Replies to “Setup Moodle CMS on Ubuntu 18.04 LTS with Apache2, MariaDB and PHP 7.1 Support

  1. work like a charm!!
    I just had one problem with the privileges of the /var/www folder, where moodle cannot write to it.

    what I did is `chmod -R 777 /var/www` and its solve the problem

  2. Hello there.

    I’m trying to follow your tutorial, but I can’t get past the third line. There is no such thing as “mariadb-server” or “mariadb-client” on the Ubuntu repos. Did you mean “mysql-server” and “mysql-client”?

    Thanks

      1. It does and it doesn’t.

        Ubuntu 18.04 has “universe” repository DISABLED by default. And guess where those packages are…
        So, before installing any “dangerous” package from “universe” (mariadb-server/mariadb-client/php7.1-zip/php7.1-mcrypt) you need to ENABLE that repository with:

        “sudo add-apt-repository universe”

        I know it sounds weird, but that is what is happening here at least. Using VirtualBox and Ubuntu 18.04.1 server (amd64).

        I’m writing a script with those modifications and some automations for names and such, if anyone is interested, please, tell me how to share it.

  3. I had a similar problem as the first poster regarding the /var/www/moodledata directory not able to be created. I got around that. But when I got to the moodle installation via the browser and the status check page, it reported that my version of mysql was not sufficient “version 5.5.31 is required and you are running 5.5.5.10.1.34.0.0.18.04.1”
    Any thoughts on how to correct this?

  4. Thanks a lot for this clearly laid out installation process. Just a note that innodb_file_format now automatically defaults to Barracuda and does not need to be set in 50-server.cnf. Same as innodb_large_prefix. Including these in the cnf file throws an error. Also, as suggested by others, you should include side notes on how to set the bionic repositories for 18.04 if installation of MariaDB fails.
    Cheers

  5. Help me please

    Parent directory (/var/www) is not writeable. Data directory (/var/www/moodledata) cannot be created by the installer.

    Thank

  6. What I’ve to do?
    Error: Database connection failed
    It is possible that the database is overloaded or otherwise not running properly.
    The site administrator should also check that the database details have been correctly specified in config.php
    Warning: mysqli::__construct(): (HY000/2002): Invalid argument in /var/www/html/moodle/lib/dml/mysqli_native_moodle_database.php on line 79

  7. Installed OK but I am having problems emailing to new users. I got this message:
    Failed to send an email from the user with id ‘2’ to the user with id ‘3’ due to the following error: “Invalid address: (punyEncode) noreply@185.166.213.219“.

    Any clue?? thanks in advance

  8. Thanks for sharing the tutorial, it is very useful. With some minor improvements the steps result in a working installation.
    I had to change the string 5.5.31 to 5.5.5 in /var/www/html/moodle/admin/environment.xml to allow beginning installation.
    Another method to fix the problem with the data directory is to issue the commands “mkdir /var/www/moodledata; chown www-data /var/www/moodledata”.

    1. Very good.
      There seems to be small error: Instead of this:
      sudo mkdir /var/www/html/moodledata
      This should be:
      sudo mkdir /var/www/moodledata

    2. can you show me the process how to change the string string 5.5.31 to 5.5.5 in /var/www/html/moodle/admin/environment.xml to allow beginning installation.? that is my problem

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: