How to Change Default Apache ‘DocumentRoot’ Directory in Linux

To modify the document root directory, This approach makes use of symbolic links and necessitates the use of SSH. 

Do not use the techniques below if you want to alter the document root directory for an addon domain. Instead, utilize cPanel to update the document root directory for the addon domain.

  • Use SSH to access your account.
mv ~/public_html ~/public_html_backup
  • This command copies the contents of the public_html directory, including all files, to the public_html backup directory. If you choose, you can call it something other than public_html backup.
  • You must perform the next command in step 3 as soon as possible after running this command for two reasons. First, as long as public_html is absent, your site is unreachable. Second, if the public_html directory is absent, cPanel will automatically recreate it. Your site will not perform as expected if you have not run the command in step 3 before this happens.
ln -s ~/new_document_root ~/public_html

To test your website, use a web browser. Content from the new document root directory should be displayed in the browser.

If your website isn’t working properly, you should:

  • Ascertain that the new document root directory is present and contains web site files (such as index.html or index.php).
/home/username/public_html -> /home/username/new_document_root

Now, repeat steps 2 and 3.

Restoring the public_html Directory

  • Use SSH to access your account.
  • The symbolic link that redirects public_html to the updated document root directory is removed with this command.
  • You must perform the next command in step 3 as soon as possible after running this command for two reasons. First, as long as public_html is absent, your site is unreachable. Second, if the public_html directory is absent, cPanel will automatically recreate it. Your site will not perform as expected if you have not run the command in step 3 before this happens.
mv ~/source_directory ~/public_html

To test your website, use a web browser. Content from the public html directory should be displayed by the browser.

Apache web server is probably the most used web server across platforms including different Linux distributions and Windows. The web server is used to deliver web content and can serve many queries at once.

In this short tutorial, you are going to learn how to modify the root directory for Apache web server. For the purpose of this tutorial, we will use Ubuntu/Debian and RHEL/CentOS/Fedora based installations of the web server.

However the paths and directives are practically the same for other distributions as well, so you will be able to apply the learned in different OSes as well.

To make the necessary changes you need to modify the DocumentRoot directive of the web server. This is the directory from which Apache will read the contents that the visitor will access over browser. Or in other words, this is the directory that forms the tree of directories that will be accessible over web.

The default DocumentRoot for Apache  is:

/var/www/html
or
/var/www/

These paths are described in the Apache’s configuration file.

Ubuntu/Debian

/etc/apache2/sites-enabled/000-default
/etc/apache/apache2.conf

CentOS/RHEL/Fedora

/etc/httpd/conf/httpd.conf

To change the document root for your Apache web server simply open the corresponding file with your favourite text editor and search for DocumentRoot.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

After that change the path to the new target directory and make sure that Apache is able to read/write in that directory. Once you have modified the DocumentRoot, save the file and restart apache with:

# systemctl restart apache     [For SystemD]
# service httpd restart        [For SysVinit]    

Final thoughts

The change of default document root is an easy task that can be completed in a couple of minutes. When making such changes is important to make sure that you do not make any typos and make sure to always restart Apache after making changes to its configuration file.

Дополнительно:  Не работают кнопки на клавиатуре – 10 основных причин

I’m trying to change the XAMPP‘s web root default directory /opt/lampp/htdocs to another one like /home/me/Dropbox/public_html without success.

I’ve edited the file /opt/lampp/etc/httpd.conf

# old line: DocumentRoot "/opt/lampp/htdocs"
DocumentRoot "/home/me/Dropbox/public_html"

#...etc...

# old line: <Directory "/opt/lampp/htdocs">
<Directory "/home/me/Dropbox/Work/public_html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
# etc...

I’ve did this as said in this article: Using Ubuntu One to synchronise htdocs?

Then I’ve restarted Apache and I’ve got a permission error 403 on every page I’ve called with the web browser.

So I’ve changed folder and files permission to 755.

I’ve did this as said in this article:
What file permissions should I set on web root?

The problem still remains the same, I have the 403 error on every page I try to reach with the web browser.

I have the same problem on a Mac using XAMPP.

So everythig works fine if the folder remains the original /opt/lampp/htdocs.

How can I change it correctly?

SharpC's user avatar

asked Oct 3, 2011 at 14:01

vitto's user avatar

Open /opt/lampp/etc/httpd.conf
change nobody and nogroup

<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User nobody
Group nogroup
</IfModule>

hellocatfood's user avatar

5 gold badges36 silver badges64 bronze badges

answered Nov 25, 2012 at 3:18

Zeroows's user avatar

3 silver badges9 bronze badges

Had the same issue and here is what i did:

Run this command to stop xampp:

sudo /opt/lampp/lampp stop

Change your_folder to the folder you wanna use

 DocumentRoot "/home/username/your_folder"
 <Directory "/home/username/your_folder">
User nobody
Group nogroup

set chmod to your_folder with this command

sudo chmod 777 /home/username/your_folder

Save the file and start xampp with this command:

sudo /opt/lampp/lampp start

Open your browser and enter this url:

http://localhost/your_folder/

Done, hope this helps.

answered Jul 24, 2017 at 19:10

Exil's user avatar

1 gold badge5 silver badges17 bronze badges

answered Oct 4, 2011 at 7:33

vitto's user avatar

2 gold badges4 silver badges9 bronze badges

You can create a symbolic link

ln -s /home/username/public_html/ /opt/lampp/yoursymlink 

Zeroows's user avatar

3 silver badges9 bronze badges

answered May 19, 2012 at 11:36

baptx's user avatar

3 silver badges10 bronze badges

If you are doing this so that you can create files in htdocs folder then Just navigate to opt/lampp and then open in terminal and write

sudo chmod 777 htdocs/

answered Jun 23, 2016 at 17:11

user260214's user avatar

  1. $ sudo cd Downloads && sudo chmod a+x ./your-downloaded-xampp-file.run && sudo ./your-downloaded-xampp-file.run
    
  2. sudo chown -hR  username:root /opt/lampp
    
  3. sudo gedit /opt/lampp/etc/httpd.conf
    
  4. <IfModule unixd_module>
       User nobody
       Group nogroup
    </IfModule>
    
  5. restart xampp. And you will find forbidden error has gone

Braiam's user avatar

30 gold badges177 silver badges264 bronze badges

answered Aug 7, 2013 at 21:51

Nur Rony's user avatar

Nur Rony

3 silver badges14 bronze badges

    <IfModule unixd_module>
    User daemon 
    Group daemon
    </IfModule>
    <IfModule unixd_module>
    User myusername 
    Group myusername
    </IfModule>

and further down replace:

    DocumentRoot "/opt/lampp/htdocs"
    <Directory "/opt/lampp/htdocs">
    DocumentRoot "/home/myusername/webdir" 
    <Directory "/home/myusername/webdir"

In the terminal, stop lampp before saving the file, start again. In the browser localhost will then point to «webdir».

answered Jun 28, 2018 at 8:10

Michal's user avatar

In NTFS format you can’t set folder permissions. Open /opt/lampp/etc/httpd.conf.

And after that you can access the symbolic links in htdocs.

jokerdino's user avatar

24 gold badges132 silver badges201 bronze badges

answered May 27, 2012 at 6:50

ahmedsaoud31's user avatar

Just make an index file on your new htdocs folder.

Eliah Kagan's user avatar

54 gold badges315 silver badges489 bronze badges

answered Jul 6, 2012 at 1:58

ryan lou's user avatar

This is in effect, four steps, after you copy your data back to /var/www/html:

  1. Give Apache access to the folders and files, so it can serve the site without 403 errors.
  2. Give your user ‘owner’ over the files and folders, and give yourself read/write on all of the files and folders, as well as the ability to traverse the directories.
  3. (Optional but recommended) Set it up such that any files or folders created from hereon in the entirety of the directory structure has the group set to be www-data.
  4. (Optional) Final security cleanup, where we set up permissions so you and the web server can see the site data, but other users cannot access files or the directory structure for the site.
Дополнительно:  Выключается ноутбук сам по себе | Вторая жизнь Айтишника

(1) Allow Apache access to the folders and the files.

sudo chgrp -R www-data /var/www/html
sudo find /var/www/html -type d -exec chmod g+rx {} +
sudo find /var/www/html -type f -exec chmod g+r {} +

This recursively sets the ‘group’ to be www-data for the folders and files. This then gives the web server permission to recurse and get access to the site document root directories structure (+x for directories only). It then also ensures the web server has read permissions for all files, so site data can be received.

There may be some cases where you have to give the web server write permission to a file, or to a directory — this can be achieved by doing sudo chmod g+w /var/www/html/PATH (where PATH is the path to the file or folder in the directory structure where you need to apply the write permissions for the web server).

Note also that you may have to re-run these commands in the future if ‘new files’ get 403 issues, in order to give correct permissions to the web server to keep being able to access files and folders that are created or copied in and aren’t getting the www-data group set correctly.


(2) Give your owner read/write privileges to the folders and the files, and permit folder access to traverse the directory structure.

sudo chown -R USER /var/www/html/
sudo find /var/www/html -type d -exec chmod u+rwx {} +
sudo find /var/www/html -type f -exec chmod u+rw {} +

(3) (Optional) Make sure every new file after this is created with www-data as the ‘access’ user.

sudo find /var/www/html -type d -exec chmod g+s {} +

This sets the «set gid» bit for the group on the directories. Files and folders created inside these directories will always have www-data as the group, permitting the web server access.


(4) (Optional) Final security cleanup, if you don’t want other users to be able to see the data

sudo chmod -R o-rwx /var/www/html/

There is a slightly less invasive solution to this, as well, though it’s not guaranteed to work for all new files, nor is it guaranteed to work on all file systems, involving file access control lists. This lets you leave ownership of the files with www-data for things, but gives you effective owner rights, for all intents and purposes, even though you don’t personally own the files.

  1. Give ownership back to the webserver system user www-data.
sudo chown -R www-data:www-data /var/www/html
  1. Recursively give you read/write on the files, while giving other users (excluding www-data and root of course) no access to the files.
sudo find /var/www/html -type f -exec setfacl -m u:YOURUSERNAME:rw -m other::--- {} \;
  1. Recursively give yourself read/write/traverse on the directories, remove access to the folders for other users (excluding www-data and root) and set this as the ‘default’ ACL for new files in the directories.
sudo find /var/www/html -type d -exec setfacl -d -m u:YOURUSERNAME:rwx -m o::--- {} \;
  1. We also need to set the setgid bit for all directories, so that if you create a file the webserver can still access it as www-data via group permissions.
sudo find /var/www/html -type d -exec chmod g+s {} \;

And now you’ve got access to all the directories, and you didn’t have to take access away from www-data which helps as the webserver can still create files everywhere as it needs to (such as PHP based frontends having their own cache directories and such needing to be created and written to for proper operation).

The only caveat: If you manually create new files, you need to chown them accordingly to give ownership to the webserver. That’s a simple sudo chown www-data:www-data filename, and the access control lists should still let you have effective owner rights to the file.

I installed the ubuntu 12.04 server edition for my server pc . i had installed lamp server. i need to change the var/www location to my secondary hard disk location. i was configured so many time to at gedit /etc/apache2/sites-available/default here is my code

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    #DocumentRoot /var/www
    DocumentRoot /media/myserver/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    #<Directory /var/www/>
        <Directory /media/myserver/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

and also used

sudo chown -R var/www /media/myserver/
chmod -R 755 /media/myserver/
Forbidden

You don't have permission to access / on this server.

Please tell anyone how to mount myserver at my var/www, thanks advance

Дополнительно:  Панель приборов (с. 86) - Ford Focus 3

Achu's user avatar

16 gold badges78 silver badges101 bronze badges

asked Aug 27, 2013 at 12:38

Moovendra Dhinesh babu's user avatar

You’ll have to edit apache2.conf and 000-default.conf to change the document root of apache.

The Apache server is installed on /var/www/html.This is the default root directory of apache.

Either change the root directory of Apache or move the project to /var/www/html.

  1. To change Apache’s root directory, run:

     cd /etc/apache2/sites-available
    
  2. Then open the 000-default.conf file using the command:

     nano 000-default.conf
    
  3. Edit the DocumentRoot option:

     DocumentRoot /path/to/my/project
    
  4. Then restart the apache server:

     sudo service apache2 restart
    

  1. Find the apache2.conf located in /etc/apache2 and open it using:

     nano apache2.conf
    
  2. Use Ctrl+W and search for Directory (It should be in line 153)

  3. It should look like this

     <Directory />
         Options Indexes FollowSymLinks
         AllowOverride All
         Require all denied
     </Directory>
    
  4. Change it to

     <Directory />
         Options Indexes FollowSymLinks Includes ExecCGI
         AllowOverride All
         Require all granted
     </Directory>
    
  5.  sudo service apache2 restart
    

I made a script that changes apache root in a single command. You can find it on my github.

Dan's user avatar

7 gold badges70 silver badges94 bronze badges

answered Feb 24, 2016 at 13:23

Harjot's user avatar

Maybe a little bit late. But still..

You should edit your directory permissions in apache.conf under /etc/apache2

Search for this

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

and add this code under of it, which gives the permission to access your directory

 <Directory /media/myserver/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
    </Directory>

answered Feb 15, 2014 at 12:10

onurbekiroglu's user avatar

Simply change the document root in your activated configuration. /etc/apache2/sites-enabled/000-default and then Make sure reloading your apache.

So try with this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /media/myserver/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /media/myserver/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then the proper permission should be given like this:

sudo adduser <username> www-data
sudo chown -R www-data:www-data /media/myserver/
sudo chmod -R g+rw /media/myserver/

answered Aug 27, 2013 at 13:06

Achu's user avatar

16 gold badges78 silver badges101 bronze badges

  1. you modify apache2.conf. Replace /var/www/ with /your/path:

    <Directory /your/path/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    
  2. Modify the 000-default.conf file:

    DocumentRoot /your/path/
    
  3. Change the ownership of the directories to yourself from file manager or terminal (e.g. sudo chown pi: path). Without this you will get this error message:

    You don't have permission to access / on this server.
    

BeastOfCaerbannog - On strike's user avatar

answered Apr 25, 2019 at 21:41

eatysweaty's user avatar

As a quick workaround (safe and quick) you can make the mounting point of your external hard driver to the default root directory ( /var/www by default).

Assigning the mounting point to a per-existing directory is safe but the old content can’t be reached unless you unmounted the driver.

answered May 27, 2017 at 9:24

Yahya Tawil's user avatar

in virtual configuration :/etc/apache2/sites-available/site.conf

ServerAdmin anilrmg@localhost.com
ServerName anilrmg.localhost.com
ServerAlias www.anilrmg.localhost.com
DocumentRoot /home/anilrmg/projects/code/anilrmg

sudo a2dissite 000-default.conf

Mudit Kapil's user avatar

7 gold badges29 silver badges45 bronze badges

answered Mar 17, 2015 at 10:59

Anil Gupta's user avatar

For those who use VirtualBox guest additions and get you don't have permission to access /on this server despite of everything mentioned above:

sudo usermod -a -G vboxsf www-data

You'reAGitForNotUsingGit's user avatar

answered May 27, 2016 at 16:01

Yuri Fedorov's user avatar

I had to make sure the whole path was granted Apache.

chown www-data /media;
chown www-data /media/MNT/;
chown www-data /media/MNT/DISK;
chown www-data /media/MNT/DISK/www-root;

answered Dec 6, 2020 at 16:07

LarsAamo's user avatar

1 silver badge8 bronze badges

Оцените статью
Master Hi-technology
Добавить комментарий