Skip to content

Instantly share code, notes, and snippets.

@bennof
Last active June 8, 2021 07:35
Show Gist options
  • Save bennof/1c7066c825b17ccc69dded32a3977b6d to your computer and use it in GitHub Desktop.
Save bennof/1c7066c825b17ccc69dded32a3977b6d to your computer and use it in GitHub Desktop.
Install Moodle, Nginx and MariaDB on Ubuntu
#!/bin/sh
# Install Moodle, Nginx and MariaDB on Debian/Ubuntu
# (c) 2020 Benjamin 'Benno' Falkner
set -e # halt on error, debug
cat <<EOF
Install Moodle with Nginx and MariaDB
(c) 2020 Benjamin 'Benno' Falkner (MIT-License)
EOF
## Settings
# all settings are in this section
URL=moodle.host.local # change !!!!
### DB (MariaDB)
DB=mariadb # do not change
DB_NAME=moodle # DB Name
DB_USER=moodle_user # DB User
DB_PASSWD=$(date +%s | sha256sum | base64 | head -c 32 ; echo) # DB Password - change this!!!!
### MOODLE
MOODLE_TGZ=https://download.moodle.org/download.php/direct/stable310/moodle-latest-310.tgz # check for newer versions on https://download.moodle.org/releases/latest/
MOODLE_DIR=/var/www/moodle
MOODLE_USER=www-data # must exist and be member of www-data
MOODLE_DATA_DIR=/var/moodledata
## Run - Do not change below this line
help(){
cat <<EOF
usage: InstallMoodleNginxMariaDBonUbuntu.sh <args>
-u= --url=[url] set url for server (default: $URL)
-v --verbose verbose mode
other switches are not listed
EOF
}
# Read arguments
for i in "$@"; do
case $i in
-u=*|--url=*) URL="${i#*=}"; shift;;
--db-name=*) DB_NAME="${i#*=}"; shift;;
--db-user=*) DB_USER="${i#*=}"; shift;;
--db-password=*) DB_PASSWD="${i#*=}"; shift;;
--moodle-source=*) MOODLE_TGZ="${i#*=}"; shift;;
--moodle-directory=*) MOODLE_DIR="${i#*=}"; shift;;
--moodle-user=*) MOODLE_USER="${i#*=}"; shift;;
--moodle-data=*) MOODLE_DATA_DIR="${i#*=}"; shift;;
-v|--verbose) set -x; shift;;
-h|--help) help; shift;;
*) ;;# unknown option;;
esac
done
### Update
sudo apt-get update
sudo apt install php-fpm php-common php-mysql php-gmp php-curl php-intl php-mbstring php-soap php-xmlrpc php-gd php-xml php-cli php-zip
sudo systemctl restart php-fpm
### Install Nginx
sudo apt-get install nginx
sudo sh -c "cat > /etc/nginx/sites-available/$URL" <<EOF
server{
listen 80;
listen [::]:80;
server_name $URL;
root $MOODLE_DIR;
index index.php index.html index.htm;
error_log /var/log/nginx/${URL}_error.log;
access_log /var/log/nginx/${URL}_access.log;
client_max_body_size 100M;
location / {
try_files $uri $uri/ =404;
}
location /dataroot/ {
internal;
alias $MOODLE_DATA_DIR;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php-fpm.sock;
#include /etc/nginx/mime.types;
include fastcgi_params;
fastcgi_param PATH_INFO \$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
}
EOF
## Install and configure MariaDB
sudo apt-get install mariadb-server
sudo mysql_secure_installation
cat << EOF | sudo mysql -u root -p
CREATE DATABASE IF NOT EXISTS $DB_NAME default character set utf8;
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWD';
GRANT ALL ON $DB_NAME.* TO '$DB_USER'@'localhost';
FLUSH PRIVILEGES;
EOF
## Install Moodle
wget -c $MOODLE_TGZ
sudo tar -zxf $(basename $MOODLE_TGZ) -C $(dirname $MOODLE_DIR)/
rm -rf $(basename $MOODLE_TGZ)
sudo chown $MOODLE_USER:www-data -R $MOODLE_DIR
sudo chmod 775 -R $MOODLE_DIR
sudo mkdir -p $MOODLE_DATA_DIR
sudo chmod 775 -R $MOODLE_DATA_DIR
sudo chown $MOODLE_USER:www-data -R $MOODLE_DATA_DIR
cd $MOODLE_DIR
sudo cp config-dist.php config.php
# ed based config editing (ugly but easy)
sudo ed config.php << EOF
/\$CFG->dbtype
c
\$CFG->dbtype = '$DB'; // 'pgsql', 'mariadb', 'mysqli', 'sqlsrv' or 'oci'
.
/\$CFG->dblibrary
c
\$CFG->dblibrary = 'native'; // 'native' only at the moment
.
/\$CFG->dbhost
c
\$CFG->dbhost = 'localhost'; // eg 'localhost' or 'db.isp.com' or IP
.
/\$CFG->dbname
c
\$CFG->dbname = '$DB_NAME'; // database name, eg moodle
.
/\$CFG->dbuser
c
\$CFG->dbuser = '$DB_USER'; // your database username
.
/\$CFG->dbpass
c
\$CFG->dbpass = '$DB_PASSWD'; // your database password
.
/\$CFG->prefix
c
\$CFG->prefix = 'mdl_'; // prefix to use for all table names
.
/\$CFG->wwwroot
c
\$CFG->wwwroot = 'http://$URL';
.
/\$CFG->dataroot
c
\$CFG->dataroot = '$MOODLE_DATA';
.
wq
EOF
sudo chown $MOODLE_USER:www-data -R $MOODLE_DIR
sudo chmod 775 -R $MOODLE_DIR
## enable all
sudo ln -s /etc/nginx/sites-available/$URL /etc/nginx/sites-enabled/$URL
sudo nginx -t
sudo systemctl reload nginx
@bajpangosh
Copy link

please add cloudflare or let's encrypt ssl support.

@bajpangosh
Copy link

root@kb:~# ./mo.sh
Install Moodle with Nginx and MariaDB
(c) 2020 Benjamin 'Benno' Falkner (MIT-License)
./mo.sh: 52: Syntax error: ";" unexpected

@bennof
Copy link
Author

bennof commented Feb 7, 2021

root@kb:~# ./mo.sh
Install Moodle with Nginx and MariaDB
(c) 2020 Benjamin 'Benno' Falkner (MIT-License)
./mo.sh: 52: Syntax error: ";" unexpected

should be working now. called help with brackets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment