How to manually migrate WordPress to a new server

Migrating WordPress to a new server is a quick and easy task, but it requires some special care. In this tutorial we will see how to migrate WordPress to a new server, using either the same domain name or using a new domain name on the new server.

For this tutorial to work, you need to have SSH access to both virtual servers.

1. Old Server

Log in to your old server and make a backup of the WordPress database using the mysqldump command:

mysqldump -u YourWPUserName -pYourWPPassword YourWPDatabaseName > dumpfile.sql

If you are moving your WordPress website to a new domain name your need to search and replace all references of the old domain name with your new domain name:

sed -i 's/oldDomain.com/newDomain.com/g' dumpfile.sql

Create a new backup archive of the WordPress installation to a directory on your server:

tar -cvzf /opt/WPSiteName.tar.gz /path/to/wordpress

Transfer the tar file to your new server. It is easiest to do this using rsync, for example:

rsync -av -e ssh /opt/WPSiteName.tar.gz [email protected]:/opt/

If you are using the same domain name on your new virtual server you should edit the coresponding DNS zone file and point the A record of your domain name to your new server IP address.

2. New Server

Create a new Apache virtual host:

<VirtualHost *:80>
ServerName your-domain.com
ServerAlias www.your-domain.com
DocumentRoot /home/yourDomain/public_html
DirectoryIndex index.php
<Directory /home/yourDomain/public_html/>
Options FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Or, create a new server block if you are using nginx, for example:

server {
server_name your-domain.com;

access_log /var/log/nginx/your-domain-access.log;
error_log /var/log/nginx/your-domain-error.log;
root /home/yourDomain/public_html/;

location / {
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Do not forget to restart the web server for the changes to take effect.

Create a new MySQL database.

mysql -u root -p
 CREATE DATABASE IF NOT EXISTS `YourWPDatabaseName` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
 GRANT ALL PRIVILEGES ON YourWPDatabaseName.* TO YourWPUserName@localhost IDENTIFIED BY 'pYourWPPassword';
 FLUSH PRIVILEGES;

Unpack your WordPress installation to a directory on the new server.

cd /opt && tar -xzf WPSiteName.tar.gz

Copy your WordPress installation to your server’s root directory:

rsync -av /opt/oldWPInstall/ /home/yourDomain/public_html/

Import the MySQL database from the dump file using the following command:

mysql -u YourWPUserName -pYourWPPassword YourWPDatabaseName < /home/yourDomain/public_html/dumpfile.sql

Change file permissions:

chown -R www-data: /home/yourDomain/public_html/  # Debian based distros
 chown -R apache: /home/yourDomain/public_html/  # RedHat based distros
 chown -R yourUsername: /home/yourDomain/public_html/  # cPanel or Directadmin Virtual Server

That’s it, the WordPress migration is complete. Once the DNS changes propagate throughout the Internet, open your browser and navigate to your WordPress website.

This tutorial should work for any Linux distribution.

Of course, you don’t have to do any of this if you use one of our Linux Cloud VPS Hosting services, in which case you can simply ask our expert Linux admins to migrate WordPress to a new server for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

2 thoughts on “How to manually migrate WordPress to a new server”

  1. Thank you for this guide, just used it in early 2020.

    A few things that tripped me up:
    rsync -av /opt/oldWPInstall/ /home/yourDomain/public_html/
    places the contents of /opt/oldWPInstall/ inside /home/yourDomain/public_html/ , when I actually wanted to end up with /home/yourDomain/public_html/oldWPInstall/

    Hard to do the migration seamlessly because both the old and new need to have HTTPS. So I managed, with some down time.

    Reply
    • You can use the command: rsync -av /opt/oldWPInstall/ /home/yourDomain/public_html/oldWPInstall/ in order to end up with /home/yourDomain/public_html/oldWPInstall/

      Reply

Leave a Comment