Skip to content

Instantly share code, notes, and snippets.

@faizalmansor
Last active June 20, 2020 22:19
Show Gist options
  • Save faizalmansor/55c8f96df1bab50427c786a1a65dba7b to your computer and use it in GitHub Desktop.
Save faizalmansor/55c8f96df1bab50427c786a1a65dba7b to your computer and use it in GitHub Desktop.
How to Install Moodle 3.5 on Centos 7 with Nginx

How to Install Moodle 3.5 on Centos 7 with nginx

Prepare the environment

Step 1 — Installing Nginx on CentOS 7

Since Nginx is not available in default CentOS repositories, we will install EPEL repository by running this command:

yum install epel-release -y

Install nginx

yum install nginx -y

After installation completes, enable nginx start on boot and run it:

systemctl start nginx
systemctl enable nginx

Step 2 — Installing MySQL (MariaDB)

yum install mariadb-server mariadb -y

After finishing the installation, enable and start the service:

systemctl start mariadb
systemctl enable mariadb

Lastly, run initial setup script which will remove some of the default settings:

mysql_secure_installation

Step 3 — Installing PHP v7.0

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Enable php70 repository:

yum install yum-utils -y
yum-config-manager --enable remi-php70

Secondly, install PHP package:

yum --enablerepo=remi,remi-php70 install php-fpm php-common

Install common modules:

yum --enablerepo=remi,remi-php70 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Step 4 — Configuring Nginx to work with PHP 7

Create a new nginx configuration file:

vim /etc/nginx/conf.d/default.conf

Input this code:

server {
    listen   80;
    server_name  your_server_ip;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

IMPORTANT! Replace your_server_ip with your actual server IP.

Save the file and restart nginx for change to take effect:

systemctl restart nginx

Now, open PHP-FPM configuration:

vim /etc/php-fpm.d/www.conf

Find and replace these lines:

user = apache to user = nginx

group = apache to group = nginx

listen.owner = nobody to listen.owner = nginx

listen.group = nobody to listen.group = nginx

And, lastly, under ;listen = 127.0.0.1:9000 add this line:

listen = /var/run/php-fpm/php-fpm.sock

Save the file again and finally, start php-fpm and enable it on boot:

systemctl start php-fpm.service
systemctl enable php-fpm.service

Installation

Create the database

CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Create a user for moodle

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword';

Create data directory

Create an empty directory to hold Moodle files. It must not be in the area served by the web server and must have permissions so that the web server user can write to it. Other than that it can be located anywhere. Typically, either make it owned by the web server user or give it write permissions for 'everyone'. If it is on a shared/NFS drive then read Caching - Moodle caches to this disk area by default and a slow share will mean terrible performance.

Install Moodle code

If you downloaded the zip or tgz file earlier, then unzip / untar / move / copy the Moodle code (obtained above) so that it will be served by your web server (e.g. on Debian based Linux, move to /var/www/html/moodle) Check the permissions and make sure that the web server does not have permissions to write to any of the files in the Moodle code directories (a very common root cause of sites being hacked). If you need to, configure your web server to serve the Moodle site with your chosen URL.

Configure Moodle

In the Moodle code directory, find the file config-dist.php and copy it to a new file called config.php (but read next step, 'Install Moodle', first). Edit config.php with your favourite editor and change the appropriate settings to point to your site, directories and database. Note: the Moodle install script will create config.php for you if it does not exist but make sure you (re-)set permissions appropriately afterwards

Install Moodle

Go to the URL for your moodle site in a browser (installation will complete automatically) or run the command line version at (requires cli version of PHP):

/usr/bin/php /path/to/moodle/admin/cli/install.php

The CLI creates the config.php for you and will not run if you created one in the previous step.

After completing the install make sure your file permissions are ok for the Moodle program files (not writeable by web server) and the Moodle data files (writeable by web server).

Set up cron

You will need a cron job to run periodically. It is recommended that the cron is run every minute, as required for asynchronous activity deletion when using the recycle bin. A typical Unix cron entry will be as follows:

* * * * *    /usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null

Your site will not work properly unless cron is running regularly. It is very important you do not skip this step.

Troubleshooting

MariaDB [(none)]> SHOW variables LIKE "%innodb_file%";
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_file_format       | Antelope |
| innodb_file_format_check | ON       |
| innodb_file_format_max   | Antelope |
| innodb_file_per_table    | OFF      |
+--------------------------+----------+
4 rows in set (0.00 sec)

MariaDB [(none)]> SET global innodb_file_format = barracuda;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SHOW variables LIKE "%innodb_file%";
+--------------------------+-----------+
| Variable_name            | Value     |
+--------------------------+-----------+
| innodb_file_format       | Barracuda |
| innodb_file_format_check | ON        |
| innodb_file_format_max   | Antelope  |
| innodb_file_per_table    | OFF       |
+--------------------------+-----------+
4 rows in set (0.00 sec)

MariaDB [(none)]> SET global innodb_file_format_max = barracuda;
Query OK, 0 rows affected (0.75 sec)

MariaDB [(none)]> SHOW variables LIKE "%innodb_file%";
+--------------------------+-----------+
| Variable_name            | Value     |
+--------------------------+-----------+
| innodb_file_format       | Barracuda |
| innodb_file_format_check | ON        |
| innodb_file_format_max   | Barracuda |
| innodb_file_per_table    | OFF       |
+--------------------------+-----------+
4 rows in set (0.00 sec)

MariaDB [(none)]> SET global innodb_file_per_table = on;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW variables LIKE "%innodb_file%";
+--------------------------+-----------+
| Variable_name            | Value     |
+--------------------------+-----------+
| innodb_file_format       | Barracuda |
| innodb_file_format_check | ON        |
| innodb_file_format_max   | Barracuda |
| innodb_file_per_table    | ON        |
+--------------------------+-----------+
4 rows in set (0.00 sec)

MariaDB [(none)]> SHOW variables LIKE "%innodb_large%";
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | OFF   |
+---------------------+-------+
1 row in set (0.00 sec)

MariaDB [(none)]> SET global innodb_large_prefix = on;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW variables LIKE "%innodb_large%";
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | ON    |
+---------------------+-------+
1 row in set (0.00 sec)

Nginx Configuration

nginx.conf location:

location ~ [^/]\.php(/|$) {
    root /var/www/html;
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             127.0.0.1:9000; # or your php-fpm socket
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Open Port 80

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --reload

Bugs

Click on calendar event caused

Error code: dmlreadexception

Fix:

calendar/lib.php line 3799

change

FROM {course}

to

FROM {course} c

e.g.

$sql = "SELECT
c.id, c.visible, {$ctxfields}
FROM {course} c
JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";

Resources

  1. https://www.dark-hamster.com/software/fatal-errorcfg-dataroot-not-writable-admin-fix-directory-permissions/
@datdinhquoc
Copy link

why is it "root /usr/share/nginx/html;" and not moodle dir?

@datdinhquoc
Copy link

the "location ~ .php$" wont work, there's something after ".php": https://docs.moodle.org/37/en/Nginx

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