Hosting WordPress with Moodle in a Subfolder wp.com/moodle Using Separate Root Folders

Hosting WordPress with Moodle in a Subfolder wp.com/moodle Using Separate Root Folders

When you want to have a strong online presence, you often need two things: a fancy website system like WordPress and a smart learning system like Moodle. Usually, people put these systems in different places. WordPress goes on the main website, and Moodle gets a little home on a subdomain. But sometimes, you might think, “Why not put them together?” So, you can have your main site with WordPress and Moodle hanging out in a subfolder. It sounds cool, but it can be a little tricky with security. In this article, we’ll show you how to do this cool setup safely, using the Apache2 and Nginx web server.

The Challenge: WordPress and Moodle in the Same Root Domain

In the standard setup, WordPress and Moodle are installed in separate root domains. For instance:

  • WordPress Root: /var/www/html/wordpress
  • WordPress Accessible at: example.com
  • Moodle Root: /var/www/html/moodle
  • Moodle Accessible at: moodle.example.com

This is a straightforward configuration, but it keeps the two platforms entirely separate, which can be beneficial for security. However, there are times when you want to host Moodle within a subfolder of your WordPress site, like this:

  • WordPress: example.com
  • Moodle: example.com/moodle

The problem here is that both WordPress and Moodle share the same root domain:

  • WordPress Root: /var/www/html/wordpress
  • Moodle Root: /var/www/html/wordpress/moodle

While this setup simplifies the URL structure, it broadens the potential attack surface, increasing security risks.

The Solution: Apache2 Virtual Hosts

Fortunately, there’s a solution if you’re using Apache2 as your web server. You can maintain the URL structure you desire while still keeping WordPress and Moodle separated in their respective root domains. Here’s how to achieve this:

Step 1: Organize Your Directories

Before diving into Apache2 configuration, make sure your WordPress and Moodle directories are correctly organized. For this example, we’ll assume the following directory structure:

  • WordPress Root: /var/www/html/wordpress
  • Moodle Root: /var/www/html/moodle

Step 2: Configure Apache2 Virtual Hosts

Now, let’s adjust the Apache2 virtual host settings to make this setup work. Create or modify the configuration file for your domain, which could be named something like example.com.conf.

example.com.conf
1
<VirtualHost *:80>
2
ServerAdmin webmaster@localhost
3
DocumentRoot /var/www/html/wordpress
4
5
ServerName example.com
6
ServerAlias www.example.com
7
8
ErrorLog ${APACHE_LOG_DIR}/error-example.com.log
9
CustomLog ${APACHE_LOG_DIR}/access-example.com.log combined
10
11
<Directory /var/www/html/wordpress>
12
Options FollowSymLinks
13
AllowOverride All
14
Require all granted
15
</Directory>
16
17
# Configure Moodle to be accessible at /moodle
18
Alias /moodle /var/www/html/moodle
19
<Directory /var/www/html/moodle>
20
Options FollowSymLinks
21
AllowOverride All
22
Require all granted
23
</Directory>
24
</VirtualHost>

Step 3: Restart Apache2

After configuring your virtual host, don’t forget to restart Apache2 to apply the changes:

Terminal window
1
sudo systemctl restart apache2

The Solution: Nginx Configuration

If you are using Nginx as your web server, you can achieve a similar setup. Here’s how to configure Nginx to host WordPress on the main domain and Moodle within a subfolder like example.com/moodle:

Step 1: Organize Your Directories

Ensure that your WordPress and Moodle directories are organized correctly:

  • WordPress Root: /var/www/html/wordpress
  • Moodle Root: /var/www/html/moodle

Step 2: Configure Nginx

Create or modify the Nginx configuration file for your domain, which could be named something like example.com.conf.

example.com.conf
1
server {
2
listen 80;
3
server_name example.com www.example.com;
4
5
root /var/www/html/wordpress;
6
index index.php index.html index.htm;
7
8
location / {
9
try_files $uri $uri/ /index.php?$args;
10
}
11
12
location /moodle {
13
alias /var/www/html/moodle;
14
index index.php index.html index.htm;
15
try_files $uri $uri/ /moodle/index.php?$args;
16
}
17
18
# Add your PHP configuration here if needed
19
20
location ~ \.php$ {
21
include snippets/fastcgi-php.conf;
22
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
23
}
24
25
# Add your additional Nginx configurations here
26
27
error_log /var/log/nginx/error-example.com.log;
28
access_log /var/log/nginx/access-example.com.log;
29
}

Step 3: Test and Reload Nginx

After configuring Nginx, test the configuration for syntax errors:

Terminal window
1
sudo nginx -t

If there are no errors, reload Nginx to apply the changes:

Terminal window
1
sudo systemctl reload nginx

Conclusion

With these configurations, you can enjoy the benefits of having WordPress and Moodle coexist within the same domain while maintaining a secure separation of their root directories. This setup streamlines your web presence, making it more user-friendly, and ensures that both platforms are easily accessible to your users. Just remember to regularly update and monitor your website’s security to keep your online education and content delivery safe and reliable.