Installing Moodle on Ubuntu 20.x

The following are steps to install Moodle on Ubuntu 20.x using PHP 7.4. Your versions may differ.


Please note that the instructions on Moodle's page (https://docs.moodle.org/400/en/Step-by-step_Installation_Guide_for_Ubuntu#Step_2:_Install_Apache.2FMySQL.2FPHP ) are NOT correct.


There are some substantial errors on that page.

  1. They do not tell you to remove php8.1
  2. They tell you to reset the root password using a method that is confusing and causes problems.
  3. Their steps for SSL do not work.


Rather follow these instructions.



1. Install PHP and set up apache


Note that Moodle at present (2022) only likes php7.4 or older, so if the server system is preinstalled, you need to remove the existing php if it is the wrong version. First find out the version:


php -v


If it returns version higher than 7.4, e.g. version 8.1, you have to remove it. At present, Moodle does NOT like php 8.


sudo apt remove php8.1


Now tell it about the older php version repository:


sudo add-apt-repository ppa:ondrej/php -y

sudo apt update


1.1 Install other bits


apt-get install -y mysql-client mysql-server

apt-get install -y graphviz aspell ghostscript clamav php7.4-pspell php7.4-curl php7.4-gd php7.4-intl php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-ldap php7.4-zip php7.4-soap php7.4-mbstring

update-alternatives --set php /usr/bin/php7.4

a2enmod php7.4 

service apache2 restart

If it gives an error it might be a coredump due to having php8.1 residue. 


cd /etc/apache2/mods-available

rm php8*



2. Set up MySQL


After this, set up mysql. Login as root on mysql


mysql -u root -p


If you have just installed mysql for the first time, there should be no password. If there is a root password already and you do not know how to reset it, see the notes below on how to reset a root password on mysql.


2.1 First, create the database:

create database moodle;


When creating the moodle database, you need to set the owner - the user who will have write permissions. Obviously, "somepassword" is not a good password, so you must choose a better one.

create user 'moodle'@'localhost' identified with mysql_native_password by 'somepassword'; 


grant all on moodle.* to 'moodle'@'localhost'; 


flush privileges;

exit; 



2.2 If you forget the password, you can change it again like so:

alter user 'moodle'@'localhost' identified with mysql_native_password by 'somepassword';


flush privileges;

exit;


If you are using mysql version 8 or later, you need "with mysql_native_password" otherwise moodle doesn't authenticate the database owner. At least, that was still true at the time of writing this.


If it complains about the password being too weak, it just means it is not strong enough. Try add some punctuation marks to it and make it longer.



3. Resetting root password on mysql [OPTIONAL]


If you can't login as root on mysql, you need to reset the root password. IF you CAN login as root on your mysql system, IGNORE this step.


sudo su

service mysql stop

killall mysqld

killall mysqld

mysqld_safe --skip-grant-tables &

mysql -u root


You will then be inside the mysql command prompt

Type:


use mysql;

ALTER USER 'root'@'localhost' identified WITH mysql_native_password BY  'somepassword';


flush privileges;

exit;


Obviously choose a better password than "somepassword" and make sure you write it down because you will use it a few times later in this process.


You will now be back in bash commandline: type:


service mysql restart



4. Start services


At this point you can start the services. We use "Restart" in case we previously had to reset the mysqld password.


service apache2 start

service mysql restart 



5. Install Moodle itself


What the below instructions do, is install moodle from git, and then copy the installed copy to /var/www/ . The reason for this is so that if you update the moodle software via git, at a later stage, it does NOT overwrite the copy in /var/www/ which is your LIVE copy.


apt-get install git

cd /var/www

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

cd moodle

git branch -a

git git branch --track MOODLE_400_STABLE origin/MOODLE_400_STABLE

git checkout MOODLE_400_STABLE

ln -s /var/www/moodle /opt # in case it expects to find it in /opt

ln -s /var/www/moodle /var/www/html/moodle # in case it expects to find it in /var/www/html/moodle

mkdir -p /var/www/moodledata

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

chmod -R 777 /var/www/moodledata

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



6. Set up SSL


If we do not do this, then Moodle will complain about an SSL certificate. You can install that by installing Letsencrypt.


apt-get install letsencrypt

certbot certonly



Follow the onscreen prompts. It will ask you for your server name (website address), and where to put the certificate testing files (answer is /var/www/html). It should "just work". It looks like this. Items bold/underlined in RED below are parts you should type.


Saving debug log to /var/log/letsencrypt/letsencrypt.log


How would you like to authenticate with the ACME CA?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

1: Spin up a temporary webserver (standalone)

2: Place files in webroot directory (webroot)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Plugins selected: Authenticator webroot, Installer None

Enter email address (used for urgent renewal and security notices) (Enter 'c' to

cancel): your@email.address


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Please read the Terms of Service at

https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must

agree in order to register with the ACME server at

https://acme-v02.api.letsencrypt.org/directory

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(A)gree/(C)ancel: A


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Would you be willing to share your email address with the Electronic Frontier

Foundation, a founding partner of the Let's Encrypt project and the non-profit

organization that develops Certbot? We'd like to send you email about our work

encrypting the web, EFF news, campaigns, and ways to support digital freedom.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(Y)es/(N)o: Y

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'

to cancel): your.website.address

Obtaining a new certificate

Performing the following challenges:

http-01 challenge for your.website.address

Input the webroot for your.website.address: (Enter 'c' to cancel): /var/www/html/  

Waiting for verification...

Cleaning up challenges


IMPORTANT NOTES:

 - Congratulations! 


Then check the Apache Config file is right, it is in /etc/apache2/sites-enabled/000-default.conf


It should look like this after you have edited it. The RED font shows the SSL entry.


<VirtualHost *:80>
ServerName your.website.address
ServerAdmin your@email.address
DocumentRoot /var/www/html
ErrorLog /var/log/apache2//error.log
CustomLog /var/log//access.log combined
</VirtualHost>
<VirtualHost *:443>
    DocumentRoot /var/www/html
    Options All Indexes ExecCGI FollowSymLinks
    ErrorDocument 404 /error.html
    ServerName your.website.address
    #Include  /etc/letsencrypt/options-ssl-apache.conf 
    SSLCertificateFile  /etc/letsencrypt/live/your.website.address/fullchain.pem 
    SSLCertificateKeyFile /etc/letsencrypt/live/your.website.address/privkey.pem 
</VirtualHost>

If it does not look like that, then copy/paste the example above. I suggest you edit the above in Wordpad or Textedit before pasting, making sure that your website address (URL) is correct.

vi  /etc/apache2/sites-enabled/000-default.conf
i
PASTE
esc
use the cursor keys to find the things to change, and replace them e.g. with your website address
esc ZZ

Then type:


a2enmod ssl

service apache2 restart


7. Configure PHP


We must set the file limits in php:


cd /etc/php/7.4/apache2/

vi php.ini


Search by typing


/


followed by max_input_vars (so that it looks like so: /max_input_vars) and change the 1000 to 5000. To do this, use your cursor keys to move the cursor to the 1, and then type an R on the keyboard (lowercase R, however). Straight away, type 5. That will replace the 1 with a 5.

 

Search for the following items, using the forward-slash (/) again. Search for these items and set their values as follows (my suggestion):


memory_limit = 1024M

post_max_size = 2000M

upload_max_filesize = 2000M

max_file_uploads = 20


To edit these items, you just move the cursor to the position and press the key "i" on the keyboard to insert new letters. To exit that mode after typing what you want, e.g. to erase things, press ESC on the keyboard. You can now move around again and use the key "X" to erase characters that you do not want. Note that the semicolon ( ; ) in this file, is a "comment", it DISABLES anything that follows it. 


Once you are done to save and quit, hit the escape key: ESC

then press shift-Z twice:


ZZ


In the example above, I am assuming that you want your users to be able to upload 2 GB video files (hence the 2000M entry). If your users will not upload videos, then set these values smaller. E.g. if your users are likely to upload 100MB PDF files, then you want to set it to 100M. Etc. Be aware that this opens you to a DDOS (distributed denial of service) attack. Because end-users could knock you over by repeatedly uploading large files. Only set high values like this if you HAVE TO accept large files.



8. Restart apache to recognise changes to PHP and SSL


Set up a redirect-url in case the end-user does not go to the /moodle/ url:


rm /var/www/html/index.html

vi /var/www/html/index.html


Go into insert mode by pressing:

i


PASTE this:


<html>
<head>
<meta http-equiv="refresh" content="0; URL='https://my.website.address/moodle/'" />
</head>
<body>
</body>
</html>


ESC

Now, obviously cursor over to the wording "my.website.address" and replace with the actual address followed by /moodle/ as shown

ESC

ZZ


Type the following:


service apache2 restart


If it gives an error it might be a coredump due to having php8.1 residue which in 2022 we do not want for moodle. 


cd /etc/apache2/mods-available

rm php8*


Another cause of errors is the apache config file can't find its variables. In that case, when you type service apache start you might see this error:


Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xe" for details.


This means usually there's a syntax error in the config file OR it can't find its files.

You need to read the error message it gives and fix the error. Unfortunately there are so many possible errors I can't list them here. Just google them.



9. Final setup and configuration


Go to your website in your web browser. Let's say for argument's sake that you are installing this on your OWN server. You would then go to :


https://127.0.0.1/moodle/


If however your website is www.my.web.address then you go to :


https://www.my.web.address/moodle/


By the way: The part /moodle/ means the folder moodle that we copied earlier, and which is now located inside /var/www/html/moodle/ on the server. Anything on your server that is inside the /var/www/html/ folder will be accessible via: http://127.0.0.1/


At this point, you should have a web page asking you to choose which language you want to install moodle in. Just agree (select yes) for every option that comes up. From this point onwards you should be able to set it up yourself without further problems.

  • It will ask you for the database db user name. We selected "moodle". 
  • It will ask you for the password. Above we used "somepassword". 
  • It will ask you for the moodledata folder; that is /var/www/moodledata/  which we created in step 5 above.
  • It will ask you for the moodle application directory; that is /var/www/html/moodle/ which we created in step 5 above. 
  • If it asks you for the mysql port and the run file, ignore that, it will use defaults.

It will then continue the installation process and download other components. As it activates them in the database, it will show a green bar with the word "success". 


If it complains about permissions and says that moodledata is insecure, you can try two things:


9.1. Use /var/moodledata instead of /var/www/moodledata:


mv /var/www/moodledata/ /var/

ln -s /var/moodledata/ /var/www/moodledata


AND/OR


9.2. Set permissions on that folder:


chown -R www-data:www-data /var/moodledata/ /var/www/moodledata/

chmod -R 775 /var/www/moodledata /var/moodledata


If during the installation process it shows any item in pink or red rather than green, that is a problem you need to resolve. Generally this will be a missing file or software. For example, if it says it is setting up png-graphics, and then says "png-graphics not found" it means you just need to go back to the command line and type


apt-get install png-graphics


etc., for any red items that come up.


If I have missed any steps or you encountered any errors, let me know.

Popular posts from this blog

could not find course/ could not find top level course/ could not find course category

migrating moodle from one server to another

installing moosh to make your life easier