How to Install Moodle on Ubuntu Linux with Apache

|

,

|

This article provides a detailed guide on installing the Moodle course management platform on Ubuntu using Apache web server and Let’s Encrypt SSL certificate. The process includes domain setup with IP configuration, installation of Apache, MariaDB, and PHP, Moodle database creation, Moodle installation, Apache configuration for Moodle, setting up Let’s Encrypt, and Moodle setup wizard.…

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 ensure visitors communicate over HTTPS only.

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

To run Moodle, you need a web server, a database server, and PHP. Apache is one of the most popular open-source web servers available today, and Ubuntu is a popular Linux distribution widely used for web hosting and server management.

Installing Moodle on Ubuntu Linux with Apache ensures a stable and secure environment for running and managing courses or training materials online.

Follow the steps below to install Moodle on Ubuntu Linux with Apache and Let’s Encrypt.

How to set your domain

If you’re using Let’s Encrypt, you should then ensure your domain is configured correctly. This setup assumes that your domain name is called example.com and is pointing to your server with IP address 192.168.1.2

Don’t forget also to make sure www CNAME is pointing to the domain name. It should look like something below:

example.com        A       ==========>    192.168.1.2
www               CNAME    ==========>    example.com

How to install Apache on Ubuntu Linux

As mentioned above, we will use 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 stop, start, and enable Apache services to 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.

http://localhost

Apache works as expected if you see the above page in your browser.

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 and secure and is 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 always to 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 from the server console without a password.

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.

[mysqld]
#
* Basic Settings
#
user = mysql
pid-file = /run/mysqld/mysqld.pid
socket = /run/mysqld/mysqld.sock
#port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
#skip-external-locking
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 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.

Run the commands below to a third-party repository with the latest versions of PHP.

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 great PHP configuration settings 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 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 a 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

CREATE DATABASE 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.

FLUSH PRIVILEGES;
EXIT;

How to download and install Moodle on Ubuntu Linux

You may want to use the GitHub repository to get Moodle’s latest release. 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://git.moodle.org/moodle.git example.com
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 example.com.conf

sudo nano /etc/apache2/sites-available/example.com.conf

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>
  ServerName example.com
  ServerAlias www.example.com
  ServerAdmin admin@example.com
  DocumentRoot /var/www/example.com
    
  <Directory /var/www/example.com/>
       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.

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

sudo a2ensite example.com.conf

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

http://localhost

However, we want to protect our server 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 generating and managing 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 successfully generated a Let’s Encrypt SSL certificate, you should reopen the server block for our Moodle website by running the commands below.

sudo nano /etc/apache2/sites-available/example.com.conf

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>
  ServerName example.com
  ServerAlias www.example.com

  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com

  Protocols h2 http:/1.1

  <If "%{HTTP_HOST} == 'www.example.com'">
    Redirect permanent / https://example.com/
  </If>
  
  ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
  CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
  
  SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384

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

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

  <Directory /var/www/example.com/>
       Options FollowSymlinks
       AllowOverride All
       Require all granted
  </Directory>
 
</VirtualHost>

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 the Moodle setup wizard by browsing to the server hostname or IP address over HTTPS.

https://example.com/

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

Next, select the MariaDB connection driver and continue.

Enter the database connection info you created above on the next screen and continue.

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

You should configure your main administrator account on this page, which will completely control the site. Ensure you give it a secure username and password and a valid email address. You can create more admin accounts later on.

Log in and begin configuring your Moodle website.

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/example.com /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://git.moodle.org/moodle.git example.com

Next, copy the 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/example.com
sudo cp -pr /var/www/example.com_bak/theme/mytheme /var/www/example.com/theme/mytheme
sudo cp -pr /var/www/example.com_bak/mod/mymod /var/www/example.com/mod/mymod

After that, update the web server permissions.

sudo chown -R www-data:www-data /var/www/example.com/
sudo chmod -R 755 /var/www/example.com/

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 necessary SQL database or file system upgrades. 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), you can start using your new version of Moodle and enjoy the new features!

That should do it!

Conclusion:

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.


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



5 responses to “How to Install Moodle on Ubuntu Linux with Apache”

  1. Mark Avatar
    Mark

    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?
    Thanks

  2. Ilir Avatar
    Ilir

    Remove this from sites-enabled

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

    and it will work

    1. Najam Avatar
      Najam

      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. dennis Avatar
        dennis

        i remove this part on mine

        Redirect permanent / https://example.com/

  3. random Avatar
    random

    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 to dennisCancel reply

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

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading