How to Install Moodle on Ubuntu Linux with Apache

presentation 1794128 640
presentation 1794128 640

This post shows students and new users steps to install Moodle course management platform on Ubuntu with Apache web server and free Let’s Encrypt SSL certificate to make sure visitors are communicating over HTTPS only.

Moodle is an open-source course management system that works great for professionals, businesses, and colleges who want to run and manage their courses or training materials online securely.

It is probably the best and most popular open-source learning management platform available today. However, if you’re going to be setting up Moodle in your environment, make sure to run it over HTTPS.

This setup might take a while to complete and the process below should work on other Linux platforms as well.

To get started with installing Moodle on Ubuntu Linux with Apache and Let’s Encrypt, follow the steps below.

How to setup your domain

If you’re going to be using Let’s Encrypt, you should then make sure your domain is configured correctly. This setup assumes that your domain name is called and is pointing to your server with IP address

Don’t forget to also make sure www CNAME is pointing to the domain name. Should look like something below:        A       ==========>
www               CNAME    ==========>

How to install Apache on Ubuntu Linux

As mentioned above, we’re going to be using the Apache web server to run Moodle. Moodle requires a web server to function, and Apache is one of the most popular open-source web servers available today.

To install Apache on Ubuntu, run the commands below:

sudo apt update
sudo apt install apache2

After installing Apache, the commands below can be used to stop, start and enable Apache services to always start up every time your server starts up.

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

To test whether Apache is installed and functioning, open your web browser and browse to the server’s IP address or hostname.


If you see the above page in your browser, then Apache is working as expected.

How to install MariaDB on Ubuntu Linux

A database server is required for Moodle to function. Moodle stores its content in a database, and MariaDB is probably the best database server available to run Moodle.

MariaDB is fast, secure and the default server for almost all Linux servers. To install MariaDB, run the commands below:

sudo apt install mariadb-server
sudo apt install mariadb-client

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

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

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, use the guide below to answer:

If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] n

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

All done!

To verify and validate that MariaDB is installed and working, log in to the database console using the commands below:

sudo mysql -u root -p

You should automatically be logged in to the database server since we initiated the login request as root. Only the root can log in without a password, and only from the server console.

mariadb welcome

If you see a similar screen as shown above, then the server was successfully installed.

Next, run the commands below to open the MariaDB default config file…

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

Then add the highlighted lines below and save.

* Basic Settings
user = mysql
pid-file = /run/mysqld/
socket = /run/mysqld/mysqld.sock
#port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_large_prefix = ON

Restart MariaDB after that.

sudo systemctl restart mariadb.service

How to install PHP on Ubuntu Linux

As we also mentioned above, we’re installing PHP on Ubuntu since Moodle requires it. PHP packages are added to Ubuntu repositories. The versions of the repositories might not be the latest. If you need to install the latest versions, you’ll need to add a third-party PPA repository.

To a third-party repository with the latest versions of PHP, run the commands below.

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

At the time of this writing, the latest PHP version is 7.4.

sudo apt update

Next, run the commands below to install PHP 7.4 and related modules.

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip

Next, you’ll want to change some PHP configuration settings that work great with Moodle. Run the commands below to open the PHP default configuration file.

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

Then change the line settings to be something like the lines below. Save your changes and exit.

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

How to create Moodle database on Ubuntu

At this point, we’re ready to create Moodle database. As mentioned above, Moodle uses databases to store its content.

To create a database for Moodle, run the commands below:

sudo mysql -u root -p

Then create a database called moodle


Next, create a database user called moodleuser and set a 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' WITH GRANT OPTION;

Finally, save your changes and exit.


How to download and install Moodle on Ubuntu Linux

To get Moodle’s latest release you may want to use the GitHub repository. Install Curl and other dependencies to get started.

sudo apt install git curl

After installing git and curl above, change into the Apache2 root directory and download Moodle packages from Github. Always replace the branch number with the latest branch. The current major version is 36.

cd /var/www/html
sudo git clone -b MOODLE_36_STABLE git://
sudo mv moodle /var/www/

Then run the commands below to set the correct permissions for Moodle to function.

sudo mkdir -p /var/www/moodledata
sudo chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www/

How to configure Apache for Moodle

Next, configure the Apache 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

sudo nano /etc/apache2/sites-available/

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

<VirtualHost *:80>
  DocumentRoot /var/www/
  <Directory /var/www/>
       Options FollowSymlinks
       AllowOverride All
       Require all granted

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

Save the file and exit.

Now that the configuration file is created, run the commands below to enable it.

sudo a2ensite

At this stage, Moodle is ready and can be launched by going to the server’s IP or hostname.


However, we want to make sure our server is protected with Let’s Encrypt free SSL certificates. So, continue below to learn how to generate a Let’s Encrypt SSL certificate for websites.

How to setup Let’s Encrypt for Moodle

We have written a great post on how to generate and manage Let’s Encrypt SSL certificates for Apache web servers. You can use that post, to apply it here for your Moodle website.

To read the post on how to generate Let’s Encrypt SSL certificates for a website, click on the link below:

How to Setup Let’s Encrypt on Ubuntu Linux with Apache – Website for Students

If you were successful in generating a Let’s Encrypt SSL certificate, you should then reopen the server block for our Moodle website by running the commands below.

sudo nano /etc/apache2/sites-available/

The new Moodle server block configurations should look similar to the line below. Take notes of the highlighted lines.

  • The first server block listens on port 80.  It contains a 301 redirect to redirect HTTP to HTTPS.
  • The second server block listens on port 443. It contains a 301 redirect to redirect www to the non-www domain.
<VirtualHost *:80>

  Redirect permanent /

<VirtualHost *:443>
  DocumentRoot /var/www/

  Protocols h2 http:/1.1

  <If "%{HTTP_HOST} == ''">
    Redirect permanent /
  ErrorLog ${APACHE_LOG_DIR}/
  CustomLog ${APACHE_LOG_DIR}/ combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/
  SSLCertificateKeyFile /etc/letsencrypt/live/
  SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

  SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
  SSLCompression off
  SSLUseStapling on

  Header always set Strict-Transport-Security "max-age=63072000"

  <Directory /var/www/>
       Options FollowSymlinks
       AllowOverride All
       Require all granted

Save the file above, then restart Apache and PHP using the commands below.

sudo systemctl reload apache2

Finally, if everything went as planned, you should be able to start Moodle setup wizard by browsing to the server hostname or IP address over HTTPS.

Then follow the on-screen instructions and select the installation language here.

Moodle Ubuntu setup

Next, select the MariaDB connection driver and continue.

ubuntu moodle install

On the next screen, enter the database connection info you created above and continue.

Ubuntu install moodle

Then create an admin account and the Moodle site info and finish the installation.

On this page, you should configure your main administrator account which will have complete control over the site. Make sure you give it a secure username and password as well as a valid email address. You can create more admin accounts later on.

Moodle wizard ubuntu

Log in and begin configuring your Moodle website.

Ubuntu moodle setup

In the future when you want to upgrade to a newly released version, simply run the commands below to upgrade.

How to upgrade Moodle on Ubuntu Linux

First, stop the web server…

sudo systemctl stop apache2

For students and new users who already have Moodle installed and wish to upgrade, assuming that you followed the steps above to install, run the commands below to backup your old Moodle folder…

sudo mv /var/www/ /var/www/example.com_bak

Then change into the web server root directory and download the latest version of Moodle from Github…. always change the version number to the current (latest)

cd /var/www/html
sudo git clone -b MOODLE_37_STABLE git://

Next, copy Moodle config file, theme, and data folder… If you updated your themes… theme content should be there…. If you also installed additional modules… you should find them in the /mod directory… copy them to the new Moodle folder….

sudo cp /var/www/example.com_bak/config.php /var/www/
sudo cp -pr /var/www/example.com_bak/theme/mytheme /var/www/
sudo cp -pr /var/www/example.com_bak/mod/mymod /var/www/

After that, update the web server permissions.

sudo chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www/

Restart your web server.

sudo systemctl start apache2

The last step is to trigger the upgrade processes within Moodle…. If you put your site into Maintenance mode earlier; take it out now!

Once you browse to the server IP or hostname, Moodle should prompt you to begin upgrading your database… After upgrading the database, login to Moodle and go to:

Administration > Site administration > Notifications.

Moodle will automatically detect the new version and perform all the SQL database or file system upgrades that are necessary. If there is anything it can’t do itself (very rare) then you will see messages telling you what you need to do.

Assuming all goes well (no error messages) then you can start using your new version of Moodle and enjoy the new features!

Moodle Upgrade ubuntu

That should do it!


This post showed you how to install Moodle on Ubuntu Linux with Apache and Let’s Encrypt. If you find any error above or have something to add, please use the comment form below.

Posted by
Richard W

I love computers; maybe way too much. What I learned I try to share at


  1. Hi there
    I get a The page isn’t redirecting properly error when i try to load moodle.
    Any ideas on how to fix this?

  2. Remove this from sites-enabled

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) index.php [PT,L]

    and it will work

    1. I have removed that from .conf but still I’m getting a non-css page. The login form does not login user and the css or js is not loading. However, before SSL setting, everything was working fine.

      Any ideas what to change?

      1. i remove this part on mine

        Redirect permanent /

  3. I am facing the same issue. No CSS is loading on my moodle website. Only plane HTML text. Is there any solution to this?

Leave a Reply

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

%d bloggers like this: