Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Installing Moodle on Debian based distributions.

Installing Moodle on Debian based distributions

From MoodleDocs

Introduction

Scope

  • This article documents how to install Moodle on Debian GNU/Linux and on distributions based on Debian, specifically Debian 6.0 “Squeeze” and Ubuntu Server 12.04 LTS. The same instructions should also be applicable across many other Debian and Ubuntu derivatives.
  • These instructions are generic for all the currently maintained versions of Moodle (1.9, 2.2, 2.3 and 2.4 as of Jan 2013). Even the version 1.6 works with slight modifications (see below).

  • Amoung a 'stand alone' setup which does not need a network and two setups of networking, 'LAN only' and 'Internet', are differentiated in this article. They are set in table form as in the table below.
    stand alone LAN only Internet
    You install Moodle on your Debian based computer so that it will be accessible only within that computer. This setup is ideal for testing Moodle and developing courses even when the computer is not connected to the Internet.

    Of course during the installation and later for upgrades you need Internet.

    We safely assume that your computer runs a graphical interface like Gnome, KDE, XFCE, etc.

    The Moodle computer is in a private network such as a LAN or a VPN, and Moodle is only accessible by the computers in that private network.

    With this setup a team can develop courses in collaboration, or run a Moodle for an institution, as long as all the users are in the same private network.

    Here too the server must have Internet access installation and later during upgrades.

    The Moodle server is accessible from the Internet. The machine has either a public IP address or is accessible from outside through port­forwarding. Ideally the computer also has a DNS name.
    Table 1. The three networking setups discussed
  • Please note that server maintenance including upgrading, taking backups, benchmarking, tuning, hardening, etc. are not part of this documentation.

Disclaimer

  • The Internet has become a rough terrain. You should not leave a machine connected to it, especially with a public IP address, unless you understand a whole lot about security. This article covers just the 'default setup', without any special security precautions. Be warned!
  • There is no substitute for thinking. For most part the commands here are self-explanatory. If you are in doubt, invest on building some background knowledge. These instructions are not 'Mantras' which you can cut and paste in to your command line and execute! If you do that, in the best case you will get an error message. The worst case could be anything!

Typography and conventions

Command line dialogs are set in mono typeface as in this example:

; Get the local time
$ date
Son Feb 30 09:50:00 CET 2012

The first line is of course a comment. In the second line you have just typed 'date' and got the response shown in the third line. Commands you enter are set in boldface - the response in regular type.

Sometimes parts of the commands are unique to your setup. They are set in slanted type. In the example below 'username' stands for an existing (Unix) username in your system.

$ last username
username    pts/0        172.18.222.53    Thu Feb  31 22:49 - 03:01  (04:11)
[...]

The command interpreter indicates its readiness by printing the command prompt, commonly called just the 'prompt'. The standard prompt for a non-privileged user is the $ sign - a hash (#) indicates the superuser. Caveat: Don't type the prompt!

Different interpreters have different prompts and speak different languages(!). For example the MySQL command-line tool prints 'mysql>'

mysql>  SELECT now();
+---------------------+
| NOW()               |
+---------------------+
| 2012-02-31 09:50:00 |

Setup needs superuser privileges

All the commands in this page are executed with superuser privileges. In Debian you login as the Unix superuser 'root'. Then all the commands you enter automatically have the maximum privileges.

Debian GNU/Linux 6.0 host tty1
host login: root
Password: [password_for_root]  (not shown)
(message of the day)
root@host: ~#

The convention in Ubuntu is to log in as a non-privileged user and prepend those commands which need superuser privileges with 'sudo' (do as superuser). For example:

Ubuntu 12.04.3 LTS host tty1
host login: user
Password: [password_for_user]
$ reboot
reboot: must be superuser.
$ sudo reboot
[sudo] password for user: [password_for_user]
... The system is going down for reboot NOW!

Adding sudo to every command in a long list of commands becomes tedious. Therefore in this article we assume that the non-privileged user has taken over a privileged shell by executing 'sudo -i'. From then on there is no need for the 'sudo'.

$ sudo -i
[sudo] password for user: [password_for_user]
# reboot
... The system is going down for reboot NOW!

Version history:

  • V 0.1 2012-02-10 (ratna) First draft, the OS and structure
  • V 0.2 2012-02-11 (ratna) Apache, more on type, table structure for network configs
  • V 0.3 2012-02-14 (ratna) MySQL, tar, CVS
  • V 0.4 2012-02-25 (ratna) Ubuntu 10.04, MySQL, Moodle 1.6.9, 1.9.16 finished
  • V 0.5 2012-03-01 (ratna) Basic LAMP ready. ToDo lighttpd, phpMyAdmin, PostgreSQL
  • V 0.6 2012-04-10 (ratna) phpMyAdmin, basic git
  • V 0.7 2012-04-15 (ratna) lighttpd, PostgreSQL removed
  • V 0.8 2012-04-30 (ratna) Ubuntu 12.04 LTS tested
  • V 0.9 2012-05-21 (ratna) Intro broken into subsections
  • V 0.91 2012-07-11 (ratna) Corrections to the intro
  • V 0.92 2012-08-27 (ratna) Get the code via git expanded
  • V 0.93 2012-11-10 (ratna) Ubuntu 12.04, MySQL 5.5
  • V 0.94 2013-01-04 (ratna) Get code, transparently from sourceforge.net
  • V 0.95 2013-01-05 (ratna) CVS servers vanishing

Install Linux

Install the system software

There are numerous ways of installing Linux. Usually one boots the computer initially from an external media and then transfer the operating system into an internal 'drive'. This guide summarizes the steps during an installation from a CD-ROM or an USB memory stick into an internal hard disk. For more details refer to the documentation of your distribution.

Important:

  • Do a minimal installation: i.e. no server components yet
  • If for a server, do not install a GUI. For a server, you are strongly recommended to get a server distribution, they don't have a graphical interface (GUI). In the case of Debian, the basic installation installs no GUI - and in the case of Ubuntu, the Server edition installs no GUI by default.
Debian 6.0 Ubuntu Server 12.04 LTS
  1. Boot from CD-1, in the Installer boot menu select 'Install' (not 'Graphical Install')
  2. Language for the installation process. It will also be the default language for the installed system.
  3. Location
  4. default locale, most likely en_US.UTF-8
  5. Keymap
  6. (Needs some time to detect hardware, load modules, try to configure the network via DHCP) Hostname
  7. Domain name. If proper server with DNS, enter your domain.
  8. Set a root password
  9. Set an account for a for a non-privileged user
  10. Partition disks: Guided or manual
  11. Finish partitioning, write the changes to disk.
  12. (Takes time to copy software)
  13. Configure the network mirror. Choose one closest to you. If your network has a HTTP-Proxy enter next or leave empty.
  14. Checking repositories, configuring apt, scanning the mirror...
  15. Important: In the final "Software selection", accept the default 'Standard system utilities" - don't tick anything else. This is the minimal installation mentioned above.
  16. Install Grub, in the boot sector: Yes
  17. Installation complete. Continue to reboot the new system.
  18. Boots through Grub
  19. Login as root
  20. Edit /etc/apt/sources.list to comment the line 'deb cddom: Official ...CD Binary .../ squeeze main'. Otherwise software update wants to read the CD all the time.
  1. Boot the machine from a bootable CD or memory stick
  2. Select the language of the boot screen
  3. The boot screen appears, select 'Install Ubuntu Server'
  4. Choose the language for the installation process (this will also be the default language of the final system)
  5. Choose the country where the machine is located
  6. Origin of the keyboard
  7. Keyboard layout
  8. Enter the host a name. We'll call it simply hostname
  9. Select the time zone where the computer is located
  10. Partition disks. The simplest variation would be 'Guided - user entire disk'
  11. Choose the correct disk, confirm writing the partition table to the disk
  12. "Installing the base system ..." (takes a couple of minutes)
  13. You are supposed to create a non-privileged user. Set a password - and don't forget those!
  14. Encrypting home partition: No
  15. HTTP-Proxy: Very likely to be none (empty). Your network administrator should tell you otherwise
  16. How do you want to manage upgrades? You are free, 'Install security updates automatically' won't be bad idea
  17. Software selection: In the list 'Software selection', select only 'OpenSSH server' - nothing else! (This is the 'minimal' installation mentioned above.)
  18. Further software will be copied..., 'Install the GRUB boot loader to the master boot record?' Yes
  19. Install finishes. Before rebooting requests you to remove the install media!

Table 2. Dialog during the installation of Linux

Configure network

stand alone LAN only Internet
You only need the 'localhost'. Therefore the /etc/network/interfaces file will be:
auto lo
iface lo inet loopback

The /etc/hosts file will define the localhost and a hostname

127.0.0.1        localhost
127.0.1.1        hostname
In a LAN you need an IP address, most likely a private address. Your /etc/network/interfaces will be accordingly:
auto lo
iface lo inet loopback

auto interface
iface interface inet static
  address the.ip.address
  netmask your.net.mask

and your /etc/hosts will define:

127.0.0.1         localhost
the.ip.address    hostname

If the LAN has DNS set, you need put its IP-address in /etc/resolv.conf:

nameserver ip.address.of.nameserver

If the LAN has a gateway to the Internet, by adding that to the interfaces file, your computer can connect to the Internet, for example to fetch updates. See the example in the next column.

If the computer is in the Internet, it will also have a 'gateway' and access to a domain name server. See to that your server name is properly resolved in the Internet. (ping yourserver.your.domain should show you the right IP-address, etc.) You must contact your hoster or the network administrators for further details.
auto lo
iface lo inet loopback

auto interface
iface interface inet static
  address the.ip.address
  netmask your.net.mask
  gateway your.gateway

and your /etc/hosts will contain your domain name:

127.0.0.1       localhost
the.ip.address  hostname.your.domain  hostname

and the resolver, /etc/resolv.conf, should be complete:

nameserver ip.address.of.nameserver 

Table 3. Network configurations

Update the system software

# apt-get update           ; updates the package list
# apt-get upgrade          ; upgrade the outdated packages

Install Apache

# apt-get install apache2          ; the 'meta package' apache2 fetches a whole lot
                                   ; of packages like ...
... apachd2-mpm-worker apache2-utils apache2-bin apache2-common ...
...

Once successfully completed the installation script starts the server daemon. Check it by calling the test page as described in the table below. You should get the "It works" page.

stand alone LAN only Internet
Visit the URL http://localhost/ in a browser in your computer from another computer in the LAN visit http://the.ip.address/ in a browser from any computer in the Internet visit the site http://the.dns.name/

Table 4. Testing Apache installation By Default Apache display the contents of /var/www/, which Apache calls the 'DocumentRoot'. This and all other configuration parameters of Apache are to be found under /etc/apache2/. See the relevant documentation in http://httpd.apache.org/docs/.

If you want to restart the web server in the future, you can do so by restarting the boot script:

# /etc/init.d/apache2 restart

Install MySQL

# apt-get install mysql-server
mysql-client-5.1 mysql-common mysql-server mysql-server-5.1 mysql-server-core-5.1
...
New password for the MySQL "root" user:
[       ]    ; don't forget it!
Setting up mysql-server ...

Test the success by logging in with the MySQL client:

# mysql -u root -p
Enter password: XXXXXX
...
mysql>                    ; you should be able to login successfully

Install PHP

PHP Core

# apt-get install php5
... apache2-mpm-prefork libapache2-mod-php5 php5-common
...
The following packages will be REMOVED:
 apache2-mpm-worker
...
# /etc/init.d/apache2 restart   ; restart the web server for it to register the changes

Test the success by requesting phpinfo. For that you have to create a file in the webroot containing just a single call to phpinfo() and visit it in the browser:

# editor /var/www/moodle/phpinfo.php   ; editor any text editor: nano, vi, ...
; add the single line below, save and exit.
<?php phpinfo(); ?> 
stand alone LAN only Internet
Visit http://localhost/moodle/phpinfo.php in the same computer. Visit http://the.ip.address/moodle/phpinfo.php from any computer in the LAN. Visit http://dnsname.of.server/moodle/phpinfo.php from any computer in the Internet.

Table 5. Checking phpinfo Keep in mind to delete this file once the installation is over. It gives too much information to potential intruders! Later the Moodle administrator has access to this information by visiting http://your.server/moodle/admin/phpinfo.php. # rm /var/www/moodle/phpinfo.php  ; phpinfo() gives away information to intruders!

PHP Modules

; All versions of Moodle need the following two modules
# apt-get install php5-gd php5-mysql 
; Since version 2.0 Moodle needs the following additional modules 
# apt-get install php5-curl php5-xmlrpc php5-intl

You need to restart Apache for it to register the new modules:

# /etc/init.d/apache2 restart

If you need to change configuration of the PHP modules for Apache2, the configuration file is /etc/php5/apache2/php.ini.

PHP CLI

# apt-get install php5-cli             ; possibly only Ubuntu requires this, in Debian comes with the core

You can check the version of the PHP interpreter with:

# /usr/bin/php -v
PHP 5.3.3-7+squeeze8 with Suhosin-Patch (cli) ...

Notice that the configuration file for the PHP CLI is /etc/php5/cli/php.ini.

Install Moodle

Get the code

By downloading the tar-archive

; Get the archive you need:
# wget http://download.moodle.org/download.php/direct/stable16/moodle-1.6.9.tgz
# wget http://download.moodle.org/download.php/direct/stable17/moodle-1.7.7.tgz
# wget http://download.moodle.org/download.php/direct/stable18/moodle-1.8.14.tgz
# wget http://download.moodle.org/download.php/direct/stable19/moodle-latest-19.tgz
# wget http://download.moodle.org/download.php/direct/stable20/moodle-latest-20.tgz
# wget http://download.moodle.org/download.php/direct/stable21/moodle-latest-21.tgz
# wget http://download.moodle.org/download.php/direct/stable22/moodle-latest-22.tgz
# wget http://download.moodle.org/download.php/direct/stable23/moodle-latest-23.tgz
# wget http://download.moodle.org/download.php/direct/stable24/moodle-latest-24.tgz
# wget http://download.moodle.org/download.php/direct/moodle/moodle-latest.tgz ; latest development version

The versions above are just a sample taken from the official download site http://download.moodle.org/. Refer that page for more details. Note that the actual files are fetched transparently from sourceforge.net.

; Untar the archive to /var/www/moodle
# tar xzvf moodle-whatever.tgz -C /var/www 

Download via CVS

# apt-get install cvs         ; 'course you need a CVS client
# cd /var/www
; In the following instructions replace SERVER with es, us, ... whatever appropriate
# cvs -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle login 
CVS password:                 ; no password, just press Enter
; Select one of these:
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_169 moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_177 moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_1814 moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_19_STABLE moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_2010 moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_21_STABLE moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_22_STABLE moodle
# cvs -z3 -d:pserver:anonymous@SERVER.cvs.moodle.org:/cvsroot/moodle co -P -r MOODLE_23_STABLE moodle
; from Version 2.4 upwards are not in the CVS

For details see: https://docs.moodle.org/en/CVS_for_Administrators.

Download via git

Git allows numerous ways of organizing your local repository. The example below shows the bare minimum you need to track the "upstream", the repository at git.moodle.org.

# apt-get install git ; install git if you haven't done it already
# cd /var/www         ; change to DocumentRoot
; In the command below use MOODLE_19_STABLE, MOODLE_20_STABLE, etc. in place of BRANCH_TAG
# git clone -b BRANCH_TAG git://git.moodle.org/moodle.git
Cloning into moodle...
..., done 

For details, see: https://docs.moodle.org/en/Git_for_Administrators.

Setup the moodledata directory

# mkdir /var/moodledata         ; This cannot be under the Apache Document Root /var/www
# chown www-data /var/moodledata  ; Apache server needs write permissions to moodledata

Create a database and a user

# mysql -p
Enter password:
mysql> CREATE DATABASE dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
query OK, 1 row affected (0.03 sec)
mysql> GRANT ALL PRIVILEGES ON dbname.* TO 'dbuser'@'localhost' IDENTIFIED BY 'dbpass';
Query OK, 0 rows affected (0.00 sec)
mysql> exit

Check the correctness by accessing the new database as dbuser:

# mysql -u dbuser -p
Enter password: [dbpass]
mysql> use dbname;
...
Database changed

Run the installation script

Before starting the installation script, make the moodle directory writable to the web server. This is necessary to allow the installer to create the configuration file 'config.php'.

# chown www-data /var/www/moodle

Initiate through the browser

stand alone LAN only Internet
Visit http://localhost/moodle from a browser within the computer From any computer in the LAN, call http://the.ip.address/moodle/ From any computer in the Internet, call http://dnsname.of.server/moodle/

Table 6. Initiate through the browser

Alternatively run the PHP CLI script

# php /var/www/moodle/admin/cli/install.php

The installation procedure is self-explanatory. Important parameters to remember note are:

Web address: http://localhost/moodle or http://the.ip.address/moodle or http://dnsname.of.server/moodle
Moodle Directory: /var/www/moodle
Data Directory: /var/moodledata
Type: Improved MySQL (native mysqli)
Database host: localhost
Database name: dbname
Database user: dbuser
Database password: dbpassword

These parameters get written to config.php, as in this example:

$CFG->dbtype    = 'mysqli';           // 'pgsql', 'mysqli', 'mssql' or 'oci'
$CFG->dblibrary = 'native';           // 'native' only at the moment
$CFG->dbhost    = 'localhost';        // eg 'localhost' or 'db.isp.com' or IP
$CFG->dbname    = 'dbname';           // database name, eg moodle
$CFG->dbuser    = 'dbuser';           // your database username
$CFG->dbpass    = 'dbpasssword';      // your database password
...
$CFG->wwwroot   = 'http://localhost/moodle'; // or http://the.ip.address/moodle or http://dnsname.of.server/moodle
$CFG->dataroot  = '/var/moodledata';

Note: MySQL changed the syntax of CREATE TABLE in version 5.5. Instead of TYPE=MyISAM (or InnoDB) now you have write ENGINE=MyISAM. http://dev.mysql.com/doc/refman/5.5/en/myisam-storage-engine.html Moodle 1.6 crashed due to this. The needed changes are recorded in this patch. [???]

Congratulations! Now you are the owner of a shiny Moodle site! Don't foget to secure the moodle directory and config.php though:

# chown root /var/www/moodle
# chown root:root /var/www/moodle/config.php
# chmod 644 /var/www/moodle/config.php

Setup cron

The script cron.ini has to be run regularly for Moodle to finish pending tasks like sending forum posts as mail. You could add a line to the roots crontab.

# crontab -e
; add a single line similar to the following, save and exit.
*/15 * * * *        /usr/bin/php /var/www/moodle/admin/cron.php      ; for Moodle 1.x
*/15 * * * *        /usr/bin/php /var/www/moodle/admin/cli/cron.php  ; for Moodle 2.x

See https://docs.moodle.org/en/Cron_with_Unix_or_Linux for details.

Optionally, install phpMyAdmin

# apt-get install phpmyadmin
dbconfig-common javascript-common libjs-mootools libmcrypt4 php5-mcrypt ...
...
Web server to reconfigure automatically:
  [*] apache2              ; select Apache
  [ ] lighttpd
Configure database with dbconfig-common? Yes
Enter MySQL root password;
stand alone LAN only Internet
w3m http://localhost/phpmyadmin/ From any computer in the LAN, call http://the.ip.address/phpmyadmin/ From any computer in the Internet, call http://dnsname.of.server/phpmyadmin/

Table 7. Call phpMySQL

Appendix

External links