# Install Moodle In Ubuntu 24.04 & Access from local ip (http,https) and domain

Prepare Your Container, do update &amp; upgrade:

```
apt update && apt upgrade -y
apt install -y unzip git curl software-properties-common
```

Install Dependencies (Nginx only, no Apache):

```
apt install -y nginx mariadb-server mariadb-client php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-gd php-intl php-soap php-ldap php-bcmath unzip git
```

Enable services:

```
systemctl enable --now nginx mariadb php8.3-fpm
```

Secure MariaDB &amp; Create DB:

```
mysql_secure_installation
mysql -u root -p
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'StrongPass123!';
GRANT ALL PRIVILEGES ON moodle.* TO 'moodleuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

Install Moodle:

```
cd /var/www
git clone -b MOODLE_405_STABLE https://github.com/moodle/moodle.git
mkdir /var/moodledata
chown -R www-data:www-data /var/www/moodle /var/moodledata
chmod -R 755 /var/www/moodle
```

Nginx Config (Domain via NPM):

```
server {
        listen 80;
        server_name 172.16.X.X;   # Change to your server IP
        root /var/www/moodle;
        index index.php;
        client_max_body_size 100M;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
```

Enable configuration file and delete nginx default file:

```
ln -s /etc/nginx/sites-available/moodle /etc/nginx/sites-enabled/
rm /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx
```

Nginx Config (Local IP with Self-Signed SSL), Create Self Signed Certificate:

```
mkdir -p /etc/ssl/moodle
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout /etc/ssl/moodle/moodle.key \
    -out /etc/ssl/moodle/moodle.crt \
    -subj "/C=ID/ST=Local/L=Local/O=Moodle/OU=IT/CN=(server-ip)"
```

Create nginx site configuration:

```
nano /etc/nginx/sites-available/moodle-ssl
```

Paste to moodle-ssl configuration:

```
    server {
        listen 443 ssl;
        server_name 172.16.x.x;   # replace with your server IP

        ssl_certificate /etc/ssl/moodle/moodle.crt;
        ssl_certificate_key /etc/ssl/moodle/moodle.key;

        root /var/www/moodle;
        index index.php;
        client_max_body_size 100M;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
```

Enable configuration file:

```
ln -s /etc/nginx/sites-available/moodle-local /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
```

PHP Tuning for Moodle:

```
nano /etc/php/8.3/fpm/php.ini
```

Change the following config, depending your server spec:

```
max_input_vars = 5000
memory_limit = 512M
upload_max_filesize = 128M
post_max_size = 128M
max_execution_time = 300
```

Restart php-fpm:

```
systemctl restart php8.3-fpm
```

Now open moodle setup page, please access from https, and follow the instruction:

```
https://<your-server-ip>
```

After finish setup, change config.php file

```
nano /var/www/moodle/config.php
```

Disable or remove defult $CFG-&gt;wwwroot config, change it with this:

```
if (isset($_SERVER['HTTP_HOST'])) {
    $host = $_SERVER['HTTP_HOST'];
    $scheme = (!empty($_SERVER['HTTPS']) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'))
        ? 'https' : 'http';
    $CFG->wwwroot = $scheme . '://' . $host;
} else {
    // Fallback
    $CFG->wwwroot = 'http://localhost';
}
```

You can open moodle using http://&lt;server-ip&gt; or https://&lt;server-ip&gt; and https://yourdomain.com