Our goal here is to move apache web root to a new location on Ubuntu 16.04. Before getting into that let's take a look at the basics.
What is Apache web root?
The answer to that would be /var/www/html.
It further makes way for new questions like what is /var/www/html?
where is directory /var/www/html/? and how to access /var/www/html?
Aren't these few questions you would like the answers to?
The answer to the first one is simple enough.
'var/www/html' Ubuntu is the default web root location,
where the server stores all of its documents.
You can find it on the root file system like any other files on the operating system.
To gain access to /var/www/html you can use the following command.
sudo chmod -R 777 /var/www/html
Although you can gain access to the root directory with the above command, it is recommended that you avoid messing with root access.
Because even the smallest change might prove disruptive.
Before proceeding let's clear something up. Some of you might ask questions like what is a document root apache?
And what's the difference between a document root and a web root?
Don't worry. The answer to that is not a difficult one.
In case of web development, document root and webroot mean one and the same thing.
They are two different terms used for the same purpose. So if you want to change document root apache2 ubuntu, what you basically need to do is follow the steps in this article to move apache web root to a new location on Ubuntu 16.04.
Things that you will need before you begin the process of moving the web root to a new location.
An Ubuntu 16.04 Server. The Ubuntu server should have user that has access to sudo privileges. Here's how you can create a user with sudo privileges.
An installed Apache2 Web server. Follow the steps in this How To Install LAMP stack on Ubuntu guide 16.04 (Easy Guide) to install apache2 web server.
A new location where you want to move the apache web root. It could either be a new mount location or a new folder location.
Steps to move apache web root to a new location on Ubuntu 16.04:
Step 1: Get the apache web root location.
Your apache root directory ubuntu will most probably be /var/www/html. You can search the location of root using the following command.
The -R is used to ensure that both DocumentRoot and filename are being displayed.
grep -R "Document Root" /etc/apache2/sites-enabled
The output of this command will give you apache root directory linux i.e. it will give you apache web root directory location. as shown below. In this case it is /var/www/html.
/etc/apache2/sites-enabled/000-default.conf: DocumentRoot /var/www/html
Step 2: To move/copy data from old location to a new location.
Now that we have the information regarding the current location of web root.
We can copy the files from the current web root to a new location.
In our case the old location is /var/www/html and the new location is /home/ubuntu/Images.
sudo rsync -av /var/www/html /home/ubuntu/Images
The above command will copy the files and directories from /var/www/html to /home/ubuntu/Images.
Step 3: To update the configuration files.
Once you have moved the files to the new location, next step is to update the configuration.
To update configuration we have to change all instances of /var/www/html to /home/ubuntu/Images/html.
Let's start by updating the 000-default.conf file.
sudo gedit /etc/apache2/sites-enabled/000-default.conf
Look for DocumentRoot and replace /var/www/html by home/ubuntu/Images/html.
Next, you have to update the apache2.conf file.
Go to the bottom of the file. You will find little code similar to this.
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
Look for the directory block and replace /var/www/ with the new location, where you want to move your apache web root. Here it will be /home/ubuntu/Images/html/.
After replacing the old location/path with the new location/path, your code should look somewhat like this.
In place of /home/ubuntu/Images/html you will have to input a new location where you want to move your apache root.
<Directory /home/ubuntu/Images/html/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
Remember to replace the old path with the new one in all the files. If you miss replacing even one root location it might affect your outcome.
Step 4: Testing the configuration.
After you have made the above changes to the configuration, you can run a quick check to ensure that the configuration is okay.
You can make use of the following command to test the configuration file.
If the configuration has no error, the output of this command will be "Syntax Ok".
But if there is an error, detailed information of that Syntax error will be given in the output.
Step 5: Ownership and Permission.
Next, you will need to change the ownership and permission.
To change the ownership just use the below command.
The user:group pair (www-data:www-data) is given the ownership.
chown www-data:www-data -R /home/ubuntu/Images
The permission is set to 755 mode. Where in, the user has the permission to read, write and execute.
But the group members and other users can only read and execute it. They cannot write.
chmod 755 -R /home/ubuntu/Images
Step 6: Restart the web server (Apache web server).
To ensure that all the changes have been applied you will have to restart the server. Restart your server using the following code.
sudo systemctl restart apache2
Follow all the steps and you will have no trouble moving your apache web root to a new location, let it be a folder or a new mount.
So, that is how you move apache web root to a new location.
Moving apache root to a new location makes things easier for the user and has plenty of advantages.
Now you can reap the benefits that come with it. One of its advantages is being able to host multiple sites from a single server.
Another one is getting to use network block storage which makes scaling a website a lot easier.