Sling Academy
Home/DevOps/Apache: How to Set the Root Directory for a Domain

Apache: How to Set the Root Directory for a Domain

Last updated: January 20, 2024

Introduction

Apache HTTP Server, commonly referred to as Apache, is one of the most widely used web servers due to its robustness, flexibility, and open-source license. Configuring the root directory for a domain is a critical step in setting up your site to ensure that the server serves your content correctly. This guide assumes a working installation of Apache on a Unix-like operating system and will walk you through the process of setting the root directory for a domain from basic to advanced configurations, including troubleshooting common pitfalls.

Understanding the DocumentRoot Directive

The DocumentRoot directive is the critical setting in Apache’s configuration files that defines the root directory, which is the directory from where Apache serves files for a particular domain. It is typically set using a <VirtualHost> block. Here’s a basic example:

<VirtualHost *:80>
    ServerName mydomain.com
    DocumentRoot "/var/www/mydomain.com/public_html"
</VirtualHost>

In this example, ‘mydomain.com’ will serve files from ‘/var/www/mydomain.com/public_html’. This configuration assumes a standard port 80 for HTTP. Whenever you make changes to your Apache configuration, make sure to restart the server with sudo systemctl restart apache2 (on Debian/Ubuntu) or similar commands on other Unix-like OS.

Creating a Virtual Host File

The preferred method for setting a domain’s root directory is by creating a dedicated Virtual Host file.

The file path for virtual host configurations may vary based on OS; however, it usually resides within ‘/etc/apache2/sites-available/’ on Debian/Ubuntu systems.

You would first create a new configuration file for your domain:

sudo nano /etc/apache2/sites-available/mydomain.com.conf

And then, populate the file with the basic virtual host setup:

<VirtualHost *:80>
    ServerName www.mydomain.com
    ServerAlias mydomain.com
    DocumentRoot /var/www/mydomain.com/public_html
</VirtualHost>

After saving the file, enable this site and restart Apache:

sudo a2ensite mydomain.com.conf
sudo systemctl restart apache2

This activates the configuration you’ve just set up; the site should now be live, serving content from the specified DocumentRoot.

Setting Up Permissions and Ownership

More often than not, permissions and ownership misconfiguration can lead to a 403 Forbidden error. The root directory should be owned by the user that Apache runs as, usually ‘www-data’ on Debian/Ubuntu:

sudo chown -R www-data:www-data /var/www/mydomain.com/public_html
sudo chmod -R 755 /var/www/mydomain.com/public_html

Additionally, setting the right permissions is crucial. The ownership change grants Apache the ability to read and write files (if necessary) within your root directory, and appropriate permissions ensure the files are readable and executable.

Advanced Configuration: Adding SSL and Custom Error Pages

An essential step in setting up your domain is to secure it with SSL. After obtaining an SSL certificate, you add the following to your Virtual Host:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName www.mydomain.com
        ServerAlias mydomain.com
        DocumentRoot /var/www/mydomain.com/public_html

        SSLEngine on
        SSLCertificateFile /path/to/your_certificate.crt
        SSLCertificateKeyFile /path/to/your_private.key
        SSLCertificateChainFile /path/to/chainfile.pem

        <Directory /var/www/mydomain.com/public_html>
            Require all granted
            AllowOverride All
        </Directory>
    </VirtualHost>
</IfModule>

In doing so, your website will support HTTPS. Also, consider using AllowOverride All to enable the use of .htaccess files for further customization.

Conclusion

Correctly setting the root directory and permissions for your domain is a foundational step in ensuring your Apache server functions efficiently. MAke sure to handle permissions carefully to maintain your server’s security integrity.

Next Article: Apache: How to redirect all HTTP requests to HTTPS

Previous Article: Apache: How to Disable Directory Browsing

Series: Apache Tutorials

DevOps

You May Also Like

  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List
  • Terraform: How to convert a number to a string and vice versa
  • Using bcrypt() and md5() functions in Terraform