[WordPress] Migration to a new hosting

[

Migrating a WordPress site to a new server can be a daunting task, but with careful planning and the right steps, it can be a smooth and successful process. As someone who recently navigated this process, I understand the challenges that come with it. In this guide, I will walk you through the essential aspects of migrating your WordPress site, covering everything from exporting and importing the database to updating settings and handling .htaccess files. I hope my experience can help you to migrate your WordPress sites successfully, ensuring a seamless transition and minimizing any potential hiccups along the way.

1. Backup Your WordPress Site

Before you embark on the migration process, it’s crucial to create a backup of your entire WordPress site. This includes both the files and the database. There are several methods to perform a backup.

Using Backup Plugins

You can consider using a reliable backup plugin to simplify the backup process. One popular plugin is UpdraftPlus. It is very useful when you don’t have access to database administration panel or command line. To prepare a backup follow these simple steps:

  1. Install and activate the UpdraftPlus plugin.
  2. Navigate to Settings > UpdraftPlus Backups.
  3. Click on the “Backup Now” button to create a full backup of your site.

Manual Backup

You can prepare a backup of migrated WordPress website manually. At the first eyesight it may look a bit difficult, however it’s not so hard actually. Just follow my instructions and you can do it successfully with no headaches.

Backup of WordPress website is taken into two parts, related to database and files. Let’s start from database backup.

Database backup

You can prepare a database backup using different tools. It depends on what is available for you and what you like to use more.

Using Database Administration Panels

If you have access to a database administration panel like phpMyAdmin, creating a database backup is a straightforward process:

  1. Access your server’s phpMyAdmin panel.
  2. Select your WordPress database.
  3. Click on the “Export” tab and choose the export method (Quick or Custom).
  4. Export your database to a .sql file.
Using Command Line

This option is recommended rather for more advanced users. If you have access to the database from the command line, you can use commands like mysqldump (doc) or pg_dump (doc) to export a database dump file.

MySQL

mysqldump -u username -p dbname > backup.sql

PostgreSQL

pg_dump -U username -d dbname > backup.sql

Remember to replace username with your PostgreSQL database username and dbname with your PostgreSQL database name.

Using these commands, you can create a comprehensive backup of your database, ensuring that you have a snapshot of your data that can be restored in case of any issues during the migration process.

File backup

Backup of WordPress site is not only related to database. You need to copy content of wp-content directory where are located installed plugins, themes and other stuff.

On the old server, locate the wp-content directory in your WordPress installation. Copy the entire wp-content directory, preserving its structure and contents. You can do it in two ways, depending on what option is better for you. At the end of doing the backup, confirm that the whole directory is really copied, including all hidden files.

Additionally, you should copy wp-config.php file. However this file contains sensitive data and backup should be removed just after successfull WordPress migration.

Using FTP clients

You can use FTP clients like Filezilla to connect to a server where your WordPress site is hosted and then find wp-content directory. Details of doing backup depends on a concrete FTP client.

Using SSH Console with Bash Commands

Having access to SSH you can do files backup using scp command. Access the terminal or command prompt on your local machine and write this command:

scp -r /path/to/local/wp-content username@oldserver:/path/to/remote/wp-content

Example:

scp -r ~/Desktop/wp-content user@oldserver:/var/www/html/

In the same way you can copy wp-config.php file which is located in the WordPress installation directory.

2. Create a new WordPress instance

Now that you’ve backed up your WordPress site and prepared the database for migration, it’s time to set up a new WordPress instance on your destination server. While we’ll delve into the manual installation process, it’s worth noting that some hosting providers offer the convenience of automatic WordPress installation, including the creation of the necessary database. This streamlined approach can be a preferred option for those seeking a hassle-free setup experience.

1. Download WordPress

Visit the official WordPress website and download the latest version of WordPress.

2. Upload WordPress Files
  • Connect to your new server using FTP or a file manager provided by your hosting provider.
  • Upload the WordPress files to the desired directory on your new server.
3. Create a Database
  • Access your hosting control panel and create a new MySQL or PostgreSQL database.
  • Note down the database name, username, and password.
4. Configure wp-config.php
  • Rename the wp-config-sample.php file to wp-config.php.
  • Open wp-config.php and enter your database details:

MySQL

define('DB_NAME', 'your_db_name');
define('DB_USER', 'your_db_user');
define('DB_PASSWORD', 'your_db_password');

Remember to replace username with your MySQL database username and dbname with your MySQL database name.

PostgreSQL

define('DB_NAME', 'your_db_name');
define('DB_USER', 'your_db_user');
define('DB_PASSWORD', 'your_db_password');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

Replace your_db_name, your_db_user, and your_db_password with the details of the database you created.

5. Run the WordPress Installer
  • Open your web browser and navigate to the URL where you uploaded WordPress.
  • Follow the on-screen instructions to complete the installation.
  • If you have a possibility to set the same database prefix as was set in the old instance, it’s highly recommended to do it as it reduces a lot of potential issues with different prefixes while migration.
6. Migrate .htaccess file
  • Copy the content of the old site’s .htaccess file.
  • Replace the .htaccess file on the new server with this content.

Below is an example of .htaccess file for wordpress. You can use it if you don’t have any or you lost the old one. .htaccess file should be located in the main directory where WordPress is installed.

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

After all these steps WordPress instance should be installed and accessible from the new server.

3. Import files

You should already have done backup of wp-content directory from original WordPress website. Now it’s the time to migrate it to a new server.

1. Clean existing files

Locate wp-content directory on the new server and remove all files existing there. Make sure you don’t leave any hidden files.

2. Migrate WP-CONTENT files from old server

Take content of wp-content from old server and move it to the new one. You can use both FTP clients or SSH commands, depending on what you can do or what you preffer. Using scp command you can migrate wp-content directory in this way:

scp -r username@newserver:/path/to/remote/wp-content /path/to/local/wp-content

Example:

scp -r user@newserver:/var/www/html/ ~/Desktop/wp-content
3. Adjust wp-config.php file on the new server

The wp-config.php file is a crucial configuration file for a WordPress installation. It contains various settings and configurations that WordPress uses to connect to the database, define authentication keys, set file permissions, and configure other essential aspects of the site.

You can find there sections like: database connection configuration, authentication keys and salts, database table prefix, debugging and error handling, file permissions and ownership, multisite and localization configuration.

In this time you can check in the new wp-config.php file if database connection is configured correctly. In the next chapter I described adjusting tables prefix which may require you to look at this file too. Now, you should find a section with authentication keys and salts in both old and new wp-config.php file. It should look like this:

define('AUTH_KEY', 'your_random_key');
define('SECURE_AUTH_KEY', 'your_random_key');
// ... (similar definitions for other keys and salts)

You must replace this section in the new file by content from the old one to migrate WordPress users successfully. If you skip this step, any user will not be able to log in to your new website using old password.

4. Import the Database

Next step of WordPress migration is to import data from database. It’s a very crucial step as it may be broken very easily. Please do it carefully.

Before doing any change in the dump file, please create a backup of it. In a case you did something wrong, you have still dump file and you don’t need to export it again.

1. Change Database name

.sql dump file contains database name in few places. Mostly it’s on the top of the file, eg.:

-- MySQL dump 10.13  Distrib 8.0.35, for Linux (x86_64)
--
-- Host: localhost    Database: genodon111

ChatGPT

However, sometimes the database name can even be included in a value of a table. Check the file carefully, keeping in mind that some values may not be related to the database name.

1. Adjust TABLES PREFIX

Follow instruction from this step, if WordPress on the new instance is already configured with different tables prefix. If you used the built-in installer of WordPress, you were able to choose the prefix on your own. However, some installers offered by hosting providers don’t allow you to set the prefix manually. Default prefix is wp_.

You can find the prefix configuration in wp-config.php file. Look for a line looking like this:

$table_prefix = ‘wp_’;

You have two options. You can change the prefix in wp-config.php file or you can adjust it in the dump file before importing it. First option doesn’t need to be described more. In the next two subchapters I gave you detailed information about the second option.

Prefix in the names

Example part of SQL dump file with wp123_ used as a prefix:

-- ...

--
-- Table structure for table `wp123_usermeta`
--

DROP TABLE IF EXISTS `wp123_usermeta`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `wp123_usermeta` (
  `umeta_id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_520_ci,
  PRIMARY KEY (`umeta_id`),
  KEY `user_id` (`user_id`),
  KEY `meta_key` (`meta_key`(191))
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

-- ...

After changing wp123_ prefix to eg. wp987_, the same example will look like this:

-- ...

--
-- Table structure for table `wp987_usermeta`
--

DROP TABLE IF EXISTS `wp987_usermeta`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `wp987_usermeta` (
  `umeta_id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_520_ci,
  PRIMARY KEY (`umeta_id`),
  KEY `user_id` (`user_id`),
  KEY `meta_key` (`meta_key`(191))
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

-- ...

If you have non-standard prefix like wp123_ it is easy to find all occurencies in the dump file. However, for standard wp_ prefix it is not trivial, if you want to change it to a non-standard value. You can’t just change all occurences of wp_ string in the file. In many places wp_ is just constant expression, independent from used prefix. You may assume that prefix is used only at start of every table name and change all these places. Some additional specific places to change I covered in the next subchapter.

If you are doing it in a code editor, you can follow me:

I wanted to change default prefix wp_ to wp213_. I tried to find and replace the prefixes using backticks at the start, both for finding and replacing values. Replacing values one by one makes the process longer but gives you full control of what is actually being changed.

Prefix in values

Prefix is used in few places in some tables. You should locate them in the dump file and change. You can also import data to the database first and then use the SQL commands below to change prefix in tables’ values.

update NEWPREFIX_usermeta set meta_key = 'NEWPREFIX_capabilities' where meta_key = 'OLDPREFIX_capabilities';
update NEWPREFIX_usermeta set meta_key = 'NEWPREFIX_user_level' where meta_key = 'OLDPREFIX_user_level';
update NEWPREFIX_usermeta set meta_key = 'NEWPREFIX_autosave_draft_ids' where meta_key = 'OLDPREFIX_autosave_draft_ids';
update NEWPREFIX_options set option_name = 'NEWPREFIX_user_roles' where option_name = 'OLDPREFIX_user_roles';

where OLDPREFIX_ is your old prefix and NEWPREFIX_ is the new one.

Remember just, that I included here only places common for all WordPress instances. Sometimes you may use a plugin which also uses db prefix in its configuration. This is a really bad case, especially if old prefix was wp_, because it’s not always easy to find out if the value is related to real prefix or it is just related to WordPress itself. However, you may take a look at values in tables created by your plugins and check them. After migration process, you should do a detailed tests of the whole migrated website.

2. Import data to database

When db prefix is the same in the wp-config.php file and in database dump, you can import data to the new database.

Please take a look at used collation and make sure that it’s supported by new database. In a case that it’s not, just change it to a supported one.

Using Database Administration Panels

If you have access to a database administration panel like phpMyAdmin, you can follow just these steps:

  1. Access phpMyAdmin on your new hosting.
  2. Create a new database if it’s not yet created.
  3. Select the database and click “Import.”
  4. Upload the .sql file you exported earlier from your old hosting.
Using Command Line

This option is recommended rather for more advanced users. If you have access to the database from the command line, you can use commands like mysql or pgsql to export a database dump file. In the examples below I assumed that you run the command in the same directory where your db dump file is located and its name is backup.sql. You can adjust the path to your concrete case.

MySQL

mysql -u username -p dbname < backup.sql

Remember to replace username with your MySQL database username and dbname with your MySQL database name.

PostgreSQL

pgsql -U username -d dbname < backup.sql

Remember to replace username with your PostgreSQL database username and dbname with your PostgreSQL database name.

By following these steps, you can import your WordPress database using the command line.

When your database is imported to the new hosting and you have the need to do it, you can adjust db prefix in values as described in the previous chapter.

5. Verification and Testing

This time your WordPress site should be migrated and ready for testing.

1. Login to the Admin Panel

After migrating your WordPress site, the first step is to log in to the admin panel. Navigate to the admin login page by appending /wp-admin to your site’s URL (e.g., http://yourdomain.com/wp-admin). This is standard configuration. If you had different location configured on the old wwebsite, use the same location for migrated website as all configuration is moved.

2. Refresh Permalinks Settings

In the WordPress admin, go to Settings > Permalinks. Without making any changes, click the “Save Changes” button. This step helps refresh the permalink structure to ensure that links are generated with the correct URL format on the new server. While this step is not strictly required during the migration process, it’s a good practice to avoid potential issues with broken links or incorrect URL structures.

3. Verify Themes and Plugins

Navigate to Appearance > Themes to ensure that your active theme is listed. Check Plugins to confirm that all necessary plugins are present and activated. If your theme or plugins were custom-built or are not available in the WordPress repository, make sure to upload them to the new server. You may need to adjust eg. SMTP configuration for email sending.

Some themes and plugins may have settings that need to be configured. Check and update settings as needed. If your theme or plugins store absolute URLs or file paths, make sure to update them to match the new server.

4. Test Basic Website Functionality

Navigate to your site’s homepage and verify that it loads correctly. Test navigation through different pages and posts. Check if images and media files are displayed correctly. If your site has contact forms or user registration, submit forms and ensure that emails are sent correctly. If your site has an online store, perform test transactions to ensure that the checkout process is functioning correctly. If your site has custom functionalities, such as custom post types or taxonomies, test them thoroughly. Check any custom scripts or features that were implemented.

Ensure that your website is responsive on different devices. Verify that your theme adapts appropriately to various screen sizes. Test your site in different browsers to ensure compatibility.

If you use SEO plugins, check and update settings if necessary. Ensure that meta tags, titles, and descriptions are displaying correctly. You may use tools like Google PageSpeed Insights or GTmetrix to check page load times and compare current results with the ones obtained for the old instance.

Check server error logs for any issues related to the migration. Address any error messages or warnings.

After confirming that your site is functioning correctly, make a backup of the new instance to ensure that you have a recent snapshot in case of any future issues.

Conclusion

Migrating a WordPress site involves careful planning and execution. Following these steps should help you successfully move your site from the old hosting/VPS to a new one. Always keep backups and test your site thoroughly after migration to ensure everything is working as expected.

About the author

tomasz.bajorek

Add Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

tomasz.bajorek

Get in touch

Please obserce my profil in social media to get in touch with me and get fresh information about new posts here.