DEV Community

kunzhu0710
kunzhu0710

Posted on

Migrate Apache data on Tencent Cloud CVM

Introduction

On Debian 8, the Apache2 web server default file is /var/www/html in . This directory is located on the root file system along with the operating system. You might want to move the document to another directory, such as mounting the filesystem separately.
In this tutorial, you will migrate Apache default files to a new location by moving files and changing Apache's configuration files.

Prepare

To complete this tutorial, you will need:

  • A non-root Debian 8 server that can use the sudo command, I recommend using Tencent Cloud's configured server . Or use Tencent Cloud Developer Lab for*free* to experiment.
  • An Apache2 web server. For the installation tutorial, you can refer to here.
  • Move the new location of the directory, here I use Tencent Cloud's file storage service .

In this tutorial, we will use a directory, which points to a file storage /mnt/volume-nyc1-01 service mounted to the server .

Step 1 - Copy the files to the new location

When installing a fresh Apache, the document root is located at /var/www/html . If you have a lot of websites on your server, the documentation directory will be different, and it is recommended that you search using the scheme below.
You can grep search the directory for Apache configuration documentation using the /etc/apache2/sites-enabled following command:
grep -R "DocumentRoot" /etc/apache2/sites-enabled copy
-R The flag ensures that grep the contents of the DocumentRoot are output in it.
The results will be similar to the following, if you install the site multiple results names and numbers may be different:
sites-enabled/000-default.conf DocumentRoot /var/www/html copy
Now that you have confirmed the location of the document root, rsync copy the files to the new location using the command.
First, install with the following command rsync :
sudo apt-get install rsync copy
Then execute this command to copy the files:
sudo rsync -av /var/www/html /mnt/volume-nyc1-01 copy
Using -a flags preserves permissions and other directory attributes, while -v providing verbose output so you can track progress.

Note: Make sure there are no trailing slashes on the directory. When there is a slash, rsync dumps the contents of the directory to the mount point instead of transferring it into the html directory.

These files will be synced to the new location, but now that the Apache configuration files are still using the old directory, we need to configure the new files.

Step 2 - Update the configuration file

By default Apache comes with two virtual host files, 000-default.conf and default-ssl.conf .
First edit the 000-default.conf file:
sudo nano /etc/apache2/sites-enabled/000-default.conf copy
Find DocumentRoot the line starting with and update it with the new position:

<VirtualHost *:80>
 ...
        ServerAdmin webmaster@localhost
        DocumentRoot /mnt/volume-nyc1-01/html
Enter fullscreen mode Exit fullscreen mode

copy
Next, look up Directory to see if it points to the original path, we need to update it to the new path. In a fresh install of Apache, there is no entry in the Directory default site. Add the following code to the configuration file so that Apache can use the new file from the new location:

 ...
     ServerAdmin webmaster@localhost
     DocumentRoot /mnt/volume-nyc1-01/html
​
     <Directory />
         Options FollowSymLinks
         AllowOverride None
     </Directory>
     <Directory /mnt/volume-nyc1-01/html/>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Require all granted
    </Directory>
 ...
Enter fullscreen mode Exit fullscreen mode

copy
The first Directory block sets some restrictive default permissions, and the second block configures the options for the new web /mnt/volume-nyc1-01/html/ in .

Note: You should look for the other location in the config file where the original path shows and change it to the new location. In addition to DocumentRoot and Directory settings, you can also find things like aliases and overrides that need to be updated. Wherever you grep see the path to the original document root in the output, you will need to update the path to reflect the new location.

After making the necessary changes, save the file.
Next, we turn to SSL configuration. On a fresh install, SSL has not been configured, if you have never configured SSL, ignore the following.

Note: If SSL is not enabled, the ssl-default.conf files are only in the /etc/apache2/sites-available directory. If you have SSL enabled, the server will sites-available link to the /etc/apache2/sites-enabled directory from the file. In this case, you can edit the file from either directory.

sudo nano /etc/apache2/sites-available/ssl-default.conf copy
Make the same changes as before by making changes DocumentRoot and making sure to configure them correctly: Directory

 ...
<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
     ServerAdmin webmaster@localhost
     DocumentRoot /mnt/volume-nyc1-01
​
     <Directory />
         Options FollowSymLinks
         AllowOverride None
     </Directory>
     <Directory /mnt/volume-nyc1-01/html/>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Require all granted
    </Directory>
 ...
Enter fullscreen mode Exit fullscreen mode

copy
After making configuration changes, make sure the syntax is correct with the following command:
sudo apachectl configtest copy
You will see results like this:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, 
using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Enter fullscreen mode Exit fullscreen mode

copy
In a default installation, you will see the preceding message, which you can safely ignore. As long as you see Syntax OK, restart the web server.
Restart Apache with the following command:
sudo systemctl reload apache2 copy
After the server restarts, go to the website you changed, and if all is well, don't forget to delete the original copy of the data.

in conclusion

In this tutorial, you learn to change the Apache document root to a new location. This can help you with basic web server management, and it also allows you to take advantage of other storage devices, such as Tencent Cloud File Storage Service , an important step in scaling your website as your needs change.

Top comments (0)