Here you will find out, how to make a copy of your already existing website and put it into a virtual machine. So, if you want to make some code changes or just want to test out your themes new updates, you can do it without breaking down your online site.
This is the continuation of the previous, first part, of this two-part series. In the first part, I have shown how to prepare your own web server running WordPress in a virtual machine. You should read the first post, if you want to have a good starting point. On the other hand, if you have some ready to use environment, like a server with WordPress already installed, then you can skip the first part and read along.
Table of Contents
- Current setup
- WordPress removal
- Download your online site’s files and database
- Uploading and inserting files into Virtual Machine
- Editing some WordPress files
- Deactivate unneeded plugins
For those, who will skip the first part, my setup is as follows:
- Windows 10 Host machine, running Virtual Machine with Ubuntu 20.04 server, Apache, MySQL and other packages (needed for WordPress) installed
- Virtual Machine has two network interfaces: one NAT with dynamic IP, the second one with static IP: 192.168.200.2
- Static IP is used to access WordPress site (only from the Host machine), dynamic IP – Internet access
- Additional server’s web management software (Webmin) installed – makes easier to edit files (accessible via https://192.168.200.2:10000)
- At this point, default (empty) WordPress site installed and running
- MySQL has one database which is used by WordPress: database name – wordpress, username – wordpress, password – yourpassword
- WordPress directory on the server: /srv/www/wordpress
Some of this data will be used trough all this tutorial, so you will have to change it to your own names/IPs/passwords. Finally, if you have some questions, why such choices were made, you might find some answers in the tutorial’s first part.
Firstly, you will need to remove old WordPress installation and database from the Virtual Machine.
Via terminal write commands:
sudo mysql -u root DROP DATABASE wordpress; CREATE DATABASE wordpress; quit;
These will delete current database and create a new, empty one.
Next, remove WordPress directory:
cd /srv/www sudo rm -r wordpress
At this point you shouldn’t be able to access WordPress via 192.168.200.2
Download your online site’s files and database
Most hosting providers give an access to you to the site files and database. Although, different hosting providers might use different management software, the steps should be similar across the board. Mine uses cpanel, so this part will be based on it.
Log in into your account on hosting provider’s site. There, you should be able to locate two main parts: a file explorer and database management tool. In my case it looks like in the pic below:
In the file manager find WordPress installation folder. For me, it was named public_html. You can find out whether the folder is the one by opening it and looking what files it holds. If it has files like index.php, wp-config.php, etc. – that’s the folder you need. Compress the folder (I used .zip extension) and download to your PC.
Next, go to database administration panel (in my case it is phpMyAdmin). select you database, press export (I have used quick export selection) and save the exported file to your PC.
So, now you should have two files. Let’s assume the naming is as follows:
- public_html.zip – compressed WordPress files
- database.sql – database file
Note, that your files might be named differently, but the names above will be used in the following steps.
Uploading and inserting files into Virtual Machine
When you have the database and WordPress folder files on your PC, next step is to put them into Virtual Machine.
I will suggest using web administration tool to put files into VM, but it is also possible to do everything via terminal. How to use Webmin’s File Manager was briefly explained in the first part.
Both files (public_html.zip and database.sql) needs to be placed in the /srv/www folder (see My Current Setup for details and change the path according to your setup).
Now, open the VM’s terminal and type in:
cd /srv/www sudo unzip public_html.zip
This will extract folder from zip archive. There is a possibility, that you will have .tar, .tar.gz or similar extension instead of .zip. In that case you will have to use tar command to extract the folder.
Change unzipped folder’s name to the needed one. In my case it is wordpress:
sudo mv public_html wordpress
Finally, you can remove the .zip archive:
sudo rm public_html.zip
By default extracted folder’s owner is root user. We need to change that to www-data:
sudo chown -R www-data:www-data wordpress
Also, change folder permissions:
sudo chmod 755 wordpress
Now, we finished with the .zip archive. Let’s go to the database file (database.sql).
sudo mysql wordpress < database.sql
The above command will insert tables from database.sql file into wordpress database.
Finally, you can remove the .sql file:
sudo rm database.sql
So, now we have WordPress files and database uploaded, but it is still not ready for use.
Editing some WordPress files
Your online website probably will use different database naming than the offline Virtual Machine uses. So, we need to change those settings.
Go to the WordPress installation folder:
There will be a file wp-config.php. We need to change some lines there.
sed -i "/define('DB_NAME'/c\define('DB_NAME', 'wordpress');" wp-config.php sed -i "/define('DB_USER'/c\define('DB_USER', 'wordpress');" wp-config.php sed -i "/define('DB_PASSWORD'/c\define('DB_PASSWORD', 'yourpassword');" wp-config.php
The commands above change parameters as follows (these are used in wordpress MySQL database inside my Virtual Machine):
- DB_NAME sets to wordpress
- DB_USER sets to wordpress
- DB_PASSWORD changes to yourpassword
You should change those values to your used ones. Of course, you can also change the same wp-config.php file via Webmin’s graphical user interface. Just find in the file lines which define DB_NAME, DB_USER and DB_PASSWORD values.
Lastly, your online WordPress site will use some kind of domain, like daumemo.com 😀 Your offline version will use some IP address (mostly static) instead of the domain name. In this tutorial that IP is 192.168.200.2 . So, we also need to make some changes to the domain name settings.
Go to your themes directory:
In the command above change your-used-theme-name to the name of the theme that your actual site uses.
In that folder you should locate a file named functions.php. Add more lines to the file:
sed -i "1s/.*/<?php update_option( 'siteurl', 'http:\/\/192.168.200.2' ); update_option( 'home', 'http:\/\/192.168.200.2' );/" functions.php
The command above adds two lines to the file:
- update_option( ‘siteurl’, ‘http://192.168.200.2’ );
- update_option( ‘home’, ‘http://192.168.200.2’ );
You can also add these line via Webmin’s web interface.
Now, you should be able to access you site copy via 192.168.200.2 IP address. The login name and password to WordPress admin panel is the same as into your online site.
Finally, don’t close this post yet – there are some more things to do. Read below.
Deactivate unneeded plugins
I would suggest deactivating WordPress plugins such as:
- Analytics plugins (like Google Analytics)
- Caching plugins
- SEO plugins (optional)
Analytics plugins might change the statistics of your online site, so it is best to deactivate it.
It is also a good idea to deactivate any caching plugins as there might be some issues with displaying correctly your site.
Previously, I personally had some problems with correct display of my blog posts. A solution was: Settings-> Permalinks choose Plain. Although, I did not have such problem during this tutorial.
Also, you might want to change administrator account password to something simple – as the Virtual Machine is not public.
So, by following this tutorial you should have a working offline site copy (or a developing environment) where you can test out your new theme’s features or try out some new custom code lines without breaking down your precious online site.