Sunday, October 27, 2013

Ubuntu Apache2 : Change default DocumentRoot /var/www

By default the document root folder for apache2 in Ubuntu is /var/www.
This is where you can store your site documents.
In order to change the default site location to a different one, /opt/mysite use the following method.
A detailed steps to install LAMP on ubuntu is given here.
To do this, we must create a new site and then enable it in Apache2.

To create a new site:

Copy the default website as a starting point.

 - sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite

Edit the new configuration file in a text editor “sudo nano” on the command line or “gksudo gedit”, for example:

 - gksudo gedit /etc/apache2/sites-available/mysite

Change DocumentRoot /var/www to  DocumentRoot /opt/mysite

Change the Directory directive, replace <Directory /var/www/> to <Directory /opt/mysite/>

You can also set separate logs for each site. To do this, change the ErrorLog and CustomLog directives. This is optional, but handy if you have many sites

Save the file

Now, we must deactivate the old site, and activate our new one. Ubuntu provides two small utilities that take care of this: a2ensite (apache2enable site) and a2dissite (apache2disable site).

 - sudo a2dissite default && sudo a2ensite mysite

Finally, we restart Apache2:

 - sudo service apache2 restart

FOR UBUNTU 13.10

Previously, files in /etc/apache2/sites-available had no extension. For example /etc/apache2/sites-available/default. Now a .conf extension is required.

Example:
If you had /etc/apache2/sites-available/some-site, in 13.04 you can just enable it using sudo a2ensite some-site. Now it will give you an error saying

ERROR: Site some-site does not exist!

To fix this, append a .conf to all your config files in sites-available. You can do the same in sites-enabled, or you can delete all the files and re-enable them each manually.
I recommend doing them manually since you probably need to fix each VHost (next step).

sudo find /etc/apache2/sites-available/ ! -iname '*.conf' -type f -exec mv '{}' '{}'.conf \;
if you decided to do them manually:

sudo rm /etc/apache2/sites-enabled/*
sudo a2ensite your-site-name

No comments:

Post a Comment