By default, the Apache web root or Document root folder location is at /var/www/html.
The DocumentRoot is the top-level directory in the document tree visible from the web and this directive sets the directory in the configuration from which Apache2 or HTTPD looks for and serves web files from the requested URL to the document root. For example: DocumentRoot "/var/www/html"
You need to change the DocumentRoot setting in your httpd. conf file. Chances are it will be under something like /etc/apache2/conf/httpd. conf .
Please note, that this only applies for Ubuntu 14.04 LTS (Trusty Tahr) and newer releases.
In my Ubuntu 14.04 LTS, the document root was set to /var/www/html
. It was configured in the following file:
/etc/apache2/sites-available/000-default.conf
So just do a
sudo nano /etc/apache2/sites-available/000-default.conf
and change the following line to what you want:
DocumentRoot /var/www/html
Also do a
sudo nano /etc/apache2/apache2.conf
and find this:
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
And change /var/www/html
to your preferred directory and save it.
After you saved your changes, just restart the Apache 2 web server and you'll be done :)
sudo service apache2 restart
If you prefer a graphical text editor, you can just replace the sudo nano
with a gksu gedit
.
You need to change the DocumentRoot
setting in your httpd.conf
file. Chances are it will be under something like /etc/apache2/conf/httpd.conf
.
Use your favourite editor (I recommend Vim) and look for the DocumentRoot
and change it to /users/spencer/projects
. Also look a little further down for a setting that looks like this:
<Directory "/var/www">
You will also want to change what is in the quotes to your new directory. This gives Apache access to read from that directory when a user makes a request that call on it.
Now restart your Apache service (httpd -k restart
) and you should be good to go.
Apache 2 site configuration files are now typically kept in /etc/apache2/sites-available/
(Debian, Ubuntu, etc.).
I had to edit /etc/apache2/sites-available/default
. The lines are the same as mentioned by RDL.
This is for Ubuntu 14.04 (Trusty Tahr):
In file /etc/apache2/apache2.conf
it should be as below without the directory name:
<Directory /home/username>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
And in file /etc/apache2/sites-available/000-default.conf
you should include the custom directory name, i.e., www:
DocumentRoot /home/username/www
If it is not as above, it will give you an error when loading the server:
Forbidden You don't have permission to access / on this server
The right way to change directory or run from multiple directories under different port for Apache 2 is as follows:
For Apache 2, the configuration files are located under /etc/apache2
and doesn’t use a single configuration file as in older versions but is split into smaller configuration files, with /etc/apache2/apache2.conf
being the main configuration file. To serve files from a different directory we need a new virtualhost conf file. The virtualhost configuration files are located in /etc/apache2/sites-available
(do not edit files within sites-enabled). The default Apache installation uses virtualhost conf file 000-default.conf
.
Start by creating a new virtualhost file by copying the default virtualhost file used by the default installation of Apache (the one that runs at localhost on port 80). Change into directory /etc/apache2/sites-available
and then make copy by sudo cp 000-default.conf example.com.conf
, now edit the file by sudo gedit example.com.conf
to:
<VirtualHost *:80>
ServerAdmin example@localhost
DocumentRoot /home/ubuntu/example.com
</VirtualHost>
I have deleted the nonimportant lines from the above file for brevity. Here DocumentRoot
is the path to the directory from which the website files are to be served such as index.html
.
Create the directory from which you want to serve the files, for example, mkdir example.com
and change owner and default group of the directory, for example, if your logged in user name is ubuntu
change permissions as sudo chown ubuntu:www-data example.com
. This grants full access to the user ubuntu
and allows read and execute access to the group www-data
.
Now edit the Apache configuration file /etc/apache2/apache2.conf
by issuing command sudo gedit apache2.conf
and find the line <Directory /var/www/>
and below the closing tag </Directory>
, add the following below:
<Directory /home/ubuntu/example.com>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
Now there are two commands to enable or disable the virtualhost configuration files, which are a2ensite
and a2dissite
respectively. Now since our example.com.conf
file uses the same port(80
) as used by the default configuration file(000-default.conf
), we have to disable the default configuration file by issuing the command sudo a2dissite 000-default.conf
and enable our virtualhost configuration file by sudo a2ensite example.com.conf
Now restart or reload the server with command sudo service apache2 restart
. Now Apache serves files from directory example.com
at localhost
on default port of 80
.
The a2ensite
command basically creates a symbolic link to the configuration file under the site-enabled directory.
Do not edit files within sites-enabled (or *-enabled) directory, as pointed out in this answer.
Now if you need to run the directory on a different port, change the port number from 80 to 8080 by editing the virtualhost file as:
<VirtualHost *:8080>
ServerAdmin user@localhost
DocumentRoot /home/ubuntu/work
</VirtualHost>
and editing /etc/apache2/ports.conf
and adding Listen 8080
just below the line Listen 80
Now we can enable the default virtualhost configuration file that runs on port 80 since example.com directory uses port 8080, as sudo a2ensite 000-default.conf
.
Now restart or reload the server with command sudo service apache2 restart
. Now both the directories can be accessed from localhost
and localhost:8080
.
If you couldn't find http.conf
and followed Nick's way.
Restart Apache using sudo service apache2 restart
.
I was working with LAMP and to change the document root folder, I have edited the default file which is there in the /etc/apache2/sites-available folder.
If you want to do the same, just edit as follows:
DocumentRoot /home/username/new_root_folder
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/username/new_root_folder>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
After this, if you type "localhost" in the browser, it will load the /home/username/new_root_folder content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With