cPanel Shared Hosting Installation
Overview
Shared hosting is a good choice for providing internet access for a small number of students on a self managed Moodle site at a moderate cost. Using cPanel is a relatively easy way to install Moodle but you may soon run into performance problems and restrictions on student numbers.
This method sets up moodle as a sub-folder in your domain ie www.example.com/moodle. This allows other applications to be run on the same domain.
These insructionns
- A hosting and domain package with a SSL Certificate. (Check by going to https://example.com (Change example.com to your own site name) You should see a landing page.
- To follow this guide, these cPanel Tools ( PHP Selector, phpMyAdmin, Database Wizard or Manage Databases, Terminal and File Manager) must be available.
Note on cPanel, there is a search box to allow you to find tools.
- Go to PHP Selector. Moodle 5.1 requires php 8.2 or higher
- Go to phpMyAdmin and find Database type and version. Moodle 5.1 requires a minimum of MySql 8.4, MariaDB 10.11.0, or PostgreSQL 13.
If your hosting service does not meet these requirements, this installation will fail
You can:.
- Ask your provider for an upgrade
- Find another provider
What you need to know before starting a Moodle installation
- The type of database you have (Moodle will ask this during installation)
- The name of your home directory (look on top of the tree at the left of cPanel File Manager). On most shared hosting, it is /home/username)
- The location of the web root. This is where your html files are placed and usually it is called public_html
Set up PHP for Moodle
- Log in to cPanel and click on PHP Selector.
- Choose PHP 8.3 from the available versions. (Version 8.2 also meets Moodle requirement.'
- Enable required PHP extensions by checking these at minimum: `sodium`, `curl`, `openssl`, `mbstring`, `xml`, `intl`, `json`, and `fileinfo`.
- Adjust PHP settings in the Options tab:
- Set memory_limit to at least 128M. -
- Ensure max_input_vars is 5000 or higher.
- Confirm file_uploads is On.
- Save or apply the changes.
Database settings
Go to the cPanel phpMyAdmin tool to find your database type.
- It may be mariadab, mysql, or postgresql. The web based install will ask this later.
On the cPanel phpMyAdmin, set your character set and collation.
- Character set: Use utf8mb4 (full UTF-8 support) to handle all Unicode characters.
- Collation: Use utf8mb4_unicode_ci for case-insensitive, language-aware sorting
Use cPanel Terminal for Code and Composer
Go to Terminal in cPanel and paste these commands one after another, pushing enter after each line.
git clone -b v5.2.2 https://github.com/moodle/moodle ~/moodle
mkdir ~/moodledata
chmod 755 ~/moodledata ~/moodle
ln -s ~/moodle/public ~/public_html/moodle
cd ~
mkdir -p bin
curl -sS https://getcomposer.org/installer | php
mv composer.phar bin/composer
echo "alias composer='php ~/bin/composer'" >> ~/.bashrc
source ~/.bashrc
cd ~/moodle
composer install --no-dev --classmap-authoritative
These commands will
- Obtain moodle and place it in your home directory
- Create a moodledata folder to hold files uploaded to moodle
- Set permissions for the folderexa
- Create a link so your moodle will open in example.com/moodle
- Install composer and create the vendor directory
Create a .htaccess file for Router and Public/Private Paths
Now you must create a .htaccess file in the /moodle/public directory using nano. The command will open the file for editing. Copy and paste all the lines, than enter CTRL O ( that is. push both the ctrl and O keys on your keyboard at the same time, push enter to accept the name, then press CTRL X to exit. You could also edit the file in your file manager but remember to put the dot in .htaccess file name,
nano .htaccess
These are the lines to be copied into the .htaccess file:
DirectoryIndex index.php
# Enable SymLink following explicitly for this directory context
Options +FollowSymLinks
# Disable directory browsing safely
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
# ==========================================
# 1. PATH SECURITY RESTRICTIONS
# ==========================================
RewriteRule "(\/vendor\/)" - [F]
RewriteRule "(\/node_modules\/)" - [F]
RewriteRule "(^|/)\.(?!well-known\/)" - [F]
RewriteRule "(composer\.json)" - [F]
RewriteRule "(\.lock)" - [F]
RewriteRule "(\/environment.xml)" - [F]
RewriteRule "(\/install.xml)" - [F]
RewriteRule "(\/README)" - [F]
RewriteRule "(\/readme)" - [F]
RewriteRule "(\/moodle_readme)" - [F]
RewriteRule "(\/upgrade\.txt)" - [F]
RewriteRule "(\/UPGRADING\.md)" - [F]
RewriteRule "(phpunit\.xml\.dist)" - [F]
RewriteRule "(\/tests\/behat\/)" - [F]
RewriteRule "(\/fixtures\/)" - [F]
RewriteRule "(\/upgrade\.txt|UPGRADING\.md|UPGRADING\-CURRENT\.md)" - [F]
# ==========================================
# 2. CATCH-ALL ROUTING FALLBACK
# ==========================================
# If the file or directory does not exist physically on disk, route to r.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ r.php [QSA,L]
</IfModule>
Web based Moodle Installation
Go to https://example.com/moodle and follow the instructions.
- First screen - Choose your language.
- Second screen - Confirm paths
- Web Address: https://example.com/moodle (Replace example.com with your actual domain name)
- Moodle Directory: /home/cpanelname/public_html/moodle (Replace cpanelname with your actual cPanelname, find it on File Manager if you are not sure)
- Data Directory : /home/cpanelname/moodledata
- Third screen - Choose database driver (a choice of MySql, MariaDB or PostgreSQL). Check phpAdmin if you have forgotten.
- Fourth screen -Database settings
- Database settings: localhost
- Database name
- Now we have to set up the database in cPanel Database Wizard.
- cPanel Database Wizard Step 1: Create A Database - call your new database moodle in the Database Wizard.
- Copy the database name ( including the cpanel prefix) from cPanel to your web based installations screen)
- Database user
- cPanel Database Wizard - next to create a user called moodleuser
- Copy the user name ( including the cpanel prefix) from cPanel to your web based installations screen)
- Database password
- Copy the password from cPanel to your web based installations screen)
- cPanel Database Wizard Step 3: Add user to the database
- Important - check All Privileges to allow your new user access to the database
- Continue to the Check page - if there are any php problems, resolve them in PHP Selector.
- If all green, continue with the installation.
Finish the installation in the terminal
Enter this line towards the end of the Moodle config file
nano ~/moodle/config.php
$CFG->routerconfigured = true;
Tighten security on the Moodle config files
chmod 444 ~/moodle/config.php
chmod 444 ~/moodle/public/config.php
Set up the Schedule Using a Cron job
- Go to Cron Jobs under Advanced in cPanel
- Open Add New Cron Job'
- Under cron settings, set as frequently as possible (On many shared cPanel hosts, the minimum allowed cron frequency is every 5 minutes, every minute is not permitted under many hosting plans)
- Paste in this coomand /usr/local/bin/php ~/moodle/admin/cli/cron.php