When it comes to managing your website, understanding how to configure your web server is crucial. Apache, one of the best web servers available, is renowned for its power and flexibility. This tutorial will guide you through the process of changing the default Apache ‘DocumentRoot’ directory in Linux, specifically focusing on Ubuntu and CentOS distributions.
The ‘DocumentRoot’ is the directory set in the configuration file where Apache looks for files to serve on your domain. By default, Apache web server serves web pages from the ‘/var/www/html’ directory. However, there might be situations where you want to change this directory to a different path. This could be due to reasons such as security, organization, or the use of a dedicated server or VPS server.
Before we start, it’s important to note that you should have administrative or ‘sudo’ privileges to make these changes. Also, you should have a basic understanding of Apache and its configuration files. If you need a refresher, you can check out this detailed explanation of the Apache HTTP Server.
In this guide, we will first cover how to change the ‘DocumentRoot’ directory in Ubuntu, and then we will move on to CentOS.
Let’s get started.
Changing Default Apache ‘DocumentRoot’ Directory in Ubuntu
- Open the Terminal: You can do this by pressing Ctrl+Alt+T or by searching for ‘Terminal’ in your system’s search bar.
- Open the Apache configuration file: Use the following command to open the Apache configuration file in a text editor. We will use ‘nano’ for this tutorial, but you can use any text editor you prefer.
sudo nano /etc/apache2/sites-available/000-default.conf
- Change the ‘DocumentRoot’: Look for the line that starts with ‘DocumentRoot’. Change ‘/var/www/html’ to your new directory path. For example, if you want to change it to ‘/var/www/mywebsite’, the line should look like this:
DocumentRoot /var/www/mywebsite
- Save and close the file: Press Ctrl+O to save the changes, then press Ctrl+X to close the file.
- Restart Apache: To apply the changes, you need to restart Apache. Use the following command:
sudo systemctl restart apache2
Now, Apache in your Ubuntu system will serve files from the new directory.
Changing Default Apache ‘DocumentRoot’ Directory in CentOS
The process for CentOS is similar to Ubuntu, with a few differences in the commands and file paths.
- Open the Terminal: You can do this by searching for ‘Terminal’ in your system’s search bar.
- Open the Apache configuration file: Use the following command to open the Apache configuration file in a text editor. We will use ‘vi’ for this tutorial, but you can use any text editor you prefer.
sudo vi /etc/httpd/conf/httpd.conf
- Change the ‘DocumentRoot’: Look for the line that starts with ‘DocumentRoot’. Change ‘/var/www/html’ to your new directory path. For example, if you want to change it to ‘/var/www/mywebsite’, the line should look like this:
DocumentRoot "/var/www/mywebsite"
- Save and close the file: Press ‘:wq’ to save the changes and quit the editor.
- Restart Apache: To apply the changes, you need to restart Apache. Use the following command:
sudo systemctl restart httpd
Now, Apache in your CentOS system will serve files from the new directory.
Important Considerations
While changing the ‘DocumentRoot’ directory, there are a few important considerations to keep in mind:
- Permissions: Make sure that the Apache user (usually ‘www-data’ or ‘apache’) has the necessary permissions to read and write to the new directory.
- .htaccess files: If you’re using .htaccess files for configuration at the directory level, ensure they’re moved to the new directory.
- File Paths: Update any file paths in your scripts that are affected by the change in the ‘DocumentRoot’ directory.
- SELinux: If you’re using SELinux, you need to update the context of the new directory to match that of the default ‘/var/www/html’ directory.
FAQ
-
What is the ‘DocumentRoot’ directory in Apache?
‘DocumentRoot’ is the directory in Apache where it looks for files to serve on your domain. By default, this is set to ‘/var/www/html’.
-
Why would I want to change the ‘DocumentRoot’ directory?
You might want to change the ‘DocumentRoot’ directory for reasons such as security, organization, or if you’re using a dedicated or VPS server.
-
What permissions does the new ‘DocumentRoot’ directory need?
The Apache user (usually ‘www-data’ or ‘apache’) needs to have read and write permissions for the new ‘DocumentRoot’ directory.
-
What should I do with .htaccess files when changing the ‘DocumentRoot’ directory?
If you’re using .htaccess files for directory-level configuration, ensure they’re moved to the new ‘DocumentRoot’ directory.
-
What should Ido if I’m using SELinux?
If you’re using SELinux, you need to update the context of the new directory to match that of the default ‘/var/www/html’ directory.
Commands Mentioned
- sudo nano /etc/apache2/sites-available/000-default.conf – Opens the Apache configuration file in Ubuntu.
- sudo systemctl restart apache2 – Restarts Apache in Ubuntu.
- sudo vi /etc/httpd/conf/httpd.conf – Opens the Apache configuration file in CentOS.
- sudo systemctl restart httpd – Restarts Apache in CentOS.
Conclusion
Changing the default Apache ‘DocumentRoot’ directory in Linux can be a straightforward process if you follow the right steps. Whether you’re using Ubuntu or CentOS, this guide has provided you with the necessary instructions to make this change. Remember to consider important factors such as permissions, .htaccess files, file paths, and SELinux when making this change.
By understanding how to configure your Apache server, you can better manage your website, whether it’s hosted on a dedicated server, VPS server, or even a cloud hosting platform.
Remember, the key to successful web management lies in understanding the tools at your disposal and how to use them effectively.
Happy web hosting!