如何将本地定制化Moodle纳入Bitbucket版本控制并部署至服务器?
Hey there! Let's break this down into two clear, actionable parts—getting your local Moodle setup into Bitbucket version control, then deploying it to your server. I’ve walked through this process multiple times, so here’s what works reliably:
1. Add Your Local Moodle to Bitbucket Version Control
First, you need to make sure you’re only tracking the files that matter (no auto-generated junk, sensitive configs, or user-uploaded content). Here’s the step-by-step:
- Create a
.gitignorefile in your local Moodle root directory. This tells Git what to skip—add these lines at minimum:# Moodle auto-generated/system files moodledata/ cache/ temp/ backup/ local/cache/ # Sensitive config (we'll use a template instead) config.php # Any custom temp files or plugins you don't want tracked - Initialize Git locally:
Open your terminal, navigate to your Moodle folder, and run:cd /path/to/your/local/moodle git init - Stage and commit your files:
Add all tracked files to Git, then make your first commit:
Note: If you made changes to Moodle core files (not recommended—use plugins/themes instead), they’ll be included here. Stick to custom code where possible to avoid upgrade headaches later.git add . git commit -m "Initial commit: Custom Moodle setup with courses and customizations" - Connect to your Bitbucket repo:
Go to Bitbucket, create a new private repository, copy its SSH or HTTPS URL. Then run:git remote add origin your-bitbucket-repo-url git push -u origin main - Handle config safely:
Don’t commitconfig.php(it has database credentials!). Instead, make a copy namedconfig-dist.php(with placeholder values) and commit that. When deploying, you’ll copy this template and fill in server-specific details.
2. Deploy Moodle to Your Server
I’ll cover two approaches: manual deployment (great for small setups) and automated deployment (scalable for ongoing updates).
Manual Deployment Steps
- Prep your server:
Ensure your server meets Moodle’s requirements (correct PHP version, MySQL/MariaDB, Apache/Nginx, and required extensions). Install all dependencies first. - Clone the Bitbucket repo:
On your server, navigate to your web root (e.g.,/var/www) and run:git clone your-bitbucket-repo-url moodle - Set up
moodledata:
Create a dedicated directory for Moodle’s data (outside the web root for security) and set proper permissions:mkdir /var/moodledata chown www-data:www-data /var/moodledata chmod 770 /var/moodledata - Configure
config.php:
Copy yourconfig-dist.phptemplate toconfig.php, then fill in your server’s database credentials,moodledatapath, and site URL:cp /var/www/moodle/config-dist.php /var/www/moodle/config.php # Edit config.php with your server details (use nano or vim) - Import your local database:
Export your local Moodle database (usemysqldumpor phpMyAdmin), then import it into your server’s database. Update the site URL in the database if needed:# On your server, run this to set the correct site URL php /var/www/moodle/admin/cli/cfg.php --name=wwwroot --set=https://your-server-domain.com - Fix file permissions:
Ensure the web server user has access to Moodle files:chown -R www-data:www-data /var/www/moodle find /var/www/moodle -type f -exec chmod 644 {} \; find /var/www/moodle -type d -exec chmod 755 {} \; - Final check:
Visit your server’s domain in a browser—Moodle will either load directly or walk you through final setup. Verify your courses and customizations are intact.
Automated Deployment with Bitbucket Pipelines
For ongoing updates, automate deployment so every push to Bitbucket triggers a server update:
- Enable Bitbucket Pipelines:
Go to your Bitbucket repo’s Settings > Pipelines and enable it. - Create a
bitbucket-pipelines.ymlfile in your local repo:
Here’s a basic example (adjust for your server setup):image: php:7.4-cli pipelines: default: - step: name: Deploy to Production deployment: production script: # Install dependencies for deployment - apt-get update && apt-get install -y git rsync ssh-client # Add server SSH key (store this as a Bitbucket environment variable) - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa # Sync files to server - rsync -avz --exclude='.git' --exclude='config-dist.php' ./ user@your-server-ip:/var/www/moodle/ # Run maintenance mode and upgrade - ssh user@your-server-ip "chown -R www-data:www-data /var/www/moodle && php /var/www/moodle/admin/cli/maintenance.php --enable" - ssh user@your-server-ip "php /var/www/moodle/admin/cli/upgrade.php --non-interactive" - ssh user@your-server-ip "php /var/www/moodle/admin/cli/maintenance.php --disable" - Add environment variables:
In Bitbucket Pipelines settings, addSSH_PRIVATE_KEY(your server’s private key) and any other sensitive values. - Push the config:
Commit and push thebitbucket-pipelines.ymlfile—now every push to your repo will auto-deploy to the server.
Key Notes to Avoid Headaches
- Don’t modify Moodle core: Use custom themes, local plugins, or blocks instead. Core changes will break when you upgrade Moodle.
- Backup everything: Always backup your database and files before deploying updates.
- Test first: Deploy to a staging server before production to catch issues early.
内容的提问来源于stack exchange,提问作者Priyanca Kamra
相关产品推荐
相关产品推荐

