When working with Apache2 web server on Ubuntu Linux, we often need to serve different content from separate directories under the same domain. This is particularly useful when you want to maintain modular applications or separate different sections of your website while keeping everything under a single domain.
In this scenario, we have:
/srv/www/blog/ # Blog content
/srv/www/mainsite/ # Primary website content
The solution involves using Apache's Alias
directive and Directory
blocks rather than separate virtual hosts. Here's a complete configuration example for your /etc/apache2/sites-available/000-default.conf
or a new configuration file:
<VirtualHost *:80>
ServerName mysite.com
DocumentRoot /srv/www/mainsite
Alias /blog /srv/www/blog
<Directory /srv/www/mainsite>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory /srv/www/blog>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After creating or modifying your configuration file:
sudo a2ensite your-config-file.conf
sudo systemctl reload apache2
For more complex scenarios, you might want to consider:
# Example of additional directives you might need
<Location /blog>
SetEnv BLOG_MODE production
Header set X-Blog-Version "2.0"
</Location>
If you encounter permission issues, ensure:
sudo chown -R www-data:www-data /srv/www
sudo chmod -R 755 /srv/www
Check Apache error logs for detailed debugging:
tail -f /var/log/apache2/error.log
For better performance with multiple directories:
# Enable content caching
<IfModule mod_cache.c>
CacheQuickHandler on
CacheLock on
CacheLockPath /tmp/mod_cache-lock
CacheLockMaxAge 5
CacheIgnoreCacheControl On
CacheIgnoreNoLastMod On
CacheStoreNoStore On
CacheStorePrivate On
</IfModule>
When working with Apache2 on Ubuntu Linux, you might need to serve multiple websites from different directories under the same domain. This is particularly common when:
- Hosting a blog as a subdirectory of your main site
- Serving different applications under the same domain
- Maintaining separate codebases for different site sections
Instead of using separate virtual hosts (which typically require different domains or IPs), we'll use Apache's Alias
directive and Directory
settings:
<VirtualHost *:80>
ServerName mysite.com
DocumentRoot /srv/www/mainsite
Alias /blog /srv/www/blog
<Directory "/srv/www/blog">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory "/srv/www/mainsite">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
1. Create a new configuration file:
sudo nano /etc/apache2/sites-available/mysite.conf
2. Paste the configuration above, adjusting paths as needed
3. Enable the site and restart Apache:
sudo a2ensite mysite.conf
sudo systemctl restart apache2
For more complex scenarios, you might need additional directives:
# For PHP applications:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
# Custom error documents:
ErrorDocument 404 /errors/404.html
# URL rewriting:
RewriteEngine On
RewriteRule ^blog/old/(.*)$ /blog/new/$1 [R=301,L]
If you encounter problems:
- Check Apache error logs:
tail -f /var/log/apache2/error.log
- Verify file permissions:
sudo chown -R www-data:www-data /srv/www
- Test configuration:
sudo apache2ctl configtest
When serving multiple directories:
- Enable caching for static assets
- Consider using
mod_deflate
for compression - Implement proper .htaccess rules to avoid performance hits