Home >Backend Development >PHP Problem >How to change server root directory in PHP

How to change server root directory in PHP

PHPz
PHPzOriginal
2023-04-10 09:44:14964browse

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:

  1. Open the Apache configuration file

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

  1. Change DocumentRoot

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/

  1. Add Directory configuration

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.

  1. Saving and Exiting the File

Enter Ctrl X, then Y and press Enter to save and exit the file.

  1. Restart Apache

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn