/ WordPress

Setting Up Local Development on a WordPress Blog at Dreamhost

We will be working with a Mac running MAMP and a Dreamhost account running a 1-click install of WordPress.

Basic Overview

To run WordPress on Dreamhost, locally on your Mac there are a few things you'll need to do:

  • Setup a web server
    • A web server like Apache or Nginx must be installed and running. The server 'listens' for http requests so that when you go to http://localhost, the server responds.
    • All servers have a 'web root' or 'document root' directory. When it gets an http request, it loads the files in this directory. (In our case, that's where we'll put the WordPress files.)
  • Setup a database server and the database.
    • Things to know
      • There are different types of databases, Postgres, mySQL, and others. WordPress uses a mySQL database.
      • It's important to understand that the 'database' is basically just a file. The 'database server' knows how to read this file and it knows how to talk to the web server so it can show data on the page. The difference between a database and a database server can be confusing because even though they are seperate parts, the terms are used interchangeably.
    • Steps to setup. (As noted, WordPress uses a mySQL server so that's what we'll setup here.)
      • Install a MySQL server.
      • Start up the MySQL server and leave it running.
      • Now the server is running, but we also need to create a database locally.
      • Now that the database is created, to import we do a data dump from the original database, and then export it to this one.

Install the web server and SQL server using MAMP

To run WordPress locally, we will need to setup MAMP. (My Apache - MySQL - PHP) You just download and install a copy from MAMP. In installing MAMP, we have installed the web server and SQL server at the same time.

Things to note:

  • Preferences > Web Server has a document root (aka the web root folder where files for websites are kept) that defaults to /Applications/MAMP/htdocs This is where you will place your files. So if your WordPress folder is /myWebSite and you place it at /Applications/MAMP/htdocs/myWebSite after everything is setup you can view it at http://localhost/myWebSite  You can change that to some other web root if you like. All of my projects are in a folder called /code and I'd rather work out of there but don't want to make all of /code public so I like to create a symlink in htdocs to the project that will be served out of htdocs, but we will continue the tutorial assuming you want to place the files in htdocs. It will have permissions set so it is accessible to the web. (htdocs is a standard name for the public folder where website files are located and it stands for 'HyperText Documents'.) The name of the web root folder is different in different systems. Other names for the folder you might see are www and public_html.    

Download the WordPress files

For most website hosts, you'll use FTP to download the files. If you're not sure how to use FTP start by searching the help docs for your website host. That is usually the best place to find out how to login to your particular website host, how to connect to their FTP, and it will often have information on FTP clients.

After you've downloaded your WordPress site folder, place it in htdocs.

Now we have the files but not the database so let's go get that.

Export your database

Your website host will probably have phpMyAdmin which is "A tool written in PHP intended to handle the administration of MySQL over the WWW." Login to that to do the export of your data. Again, if you're not sure how, your hosts help docs will be the best place to look.

You'll need the database name, username, password, and hostname. You should see most of this in your web host panel (also known as cPanel) but you can also find some of this in the wp-config.php file which will be in the WordPress files you downloaded.

After you are logged in:

  • Click on the database you want to export
  • Click the 'Export' tab at the top of the screen
  • phpMyAdmin will give you the option of a quick or custom export method. I  recommend custom method. Choose zip as the compression method but leave all the other options as they are and click 'Go' to download your database.

We now have all the parts we need from the host!

Start up the web server and SQL server

This is pretty easy. Just open MAMP and hit Start Servers.

Import the database

After MAMP is started you can administer your databases at http://localhost/phpmyadmin/

As of this writing, the default username and password is username: root, password: root

After you're logged into phpMyAdmin:

  • Click Databases and create a new database using a descriptive name so that when you look at your databases later, you'll know what this is for. (I'm using my site name)
  • After creating the database you should be on it's page. You'll know because there will be breadcrumb navigation showing the database name "Server: localhost:3306 »Database: mysite". (phpMyAdmin can be confusing so make sure you see this.) From the database page, click "Import"
  • On the import page choose the .sql.zip file we created before and import this file. When it's all done you will see a success message.

Update settings for your new host

All the parts are there but there are a couple steps left. There are some WordPress settings that reference the domain name. Since the domain is now localhost and not your actual domain, we'll need to update these.

Update entries in the database

Some WordPress settings that reference the domain name are in the database. Because we're working on localhost and not mydomain.com, we'll have to update these. To update all of them using SQL,

UPDATE wp_options SET option_value = replace(option_value, 'http://mydomain.com', 'http://localhost/mydomain-com') WHERE option_name = 'home' OR option_name = 'siteurl';
  
UPDATE wp_posts SET post_content = replace(post_content, 'http://mydomain.com', 'http://localhost/mydomain-com');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://mydomain.com','http://localhost/mydomain-com');
  • wp_options, wp_posts, and wp_postmeta are tables in the database. If you get an error that the tables aren't found, check the database itself for the table names. Sometimes WordPress is setup giving these table names prefixes. If they are different just change the table name in the SQL statement to match the actual tables.

Update the WordPress config file

We're almost done! Next, we're going to update the wp-config.php file so it has the correct database information.

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

Set the MySQL username as your local mysql username, usually the default is root. If you have set password for mySQL user root on your localhost, then enter that password. (The default password should be root on a MAMP install) Otherwise leave it empty and save your changes.

If you're unsure of the MySQL password it can be a pain to change. Instead of giving instructions here, check out the current MAMP docs.

After the config change is made you're site should now be live at http://localhost/mysite