If you design WordPress websites for a living, or perhaps as a hobby, you have likely encountered the problem where you want to replace an existing website with a new version that you have created locally.
If your website is a static website, with only HTML and CSS files, then you can easily copy these files over to your website hosting provider or your own server with minimal downtime. However, it can get more complicated if you use a WordPress website - Or any website that has a database-driven backend.
You can use plugins to import/export WordPress websites but sometimes they are unreliable. If you are anything like me, you will enjoy being able to do this manually (mostly).
In this article, I will show you the steps you need to take to export your local version of your new WordPress website. I use Laragon for local development but you could equally apply the export process to XAMPP, other local platforms or Web hosting companies.
I will then demonstrate how you can import the WordPress website into a new VirtualHost on Ubuntu Server, whilst still running your current website. Finally, we will activate your new website to make it live and available to the World.
Let’s get started on how to manually replace an existing WordPress website on Ubuntu Server.
Export Laragon MySQL/Maria Database
The easiest way to export the database is to use PHPMyAdmin as it is bundled with Laragon.
Start the Laragon application, so that your website is running locally.
Right-click on Laragon to open the menu and select ‘MySQL > PHPMyAdmin’.
Login and select your database from the list on the left side of the screen.
Click on the Export Tab.
Switch to the Custom export method.
Select Format as SQL.
Select all tables within the database.
Check Save output to file.
Uncheck Add CREATE DATABASE / USE statement as this can cause problem when importing.
Press Go and save the file to the location you will be importing from. This will be your computer, USB drive or network drive.
You can also export the database from the command line but you first have to install MySQL and point Laragon to the binary files. Then you can run this command from the command line.
mysql -u mysql_username -p database_name < exported_db.sql
Import Database into MySQL on Server
Now we will create a new empty database and import your local database, that we just exported, to its new location.
Create MySQL Database
Login to MySQL as root user.
sudo mysql -u root -p
Enter MySQL root user password.
Now, create the database where we will import to.
CREATE DATABASE new_DB_name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Confirm you have successfully created the database. You should see your new database on the list.
You can delete the database if you make an error in naming and try again.
DROP DATABASE new_DB_name;
Create and assign a MySQL User to created database
It is good practice to assign each database its own unique user, rather than using the root user. This ensures that if one user account is hacked, the hacker cannot access all your databases.
CREATE USER 'new_mysql_user'@'localhost' IDENTIFIED BY 'your_random_password';
Now give this user access rights to your database.
GRANT ALL PRIVILEGES ON new_DB_name.* TO 'new_mysql_user'@'localhost';
Back at the system command prompt, we can now import the SQL file that we exported from your local database / old web host earlier. I’m using an example of a mounted volume on Ubuntu. Use whatever location you saved your database export to.
mysql -unew_msql_user -p new_DB_name < /media/Network/WordPress_Backup/exported_db.sql
Note: There is no space between the -u and username.
Open the MySQL command prompt as root user.
sudo mysql -u root -p
Enter your root user password when prompted.
Verify the database has been populated with the imported tables.
If the import has succeeded, you should see a list of imported tables.
Go back to system command prompt by typing.
Copy New WordPress Website to VirtualHost Directory
The first step in transferring the website files is to copy the contents of the Laragon WWW folder to your backup location.
The source of these will be your Laragon install directory. For example:-
Simply copy and paste to your backup location. I’ll use the example of a network drive location.
If you are using a network drive, you need to create a directory structure on Ubuntu Server where we will map the network drive backup folder to. Otherwise, just use the paths to your USB drive or wherever you saved the files. This example uses the more complex method by mapping a network drive.
Note: It is safer to map to a specific folder rather than the root of your network drive. As this protects you from accidentally deleting other files. Or you can setup user rights on your network attached storage, if supported, to specific folders that limit the network user access to other folders.
We need to create the local directories where the mapped drive will point to.
sudo mkdir /media/network
sudo mkdir /media/network/WordPress_Backup
If you do not have Samba installed, you should do this first. Then you can map the network drive to the above directory with the following command. Use the IP address of your network attached storage and your network user name, rather than the example used here.
sudo mount -t cifs -o username=your_network_user,vers=1.0 "//192.168.1.254/WordPress_Backup" //media/network/WordPress_Backup
Then check if the network drive has mapped correctly and if the files are available in Ubuntu.
sudo ls /media/network/WordPress_Backup/your_new_website_name/
Copy Website Files to the New VirtualHost Directory
The VirtualHost will be configured later but we can copy the website files to the VirtualHost directory now.
First, create the directory structure.
sudo mkdir /var/www/your_new_website_name
sudo mkdir /var/www/your_new_website_name/public_html
Then copy all website files to the new directory.
sudo cp /media/network/WordPress_Backup/your_new_website_name/* /var/www/your_new_website_name/public_html/
Check the files have copied correctly.
sudo ls /var/www/your_new_website_name/public_html/
Change ownership of the website directory and its content to the Apache user.
sudo chown -R www-data:www-data /var/www/your_new_website_name
Update WordPress Database Connection Configuration
Your new WordPress website will try to use your old Laragon MySQL connection details, so we must change the connection details to match the database and user we created.
sudo nano /var/www/your_new_website_name/public_html/wp-config.php
Replace the old Laragon database name, user and password with your new database name, MySQL user and password.
Redirect to the New VirtualHost
Your website should already be configured as a VirtualHost in Apache to serve your current website. We want to direct the VirtualHost to the new directory path.
First, open the VirtualHost configuration file for your website.
sudo nano /etc/apache2/sites-available/your_virtualhost_domain.com
Change DocumentRoot to the VirtualHost directory path where you copied your new Website files to. For example:-
Change: DocumentRoot /var/www/your_current_website_name/public_html
To: DocumentRoot /var/www/your_new_website_name/public_html
I also do the same for ErrorLog and CustomLog. You can see how to set custom paths in my article Running a Live Web Server on a Raspberry Pi from Home. This detailed tutorial will also show you how to setup an SSL connection. If you do this, then you would change the paths above in the SSL VirtualHost file too.
Restart Apache for changes to take effect.
sudo systemctl restart apache2
If you browse to your domain in your browser, you should now see your new website. The old website is still on the server but the VirtualHost now points to the new one.
Update URL’s in Database
The URL’s in your site will still be pointing to your old paths. You can change these a few ways.
You can login to your WordPress Admin section and browse to Settings > General. Change the WordPress Address (URL) and Site Address (URL) fields to the correct domain.
Alternatively, you can use the command line method where you can edit the functions.php file of the WordPress theme.
Open the file in the editor.
sudo nano /var/www/your_new_website_name/public_html/wp-content/themes/your_new_theme_name/functions.php
Add these two lines at the end of the file.
update_option( 'siteurl', 'https://example.com' );
update_option( 'home', 'https://example.com' );
If you still have issues browing to pages, go back to WordPress Admin, go to Settings > Permalinks, and ensure you have the same setting for permalinks as you did on your source website. If it’s the same, click Save anyway as this will update your .htaccess file with the correct settings.
If you still have problems, then ensure you assigned access rights to the directory containing your website files, to the Apache user, as directed in the ‘Copy Website Files to the New VirtualHost Directory’ section of this tutorial. This is necessary as WordPress inserts the Rewrite directive into the .htaccess file and if the Apache user does not have write access to this file, this cannot be done.
If you then go back to the Permalinks page and save again. The rules should be written to the file.
If these methods don’t work, then you may have to update your VirtualHost and/or main Apache configuration file.
So edit one or both, of these files.
sudo nano /etc/apache2/sites-available/your_virtualhost_domain.com
sudo nano /etc/apache2/apache2.conf
Edit or add the AllowOverride directive by setting it from None to All.
Usually this is enough to ensure that you do not have any 404 errors when visiting pages, but you might still have anchor links within pages that still point to your original URL, depending on whether you used relative or absolute links when writing content. The easiest way to replace these in WordPress is using the ‘Velvet Blues Update URLs’ plugin. It allows you to do a simple search and replace on URLs. However, if you have hardcoded links in your WordPress PHP files, e.g. footer.php, you will have to edit these manually.
Your new WordPress website should now be live. Whilst this process isn’t as easy as a one-click plugin, it does give you the peace of mind that you are following a known process that you have performed yourself, rather than relying on third-party plugins, that may work, or worse still, cause a catastrophic error on your website.
Anytime you want to migrate a website, you can revisit this page and follow these instructions. Bookmark this article for later use.