How To Change The Document Root Of The Apache Server In Linux ?

This post shows, how to change The Document Root of The Apache Server. By default, /var/www/html is root directory of Apache Web Server. You need superuser privilege to create and modify file inside the default root directory, that is very tedious. Fortunately, you can change the default root /var/www/html to any other directory, say /home/username/www/html.
The steps in this post are performed on Ubuntu 16.04, but applied to other Linux Distros. So, let’s start.

  1. Install Apache Web Server

    First of all, you need to install Apache Web Server on you system. If you’ve already installed, then you can skip to step 2 now.
    To install Apache, run the following commands in terminal:
    $ sudo apt-get update
    $ sudo apt-get install apache2
    This will install Apache web Server without any problem. You can check if it is installed and working properly by typing localhost or 127.0.0.1 in the address bar of a web browser. This will open a page that will look something like this:
    Screenshot-2017-10-27 Apache2 Ubuntu Default Page It works
    If you see this page, it means your web server is now installed and working correctly. Now you can proceed to next step.

  2. Copy Files (Optional)

    Copy files in the default root directory at your new location, say, /home/username/www/html. To do that, execute this command:
    $ sudo rsync -av /var/www/html /home/username/www/html
    This will copy all files to your new location.
    This step is optional, if you don’t want to copy any anything, move to next step.

  3. Modify Default Site Configuration File

    The document root of Apache Web Server is set to /var/www/html. It was configured in the following file called /etc/apache2/sites-available/000-default.conf.
    Open the file in nano editor by typing this in terminal:
    $ sudo nano /etc/apache2/sites-available/000-default.conf
    This will open the file. Now locate DocumentRoot /var/www/html in the file and change /var/www/html to your desired location, say, /home/username/www/html. Now it should look like this:
    DocumentRoot /home/username/www/html.
    Now save the file and exit.

  4. Modify Apache Configuration File

    Now open sudo nano /etc/apache2/apache2.conf and open it:
    $ sudo nano /etc/apache2/apache2.conf.
    After you open the file, locate this in the file:

    <Directory /var/www/html/>
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted
    </Directory>
    

    Change /var/www/html/ to you desired location, say, /home/username/www/. Now it’ll look like this:

    <Directory /home/username/www/>
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted
    </Directory>
    

    Then save and exit.

  5. Restart Apache

    Now restart apache, so that it can load new configurations. Run this command to restart apache:
    $ sudo service apache2 restart.

  6. Check If It Is working ?

    After restart, create a html file called test.html and place it under /home/username/www/html/ directory. Now open any browser and type localhost/test.html in the address bar and you will see your page in the browser. If not so, you’ve messed up with any step above. Recheck the paths you set in the files you modified.

  7. Note : In /etc/apache2/sites-available/000-default.conf file, document root is set to /home/username/www/html but in /etc/apache2/apache2.conf, it is set to /home/username/www/. Why? This is because, /etc/apache2/apache2.conf file tells the Server to look at the specified directory, /home/username/www/, for all the sites available, but /etc/apache2/sites-available/000-default.conf file tells the Server to look at the specified directory, /home/username/www/html, for the specific site, here the default site, which is localhost. It means, if you type localhost in the address bar, the server will look for it’s index file inside /home/username/www/html directory.

If this post helps you, the share it and please report us if you found any mistake in the post. Thank you.

Author: Anshuman

I'm a computer science student, Linux enthusiast and blogger, who love programming and learning new things.

One thought on “How To Change The Document Root Of The Apache Server In Linux ?”

Leave a comment