Software & AppsOperating SystemLinux

How To Change Apache Document Root Folder to Secondary Hard Drive in Ubuntu Server

Ubuntu 16

In this comprehensive guide, we will walk you through the process of changing the Apache document root folder to a secondary hard drive in an Ubuntu server. This is a common task for system administrators who want to expand their server’s storage capacity or segregate their data for security or organizational purposes.

Quick Answer

To change the Apache document root folder to a secondary hard drive in Ubuntu Server, you need to mount the secondary hard drive, modify the Apache configuration file to point to the new document root, change ownership and permissions of the new directory, and restart the Apache service.

Prerequisites

Before we begin, ensure that you have the following:

  • A secondary hard drive that is formatted and mounted to your desired location.
  • Root or sudo access to your Ubuntu server.
  • Apache web server installed on your Ubuntu server.

Step 1: Mounting the Secondary Hard Drive

First, you need to make sure that your secondary hard drive is mounted to your desired location. If it’s not already mounted, you can add an entry to the /etc/fstab file to automatically mount it on boot.

sudo nano /etc/fstab

Add an entry similar to the following, replacing /dev/sdb1 and /media/myserver/ with your hard drive device and mount point respectively.

/dev/sdb1 /media/myserver/ ext4 defaults 0 0

After saving and exiting the file, mount all filesystems mentioned in fstab by running:

sudo mount -a

Step 2: Modifying Apache Configuration

Next, you’ll need to modify the Apache configuration file. This file is typically located at /etc/apache2/sites-available/default or /etc/apache2/sites-available/000-default.conf in newer versions of Apache.

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

In this file, locate the DocumentRoot directive and change it to the path of your secondary hard drive:

DocumentRoot /media/myserver/

Also, update the <Directory> section to match the new document root:

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

The Options directive controls which server features are available in a particular directory. Indexes allows server to generate a listing of a directory if no DirectoryIndex (such as index.html) is found. FollowSymLinks allows the server to follow symbolic links in this directory. MultiViews is used for content negotiation.

AllowOverride None disables the use of .htaccess within this directory. Order allow,deny and allow from all are used to control access to this directory.

Step 3: Changing Ownership and Permissions

Next, change the ownership of the new document root directory to the Apache user (usually www-data). This can be done using the chown command:

sudo chown -R www-data:www-data /media/myserver/

The -R option makes the command recursive, changing the ownership of the directory and its contents. www-data:www-data specifies the user and group that should own the directory.

Set the correct permissions for the new document root directory using the chmod command. The recommended permissions are 755 for directories and 644 for files:

sudo chmod -R 755 /media/myserver/

The 755 permission means that the owner can read, write, and execute, while others can only read and execute.

Step 4: Restarting Apache

Finally, restart the Apache service for the changes to take effect:

sudo service apache2 restart

After following these steps, Apache should now be configured to use the secondary hard drive as the document root. You should be able to access your files through the browser without any permission errors.

We hope this guide has been helpful. If you have any questions or run into any issues, feel free to ask for help on the Apache Users Mailing List or the Ubuntu Forums.

How do I check if my secondary hard drive is mounted correctly?

You can check if your secondary hard drive is mounted correctly by running the command df -h. This will display a list of all mounted filesystems, including your secondary hard drive. Make sure that the mount point you specified in the /etc/fstab file matches the output of this command.

Can I choose a different mount point for my secondary hard drive?

Yes, you can choose a different mount point for your secondary hard drive. In the /etc/fstab file, you can specify any directory as the mount point. Just make sure that the directory exists and has the necessary permissions before adding the entry to /etc/fstab.

What should I do if I encounter permission errors after changing the document root?

If you encounter permission errors after changing the document root, you should check the ownership and permissions of the new document root directory. Make sure that the directory is owned by the Apache user (www-data) and has the correct permissions (755 for directories and 644 for files). You can use the chown and chmod commands as mentioned in Step 3 to set the correct ownership and permissions.

How can I revert back to the default document root folder?

To revert back to the default document root folder, you need to modify the Apache configuration file again and change the DocumentRoot directive back to the original path. After saving the changes, restart the Apache service using sudo service apache2 restart for the changes to take effect.

Can I use a different web server instead of Apache?

Yes, you can use a different web server instead of Apache. The process of changing the document root folder may vary depending on the web server you choose. However, the general concept remains the same – you need to modify the configuration file of the web server and specify the new document root path.

Leave a Comment

Your email address will not be published. Required fields are marked *