How to Change Apache Default Web Root Folder to a New Location on Ubuntu 16.04


In this article, we will learn how to change the Apache default web root folder to a new location. By default, the Apache web root or Document root folder location is at /var/www/html.

These type of changes will be useful for security reason or having the space issue due to data size, we want to change the document root folder to another location or mount point. This will be helpful if we have multiple instances and we want to keep the data for each website into their own volumes or folders.

Prerequisites

  • An Ubuntu 16.04 server with a user with Sudo permissions on the machine.
  • Apache2 web server installed on the machine
  • A new mount location or the new folder location for moving the Site information or files.

Moving the Website Data to a New Location

As we already have knowledge that, the default location of the Apache web server is /var/www/html, or if you have a different setup with multiple sites with multiple document roots for different sites.

If we have a multiple site which configured and enabled in the Virtual Host of the Apache, we can search for the document root in the /etc/apache2/sites-enabled folder.

Below is the command to find the folder name for all the multiple sites published in the server.

$ grep –R “DocumentRoot” /etc/apache2/sites-enabled

Since I have only one site, enable the output will be like this –

sites-enabled/000-default.conf DocumentRoot /var/www/html

Using the rsync command we will copy all the files to a new folder location where we want to move the default document root folder of the Apache web server.

For example, in our environment the new web root document folder will be ‘/mnt/newdatavol’

$ sudo rysnc –av /var/www/html /mnt/newdatavol

Modifying the Apache Configuration Files

Apache uses two types of configuration files, one is global and another is site-specific configurations, as we are working with the existing installation. We will modify the virtual host files which are found using the grep command.

By default, we have to change two virtual host configuration files which come with Apache 000-default.conf and default-ssl.conf.

$ sudo vi /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
   # The ServerName directive sets the request scheme, hostname and port that
   # the server uses to identify itself. This is used when creating
   # redirection URLs. In the context of virtual hosts, the ServerName
   # specifies what hostname must appear in the request's Host: header to
   # match this virtual host. For the default virtual host (this file) this
   # value is not decisive as it is used as a last resort host regardless.
   # However, you must set it for any further virtual host explicitly.
   #ServerName www.example.com
   ServerAdmin webmaster@localhost
   DocumentRoot /mnt/newdatavol
   # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
   # error, crit, alert, emerg.
   # It is also possible to configure the loglevel for particular
   # modules, e.g.
   #LogLevel info ssl:warn
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
   # For most configuration files from conf-available/, which are
   # enabled or disabled at a global level, it is possible to
   # include a line for only one particular virtual host. For example the
   # following line enables the CGI configuration for this host only
   # after it has been globally disabled with "a2disconf".
   #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

We save the file and now we will change the default folder location for the Apache web server for SSL port below to see the command to edit the default-ssl.conf

$ sudo vi /etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
   ServerAdmin webmaster@localhost
   DocumentRoot /mnt/newdatavol
   # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
   # error, crit, alert, emerg.
   # It is also possible to configure the loglevel for particular
   # modules, e.g.
   #LogLevel info ssl:warn
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
   # For most configuration files from conf-available/, which are
   # enabled or disabled at a global level, it is possible to
   # include a line for only one particular virtual host. For example the
   # following line enables the CGI configuration for this host only
   # after it has been globally disabled with "a2disconf".
   #Include conf-available/serve-cgi-bin.conf
   # SSL Engine Switch:
   # Enable/Disable SSL for this
   ….
   ..
   ..
</VirtualHost>

Restarting the Apache Web Server

Once we change the configuration changes to the Default document root, we need to restart the Apache server to make the changes effect. Before that we will check for the syntax errors

$ sudo apachectl configtest
Syntax OK

As we can see the message Syntax OK, now we can go ahead and restart the server to make changes effective.

$ sudo systemctl restart apache2

In the above article we have learned how to change the default Document location for the Apache server on Ubuntu 16.04. This will be easy to take backup of the sites which we published without any system default files and they are useful when we have multiple sites or client website data in a different location. We can also use other network storage devices such as NAS and SAN to store the date safely.

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 21-Jan-2020

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements