Most people create their online presence with a shared hosting account, but sooner or later they need more power to fulfill their needs and more control over their server. Therefore, they switch to VPS.
In this tutorial, we will help you to migrate WordPress website from a shared hosting to a VPS server, but the same tactic can be applied to any kind of website.
What you’ll need
Before you begin this guide you’ll need the following:
- Access to shared hosting account and VPS;
Step 1 – Creating a Backup of Your Website
Before making any kind of changes to your website, a backup is necessary. It will not only help to transfer files and databases from one server to another but it will also prove to be useful in case something goes haywire.
In this example you will see how to make a backup on Hostinger’s control panel:
- Access Hostinger control panel;
- Go to Backups option under Files tab;
- After entering the Backups area you will see a list of files. The Type column specifies whether the backup is for MySQL Database or the Website files. You can also see if the backup was generated under the column Backup Date.You can also create a new backup if you wish to. To do that follow these steps:
- Once the backup is generated (or already present), download both MySLQ and website files backups by clicking the Download button in the Action column.
Once you have downloaded all the unique files, follow below steps to setting up VPS Server.
Step 2 – Setting up VPS
In this section we will learn to setup a VPS server. It is recommended to choose a template which has LAMP, MySQL and Webmin/Virtualmin pre-installed. It is easier to manage the server in the future with such template.
Alternatively, you can setup your own stack (without GUI). Some useful tutorials on how to setup different stacks:
- How to Install Nginx, MySQL, PHP v7 (LEMP) stack on CentOS 7
- How to Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04
- How to Install Linux, Apache, MySQL, PHP (LAMP) stack on CentOS 7
To change your VPS operating system and pre-installed software, follow steps below:
- In Hostinger control panel navigate to Servers section.
- Scroll down till you reach Other Settings panel.
- For Operating System select Ubuntu 16.04 64bit with Webmin/Virtualmin/LAMP and click on Save Changes.
Step 2.1 – Accessing Webmin / Virtualmin
Now allow a few minutes for new operating system to install and proceed to setup your VPS further:
- Your VPS IP, address, username, SSH port, along with other details, is listed under SSH Details. If you don’t know your root password, you can reset it in the same section (allow a few minutes for new changes to update).
- Now access and copy your VPS IP address, add https in front of it and :10000 port at the end to access Webmin / Virtualmin. It should look like this: https://18.104.22.168:10000
- You will now see a login box, enter your server SSH credentials to login.
- You will now see a Webmin Post-Installation Wizard which will guide you through step by step server setup. Click on next button to begin the setup.
Step 2.2 – Setting Up Virtualmin
We will walk through each step of installation one by one:
- Preload Virtualmin libraries? : It is recommended to choose an option No as we don’t use GUI very often since most of the work will done through SSH.
- Run email domain lookup server?: If emails are important to you and you will want to run Email server on your VPS, select Yes; it helps to make email services faster. The downside of it is that it takes a bit more RAM space.
- Virus scanning: It’s recommended to enable ClamAV virus scanner, so choose Yes.
- Spam filtering: This option is pretty well explained on the site. In short if you want to run email server on your VPS, enable SpamAssassin server.
- Database servers: Select the back end database service you would like to use. In most cases, you will only need MySQL database server.
- MySQL password: To prevent other users from managing MySQL database on your system, a password for the main user should be set. Enter strong, secure password.
- MySQL database size: It is well explained in what case you should select which setting. However it is recommended to select Medium option if you are not sure on which one to select.
- DNS Zones: For DNS zones created by Virtualmin to be resolvable, the primary nameserver record for each zone must be set to something that can be resolved by other systems on the Internet. As we will point domain name to VPS server via A record, we don’t need private nameservers for now. Thus, select Skip check for resolvability.
- Passwords: It is highly recommended to select the Only store hashed passwords option as it will encrypt all of your stored passwords.
- And that’s it. Virtualmin configuration is complete. You will now see a Re-check and refresh configuration button. Click it to test if everything is working as intended and whether the server has been setup properly or not.
Step 2.3 – Creating Virtual Server
Now we need to create a new user, MySQL database, and a directory for our website. It can be done all at once using Virtualmin:
- Press Create Virtual Server.
- Enter Domain name and Administration password and press Create Server button. This will create a simple server which is enough to host a website.
Step 3 – Transferring website files to VPS
Now that we have set up the VPS server, we will transfer our website files and databases one by one from our shared hosting server.
We will use wget command to download backup archives we have created in Step 1.
- Connect to your VPS Server via SSH (See here in case you don’t know how to).
- Once connected change your directory to the working directory, where your website files will be stored. Virtualmin automatically creates a directory based on the domain name. For example, for hostinger-tutorials.com domain it created directory named hostinger-tutorials.com. Thus, to access it we can use this command:
- Now head back to Hostinger control panel -> Backups section and copy download link for the backup archive of your website files. You can do this by right clicking on the Download button and choosing Copy Link Address.
- Use the following command to download backup archive to VPS (remember to change link address):
- Now extract the archive using:
tar -xvf u694443746.tar.gz
- It will create a new folder inside public_html, which will contain all our website files. This folder will have the same name as our Hostinger shared hosting account ID. Thus, we need to move all the files directly to public_html. There are quite a few ways to do this, but the most basic way is as below:
- Access extracted public_html folder (witch contains all your website files):
- Move all files to new public_html created by Virtualmin:
mv * ../../
- Access extracted public_html folder (witch contains all your website files):
- Now navigate back to public_html created by Virtualmin:
- Verify all files have been moved with this command:
- As you can see all files have been moved to correct public_html directory. However, our work here is not done yet. These files and folders have incorrect ownership. We need to change it to match the one created by Virtualmin. Just like directory, Virtualmin creates a user and a group based on domain name. Thus, to change ownership we can use the following command (remember to change hostinger-tutorials to match your case):
chown hostinger-tutorials:hostinger-tutorials -R *
Now you have successfully transferred all your files from shared server to VPS server. Follow the below steps to transfer your database and then a final step to get everything working!
Step 4 – Transferring Database files to VPS
Now that we have already transferred our files, we will need to transfer our databases too. Just like with files we will use wget to download the backup files from the Hostinger backup section (the one which is shown in the Step 1) and import all the tables to your VPS server’s database.
Remember, MySQL database was already created during Virtual Server creation. Thus, you don’t need to create additional databases.
Step 4.1 – Checking MySQL Database Name and Password
In order to import MySQL database backup and complete transfer process we first need to find out our new database name, username and password. Database name can be found on Virtualmin control panel, on Edit Databases section.
MySQL database username and password can be found on the same section, under Passwords tab.
In our example, our MySQL details are:
- MySQL Database Name – hostinger_tutorials
- MySQL Username – hostinger-tutorials
- MySQL User Password – OTuXH1IVHdtMpQS7
Step 4.2 – Download and Importing MySQL Backup
Once we know MySQL database name, we can proceed with importing our MySQL dump.
- Head over to Backup section and click on Copy link address for MySQL database backup.
- On our VPS we will create a new directory and enter it. We will store our MySQL backup archive in it.
mkdir sql_backup && cd sql_backup
- Once you are inside that directory, we will use wget to download the file from the hosting backup and save it on our VPS server.
- Once the file is downloaded, we will now extract the SQL file out of the compressed form using gunzip command
- You will now get a file with the same name but .sql format, we can now dump it to our database using below command (remember to change name of MySQL archive)
mysql -u root -p hostinger_tutorials < u694443746_meryr.sql
- You have now successfully imported all the tables from that backup file to our server’s database.
Step 5 – Making Final Changes
Now that you have transferred both: your website files and mysql databases to the VPS server, your WordPress site will still not work correctly. Most likely you will get Error establishing a database connection. To fix this, we will need to edit wp-config.php file and update it with new MySQL database details.
To get our site working we will use inbuilt terminal file editor nano to make the changes to the config file.
To edit the wp-config.php file follow the steps below:
- Enter the directory where your website files are stored using the cd command.
- Once we are inside the directory, we will see all the files present in the current folder. Let’s check if wp-config.php file is present in the directory.
- Once it is confirmed that the file is present, we will edit the file by using nano text editor.
- Once the file is open in editing mode, scroll down to the line // ** MySQL settings – You can get this info from your web host ** //
- Now update the following values:
- DB_NAME – MySQL database name. We already know it from Step 4.1
- DB_USER – MySQL username.
- DB_PASSWORD – MySQL password.
- DB_HOST – Enter localhost here as our MySQL database is on the same server.
- Once you have made the changes press CTRL + X on the keyboard and then press Y and then ENTER to save changes to the file.
The last step would be to point the domain name to the VPS server from shared hosting server. The easiest way to do this is to update A record.
Note: It can take up to 24 hours for DNS to fully propagate worldwide.
More Detailed tutorial on how to do point domain name to VPS server can be found here.
In this Migrate WordPress from Shared hosting to VPS tutorial you have successfully got your old site up and running on your new VPS server. It is suggested to take regular backups of your server in case of any issues.