Prerequisites:
- Apache2 web server.
- PHP (optional).
Create a PHP workspace folder
Create a directory to serve as your PHP workspace. This will hold your PHP projects and files.
Open a terminal and create a new directory for the PHP workspace, replacing username
with your actual username. The following command will create the php-workspace
folder within the Documents
directory. You can replace /home/username/Documents/
with any path you prefer.
mkdir -p /home/username/Documents/php-workspace
Update Apache configuration
Configure Apache to recognize the new directory for serving files. Open the Apache configuration file in a text editor (/etc/apache2/apache2.conf
).
Add a Directory
directive for the new directory at the end of the file (or in an appropriate section if preferred). Update the path with your actual username and the new workspace location.
<Directory /home/username/Documents/php-workspace>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Change the DocumentRoot
Configure Apache to use the new directory as its default document root. Open the 000-default.conf
file for editing (/etc/apache2/sites-available/000-default.conf
).
Change the DocumentRoot
directive to point to your newly created php-workspace
folder. Replace /home/username/Documents/php-workspace
with your actual path.
# DocumentRoot /var/www/html
DocumentRoot /home/username/Documents/php-workspace
Set proper permissions
In order for Apache to access and serve files from the new directory, you need to set the proper permissions for the workspace folder.
sudo chmod +x /home/username
sudo chmod +x /home/username/Documents
sudo chmod +x /home/username/Documents/php-workspace
sudo chmod -R 755 /home/username/Documents/php-workspace
sudo chown -R www-data:www-data /home/username/Documents/php-workspace
Restart Apache
Restart Apache for the new configuration changes to take effect.
sudo systemctl restart apache2
Test the configuration
Move the index.html
file from the /var/www/html
directory to /home/username/Documents/php-workspace
or create a simple PHP file inside the php-workspace
directory to test if everything is set up properly. For example, create a file named index.php
.
<?php
phpinfo();
?>
Open the web browser and navigate to http://localhost/index.php
.
Conclusion
You have successfully changed the default document root for Apache2 to a new location (/home/username/Documents/php-workspace
), configured Apache to allow access to that directory, and set the necessary permissions. Your server is now set up to serve PHP projects from the new location.