|
1 | 1 | ## About |
2 | 2 |
|
3 | | -This repository is forked from ONLYOFFICE/Docker-CommunityServer, we have the following modifications are provided |
| 3 | +Moodle Docker deployment with MariaDB support. |
4 | 4 |
|
5 | | -* replace README.md |
6 | | -* add README-zh.md, CHANGELOG.md, Note.md, License.md, docker-compose.yml,.env, src, .github folder |
| 5 | +## URL Configuration |
| 6 | + |
| 7 | +### Changing from IP to Domain |
| 8 | + |
| 9 | +When you need to change from IP access to domain access, you need to update both the config file and database: |
| 10 | + |
| 11 | +1. **Update config.php** (in container `/var/www/html/config.php`): |
| 12 | +```bash |
| 13 | +docker exec -it moodle sed -i "s|http://.*';|http://your-domain.com';|g" /var/www/html/config.php |
| 14 | +``` |
| 15 | + |
| 16 | +2. **Update database**: |
| 17 | +```bash |
| 18 | +docker exec -it moodle-mariadb mariadb -umoodle -p moodle -e "UPDATE mdl_config SET value = 'http://your-domain.com' WHERE name = 'wwwroot';" |
| 19 | +``` |
| 20 | + |
| 21 | +3. **Clear Moodle cache**: |
| 22 | +```bash |
| 23 | +docker exec -it moodle php /var/www/html/admin/cli/purge_caches.php |
| 24 | +``` |
| 25 | + |
| 26 | +### HTTPS Configuration with Reverse Proxy |
| 27 | + |
| 28 | +If you encounter **infinite redirect loop** after configuring HTTPS certificate (via Nginx Proxy Manager), you need to configure Moodle to work behind a reverse proxy. |
| 29 | + |
| 30 | +**Problem**: |
| 31 | +- config.php has `$CFG->wwwroot = 'http://domain.com';` |
| 32 | +- User accesses via HTTPS |
| 33 | +- Moodle detects protocol mismatch and redirects infinitely |
| 34 | + |
| 35 | +**Solution**: Add reverse proxy configuration to `/var/www/html/config.php`: |
| 36 | + |
| 37 | +```bash |
| 38 | +# Enter the container |
| 39 | +docker exec -it moodle bash |
| 40 | + |
| 41 | +# Edit config.php and add these lines BEFORE require_once(): |
| 42 | +vi /var/www/html/config.php |
| 43 | +``` |
| 44 | + |
| 45 | +Add the following configuration **BEFORE** the `require_once(__DIR__ . '/lib/setup.php');` line: |
| 46 | + |
| 47 | +```php |
| 48 | +// HTTPS Reverse Proxy Configuration |
| 49 | +$CFG->wwwroot = 'https://safeline.websoft9.cn'; // Change to HTTPS |
| 50 | + |
| 51 | +// Force HTTPS when behind reverse proxy |
| 52 | +if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
| 53 | + $_SERVER['HTTPS'] = 'on'; |
| 54 | + $_SERVER['SERVER_PORT'] = 443; |
| 55 | +} |
| 56 | + |
| 57 | +// Alternative: Force HTTPS for all requests |
| 58 | +// $CFG->reverseproxy = true; |
| 59 | +// $_SERVER['HTTPS'] = 'on'; |
| 60 | +``` |
| 61 | + |
| 62 | +**Complete config.php example**: |
| 63 | + |
| 64 | +```php |
| 65 | +<?php // Moodle configuration file |
| 66 | + |
| 67 | +unset($CFG); |
| 68 | +global $CFG; |
| 69 | +$CFG = new stdClass(); |
| 70 | + |
| 71 | +$CFG->dbtype = 'mariadb'; |
| 72 | +$CFG->dblibrary = 'native'; |
| 73 | +$CFG->dbhost = 'moodle_edf5j-mariadb'; |
| 74 | +$CFG->dbname = 'moodle'; |
| 75 | +$CFG->dbuser = 'moodle'; |
| 76 | +$CFG->dbpass = 'qB!5Glu37szh1bUd'; |
| 77 | +$CFG->prefix = 'mdl_'; |
| 78 | +$CFG->dboptions = array( |
| 79 | + 'dbpersist' => 0, |
| 80 | + 'dbport' => '3306', |
| 81 | + 'dbsocket' => '', |
| 82 | + 'dbcollation' => 'utf8mb4_unicode_ci', |
| 83 | +); |
| 84 | + |
| 85 | +// IMPORTANT: Change to HTTPS |
| 86 | +$CFG->wwwroot = 'https://safeline.websoft9.cn'; |
| 87 | +$CFG->dataroot = '/var/moodledata'; |
| 88 | +$CFG->admin = 'admin'; |
| 89 | + |
| 90 | +$CFG->directorypermissions = 0777; |
| 91 | + |
| 92 | +// HTTPS Reverse Proxy Support |
| 93 | +if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
| 94 | + $_SERVER['HTTPS'] = 'on'; |
| 95 | + $_SERVER['SERVER_PORT'] = 443; |
| 96 | +} |
| 97 | + |
| 98 | +require_once(__DIR__ . '/lib/setup.php'); |
| 99 | + |
| 100 | +// There is no php closing tag in this file, |
| 101 | +// it is intentional because it prevents trailing whitespace problems! |
| 102 | +``` |
| 103 | + |
| 104 | +**Quick Fix Command**: |
| 105 | + |
| 106 | +```bash |
| 107 | +# Method 1: Edit manually |
| 108 | +docker exec -it moodle vi /var/www/html/config.php |
| 109 | + |
| 110 | +# Method 2: Use sed to update URL to HTTPS |
| 111 | +docker exec -it moodle sed -i "s|http://safeline.websoft9.cn|https://safeline.websoft9.cn|g" /var/www/html/config.php |
| 112 | + |
| 113 | +# Then add reverse proxy config (manual edit required) |
| 114 | +docker exec -it moodle vi /var/www/html/config.php |
| 115 | +# Add the HTTP_X_FORWARDED_PROTO check before require_once() |
| 116 | +``` |
| 117 | + |
| 118 | +**Update database** (also change to HTTPS): |
| 119 | + |
| 120 | +```bash |
| 121 | +docker exec -it moodle_edf5j-mariadb mariadb -umoodle -pqB\!5Glu37szh1bUd moodle -e "UPDATE mdl_config SET value = 'https://safeline.websoft9.cn' WHERE name = 'wwwroot';" |
| 122 | +``` |
| 123 | + |
| 124 | +**Clear cache**: |
| 125 | + |
| 126 | +```bash |
| 127 | +docker exec -it moodle php /var/www/html/admin/cli/purge_caches.php |
| 128 | +``` |
| 129 | + |
| 130 | +### Verify Nginx Proxy Manager Configuration |
| 131 | + |
| 132 | +Ensure your Nginx Proxy Manager has these settings: |
| 133 | + |
| 134 | +1. **SSL Certificate**: Valid and properly configured |
| 135 | +2. **Force SSL**: Enabled |
| 136 | +3. **Custom Nginx Configuration** (Advanced tab): |
| 137 | + |
| 138 | +```nginx |
| 139 | +# Ensure these headers are passed to backend |
| 140 | +proxy_set_header X-Forwarded-Proto $scheme; |
| 141 | +proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 142 | +proxy_set_header X-Real-IP $remote_addr; |
| 143 | +proxy_set_header Host $host; |
| 144 | +``` |
| 145 | + |
| 146 | +## Troubleshooting |
| 147 | + |
| 148 | +### Issue: Infinite Redirect Loop |
| 149 | +- **Cause**: Protocol mismatch between config.php (http) and actual access (https) |
| 150 | +- **Solution**: Change `$CFG->wwwroot` to HTTPS and add reverse proxy support |
| 151 | + |
| 152 | +### Issue: "Site not secure" warning |
| 153 | +- **Cause**: Mixed content (HTTP resources on HTTPS page) |
| 154 | +- **Solution**: Ensure `$CFG->wwwroot` uses HTTPS |
| 155 | + |
| 156 | +### Issue: Session errors |
| 157 | +- **Cause**: Cookie domain mismatch |
| 158 | +- **Solution**: Clear browser cookies and cache after changing URL |
0 commit comments