How to move a WordPress site from development to production using the Terminal
Not only will it make you feel like a badass developer, moving a site from dev to production using the Terminal is also much quicker and much more satisfying than waiting for a site to upload via an FTP client! It’s also much easier than you’d think to set up.
Before you begin, be sure to set up the new server and database. For ease, make the username, password and database name the same for both the development and production databases. Export your database as a .gzipped file and make a note of the IP address and gather the same information for the dev site. Just follow the steps below to move the site from development to production. It’s a good idea to back up the development site before you get started for the first few times.
How to do it:
1. Open up the Terminal
2. Set up your workspace by opening two tabs – this will mean we can handle two sites at the same time rather than having to do one at a time by going to Shell > New Tab > New Tab with Settings Basic.
3. Connect to the new server by typing ssh followed by USERNAME@IPADDRESS into the Terminal and pressing enter. For example, with a username twkmedia and an IP address of 1.2.3.4.5, you would enter the below:
ssh twkmedia@1.2.3.4.5
4. If you have not added this server as a hostname, or have never visited the server before, you will receive the below error message:
The authenticity of host 'example.org (207.223.240.181)' can't be established. RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40. Are you sure you want to continue connecting (yes/no)?
5. You will then receive the below message:
Warning: Permanently added 'example.org,207.223.240.181' (RSA) to the list of known hosts. example.org,207.223.240.181 password:
You will then need to enter the server’s password, followed by the enter key.
Don’t be alarmed if the terminal doesn’t appear to be ‘typing’ your password. It is!
6. We now need to remove all files that we do not need for the site, including css, html and image files. When using ssh to access the files, you will normally not land in the root folder of the site (eg. httpdocs). You will normally need to use the cd command to get there. To see what is in the directory and to delete the files, follow the below steps, pressing enter after each line:
ls cd DIRECTORYNAME ll
ls = list contents
cd = change directories
ll = this is a shorhand version of ls -al, which lists files including those that start with a ‘.’ (a) and outputs the contents of the directory in a long list format (-l)
The Terminal will then return a list of the contents of the directory.
7. You will now need to delete its contents. To delete an individual file, the command is:
rm FILENAME
To delete a directory, the command is:
rm -Rf DIRECTORYNAME
Be sure to press the enter key after each command.
PLEASE NOTE: rm is irreversible! Before you press enter, always double check you are deleting the right file in the right directory
8. Then, to check the directory is empty, enter pwd followed by the enter key to print the working directory. You can then type ls > enter to check that all of the files have been successfully deleted. You are now ready to upload the dev site to the production server!
9. Open the second tab we created earlier and connect to the dev site by following the below steps (this is an example with a site stored at location /Applications/MAMP/htdocs/SITENAME):
cd / cd Applications ls cd MAMP ls cd htdocs ls cd SITENAME
If you already know your file directory, you can simply enter cd directory/directory/directory, for example. Make sure you are not already in a directory. It can be safer to do cd / to get to the root first before entering your directory extension!
10. You’re now into the right directory! We now need to set up the rsync between the two servers. To do this, use the rsync command.
rsync -avz --partial --progress /DIRECTORYEXTENSION/ NEWSERVERUSERNAME@NEWSERVERNAME:/DIRECTORYEXTENSION FOR NEW SITE
RSYNC ‘ADD ONS’:
-avz – shorthand for archive, verbose, compression
–progress – the terminal will show you how far it is through the upload as a percentage
–partial – if the transfer fails mid-way, you can continue safely without files getting corrupted
For example, for a dev site at /Applications/MAMP/htdocs/SITENAME that you’d like to move to server on IP address 1.2.3.4.5 in folder var/www/vhosts/www.twkmedia.com/httpdocs/, you should enter:
Example:
rsync -avz --partial --progress /Applications/MAMP/htdocs/SITENAME/ twkmedia@1.2.3.4.5:/var/www/vhosts/www.twkmedia.com/httpdocs/
To find your working directory in the new site, be sure to pwd to get the correct path for the rysnc!
Once you press ‘enter’, you will then need to enter the password for the new server and press enter. This will begin the file upload.
11. Once complete, you can go back to the new server tab and enter ls to see if the file transfer was successful. You should see all of the directories and files from your dev sites.
12. You then need to change the wp-config.php file on the new server. Go to the root of the WordPress installation on the new server (httpdocs would be the root directory for the example site) and then type:
vi wp-config.php
This will open the Terminal’s text editor. You then need to update the username, password and database name to the new server’s information. To do this press i to enter editor mode and use the arrow keys to navigate through the content to the relevant section:
Replace the details with the information. Once done, press esc to leave the editor mode and then type :wq (w=write (save), q=quit) to save your file and quit.
13. For ease, I like to use phpMyAdmin to simply import the dev database into the new server and replace URLs. You can see our blog post on how to change URLs in a database here!