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.


<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/wordpress

    ServerName example.com
    ServerAlias www.example.com

    ErrorLog ${APACHE_LOG_DIR}/error-example.com.log
    CustomLog ${APACHE_LOG_DIR}/access-example.com.log combined

    <Directory /var/www/html/wordpress>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Configure Moodle to be accessible at /moodle
    Alias /moodle /var/www/html/moodle
    <Directory /var/www/html/moodle>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 3: Restart Apache2

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


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.


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

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

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

    location /moodle {
        alias /var/www/html/moodle;
        index index.php index.html index.htm;
        try_files $uri $uri/ /moodle/index.php?$args;
    }

    # Add your PHP configuration here if needed

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    # Add your additional Nginx configurations here

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

Step 3: Test and Reload Nginx

After configuring Nginx, test the configuration for syntax errors:


sudo nginx -t

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


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.