Migrating a WordPress site to a new domain can seem daunting, but with the right steps, it can be smooth and error-free. This guide will walk you through the process from start to finish.
Step 1: Set Up Apache Virtual Hosts
First, you need to configure Apache to serve your site from the new domain. Create virtual host files for both HTTP (port 80) and HTTPS (port 443).
Creating Virtual Host Files
sudo touch /etc/apache2/sites-available/newsite.com.conf
sudo touch /etc/apache2/sites-available/newsite.com-le-ssl.conf
Add the following configurations:
For HTTP (newsite.com.conf):
<VirtualHost *:80>
ServerName newsite.com
ServerAlias www.newsite.com
DocumentRoot /var/www/newsite.com
ErrorLog ${APACHE_LOG_DIR}/newsite.com/error.log
CustomLog ${APACHE_LOG_DIR}/newsite.com/access.log combined
</VirtualHost>
For HTTPS (newsite.com-le-ssl.conf), ensure you have a Let’s Encrypt certificate:
<VirtualHost *:443>
ServerName newsite.com
ServerAlias www.newsite.com
DocumentRoot /var/www/newsite.com
ErrorLog ${APACHE_LOG_DIR}/newsite.com/error.log
CustomLog ${APACHE_LOG_DIR}/newsite.com/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/newsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/newsite.com/privkey.pem
</VirtualHost>
Activate the new virtual hosts:
sudo a2ensite newsite.com.conf
sudo a2ensite newsite.com-le-ssl.conf
If you don’t have a Let’s Encrypt certificate yet, use Certbot to obtain one:
sudo certbot --apache -d newsite.com -d www.newsite.com
Step 2: Copy WordPress Files
Copy your WordPress files to the new directory:
sudo cp -a /var/www/oldsite.com/. /var/www/newsite.com/
Set the correct permissions:
sudo chown -R www-data:www-data /var/www/newsite.com
sudo chmod -R 755 /var/www/newsite.com
Create and set permissions for the new log directory:
sudo mkdir /var/log/apache2/newsite.com
sudo chown www-data:www-data /var/log/apache2/newsite.com
Verify the virtual host configurations:
sudo apache2ctl configtest
Step 3: Migrate the Database
Export the existing database:
mysqldump -u username -p old_database_name > old_database_backup.sql
Create a new MySQL user for the new website:
mysql -u root -p
CREATE USER 'new_wp_user'@'localhost' IDENTIFIED BY 'new_password';
CREATE DATABASE new_database_name;
GRANT ALL PRIVILEGES ON new_database_name.* TO 'new_wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Import the backup into the new database:
mysql -u new_wp_user -p new_database_name < old_database_backup.sql
Step 4: Update wp-config.php
Edit wp-config.php
in the new site directory, updating the MySQL settings and URLs:
define('DB_NAME', 'new_database_name');
define('DB_USER', 'new_wp_user');
define('DB_PASSWORD', 'new_password');
define('WP_HOME','http://newsite.com');
define('WP_SITEURL','http://newsite.com');
Step 5: Finalizing the Migration
Access your new domain in a browser. If the layout seems off, don’t worry. Log in to the dashboard and install the “Better Search Replace” plugin. Use it to search for oldsite.com
and replace it with newsite.com
. Make sure to uncheck “dry run” to apply the changes.
Lastly, check for any remaining references to the old site:
grep -Rl 'oldsite.com' /var/www/newsite.com/ | xargs sed -i 's/oldsite.com/newsite.com/g'
Now, your WordPress site should be fully migrated to the new domain. Remember to keep both the old and new sites operational until you’ve confirmed the migration is successful and search engines have updated their indexes.