Home >Backend Development >PHP Problem >How to change server root directory in PHP
What is the server root directory?
The server root directory is the top-level directory on your web server that serves all web content. By default, the root directory of most web servers is a public HTML directory (such as /var/www/html or /var/www).
Why should we change the server root directory?
You may need to change the server root directory to hide specific files or directories or better organize your file directory structure for improved security. Additionally, changing the server root can help you make your changed content more accessible.
How to change the server root directory?
Here are the steps to change the server root directory:
Many servers use Apache to run and serve web content. How to open the Apache configuration file varies from server to server. On Ubuntu systems, you can open the Apache configuration file with the following command:
sudo nano /etc/apache2/sites-available/000-default.conf
In the Apache configuration file, find DocumentRoot. By default, this line should look like this:
DocumentRoot /var/www/html
Change this line to your new server root path. For example, if you were to change the server root to /var/www/mywebsite/, your DocumentRoot line would look like this:
DocumentRoot /var/www/mywebsite/
Adding the following configuration will ensure that Apache works when using the new server root directory:
Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted
Replace /var/www/mywebsite/ with the actual path to the new server root directory of your choice.
Enter Ctrl X, then Y and press Enter to save and exit the file.
On Ubuntu systems, you can restart Apache using the following command:
sudo service apache2 restart
Now You have successfully changed the server root directory! You should now be able to access your website content under the changed server root.
Conclusion
Changing the server root directory can be a useful trick, whether through security measures, organizing file directory structures, or improving accessibility. This article describes how to use Apache to change the server root directory.
The above is the detailed content of How to change server root directory in PHP. For more information, please follow other related articles on the PHP Chinese website!