Change Apache document root folder to secondary hard drive

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.


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>

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/