The directory that the Apache web server searches for files to provide upon a client's request is known as the DocumentRoot.By default, the DocumentRoot is usually set to something like /var/www/html on the Linux operating system. You can serve files from a different directory by changing this.
Steps to change the root directory of an Apache server
Step 1: Locate the Apache Configuration File
Usually, Apache's primary configuration file may be found at:
Ubuntu/Debian: /etc/apache2/sites-available/000-default.conf
$ sudo nano /etc/apache2/apache2.conf
Step 2: Edit the Configuration File
- Open the configuration file using a text editor like nano or vim. For example, on Ubuntu, you can use:
sudo nano /etc/apache2/sites-available/000-default.conf
- Find the DocumentRoot directive. It will look something like this:
DocumentRoot /var/www/html
- Change the path to your desired directory. For example, if you want to change it to /home/yourusername/public_html_gfg, modify the line to:
DocumentRoot /home/yourusername/gfg_html
- Update the <Directory> section if present. You might find a section that looks like this:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
- Change it to match your new DocumentRoot:
<Directory /home/yourusername/gfg_html>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Step 3: Update Permissions
- Make sure the Apache user (normally www-data or apache) has the read permission on the new directory. You can adjust the permissions using:You can adjust the permissions using:
sudo chown -R www-data:www-data /home/yourusername/gfg_html
sudo chmod -R 755 /home/yourusername/gfg_html
Step 4: Test the Configuration
- It is a good idea to check the settings for syntax issues before restarting Apache:
sudo apachectl configtest
Step 5: Restart Apache
- Restart Apache to apply the changes:
$ sudo service apache2 restart
Step 6: Verify the Change
To test the effectiveness of changing the root directory, open your web browser and enter your server’s IP address/domain name. You should see the content from the new DocumentRoot directory you created.