48

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/

and

chmod -R 755 /media/myserver/

still i was not able to connect my /media/myserver and my browser show the following message

Forbidden

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

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

Achu
  • 21,237

8 Answers8

74

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
    

If you get Forbidden You don't have permission to access / on this server after changing the root of apache then do follow these steps

  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. Restart apache

     sudo service apache2 restart
    

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

Dan
  • 13,119
Harjot
  • 756
  • It is kind of work for me but I have to add <Directory /path/to/my/project>Options Indexes FollowSymLinks AllowOverride All Require all denied </Directory>. In /etc/apache2/apahce2.conf before restarting the apache2 service. – r0ng Nov 15 '16 at 02:21
  • This works in 20.04, Thank you. – Geppettvs D'Constanzo Jun 21 '22 at 22:32
25

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>
onurbekiroglu
  • 981
  • 7
  • 5
6

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/
Achu
  • 21,237
  • yes my friend i was done this already . see the above code i was comment the default document root and inserted new document root. after apache2 stop and start. it's not worked. it's 403 error, and showed permission access denied and my browser – Moovendra Dhinesh babu Aug 27 '13 at 16:49
  • Have you seen my last edite? – Achu Aug 27 '13 at 20:07
  • yes i saw this. still it's show you dont have permission to access /on this server – Moovendra Dhinesh babu Aug 28 '13 at 02:58
  • error when running httpd.exe: Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration – mercury Nov 07 '22 at 00:06
4
  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.
    
2

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.

To learn more how to create a mounting point refer to this.

  • According to that idea I would mount certain folder, by using bindfs, as it is described here: https://askubuntu.com/a/1024308/566421 – pa4080 Jun 06 '19 at 12:25
1

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;
LarsAamo
  • 416
1

sudo gedit etc/apache2/apache2.conf add this

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

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
  • 2,051
  • 7
  • 30
  • 47
1

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

If you are trying to set the Apache document root folder to a VirtualBox shared folder,and you have tried everything above and that did not help, there is one more step.

In short, the solution is to add user 'www-data' to the group 'vboxsf':

sudo usermod -a -G vboxsf www-data

You can not change the owner and/or the group of the VirtualBox shared folder, but the solution above worked well for me.