256

How can I change the document root of the Apache server? I basically want localhost to come from /users/spencer/projects directory instead of /var/www.


I ended up figuring it out. Some suggested I change the httpd.conf file, but I ended up finding a file in /etc/apache2/sites-available/default and changed the root directory from /var/www to /home/myusername/projects_folder and that worked.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Spencer Cooley
  • 7,281
  • 13
  • 42
  • 61
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Server Fault](http://serverfault.com/) or [Webmaster Stack Exchange](http://webmasters.stackexchange.com/) would be a better place to ask. – jww Sep 24 '18 at 22:30

15 Answers15

267

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mozzbozz
  • 2,667
  • 5
  • 26
  • 39
  • On my ubuntu 13.10, I cannot find any *.conf files under /etc/apache2/sites-available/, is it because of different version? – Paul Lo Jun 25 '14 at 08:53
  • 1
    @Paul Lo: I haven't tried this on Ubuntu 13.10, only on Ubuntu 14.04; As I'm pretty sure that the first time I set up apache (which was on Ubuntu 12.04), there was no such directory. So yes, I guess that this is only available on Ubuntu 14.04 LTS and newer. However, you can have a look at the other answers. The upvotes suggest that they helped other people, maybe they also work with Ubuntu 13.10. – mozzbozz Jul 02 '14 at 11:39
  • 1
    However thanks for asking, I just edited this information into the answer so that noone else walks into the trap of trying this with older Ubuntu versions (y) – mozzbozz Sep 12 '14 at 12:51
  • 3
    @mozzbozzm, you are right. Only 14.04. Really a weird choice, now suddenly having `var/www/html` as default root. Are they actually **trying** to waste peoples time? :) – davidkonrad Oct 16 '14 at 16:44
  • This finally worked, I am hosted in azure, i realised the server data was being cached somewhere, so had to add a fake query string to get the site with the new folder. whew!!! – Sharjeel Ahmed Nov 23 '14 at 08:47
  • 4
    I'm getting a forbidden error `You don't have permission to access / on this server.` when I hit localhost:80 in my browser. Do I need to change the port? – Costa Feb 05 '15 at 20:34
  • I had to add 000-default to sites-enabled: /etc/apache2/sites-enabled$ sudo ln -s /etc/apache2/sites-available/000-default.conf . – Ken J Apr 26 '15 at 14:41
  • https://help.ubuntu.com/community/ApacheMySQLPHP#Virtual_Hosts – optimiertes Sep 21 '15 at 03:42
  • @Costa did you figure it out? I'm having the same problem – Matt G Aug 10 '16 at 18:03
  • @MattG Oh, man, it's been so long, I'm not sure. – Costa Aug 11 '16 at 16:32
  • 3
    @Costa I solved the problem by adding ` Options Indexes FollowSymLinks AllowOverride None Require all granted ` in the /etc/apache2/apache2.conf – Keenan Gebze Dec 17 '16 at 10:29
223

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.).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
RDL
  • 7,537
  • 2
  • 26
  • 31
  • 6
    I opened /etc/apache2/httpd.conf , but the file was blank. I am opening it with gedit. – Spencer Cooley May 05 '11 at 02:03
  • 4
    The file might be in /etc/httpd/conf/httpd.conf, depending on your flavor of Linux. – Chris Henry May 05 '11 at 02:07
  • 3
    the httpd.conf file is empty but I found a file at etc/apache2/sites-available/default and changed the root directory there. I prob should have mentioned that this is not on an actual server. I am setting up a local development environment. Not sure if that makes a difference – Spencer Cooley May 05 '11 at 04:44
  • it's supposed to be empty @spencer. check here http://ubuntuforums.org/showthread.php?t=278531 – olix20 Jul 02 '11 at 07:20
  • @RDL (or others) My apache have no `/etc/apache2/conf/httpd.conf`, the `locate httpd.conf` returns only `/etc/javascript-common/lighttpd.conf` ... What I do? Create a new file or change this file? – Peter Krauss Feb 17 '14 at 15:02
  • @Peter Krauss, try doing a "which httpd" from the command line. That will tell you where the service is running from and the "conf" may be up a directory from there. – RDL Feb 17 '14 at 21:35
  • Thanks! My conf (of a standard Apache!) was `/etc/apache2/sites-available/default/000-default.conf`, now is working with [similar modification (DocumentRoot) reported here](http://stackoverflow.com/q/21831272/287948). – Peter Krauss Feb 18 '14 at 10:09
  • 3
    Would it be possible to enable both /var/www and /home/myusername/projects_folder at the same time? And how should I config? thanks. – Paul Lo Jun 25 '14 at 08:57
  • I found mine (Xubuntu 14.04) in `/etc/apache2/sites-available/000-default.conf` – Coreus Sep 15 '15 at 23:41
  • I Have made a script that changes the Apache root in single command. Link:https://github.com/hsb4995/Apachange – Golden_flash Feb 16 '16 at 19:11
  • If you only change the documentroot in httpd.conf, it won't work if you have a virtual host for *:80, pointing elsewhere. So change the documentroot there too. – trogne Nov 25 '16 at 00:35
  • 4
    The "answer" is really obsolete. Correct answer according to Ubuntu 18.x docs is Either change the root directory of Apache or move the project to /var/www/html . To change Apache's root directory, run: cd /etc/apache2/sites-available. Then open the 000-default.conf file using the command: nano 000-default.conf. Edit the DocumentRoot option: DocumentRoot /path/to/my/project. (see https://askubuntu.com/questions/337874/change-apache-document-root-folder-to-secondary-hard-drive) – sea26.2 Oct 04 '19 at 23:15
43

I had to edit /etc/apache2/sites-available/default. The lines are the same as mentioned by RDL.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Nick
  • 8,822
  • 6
  • 41
  • 65
  • 3
    A suggested edit by @pabloab had this to say: "Apache 2.4 and above requires `.conf` at the end, and by default have a `000-` as prefix". The result is `/etc/apache2/sites-available/000-default.conf`. I haven't tested this out, but it may help some folks. – Nick Sep 26 '14 at 12:23
  • ^^ this fixed it. Where is it documented that I require .conf at the end of my sites-available files?!? I have about 20 virtual hosts that were disabled and offline after an update, and after a couple hours, finding this and renaming my sites-available files fixed it for me. – James Perih Jan 10 '16 at 07:08
26

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
androsfat
  • 472
  • 7
  • 14
19

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.

To change the port and run from multiple directories on different ports:

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
lordvcs
  • 1,135
  • 2
  • 14
  • 26
4

If you couldn't find http.conf and followed Nick's way.

Restart Apache using sudo service apache2 restart.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Nitin
  • 5,077
  • 3
  • 24
  • 31
4

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Shashidhara
  • 607
  • 6
  • 18
4

For Apache 2 on Linux Mint 17.3 Cinnamon 64-bit, the following works:

  1. In /etc/apache2/sites-available/ open the 000-default.conf file, and change the Document Root to the absolute path of your directory.

    sudo vim /etc/apache2/sites-available/000-default.conf

  2. In folder /etc/apache2/ open file httpd.conf, and add a <Directory> tag referencing your directory and containing the exact same settings as the tag for var/www.

     sudo vim /etc/apache2/apache2.conf
    

    On my machine it looked like this:


     <Directory /home/my_user_name/php/>
             Options Indexes FollowSymLinks
             AllowOverride All
             Require all granted
     </Directory>
    

Note: In the first step you probably want to change Document Root in the default-ssl.conf file as well for SSL purposes. But as far as I can tell, this isn't required to get a general development environment running.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
stackato
  • 41
  • 2
2

In Apache version 2.4.18 (Ubuntu).

  1. Open the file /etc/apache2/apache2.conf and search for <Directory /var/www/> and replace to your directory.

  2. Open file /etc/apache2/sites-available/000-default.conf, search for DocumentRoot /var/www/html and replace it with your DocumentRoot.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
habibun
  • 1,258
  • 1
  • 12
  • 23
2

In case you are using Ubuntu 16.04 (Xenial Xerus), please update the 000-default.conf file in the directory /etc/apache2/sites-available.

Here →

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/YourFolder

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Shashank Saxena
  • 1,834
  • 14
  • 10
  • What do you mean by "update"? Removing comment characters? Replacing something with something else? Adding "/YourFolder"? Or something else? – Peter Mortensen Feb 12 '21 at 14:48
1

I had made the /var/www to be a soft link to the required directory (for example, /users/username/projects) and things were fine after that.

However, naturally, the original /var/www needs to be deleted - or renamed.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
maan81
  • 2,839
  • 7
  • 32
  • 47
0

If someone has installed LAMP in the /opt folder, then the /etc/apache2 folder is not what you are looking for.

Look for httpd.conf file in folder /opt/lampp/etc.

Change the line in this folder and save it from the terminal.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Yogesh Gupta
  • 170
  • 1
  • 15
0

In Red Hat Linux 7.0: /etc/httpd/conf/httpd.conf

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ehacinom
  • 5,256
  • 3
  • 32
  • 56
0

If you're using Linux Mint (personal opinion, from all distributions this one is making me happy), follow this:

  1. Go to folder /etc/apache2/sites-available and edit file 000-default.conf.
  2. Search for DocumentRoot, example DocumentRoot /var/www/html. You change to your respective directory;
  3. Open a terminal and type: sudo service apache2 restart

In Linux Mint, you go for file /etc/apache2/apache.conf. Replace /var/www with your respective path, and then restart the server (step 3).

That's it.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Marcelo Agimóvel
  • 1,432
  • 2
  • 14
  • 23
0

The following applies to Ubuntu 14.04 (Trusty Tahr) and later releases. Make sure to back up following files before making any changes.

  1. Open /etc/apache2/apache2.conf and search for <Directory /var/www/> directive and replace path with /home/<USERNAME>/public_html. You can use * instead of .

  2. Open /etc/apache2/sites-available/000-default.conf and change the DocumentRoot value property from /var/www/html to /home/<USERNAME>/public_html. Also <Directory /var/www/html> to <Directory /home/<USERNAME>/public_html.

  3. Open /etc/mods-available/php7.1.conf. Find and comment the following code

    php_admin_flag engine Off

Do not turn the php_admin_flag engine OFF flag on as the reason is mentioned in a comment above the Directive code. Also the PHP version can be 5.0, 7.0 or anything you have installed.

Create public_html directory in home/<USERNAME>.

Restart the Apache service by executing command sudo service apache2 restart.

Test by running a sample script on the server.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user1184294
  • 187
  • 1
  • 3
  • 10