Tuesday 23 June 2020

Moodle on Azure using CentOS with AzureSQL for MariaDB

Hi and welcome back to my blog post. This time around i will be showing the guide on how to deploy Moodle utilizing Azure resources.



Below is the component that is needed in this deployment
1. Azure VM running CentOS (Mine is 7.8)
2. AzureSQL for MariaDB
3. MySQL workbench - optional

Some additional feature that has been using during this deployment is
1. Accelerated Network features on Azure VM
2. Service Endpoint for AzureSQL


Step 1. Deploy Azure VM name M02.



Step 2 . Ssh to that M02 and install the necessary component.
     
        2.1 Epel
        2.2 Apache
        2.3 Mysql-optional incase we need to test the connection from the MD02 to MariaDB
##########################################################################################################
sudo yum install epel-release -y
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install mod_php71w php71w-common php71w-mbstring php71w-xmlrpc php71w-soap php71w-gd php71w-xml php71w-intl php71w-mysqlnd php71w-cli php71w-mcrypt php71w-ldap -y
##########################################################################################################

Step 3. Deploy AzureSQL for MariaDB
       3.1 If you choose to use Basic tier - u cant use service endpoint as it is not supported
       3.2 Setup service endpoint for General purpose tier for MariaDB

       3.3 Connect to MariaDB and create a moodle database
 
####################################################################################################
CREATE DATABASE moodledb01 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'moodleuser'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON moodle.* TO 'moodleuser'@'%' IDENTIFIED BY 'password1' WITH GRANT OPTION;
 FLUSH PRIVILEGES;
 ###################################################################################################   

       Some note that need to be care of  the DB collation, its  turn out that im having issue with collation  "utf8mb4" so i opt for just " utf8". Second is when u create user, u need to "%" to replace localhost .


Step 4 - Download Moodle
####################################################################
wget https://download.moodle.org/download.php/direct/stable38/moodle-latest-38.tgz
sudo tar -zxvf moodle-latest-38.tgz -C /var/www/html
sudo chown -R root:root /var/www/html/moodle
sudo chown -R admin01:admin01 /var/www/html/moodle
#####################################################################

Step 5 - Create a folder for Moodle Data
#####################################################################
sudo mkdir /var/moodledata
sudo chmod -R 755 /var/moodledata
sudo chown -R apache:apache /var/moodledata
#####################################################################

Step 6 - Setup a Virtual Host - Replace the value of server admin,server name,server alias , error log and custom log accordingly
#######################################################
cat <<EOF | sudo tee -a /etc/httpd/conf.d/moodle.conf
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/moodle/
ServerName moodle.example.com
ServerAlias www.moodle.example.com
<Directory /var/www/html/moodle/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/moodle.example.com-error_log
CustomLog /var/log/httpd/moodle.example.com-access_log common
</VirtualHost>
EOF
########################################################

Dont forget to restart the apache - sudo systemctl restart httpd

Step 7- Install moodle - sudo /usr/bin/php /var/www/html/moodle/admin/cli/install.php

Please key in parameter
- Language is  > EN
-Data directory  permission > 2777
-Web Address > http://yourvmpublicIP
-Data Directory location  > /var/moodledata
-Database driver - MariaDB
-DB Host - xxxxxx.mariadb.database.azure.com
-DB Name is - moodledb01
-Table Prefix > mdl_
- Port > ""
-Socket > ""
- DB user > moodleuser@xxxxxx
-DB password > "moodleuserpassword"
-Admin User Name > "uptoyou"
-Admin password > "uptoyou" *mininum 8 char, 1 big cap, 1 small cap,1 number, 1 special character
-Admin email > "uptoyou"
-Upgrade Key  > ""
-Read the condition > y

Moodle will be install after this step


Step 8. Post Installation

 8.1 modify config file permission - chmod o+r /var/www/html/moodle/config.php
 8.2 Setup cron job - sudo sudo crontab -u apache -e
                               - * * * * *    /usr/bin/php /var/www/html/moodle/admin/cli/cron.php >/dev/null
8.4  Remain the http connection with DB - sudo setsebool httpd_can_network_connect true
                                                                   - sudo setsebool -P httpd_can_network_connect_db on
8.5 Restart Apache


Browse your moodel by Http://PublicIP . Incase you hit "unable to load cache configuration file" run the following command and restart the apache again.


sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/moodle(/.*)?'
sudo restorecon -Rv '/var/www/html/moodle/'
sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/moodledata(/.*)?'
sudo restorecon -Rv '/var/moodledata/'

And Yup, you are done...
Some credit to blog post i refer to before i can complete this guide as follows :

4, Special thanks to Mr Parikshit Savjani from Microsoft for replying my email and work together on getting the moodle up on the first place before tune to its stability.

Bye and see you on the next blog post... 

Kubecost on AKS Part 02