Changing apache document root in ubuntu

In my development machine I always like to move the default /var/www directory to my home directory, e.g. /home/tareq/www . It’s very simple to do. Open the /etc/apache2/sites-available/default file with administrator previledge and change the document root as you want

$ sudo gedit /etc/apache2/sites-available/default

Now change the file like this:

DocumentRoot /home/tareq/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /home/tareq/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Now restart your apache

$ sudo /etc/init.d/apache2 restart

Update

According to Nasim vai, there is a more elegant solution with a single line command :-

$ sudo rm -r /var/www; sudo ln -s /home/${USER}/www /var/www

10 thoughts on “Changing apache document root in ubuntu

    •  @nsmgr8 I am using my home directory in a separate partition. If somehow my ubuntu installation gets corrupted, my home directory will be safe and I can install the system without much trouble. So I prefer to move the www directory to home instead a symbolic link :)

      •  @tareq  Then try the other way around
        sudo rm -rf /var/www; sudo ln -s /home/${USER}/www /var/www
         
        The point is not to touch default settings. Of course, there are thousand other ways to do it.

      • @tareq
        Then try the other way around
        sudo rm -r /var/www; sudo ln -s /home/${USER}/www /var/www
         
        The point is not to touch default settings. Of course, there are thousand other ways to do it.
         

  1. The symlink (second) way is better imo as it avoids changing config files. You should ad a caveat to your comment though warning people that they are deleting their current doc root and all files within using that method. :)

  2. I actually like changing config files better because that’s a standard process with any other service. I don’t have to try to remember a different method or process. I do it like dns, dhcp, samba, etc – so why deviate from that even if it’s easier via one line change? Stick with a standard and you know where to go always.

Leave a Reply