WordPress is not just a content management system. It became more than a content management system when smart developers started making plugins and themes that we can use to create any kind of website using WordPress. It was first known to create and manage blogs easily. But we can see nowadays the rise of E-commerce on WordPress. Nowadays, people are also creating business or corporate websites on WordPress. However, All the developers develop a website locally on the computer. Then, they have to migrate a WordPress site from localhost to VPS.
If you are one such developer, you might want to learn how you can easily migrate a newly developed WordPress site from localhost to VPS. In this article, I will demonstrate How to accomplish this task without messing up a site or a server. Here are some prerequisites before you get into the actual step-by-step guide.
Prerequisites
I assume you have a basic knowledge of MySQL and Linux. We are going to migrate a WordPress site to a live server, It means that your VPS should be ready to host a website. If you haven’t installed LAMP or LEMP on your VPS yet, Get a Linux VPS and Install LAMP or Install LEMP on your server.
You also need a root access or sudo privilege on the server. It is because we will create a virtual host on on our server. You can follow our following guides to learn more about Virtual hosts.
If you have followed our guide to install LAMP/LEMP stack and to manage virtual hosts, you can blindly follow this guide. Otherwise, you might also have to make sure you are working according to your current configuration.
Step 1: Compress the WordPress files
WordPress is a combination of files and a database. Files meaning the WordPress codebase, your plugins, your themes and your media. Database is used to keep track of everything and to store your blogposts, theme configurations, user data and much more.
So, First of all we are going to compress WordPress files on localhost. You can use tools like 7zip or zip or tar or anything you like. Compress the whole WordPress installation directory.
Step 2: Export the Database
As I mentioned in the previous section, WordPress is made up with files and a database. The database is important because all your posts, configuration and data is stored in the database. Exporting the database can be tricky based on your environment. If you are using Linux or MacOS, you can use the following command in terminal to export your Database to an .sql
file.
$ mysqldump -uDB_USERNAME -p DB_NAME > exported_DB_NAME.sql
You can get DB_USERNAME, DB_PASSWORD and DB_NAME from the wp-config.php file. Once it is exported, we can move both the files and database to our remote server.
Step 3: Move/Copy/Upload Files and Database to VPS
In this step, you have to upload the compressed archive and the database to the VPS. You can either use SFTP, or SCP or any other method. Our goal is to upload both to the VPS.
Upload it in the home directory for now, do not upload any one of them where they are publicly exposed.
If you are using Linux or MacOS, you can use SCP which is very easy to use. You can simply execute the following commands to shift your files form localhost to any other machine.
$ scp /path/to/file user@server_ip:~
In this command, do not forget to replace the path of the file and information about your VPS. You can use the same command to migrate your compressed archive as well as the database.
Step 4: Create and Import the Database on VPS
As now your database and files are on the VPS, The first thing we will do is create a database and Import the exported .sql
file into the database. First of all, Let us create a database in MySQL. Execute the following command to login to MySQL as root.
$ mysql -uroot -p;
It will ask for a password, enter the correct MySQL root password and then execute the following queries to create a database along with a user in MySQL.
mysql> CREATE DATABASE wordpress; mysql> CREATE USER 'wordpressUser'@'localhost' IDENTIFIED BY 'strong_password'; mysql> GRANT ALL PRIVILEGES ON wordpress.* to 'wordpressUser'@'localhost';
Do not forget to replace the Database name, Username and Password in these queries. Once your database is ready, execute the exit
command to exit the MySQL CLI. Now, Execute the following command to import the database.
$ mysql -uwordpressUser -p wordpress < /path/to/exported_DB_NAME.sql
Step 5: Create a directory, extract files and Update Database Info
In this step, we will create a directory for our website and then extract all our files inside that directory. Execute the following command to create a directory for your site inside the /var/www
directory.
$ sudo mkdir /var/www/example.com
Once done, Move your compressed archive inside this directory and extract all the files inside this directory. We also have to update the database information inside the wp-config.php file. Execute the following command to edit wp-config.php file.
$ sudo nano /var/www/example.com/wp-config.php
Enter new values in DB_NAME, DB_USERNAME and DB_PASSWORD we set when we created a database on our VPS. Now, the last thing we have to do is to update the file permissions. We will give ownership to www-data
so that you can upload media and update theme directly in the WordPress. Execute the following command to complete this task.
$ sudo chown -R www-data:www-data /var/www/example.com
Now, there is only one step left. It is to create and activate the virtual host according to your web server.
Step 6: Create virtual Host and Activate it (Apache)
If you are using Apache web server, it is going to be easy for you as Apache provides in-build command to activate or deactivate the virtual hosts. Execute the following command to create a new virtual host in your Apache server.
$ sudo nano /etc/apache2/sites-available/example.conf
And paste the following content in the file. Do not forget to replace the example data with the actual data depending on your site.
<VirtualHost *:80> ServerAdmin [email protected] ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog /var/log/apache2/example-error.log CustomLog /var/log/apache2/example-access.log combined </VirtualHost>
Once done, press CTRL+X followed by Y followed by Enter to save the virtual host file. Then, execute the following command to enable the virtual hosts.
$ sudo a2ensite example $ sudo service apache2 restart
Now, your site is live on the Internet!
Step 6: Create Virtual Host and Activate it (Nginx)
If you are using Nginx server, you have to manually create a symlink of the virtual host in order to activate it. The first step is to create a virtual host. Execute the following command to create a new virtual host in Nginx.
$ sudo nano /etc/nginx/sites-available/example.conf
Now, Paste the following content in the virtual host file. Do not forget to replace the example data with the actual data in the file, otherwise your site might not work.
server { listen 80; server_name example.com www.example.com; root /var/www/example.com; access_log /var/log/nginx/example-access.log; error_log /var/log/nginx/example-error.log }
Now, execute the following commands to create a symlink to activate the virtual host. We also have to restart the Nginx server to load the new configuration.
$ sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf $ sudo service nginx restart
Once the Nginx server is restarted, your site will be live on the Internet.
Conclusion: If you just understand how WordPress works, you can migrate your WordPress site to any other operating system or a server. The main thing here is to migrate the database and files safely to a new server and then connect the same database and files again on the new server. You can use this method to migrate your WordPress site from VPS to VPS too! You can also use WordPress plugins to migrate your WordPress site from one host to another.
Please let us know if you have any questions in the comment section given below. If you are an existing InterServer customer, please contact our support if you want us to migrate your WordPress website for you.