Problem: Changing Apache's Document Root
Modifying the Apache document root in Ubuntu lets you change the default directory that serves web content. You might need to do this to organize website files or match specific project structures.
Locating the Configuration Files
Apache2 Configuration Directory
The Apache2 configuration files in Ubuntu are in the /etc/apache2/
directory. This directory has files to manage your Apache web server.
The main files to change the document root are:
-
/etc/apache2/sites-available/000-default.conf
: This file has the virtual host configuration for the default site. -
/etc/apache2/apache2.conf
: This is the main Apache2 configuration file with global settings.
These two files are important for changing the document root in Apache2 on Ubuntu. In the next section, we'll show you how to change these files and update the document root location.
Tip: Backup Configuration Files
Before making changes to your Apache2 configuration files, it's a good practice to create backups. You can use the following commands to backup your configuration files:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.bak
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
This allows you to easily revert changes if needed.
Changing the Document Root
Editing the Virtual Host Configuration
To change the Apache document root:
- Open the default virtual host file with a text editor:
sudo nano /etc/apache2/sites-available/000-default.conf
- Change the
DocumentRoot
directive to your new directory:
DocumentRoot /path/to/your/new/directory
-
Save the file and exit the text editor.
-
Update the directory permissions for your new document root:
sudo chown -R www-data:www-data /path/to/your/new/directory
sudo chmod -R 755 /path/to/your/new/directory
This gives the Apache user (www-data) ownership and permissions to access the new directory.
Tip: Verify Apache Configuration
After making changes to Apache configuration files, it's a good practice to verify the syntax before restarting the service. Use the following command:
sudo apache2ctl configtest
If there are no errors, you'll see "Syntax OK". If there are issues, the command will provide details about the problems in your configuration.
Updating Apache2 Main Configuration
Update the main Apache2 configuration file:
- Open the apache2.conf file:
sudo nano /etc/apache2/apache2.conf
- Find the
<Directory>
section that refers to the old document root:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
- Change the directory path to match your new document root:
<Directory /path/to/your/new/directory/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
- Save the file and exit the text editor.
By updating both the virtual host configuration and the main Apache2 configuration, you make sure that Apache uses the new document root and has the right permissions to access it.
Applying the Changes
Restarting Apache2 Service
After changing the Apache configuration files, you need to restart the Apache2 service to apply the changes. Here's how:
- Restart the Apache2 service:
sudo systemctl restart apache2
- Check Apache2's status:
sudo systemctl status apache2
This shows the current state of the Apache2 service. Look for "Active: active (running)" to confirm a successful restart.
-
To test the changes, open a web browser and go to
http://localhost
. You should see the content from your new document root directory. -
If you have issues, check the Apache error logs:
sudo tail -f /var/log/apache2/error.log
This displays the latest Apache error messages to help you fix any problems.
By following these steps, you can apply the changes to your Apache configuration and check if the new document root works as expected.
Tip: Graceful Restart
For minimal downtime, you can use a graceful restart instead of a full restart:
sudo systemctl reload apache2
This reloads the configuration without stopping the server, which can be useful for busy websites.