Introduction

In this article, we will migrate the WordPress website from the source domain to the subdomain.

Prerequisite

  1. Server with credentials are needed
  2. Root user or sudo access for the server is essential

Procedure

Step 1: Login into the source domain server and find the document root on the domain configuration file(depends on the webserver installed on server).

$ sudo cd /etc/<webserver>/sites-available/domain.conf

Step 2: Go to the document root path and take the backup

$ sudo tar -zcvf wordpress.tar.gz /home/domain/public_html

Step 3: Now take backup of the database using mysqldump command.

$ sudo mysqldump databasename > databasename.sql

Step 4: Copy both the backup files to the destination server using SCP command.

$ sudo scp /root/wordpress.tar.gz user@ip:/root

$ scp /root/databasename.sql user@ip:/root

Step 5: Login to destination server

Create the subdomain configuration file for virtualhost entry using the following command

$ sudo vim subdomain.com.conf

Add the below contents accordingly with respect to your subdomain name,

<VirtualHost *:80>
ServerName sub.domain.com
ServerAlias www.sub.domain.com
DocumentRoot /home/subdomain/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 6: Verify the configuration file syntax using the below command

$ sudo apachectl configtest

If the webserver is ngnix then run the following command.

$ sudo systemctl config nginx

If there are no syntax errors, then restart the webserver

$ sudo systemctl restart <webserver>

Step 7: Lets create the database and the user

Enter into the MySQL database

$ sudo mysql

Run the following queries,

> CREATE DATABASE <db_name>;
> GRANT ALL PRIVILEGES ON db_name.* TO '<db_user>'@'localhost' IDENTIFIED BY 'xxxxxx';
> FLUSH PRIVILEGES;

Step 8: Restore backup files to the subdomain document root

$ sudo cd /root

$ sudo tar -xvzf wordpress.tar.gz /home/subdomain/public_html/

Now restore the mysql database

$ sudo mysql db_name < databasename.sql

Step 9: Modify the wordpress configuration file with the new database details

Enter into the document root using the below command,

$ sudo cd /home/subdomain/public_html/

And run the following command edit wordpress configuration file .

$ sudo vim wp-config.php

Modify the file with a your database name and user

/** The name of the database for WordPress */
define('DB_NAME', 'db_name');

/** MySQL database username */
define('DB_USER', 'db_user

/** MySQL database password */
define('DB_PASSWORD', 'xxxxxx');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Step 10: Change the siteurls in the database from http://domain.com to http://sub.domain.com.

Run the queries to change the siteurls,

> UPDATE wp_options SET option_value = replace(option_value, 'http://domain.com', 'http://sub.domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

> UPDATE wp_posts SET guid = replace(guid, 'http://domain.com', 'http://sub.domain.com');

> UPDATE wp_posts SET post_content = replace(post_content, 'http://domain.com', 'http://sub.domain.com');

> UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://domain.com', 'http://sub.domain.com');

Step 11: Make sure you have the .htaccess file placed under the document root

If there is no htaccess file in document root, add a default configuration.

$ sudo vim .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Now you can access the website http://sub.domain.com on browser.

Conclusion:

The migration from the source domain to the subdomain has successfully done by following all these steps.

Leave a Reply