I design & develop websites.

I am a designer & developer. I am a communicator & strategist. I can add value to your business.

Moving WordPress – Localhost to Live

I watched Chris Coyier's excellent screencast #87 Moving up with MAMP (you certainly should too) on the subject of transferring a WordPress blog from a local server (or other development server) to a production server. Though he covered a lot of ground and set out a process perfect for less advanced users, especially those with no experience of the Terminal there are a few tricks that more advanced users will be able to use to save a lot of time and headaches.

1. Moving files

The only files that you need to upload are those specific to your installation of WordPress which are contained within the wp-content directory (with one notable exception but we will get to that later). The rest of the files can be fetched straight from the WordPress servers eliminating the lengthy period of time it takes even the better FTP clients to upload all the core WordPress files. This is done by connecting to the production server via SSH and issuing the following commands.

wget http://wordpress.org/latest.tar.gz
tar xzf latest.tar.gz

You can then go ahead and upload the contents of the wp-content directory via FTP. However one file in particular will be missing - the .htaccess file used by WordPress to create the permalink structure of your site. You may wish to upload this from your WordPress directory but because the filename is prefixed with a . you are unlikely to be able to see it without changing the settings of your FTP client. The easiest thing is simply to ensure that when the site is uploaded you have WordPress rewrite these rules by disabling and then re-enabling permalinks in the WordPress settings panel.

2. References to the development domain in the DB

In the WordPress database multiple references are made in different records and tables to WordPress' location (i.e. the domain it resides on). When installing on a local development machine these settings are of course different from those of your production server and it is these settings which you will find in the database. This can be avoided however by editing your /etc/hosts file on OSX and Linux (or \Windows\system32\drivers\etc\hosts file on Windows) so that locally the production domain points to your development server.

On Linux / Mac open Terminal and type "sudo nano /etc/hosts", enter your password and add the following at the bottom of the file.

127.0.0.1 www.productiondomain.com

On windows just browse to the \Windows\system32\drivers\etc\ folder and open the hosts file with Notepad and add the same line.

This enables you to configure WordPress as if you were using the production domain and your computer will just redirect your requests for www.productiondomain.com to your local server.

If you haven't done this are your database is littered with references to your development domain no need to fear. All you need to do is find and replace the instances of your local development domain within the exported database dump you will be uploading to the production server. This can be done by executing the following command on the database dump file in Terminal.

sed -i.bak 's/localhost/www.productiondomain.com/g' db.sql

What this does is replace all instances of the string "localhost" with the string "www.productiondomain.com" so that all your database records will reference the new domain rather than your local development version. This command only available on OSX and Linux but one could also use the find and replace functionality of your favourite text editor as long as the database wasn't to large.

3. Permalink Problems

The permalink settings for your blog manifest themselves as entries in your .htaccess file which resides in the root of your WordPress installation. However, as you aren't moving the whole WordPress installation this is often forgotten. This causes many links the worked in the development environment to give 404 errors. There are two ways of fixing this. You could FTP/scp the .htaccess file from your development server to your production server, however, there is an easier way. In the WP Admin Panel on the production server go to Permalinks (under settings) and set check the default option (taking note of the original setting). Save your changes (a message saying "Permalink structure updated" will appear) and then re-check the original setting and save changes again. This will cause WordPress to write the appropriate rules in your .htaccess file and all of your links will work again.

Anyhow, I hope you find this useful and that it speeds up the process a little. Let me know what you think in the comments and if there are any particular requests I will expand upon some more of the minutia of moving WordPress from a localhost to live.

17 Responses to Moving WordPress – Localhost to Live

  1. Kieran Mitchell says:

    Helped a lot. Cheers

  2. mnowluck says:

    Awesome.. thanks a lot

  3. Andrew Pryde says:

    No problem guys. If you have any problems just let me know :)

  4. Pingback: wp-popular.com » Blog Archive » Moving Wordpress – Localhost to Live | Pryde Design

  5. Darren says:

    I’ve just tried this for one of my sites I kick started locally.. i changed the db username and password, local to live references and the site loaded. Problem is the categories display ok (i..e category 1 (4)) but the main content doesn’t. The theme part of it looks ok but the menu is a theme menu rather than a WordPress menu, and if I click on any internal link get a 500 server error or 404 … will keep digging before recreating everything as it must just be one thing (that vital “one thing”) I’ve missed or done incorrectly! WIll update this if I find it!

  6. Andrew Pryde says:

    Hi Darren,

    What you need to do is regenerate the permalink structure as when you copied the files the .htaccess was not copied over. If you go to wp-admin and then settings->permalinks and select the top option (default) and save then re-edit the structure again back to what you had it on your local version the .htaccess file will be regenerated and your links will work again.

  7. Darren says:

    Update : thanks for the response – you were totally on the ball. The problem I had not showing the posts was due to an old setting left in .htaccess
    Also I was using the Suffusion theme which never took all the options over quite like I expected due to internal wordpress ID’s being different on my localhost and live implementation.

  8. Jeff Perry says:

    I tried to change all the URL’s in a text editor and when I upload the modified database to production I lose all my templates, header images, & themes. Are there certain URL’s that shouldn’t be changed or is there another way to modify the database for the new URL of the production site???????

  9. Andrew Pryde says:

    Did you copy over the contents of your wp-config directory? That is where the theme and plugin files are located as well as your uploaded images. If you did that already try regenerating your permalink structure (see my above comment).

  10. Josh says:

    This was a big help… thank you! The permalink structure tip saved me.

  11. Pingback: Ninique.net – Veronique Gagnon-Bilhete's stuff » Finally up and running!

  12. Thanks, Andrew. The permalink trick was a lifesaver and ended my desperate search to fix that issue! I love you man!

  13. Andrew Pryde says:

    No problem Neil. Glad it helped. If you need need anything else let me know.

  14. maemae says:

    thank you so much for this explanation – I was getting so stuck! really appreciated :)

  15. Hi,
    I used MAMP successfully for the first time a couple of months ago when designing a site for a non-profit, and got the site “live” without problems thanks to your comments and the ones on CSS-tricks….. very grateful for those posts.
    Here is my question: I need to do some work on my husband’s site as I never really finished it. Since that one is “live” and we send people there and get work based upon his portfolio, I have to leave it there while making improvements — or a complete overhaul.
    Can I go backwards, so to speak, and generate a copy that I direct to a local server and do the work and then move that back to replace the current site?
    Thanks

  16. Andrew Pryde says:

    The process should work the same in reverse. All you need to do is download the files and take a copy of the database. Then just use the same methods I mention in my post to replace the live domain with your local domain (e.g. http://localhost) If you’re having trouble pop and email to andrew@pryde-design.co.uk and I’ll see if I can help.

  17. Ryan says:

    The code snippets on this page seem wrong.

    They are filled with span classes!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>