How To Install Moodle on Ubuntu 16.04

Introduction

Moodle is a popular and open-source web-based learning management system (LMS) that is free for anyone to install and use. With Moodle, you can create and deliver learning resources such as courses, readings, and discussion boards to groups of learners. Moodle also allows you to manage user roles, so students and instructors can have different levels of access to materials. Once you install Moodle on your web server, anyone with access to your site can create and participate in browser-based learning.

In this guide, you will install and set up Moodle on your Ubuntu 16.04 server. You’ll install and configure all the software required by Moodle, run through the setup wizard, choose a theme, and create your first course.

Prerequisites

Before you begin this guide you’ll need the following:

  • A 1GB Ubuntu 16.04 server with a minimum of 200MB of disk space for the Moodle code and as much as you need to store your content. Moodle requires 512MB of memory, but recommends at least 1GB for best performance.
  • A non-root user with sudo privileges and a firewall, which you can set up by following the Ubuntu 16.04 initial server setup guide.
  • The LAMP stack (Apache, MySQL, and PHP) installed by following this tutorial. Be sure to make a note of the root MySQL password you set during this process.

Step 1 — Install Moodle and Dependencies

Moodle relies on a few pieces of software, including a spell-checking library and a graphing library. Moodle is a PHP application, and it has a few additional PHP library dependencies as well. Before we install Moodle, let’s install all of the prerequisite libraries using the package manager. First, ensure you have the latest list of packages:

  • sudo apt-get update

Then install Moodle’s dependencies:

  • sudo apt-get install aspell graphviz php7.0-curl php7.0-gd php7.0-intl php7.0-ldap php7.0-mysql php7.0-pspell php7.0-xml php7.0-xmlrpc php7.0-zip

Next, restart the Apache web server to load the modules you just installed:

  • sudo systemctl restart apache2

Now we are ready to download and install Moodle itself. We’ll use curl to download Moodle from the official distribution server.

The following command will go to the Moodle website and get the compressed package that contains the entire current, stable version of Moodle into the file moodle.tgz. The -L flag tells curl to follow redirects.

Now we can uncompress the file with the tar program and place the resulting files in the web document root:

  • sudo tar -xvzf moodle.tgz -C /var/www/html

Verify that the moodle directory is in your server’s web root directory:

  • ls /var/www/html

You should see the moodle directory listed:

Output
index.html  moodle

Now view the files within the moodle directory:

  • ls /var/www/html/moodle

You will see all of the Moodle files and directories you just downloaded and uncompressed:

Output
admin           composer.json     grade          message                    README.txt
auth            composer.lock     group          mnet                       report
availability    config-dist.php   Gruntfile.js   mod                        repository
backup          config.php        help_ajax.php  my                         rss
badges          CONTRIBUTING.txt  help.php       notes                      search
behat.yml.dist  COPYING.txt       index.php      npm-shrinkwrap.json        tag
blocks          course            install        package.json               tags.txt
blog            dataformat        install.php    phpunit.xml.dist           theme
brokenfile.php  draftfile.php     INSTALL.txt    pix                        TRADEMARK.txt
cache           enrol             iplookup       plagiarism                 user
calendar        error             lang           pluginfile.php             userpix
cohort          file.php          lib            portfolio                  version.php
comment         files             local          PULL_REQUEST_TEMPLATE.txt  webservice
competency      filter            login          question
completion      githash.php       media          rating

Now we need to create a directory outside the web root for Moodle to store all the course-related data that will be stored on the server, but not in the database. It is more secure to create this directory outside the web root so that it cannot be accessed directly from a browser. Execute this command:

  • sudo mkdir /var/moodledata

Then set its ownership to make sure that the web service user www-data can access the directory:

  • sudo chown -R www-data /var/moodledata

Then change the permissions on the folder so that only the owner has full permissions:

  • sudo chmod -R 0770 /var/moodledata

Now that you’ve got Moodle on your server, it’s time to set up the database it’ll use.

Step 2 — Configuring the Database

We need to create the MySQL database where Moodle will store most of its data. We’ll create the structure that the Moodle code expects, and we’ll create a user that Moodle will use to connect to the database.

But first we need to make a few changes to the MySQL configuration file in order our MySQL installation to be compatible with Moodle. Open the MySQL configuration file:

  • sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Then add the following highlighted lines to the ‘Basic Settings’ area, which configure the storage type that new databases should use:

mysqld configuration
...
[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
default_storage_engine = innodb
innodb_file_per_table = 1
innodb_file_format = Barracuda
## Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
...

Save this file and then restart the MySQL server to reload the configuration with the new settings.

  • sudo systemctl restart mysql

Now we can create the Moodle database. In order to do this, you’ll interact with the MySQL command-line interface. Execute this command:

  • mysql -u root -p

When prompted, supply the root password you set when you installed MySQL.

Once logged in, you’ll see the mysql> prompt. Run the following command to create the database:

  • CREATE DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Then create a Moodle user, so we don’t have to tell the Moodle application what our root password is. Execute this command:

Note: In the next two commands, replace moodler with your Moodle username and moodlerpassword with a chosen password.

  • create user ‘moodler‘@’localhost’ IDENTIFIED BY ‘moodlerpassword‘;

And give the moodler user permission to edit the database. This user will need to create tables and change permissions:

  • GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO ‘moodler’@’localhost’ IDENTIFIED BY ‘moodlerpassword‘;

Now exit the MqSQL command-line interface:

  • quit;

That takes care of the database configuration. Now we can launch Moodle in a browser and continue the setup there.

Step 3 — Configuring Moodle in the Browser

To finish configuring Moodle, we’ll bring up the site in a web browser and provide it with some additional configuration details. In order for the web server to save the configuration, we need to temporarily alter the permission for the Moodle web root.

Warning:
The permissions open this folder up to everyone. If you are uncomfortable with doing this, simply don’t change the permission. The web interface will provide instructions for you to manually modify the configuration file.

If you do change the permissions, it is very important to undo this as soon as you have completed the setup. That step is included in this tutorial.

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

Now open up a browser and go to http://your_server_ip/moodle. You’ll see a page like the following.

Initial Moodle Setup Screen

Follow these steps to configure Moodle:

  1. Set the language you want to use and click Next.
  2. On the next screen, set the Data Directory to /var/moodledata and click Next.
  3. On the the Choose Database Driver page, set Database driver to Improved MySQL (native mysqli). Then click Next.
  4. On the Database setting page, enter the username and password for the Moodle MySQL user you created in Step 3. The other fields can be left as they are. Click Next to continue.
  5. Review the license agreement and confirm that you agree to its terms by pressing Continue.
  6. Review the Server Checks page for any possible issues. Ensure the message “Your server environment meets all minimum requirements” exists at the bottom and press Continue.
  7. Moodle will install several components, displaying “Success” messages for each. Scroll to the bottom and press Continue.
  8. You’ll then see a page where you can set up your administrator account for Moodle.
    1. For Username, enter anything you’d like, ar accept the default.
    2. For Choose an authentication method, leave the default value in place.
    3. For New password, enter the password you’d like to use.
    4. For Email, enter your email address.
    5. Set the rest of the fields to appropriate values.
    6. Click Update profile.
  9. On the Front Page Settings screen, fill in the Full site name, the Short name for site, set a location, and select whether you want to allow self-registration via email. Then click Save changes.

Once you’ve done this. you’ll be taken to the dashboard of your new Moodle installation, logged in as the admin user.

Now that your setup is complete, it’s important to restrict permissions to the Moodle web root again. Back in your terminal, execute the following command:

  • sudo chmod -R 0755 /var/www/html/moodle

Let’s make one more minor change to improve Moodle’s security. By default, Moodle creates files in the /var/moodledata folder with world-writeable permissions. Let’s tighten that up by changing the default permissions Moodle uses.

Open the Moodle configuration file in your editor:

  • sudo nano /var/www/html/moodle/config.php

Locate this line:

config.php
$CFG->directorypermissions = 0777;

Change it to the following:

config.php
$CFG->directorypermissions = 0770;

Then save the file and exit the editor.

Finally, reset the permissions on the /var/moodledata directory itself, as Moodle already created some world-writeable folders and during the installation process:

  • sudo chmod -R 0770 /var/moodledata

Now that Moodle is configured, let’s make a few customizations and create a test course to get a feel for the Moodle web interface.

Step 4 — Customizing Moodle and Creating Your First Course

Now that your site is running, one of the first things you night want to do is register your Moodle site. This will subscribe you to the Moodle mailing list which will keep you up to date about things like security alerts and new releases.

To register, click the Site Administration link in the box on the left, and click on Registration. Then fill out the web form with the appropriate details. You can also choose to publish your Moodle site so others can find it.

Next, let’s change the theme for our Moodle site. Select Site Administration, select the Appearance tab, and select Theme selector. You will see a page that looks like the following figure, indicating that you’re currently using the “Boost” theme on the Default device, which refers to a modern web browser:

Theme Selector Screen

Click the Change theme button and you’ll be taken to a screen that shows you other available themes. When you click on the Use theme button under a theme name, your Moodle site will use that theme to display all of your site’s content. You can also choose different themes for different devices, like tablets or phones.

Now that you’ve got your Moodle site closer to how you want it to look, it’s time to create your first course. Select Site home from the navigation menu. You’ll see an empty list of courses and an Add a new course button. Click that button to display a form that looks like the following figure:

Course Creation Screen

Fill in the information about your course, including the name, short name, a description, and any other relevant details. Then scroll to the bottom and click Save and display.

Your first Moodle course is now ready to go. You can start adding lessons and activities to the course using Moodle’s interface.

But before you start letting people sign up to take your new course, you should ensure your Moodle installation is ready for production. For starters, you’ll want to set up a TSL/SSL certificate for Apache to encrypt the traffic between your server and clients.

Open Ldap on ubuntu 16.04

What is OpenLDAP

OpenLDAP is an open-source and fast directory server that provides network client with directory services. Client applications connect to OpenLDAP server using the Lightweight Directory Access Protocol (LDAP) to access organizational information stored on that server.  Given the appropriate access, clients can search the directory, modify and manipulate records in the directory. OpenLDAP is efficient on both reading and modifying data in the directory.

OpenLDAP servers are most commonly used to provide centralized management of user accounts.

How to Install OpenLDAP Server on Ubuntu 16.04

Run the following command to install OpenLDAP server and the client command-line utilities from Ubuntu 16.04 package repository. slapd stands for the Stand-Alone LDAP Daemon.

sudo apt install slapd ldap-utils

You will be asked to set a password for the admin entry in the LDAP directory.

Once it’s done, slapd will be automatically started. You can check out its status with:

systemctl status slapd

Be default, it runs as the openldap user as is defined in /etc/default/slapd file.

Basic Post-Installation Configuration

The installation process installs the package without any configurations. To have our OpenLDAP server running properly, we need to do some basic post-installation configuration. Run the following command to start the configuration wizard.

sudo dpkg-reconfigure slapd

You will need to answer a series of questions. Answer these questions as follows:

Omit LDAP server configuration: NO.

openldap ubuntu

DNS domain name: Enter your domain name like linuxbabe.com. You will need to set a correct A record for your domain name. You can also subdomains like directory.linuxbabe.com. This information is used to create the base DN (distinguished name) of the LDAP directory.

install openldap ubuntu

Organization name: Enter your organization name like LinuxBabe.

ldap server configuration in ubuntu 16.04 step by step

Administrator password: Enter the same password set during installation.

openldap server ubuntu 16.04

Database backend: MDB.

BDB

(Berkeley Database) is slow and cumbersome. It is deprecated and support will be dropped in future OpenLDAP releases. HDB (Hierarchical Database) is a variant of the BDB backend and will also be deprecated.

MDB reads are 5-20x faster than BDB. Writes are 2-5x faster. And it consumes 1/4 as much RAM as BDB. So we choose MDB as the database backend.

openldap mdb

Do you want the database to be removed when slapd is purged? No.

install openldap server on ubuntu 16.04 LTS

Move old database? Yes.

openldap server configuration

Allow LDAPv2 protocol? No. The latest version of LDAP is LDAP v.3, developed in 1997. LDAPv2 is obsolete.

install ldap ubuntu

Now the process will reconfigure the OpenLDAP service according to your answers. Your OpenLDAP server is now ready to use.

openldap ubuntu 16.04 configuration

Configuring the LDAP Clients

/etc/ldap/ldap.conf is the configuration file for all OpenLDAP clients. Open this file.

sudo nano /etc/ldap/ldap.conf

We need to specify two parameters: the base DN and the URI of our OpenLDAP server. Copy and paste the following text at the end of the file. Replace your-domain and com as appropriate.

BASE     dc=your-domain,dc=com
URI      ldap://localhost

The first line defines the base DN. It tells the client programs where to start their search in the directory. If you used a subdomain when configuring OpenLDAP server, then you need to add the subdomain here like so

BASE      dc=subdomain,dc=your-domain,dc=com

The second line defines the URI of our OpenLDAP server. Since the LDAP server and client are on the same machine, we should set the URI to ldap://localhost.

Testing OpenLDAP Server

Now that OpenLDAP server is running and client configuration is done, run the following command to make test connections to the server.

ldapsearch -x

Output:

# extended LDIF
#
# LDAPv3
# base <dc=linuxbabe,dc=com> (default) with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#

# linuxbabe.com
dn: dc=linuxbabe,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: LinuxBabe

# admin, linuxbabe.com
dn: cn=admin,dc=linuxbabe,dc=com
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator

# search result
search: 2
result: 0 Success

# numResponses: 3
# numEntries: 2

Result: 0 Success indicates that OpenLDAP server is working. If you get the following line, then it’s not working.

result: 32 No such object

Installing phpLDAPadmin

phpLDAPadmin is a web-based program for managing OpenLDAP server. The command-line utilities can be used to manage our OpenLDAP server, but for those who want an easy-to-use interface, you can install phpLDAPadmin.

Run the following command to install phpLDAPadmin from Ubuntu package repository.

sudo apt install phpldapadmin

If your Ubuntu server doesn’t have a web server running, then the above command will install the Apache web server as a dependency. If there’s already a web server such as Nginx then Apache won’t be installed.

If you use Apache

The installation will put a configuration file phpldapadmin.conf under /etc/apache2/conf-enabled/ directory. Once the installation is done, you can access phpLDAPadmin web interface at

your-server-ip/phpldapadmin

or

your-domain.com/phpldapadmin

To enable HTTPS, you can obtain and install a free TLS certificate issued from Let’s Encrypt.

If you use Nginx

Nginx users will need to manually create a server block file for phpLDAPadmin.

sudo nano /etc/nginx/conf.d/phpldapadmin.conf

Copy the following text and paste it to the file. Replace ldap.your-domain.com with your preferred domain name.

server {
        listen 80;
        server_name ldap.your-domain.com;

        root /usr/share/phpldapadmin/htdocs;
        index index.php index.html index.htm;

        error_log /var/log/nginx/phpldapadmin.error;
        access_log /var/log/nginx/phpldapadmin.access;

        location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include fastcgi_params;
        }
}

Save and close the file. Then text Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

Now you can access phpLDAPadmin web interface at ldap.your-domain.com. To enable HTTPS, you can obtain and install a free TLS certificate issued from Let’s Encrypt.

Configuring phpLDAPadmin

We need to do some configurations just like we did with the command-line client. The phpLDAPadmin configuration file is at /etc/phpldapadmin/config.php .

sudo nano /etc/phpldapadmin/config.php

Since OpenLDAP and phpLDAPadmin are running on the same machine, so we will configure phpLDAPadmin to connect to localhost on the default LDAP port 389 without SSL/TLS encryption.

Line 293 specifies that phpLDAPadmin will connect to localhost.

$servers->setValue('server','host','127.0.0.1');

Line 296 is commented out by default, which means the standard port 389 will be used.

// $servers->setValue('server','port',389);

Line 335 is commented out by default, which means TLS encryption is not enabled.

// $servers->setValue('server','tls',false);

Then go to line 300.

$servers->setValue('server','base',array('dc=example,dc=com'));

Change it to:

$servers->setValue('server','base',array());

This will let phpLDAPadmin automatically detect the base DN of your OpenLDAP server. Next, you can disable anonymous login. Go to line 453.

// $servers->setValue('login','anon_bind',true);

By default, anonymous login is enabled. To disable it, you need to remove the comment character (the two slashes) and change true to false.

$servers->setValue('login','anon_bind',false);

You will probably want to disable template warnings because these warnings are annoying and unimportant. Go to line 161.

// $config->custom->appearance['hide_template_warning'] = false;

Remove the comment character and change false to true.

$config->custom->appearance['hide_template_warning'] = true;

Save and close the file.

Accessing phpLDAPadmin Web Interface

We can now test out the phpLDAPadmin tool with our web browser. When phpLDAPadmin first loads, it looks something like this.

phpldapadmin

To log into our OpenLDAP server, click on the login link. You will see the login dialog box. The default login DN is cn=admin,dc=example,dc=com. You may need to change dc=example. In my case, I need to change the login DN to cn=admin,dc=linuxbabe,dc=com.

openldap web interface

The password is the admin password you set during the configuration of OpenLDAP server. Once you log into phpLDAPadmin, you can manage this directory server.

phpldapadmin configuration

That’s it! I hope this tutorial helped you install and configure both OpenLDAP server and phpLDAPadmin on Ubuntu 16.04. In the next tutorial, we will see how to configure Ubuntu to authenticate user logins with OpenLDAP.