Moodle performance issues after migration

Moodle performance issues after migration

by Jose Sanchez -
Number of replies: 8

Hello everyone!

I'm migrating everything from an old server to a new one and moving from apache2 to nginx, and from mysql to mariaDB, and from one domain to another (just asking for trouble, I know). I'm using Ubuntu Server 24.04.1.

There are two main problems that I've noticed:

1) When you first open the site it takes about a whole minute or even two for it to open. If you are clicking through it (not idle on something else) it loads the next page really slowly.

2) My logo and favicon images are broken. I deleted and reuploaded them, but they are still broken. The favicon comes and goes, sometimes being present, sometimes gone. Also in the login page the background image is the one I uploaded, but there was another image above the username part that is now broken. I'm using the standard boost theme.

I passed the database over by running mysqldump, and importing on mariaDB on my new server. I downloaded and installed Moodle, then copied the old moodledata folder over the new one. I adapted the config.php to the new settings. I'm using Moodle 4.5.1+, latest build. I also used the Search and replace tool and noticed no difference.

Thanks in advance!

Here is my nginx config for Moodle (I made it anonymous):

server {
    server_name example.com www.example.com;

    root /var/www/html/moodle;
    index index.php index.html index.htm;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    # 1) Basic location
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # 2) Slash-argument-compatible location for PHP
    #    (Captures both index.php and index.php/anything)
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index            index.php;
        include                  fastcgi_params;
        fastcgi_param PATH_INFO  $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass             unix:/run/php/php8.3-fpm.sock;
    }

    # 4) Static file caching
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
        try_files $uri /index.php?$query_string;
    }

    location /dataroot/ {
        internal;
        alias /var/moodledata/;
    }

    # 5) SSL config
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name example.com www.example.com;
    return 404; # managed by Certbot
}

And, in case it helps, my nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml ap>

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

Average of ratings: -
In reply to Jose Sanchez

Moodle performance issues after migration

by Eduardo Kraus -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hello, José Sanchez! 😄

The first step to addressing the initial slowness and broken images is to ensure that the old cache is not interfering with the new environment. Whenever we perform a migration, especially with server changes, software upgrades, or even a new domain, some cached files can cause quite a bit of trouble. So, delete the cache folders from moodledata!

In your case, navigate to the directory where moodledata is stored on the new server and remove the contents of the following folders:

  • moodledata/cache
  • moodledata/localcache
  • moodledata/temp

These folders will be automatically recreated by Moodle the next time you access the site, which will clear out any junk that might be causing issues.

Eduardo Kraus
Teacher and Programmer

Average of ratings: Useful (1)
In reply to Eduardo Kraus

Re: Moodle performance issues after migration

by Jose Sanchez -
Thank you! I tried it and thought the slowness was gone, but after testing for a while it got slow again.
 
The images are also still broken. I've noticed that it's only the images that I can't right click on and select "Open image in new tab." It's the images that are dynamically loaded somehow.
 
I should mention also that things like the "Course card colors" don't appear, showing up as a blank white space instead.

Finally, when I add for example a logo image in the Boost theme settings, I get an infinite uploading, with some broken images thrown in.

uploading troubles
 
Any thoughts on how to fix this?
In reply to Jose Sanchez

Re: Moodle performance issues after migration

by Eduardo Kraus -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hello, José Sanchez! 😄

The first thing I would suggest is to open the JavaScript console in your browser and go to the Network tab. In this tab, we’ll check if there are any loading errors with the files or images causing the issue. Often, these errors can help identify what’s happening.

When you find the errors, click on them to see the error message. This might give you a clue about what’s going on — maybe a permission issue, an incorrect URL configuration, or even a problem with the platform's cache.

Captura%20de%20Tela%202025-01-11%20a%CC%80s%2014.33.02.png

Eduardo Kraus
Teacher and Programmer

Average of ratings: Useful (1)
In reply to Jose Sanchez

Re: Moodle performance issues after migration

by Ken Task -
Picture of Particularly helpful Moodlers

And some more ...

also remove muc cache în moodledata - it too will be rebuilt.

You've shared nginx config, but let's not forget DB config.
Suggest installing MySQLTuner.pl and run script with superuser creds to see what it might recommend.

Pay attention to the Performance Metrics and InnoDB Metrics sections.

And last but not least, cron job ... is that setup to run the recommended once per minute?   You might run from moodle code directory:

php admin/cli/cron.php

to see if it's looping and/or otherwise performing as expected.

'SoS', Ken

Average of ratings: Useful (1)
In reply to Jose Sanchez

Re: Moodle performance issues after migration

by Ken Task -
Picture of Particularly helpful Moodlers

Oh, yeah ... 2 other cli scripts to run in code/admin/cli/

environment check: checks.php

and database schema check: check_database_schema.php

Also consult: https://moodledev.io/general/releases/4.5

server requirements.

'SoS', Ken

 

Average of ratings: Useful (1)
In reply to Ken Task

Re: Moodle performance issues after migration

by Jose Sanchez -
Thank you!!

So I tried all of that to no avail, but then I had a hunch. In Moodle's config.php, commented out the xsendfile lines that I had added and everything immediately worked! Now I have to go back and see what the problem was...

Here is my original Moodle config.php, which I should have included:
<?php // Moodle configuration file

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype = 'mariadb';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'your_db_name';
$CFG->dbuser = 'your_db_user';
$CFG->dbpass = 'your_db_password';
$CFG->prefix = 'mdl_';
$CFG->dboptions = array(
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
'dbcollation' => 'utf8mb4_unicode_ci',
);

$CFG->wwwroot = 'https://your_moodle_domain';
$CFG->dataroot = '/path/to/moodledata';
$CFG->admin = 'admin_username';

$CFG->directorypermissions = 0777;

$CFG->xsendfile = 'X-Accel-Redirect'; // for Nginx
$CFG->xsendfilealiases = array(
'/dataroot/' => '/path/to/moodledata',
);

require_once(__DIR__ . '/lib/setup.php');

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!
In reply to Jose Sanchez

Re: Moodle performance issues after migration

by Jose Sanchez -
Found the problem, very simple really. I found it after re-reading what the documentation says about Performance recommendations.
 
In case it ever happens to anyone else:
 
Here:
$CFG->xsendfile = 'X-Accel-Redirect'; // for Nginx
$CFG->xsendfilealiases = array(
'/dataroot/' => '/path/to/moodledata',
);
 
/dataroot/ should not point to the directory, but should instead reference the variable:
$CFG->dataroot
 
So in the end the lines should be written as:
$CFG->xsendfile = 'X-Accel-Redirect'; // for Nginx
$CFG->xsendfilealiases = array(
'/dataroot/' => '$CFG->dataroot',
);
 
At least, after fixing that everything worked perfectly fine. If it turns out to be wrong after all someone reading this can correct me, or I'll come back and post later.
Average of ratings: Useful (2)
In reply to Jose Sanchez

Re: Moodle performance issues after migration

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Jose,

Since no one else has mentioned it you might want to try this, from one domain to another (just asking for trouble, I know). This is probably the cause of your broken pictures, logos, etc.

After a domain name change you need to run the replace tool located at:  yourmoodle/admin/tool/replace/index.php

You can use it to search for your olddomain and replace with the newdomain.

Average of ratings: Useful (1)