How to change document root of Apache on Ubuntu

How to Ubuntu

In this tutorial we will learn to change document root of Apache on Ubuntu.

This will work for Ubuntu 16 and newer versions like 18.04 LTS.

Prerequisite

It is assumed that you have Apache installed on your Ubuntu computer.

Feel free to check out How to install LAMP stack on Ubuntu tutorial if you want to install Apache MySQL and Php on Ubuntu.

Step #1: Stop Apache

Open Terminal and run the following command to stop Apache server.

$ sudo systemctl stop apache2.service

Note! In the above command we are using sudo so, you will be asked to enter your password.

Step #2: Create new document root directory

By default the document root directory is /var/www/html.

In this tutorial we will create a new directory www inside the current user directory.

So, type the cd command to go inside the current user home directory.

$ cd

Now create the www directory.

$ mkdir www

You can name your document root directory whatever you want and place it wherever you want as long as you have the permission to access and modify in that location.

So, the path of the new document root directory will look like the following.

/home/username/www

Where, username is your current logged in user name.

To know the current user name run the following command in the terminal whoami.

Step #3: Set document root in apache2.conf file

Now, cd into /etc/apache2 directory and you will find the apache2.conf file.

Suggestion: Create a backup copy when making changes to configuration files. It helps when you want to revert back :-)

Open this file using vim by running the following command.

$ sudo vim apache2.conf

Now scroll to the following section in the file.

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

Change the Directory to /home/username/www/.

Don't forget to change the username to your current user name.

So, after the change you will get the following.

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

Save the changes and exit the file.

If you are using vim then type ESC key and then type :wq and it will save the changes and will exit.

Step #4: Making changes in the 000-default.conf file

cd into the /etc/apache2/sites-available directory and you will find the 000-default.conf file.

Open it using vim or nano and find the following line.

DocumentRoot /var/www/html

Change it to the following.

DocumentRoot /home/username/www

Where, username is the current user name.

Save the file and exit.

Step #5: Restart Apache

It's time to restart Apache server. Run the following command in the terminal.

$ sudo systemctl restart apache2.service

Result

You are now ready to use your new document root directory. To access it you can open localhost or 127.0.0.1 in your browser.

I have copied the index.html page from the /var/www/html directory to the new document root directory /home/yusufshakeel/www so, I am getting the following output.

Feel free to create your own index.html file in the new document root directory and see the result in the browser.

Have fun developing :-)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT