How To Migrate WordPress Manually To New Host

sohail
sohail

Table of Contents

Any website migration is regarded as one of the most difficult operations. Fortunately, WordPress provides a plethora of backup plugins to help and automate the entire backup and migration process. However, WordPress is constructed in such a way that manually migrate WordPress could not be simpler.

In this tutorial, I’ll show you how to migrate a WordPress site manually to a new host or server. This is especially useful if the site is hosted on a server with insufficient resources to enable the backup plugin. Backup plugins typically demand a minimal amount of resources to execute site backup and migration to another server. I’ve prepared a list of backup plugins for WordPress. So, if your server is healthy and has sufficient resources, I recommend that you install a backup plugin for WordPress migration.

Let’s start the process.

Prerequisites –

Estimated reading time: 8 minutes

  • SSH Access to both servers

Backup WordPress

Each WordPress site is made up of three major components: WordPress core files, User files (i.e. the wp-content directory), and the WordPress database. Unless the version of WordPress is modified, the core files do not change. wp-content and the database, on the other hand, change on a daily or even hourly basis, depending on website traffic. If you create new content on a regular basis, new images, videos, and text are uploaded every day, increasing the number of files on the server and the size of the database.

The first step is to compress wp-content in order to reduce its size and transfer it quickly to the new server. Second, export the WordPress database, compress it with the user files, and prepare it for migration.

wp-content

wp-content is a subdirectory of the WordPress directory. It stores all of the data that the user has uploaded, such as themes, plugins, attachments, photos, videos, and so on. Unless the site administrator configures the CMS to do so, most WordPress sites do not upload user content to any other directory. If you’re using a heavily customised theme, make a backup of the directories that contain data relevant to the structure of your custom website.

Many backup plugins save backup files somewhere other than wp-content. So, if you wish to migrate stored backup files to the new server, include them in the archive as well; alternatively, download them and save them somewhere secure. Adding stored backup files may significantly increase the size of the complete backup, so please download them locally if possible.

.htaccess

The .htaccess file is a configuration file that allows users to configure distinct settings for each directory. WordPress hosts multiple .htaccess files, the main one of which is located in the WordPress root.

Backup the .htaccess file because it contains useful configuration settings set by installed plugins such as firewall and cache plugins.

wp-config.php

wp-config.php is a WordPress configuration file that contains the most critical information for WordPress to work. The wp-config.php file provides database connection information. The wp-config.php file can also overwrite PHP limits established by the server, among other things. If you are migrating your site to a new host, make sure to copy wp-config.php along with the other files to the new server.

WordPress Database

When creating a new site or transferring an old one, ensure the database is appropriately configured with the site. As previously stated, the wp-config.php file contains database information such as the database name, database user (with full database access), and the user password.

To effectively migrate WordPress, we must export the entire database from the old server and prepare it for transfer to the new host.

Migrate WordPress Manually to new host

For the purposes of this demonstration, I’ll suppose our old host server IP is 192.168.29.5 and our new host server IP is 192.168.57.8.

First, let’s ssh into our old server –

ssh root@192.168.29.5

If your server’s ssh port is other than 22, use -p option followed by the port number.

ssh -p port_number root@192.168.29.5

Export database

The first step is to export the database. If your website receives a high volume of traffic, you can either put it on maintenance or divert visitors to the backup server. WordPress constantly writes data to the database, and larger websites may experience slowdowns while the database export process is in progress.

Once ready, use the following command to dump all database locally –

mkdir $HOME/site-backup
cd $HOME/site-backup
mysqldump -u root -p wordpress_database > wp_database.sql

Hit enter and enter the MySQL root password. Now sit back and relax. Depending on the size of the database, this process may take sometime. Do not press + C during the process.

After the process completes, you will have wp_database.sql file in the working directory, i.e. site-backup.

Backup wp-content, wp-config.php, and .htaccess

It’s now time to back up user files. As previously stated, I assume your WordPress is using standard WordPress themes rather than highly customised or custom-built themes. If you utilise custom-built themes, please include the relevant directories in the following command.

Get into the WordPress root directory, and run the following command –

$ tar -czf website-content.tar.gz /path-to-wordpress/wp-content/ path-to-wordpress/wp-config.php path-to-wordpress/.htaccess

Now sit back and unwind. See how easy it is to migrate a website. You get to unwind twice!

After the files have been compressed, move the compressed file in the above-mentioned site-backup directory.

mv website-content.tar.gz $HOME/site-backup/

We have the site’s compress database, WordPress user data, and customizations in our $HOME/site-backup/ directory. Now compress the entire site-backup directory so that it can be easily transferred to the new server.

tar -zcvf site-backup.tar.gz $HOME/site-backup/

You now have a full backup of your WordPress site. You may either download this backup locally and move it to the new server, or you can use rsync to transfer it directly to the new server.

Let’s download this backup directly on the new server.

SSH into the new server –

ssh root@192.168.57.8

Use rsync to download the backup file –

rsync root@192.168.29.5:/home/site-backup.tar.gz .

The dot at the end of the above command is the destination directory, i.e. the current working directory.

Enter the root password and wait until download completes.

Install WordPress on New host

Now that we have our backup data on the new server, we can setup a new WordPress site and import all our data on that site.

Create database for new site

Login to the mysql command-line and use the following commands to create a new database, new user and grant new user all privileges to the WordPress database.

sudo mysql

Create new database

create database wordpress;

Create new database user

create user 'username'@'localhost' identified by 'password';

Grant database access to new user

grant all privileges on wordpress.* to 'username'@'localhost';
flush privileges;

Import database

Now that our database has been created, we can export the downloaded database.

Untar or uncompress backup file –

tar -xvf /home/site-backup.tar.gz

It’ll export two more files, wp_database.sql and website-content.tar.gz.

Import database to wordpress with the following command –

mysql -u root -p wordpress < /home/site-backup/wp_database.sql

Enter the root password and relax. Again.

Install WordPress

I assume that you’re using Apache web server. So the default web root for Apache server is /var/www/html. cd into the root directory and delete index.html.

$ cd /var/www/html
$ rm index.html
Download WordPress
wget -O /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
sudo tar -xvf /tmp/wordpress.tar.gz -C /tmp/
sudo mv /tmp/wordpress/* /var/www/html

Import WordPress backup to new site

tar -xvf /home/site-backup/website-content.tar.gz

The above command will uncompress site’s wp-content, wp-config.php, and .htaccess. We can use rsync to move wp-content/ to the new WordPress installation.

$ rsync -avu /home/site-backup/wp-content/ /var/www/html/wp-content/
$ cp /home/site-backup/wp-config.php /home/site-backup/.htaccess /var/www/html/

Edit wp-config.php

And one of our last steps is to connect our site with the newly created database. Remember we have already imported our site’s database so it’s ready. There is no need to run WordPress installation.

$ nano /var/www/html/wp-config.php

Now replace old database name, database username, and password with the current server’s database we created above, i.e. wordpress.

Fix WordPress file permissions

Never forget to set correct file permissions. Without it, WordPress may not function properly, and you may expose your data if incorrect permissions are set.

$ chown -R www-data:www-data /var/www/html/
$ find /var/www/html/ -type d -exec chmod 755 {} \;
$ find /var/www/html/ -type f -exec chmod 650 {} \;

Change ‘siteurl’ and ‘home’ in database

If you are moving your site without changing the domain name, you can skip this step. If you change the domain name, make sure to update the database with the new domain name.

We need to update to columns in wp_options table. If you have phpMyAdmin, then you can open phpMyAdmin > wordpress > wp_options > siteurl.

Change column ‘siteurl‘ value to ‘https://www.newdomain.com’. Also, change column ‘home‘ value to ‘https://www.newdomain.com’.

Update siteurl and home using mysql command-line –

sudo mysql
use wordpress;
update wp_options set option_value='https://www.newdomain.com' where option_name='siteurl'
update wp_options set option_value='https://www.newdomain.com' where option_name='home'

At last, point your domain name to the new server. If your web server is correctly setup, you should see your old website on new host.

Final Words

The manual procedure appears to be quite complicated, but trust me when I say that once you get used to it, it will save you money on a premium backup plugin. Using the steps outlined above, we can even build a script and set up a cron job to automatically backup our website. All of this without the use of a plugin.

Finally, if you had any difficulties or errors while following this post, please let us know in the comments section below. Alternatively, you can join our Discord Server for faster responses.

Linux Tutorialshow-to