Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the root directory of an Apache server on Linux?
Apache web server is one of the most widely used HTTP servers across all platforms, including Linux distributions and Windows. As an open-source server, Apache efficiently delivers web content and handles multiple concurrent requests.
In this article, we'll explain how to change the DocumentRoot directory for your Apache web server on Linux systems.
Understanding DocumentRoot
The DocumentRoot is the directory from which Apache serves files to visitors accessing your website. By default, Apache typically uses one of these locations:
/var/www/html
Or alternatively:
/var/www
The exact default location depends on your Linux distribution and Apache configuration.
Step-by-Step Process
Step 1 − Locate the Configuration File
The DocumentRoot is configured in the Apache virtual host file. For the default site, this is typically located at:
/etc/apache2/sites-available/000-default.conf
Step 2 − Edit the Virtual Host Configuration
Open the configuration file using a text editor with sudo privileges:
sudo nano /etc/apache2/sites-available/000-default.conf
Find the DocumentRoot directive, which will look similar to:
DocumentRoot /var/www/html
Change this path to your desired directory location.
Step 3 − Update Apache Main Configuration
You also need to update the main Apache configuration file to grant permissions to your new directory:
sudo nano /etc/apache2/apache2.conf
Find the directory block for the old DocumentRoot:
<Directory /var/www/html/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
Update the directory path to match your new DocumentRoot location.
Step 4 − Create and Set Permissions
Ensure your new directory exists and has proper permissions:
sudo mkdir -p /your/new/document/root sudo chown -R www-data:www-data /your/new/document/root sudo chmod -R 755 /your/new/document/root
Step 5 − Test and Restart Apache
Test your configuration for syntax errors:
sudo apache2ctl configtest
If the test passes, restart Apache to apply the changes:
sudo systemctl restart apache2
Important Considerations
Always backup your configuration files before making changes
Ensure the new directory has appropriate read permissions for the
www-datauserTest your website functionality after making the change
Consider security implications when choosing a new DocumentRoot location
Conclusion
Changing the Apache DocumentRoot involves updating both the virtual host configuration and directory permissions in the main Apache config. After making these changes and restarting Apache, your web server will serve content from the new location while maintaining proper security and functionality.
