Install WordPress in less then a minute

If you have a WordPress website that is live and don’t have it set up locally on your computer or laptop, you’re doing it wrong. To install WordPress on your local machine is a breeze.

Why should you install WordPress on your local machine before running it on production? If you have a WordPress site, you will most certainly install some plugins to add some additional functionality to your site. However, installing plugins on your live site directly without testing them locally first could be disastrous. I’ve seen so many people break their websites by installing untested plugins directly on production. At the time of this writing there are 22,949 plugins in WordPress repository and to be honest, a LOT of them coud make your site misbehave. In most cases, this would be the plugin author’s fault for not keeping the plugin updated with the latest version of WordPress, or simply bad coding practices.

Over the last couple of years I have installed WordPress numerous times on my Mac. WordPress is awesome because it doesn’t take long to get it running, but if you’re installing new instances of WordPress frequently, you could make the process even faster by automating each step.

I presume you have some basic knowledge of PHP and MySQL. If you find this tutorial difficult, I recommend looking at Installing WordPress locally on your Mac with MAMP. Although, I think this method is the quickest and super easy provided you have the correct prerequisites on your system.

Before you install WordPress, you need to make sure that you have a MySQL server running. In regards to MySQL, you might want to download one of many MySQL GUI tools. I personally use MySQL Workbench because it’s good and it’s free. If you’re really into MySQL and would like a slightly more polished product, I recommend Navicat. Another great free tool is Sequel Pro.

Another thing you want to check before you install WordPress is that you have PHP running on your Mac. By default, I think Mountain Lion comes with PHP enabled out of the box. If you’re on older OS X machine, make sure your httpd.conf file (typically in /etc/apache2/httpd.conf) has the following line uncommented:

LoadModule php5_module libexec/apache2/libphp5.so

Once you have your new database created this PHP script will download and install WordPress directly from WordPress repository on your Mac. You won’t have to touch the hosts file, Apache Vhost and MySQL database. You only need to make sure you have MySQL server running on your system. The rest will be taken care of by this script:

#!/usr/bin/php
<?php 
require_once('includes/tools.inc.php');
$tab = (chr(9));

$WEBROOT = promptUser("Your webroot directory? (Include trailing slash. i.e. /Users/johnsmith/Sites/mysite/www/)");
$VHOSTPATH = promptUser("Enter your vhost file path: (i.e. /etc/apache2/users/mysite.conf)");
$SERVERNAME = promptUser("What is your development server name? DO NOT include http:// (i.e. mysite.dev)");
$APACHEUSER = promptUser("What is the user apache runs under? (i.e. www or yourusername)");
$MYSQLDB = promptUser("Enter MySQL Database name:");
$MYSQLHOST = promptUser("Enter MySQL host:", "127.0.0.1");
$MYSQLUSER = promptUser("Enter MySQL user:", "root");
$MYSQLPWD = promptUser("Enter MySQL password:", "");

//Need this to emulate the browser-based installation
$_SERVER['HTTP_HOST'] = $SERVERNAME;
$_SERVER['REQUEST_URI'] = "/";

msg('Creating DB ...');
if(strlen($MYSQLPWD)) {
    exc("mysql -h" . $MYSQLHOST . " -u" . $MYSQLUSER . " -p" . $MYSQLPWD . " -e  'CREATE DATABASE IF NOT EXISTS '" . $MYSQLDB . ";");
} else {
    exc("mysql -h" . $MYSQLHOST . " -u" . $MYSQLUSER . " -e  'CREATE DATABASE IF NOT EXISTS '" . $MYSQLDB . ";");
}

msg('Downloading WordPress ...');
exc('wget http://wordpress.org/latest.tar.gz');

msg('Unpacking WordPresss ...');
exc('tar xzf latest.tar.gz');

msg('moving wordpress into the webroot ' . $WEBROOT);
//make sure webroot exists
exc('mkdir -p "' . $WEBROOT . '"');
exc('cp -r wordpress/* "' . $WEBROOT . '"');
exc('rm -rf wordpress');

msg("Setup folder permissions..");
//set folder permissions to apache user
exc('chown -R ' . $APACHEUSER . ':staff ' . $WEBROOT);

//add local site to the hosts file
msg("Add entry in /etc/hosts file...");
exc('echo "127.0.0.1\t' . $SERVERNAME . '" >> /etc/hosts');

msg("Setting up the vhost...");
//set up apache vhost
$VHOST='NameVirtualHost *:80' . PHP_EOL . PHP_EOL;
$VHOST.='<Directory ' . $WEBROOT . '>' . PHP_EOL;
$VHOST.=$tab . 'Options Indexes FollowSymLinks MultiViews' . PHP_EOL;
$VHOST.=$tab . 'AllowOverride All' . PHP_EOL;
$VHOST.=$tab . 'Order allow,deny' . PHP_EOL;
$VHOST.=$tab . 'Allow from all' . PHP_EOL;
$VHOST.='' . PHP_EOL;

$VHOST.='' . PHP_EOL;
$VHOST.=$tab . 'DocumentRoot ' . $WEBROOT . PHP_EOL;
$VHOST.=$tab . 'ServerName ' . $SERVERNAME . PHP_EOL;
$VHOST.=$tab . 'DirectoryIndex index.php' . PHP_EOL;
$VHOST.='';

$fw = fopen($VHOSTPATH, "w");
fwrite($fw, $VHOST);

msg("Setting up the config file...");
//Now let's set up the config file
$config_file = file($WEBROOT . 'wp-config-sample.php');
$secret_keys = file_get_contents( 'https://api.wordpress.org/secret-key/1.1/salt/' );
$secret_keys = explode( "\n", $secret_keys );
foreach ( $secret_keys as $k => $v ) {
    $secret_keys[$k] = substr( $v, 28, 64 );
}
array_pop($secret_keys);

$config_file = str_replace('database_name_here', $MYSQLDB, $config_file);
$config_file = str_replace('username_here', $MYSQLUSER, $config_file);
$config_file = str_replace('password_here', $MYSQLPWD, $config_file);
$config_file = str_replace('localhost', $MYSQLHOST, $config_file);
$config_file = str_replace("'AUTH_KEY',         'put your unique phrase here'", "'AUTH_KEY',         '{$secret_keys[0]}'", $config_file);
$config_file = str_replace("'SECURE_AUTH_KEY',  'put your unique phrase here'", "'SECURE_AUTH_KEY',  '{$secret_keys[1]}'", $config_file);
$config_file = str_replace("'LOGGED_IN_KEY',    'put your unique phrase here'", "'LOGGED_IN_KEY',    '{$secret_keys[2]}'", $config_file);
$config_file = str_replace("'NONCE_KEY',        'put your unique phrase here'", "'NONCE_KEY',        '{$secret_keys[3]}'", $config_file);
$config_file = str_replace("'AUTH_SALT',        'put your unique phrase here'", "'AUTH_SALT',        '{$secret_keys[4]}'", $config_file);
$config_file = str_replace("'SECURE_AUTH_SALT', 'put your unique phrase here'", "'SECURE_AUTH_SALT', '{$secret_keys[5]}'", $config_file);
$config_file = str_replace("'LOGGED_IN_SALT',   'put your unique phrase here'", "'LOGGED_IN_SALT',   '{$secret_keys[6]}'", $config_file);
$config_file = str_replace("'NONCE_SALT',       'put your unique phrase here'", "'NONCE_SALT',       '{$secret_keys[7]}'", $config_file);

if(file_exists($WEBROOT .'wp-config.php')) {
    unlink($WEBROOT .'wp-config.php');
}

$fw = fopen($WEBROOT . 'wp-config.php', "a");

foreach ( $config_file as $line_num => $line ) {
    fwrite($fw, $line);
}

msg("Installing WordPress...");
define('ABSPATH', $WEBROOT);
define('WP_CONTENT_DIR', 'wp-content/');
define('WPINC', 'wp-includes');
define( 'WP_LANG_DIR', WP_CONTENT_DIR . '/languages' );

define('WP_USE_THEMES', true);
define('DB_NAME', $MYSQLDB);
define('DB_USER', $MYSQLUSER);
define('DB_PASSWORD', $MYSQLPWD);
define('DB_HOST', $MYSQLHOST);

$_GET['step'] = 2;
$_POST['weblog_title'] = "My Test Blog";
$_POST['user_name'] = "admin";
$_POST['admin_email'] = "[email protected]";
$_POST['blog_public'] = true;
$_POST['admin_password'] = "admin";
$_POST['admin_password2'] = "admin";

require_once(ABSPATH . 'wp-admin/install.php');
require_once(ABSPATH . 'wp-load.php');
require_once(ABSPATH . WPINC . '/class-wp-walker.php');
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

msg('restarting apache');
exc('apachectl -k graceful');
msg('Your WordPress site is ready. Navigate to http://' . $SERVERNAME . ' in your web browser');

If you decide to use this, I suggest getting the script from my GitHub repository since it will be up to date.

Now let me briefly explain the script that will install WordPress.

Usage

sudo ./wpinstall.php

To install WordPress successfully, you must run this as root.

Input Parameters

To install WordPress successfully, you will be asked to input some basic parameters:

  1. Your webroot directory?
  2. vhost file path: (i.e. /etc/apache2/users/mysite.conf)
  3. Server name? DO NOT include http:// (a valid entry would be: mysite.dev)
  4. User apache runs under? (i.e. www or yourusername)
  5. MySQL Database name?
  6. MySQL host [127.0.0.1]
  7. MySQL user [root]
  8. MySQL password (leave blank if not sure)
  9. Navigate to http://mysite.dev or whatever server name you specified in Step 3 and your WordPress site will be ready to go!

Double check your parameters before hitting ‘enter’. Challenge yourself to install WordPress correctly the first time!

If you know exactly what you’re doing, this particular setup will take you approximately 45 seconds to complete. That’s if you take out the time it takes to download WordPress source of course. Once you understand the process, you can probably do it quicker than 45 seconds. Not bad for a CMS with it’s famous 5-minute install, which doesn’t take into account downloading WordPress, setting up vhost, ip mapping and db creation. Just google “WordPress 5 minute install”, you’ll get plenty of results :) This script, with a full
source download will take about 2 minutes to complete.

No seriously, I’m not doing this to show off! I really find this script useful and it saves me 10 minutes every time I set up a new WordPress site.

Notes

  • This script downloads the latest WordPress tarball from wordpress.org. I think WordPress has a limit of 3-5 pull requests in a given timeframe so try to get the installation right the first time. If you don’t, you might have to modify the script and comment out the line that downloads the WordPress tarball if your previous attempt already did that. This is a utility script that I use for myself and it works like a champ. Feel free to fork and modify.
  • The script will append ’127.0.0.1 servername’ into your hosts file each time you run it. If your first attempt fails for some reason, cleanup your hosts file before running it again.

Prerequisites

wget. I don’t think wget is installed on all OS X systems by default. You can check this by simply running

wget

If you don’t have it, the best way to install it is to run:

brew install wget

If you got an error you probably don’t have homebrew installed. Try installing homebrew by running:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

Then retry

brew install wget

Finally, you will need MySQL. If you don’t already have it, install it with homebrew:

brew install mysql

And you should be ready to install WordPress.

Once you have the script working, then all you need to do is navigate to http://mysite.dev in your browser and your WordPress site should be waiting for you with a nice responsive theme! You can also log into your WordPress admin using the default admin/admin credentials. I suggest you change this as soon as you install the site.

You could also modify this script to allow for different database prefix and site title options, but I generally leave those to default values.

Check out the script on my GitHub page to stay up to date.

Use this at your own risk of course. As I said, I have only tested this on my Mountain Lion laptop and it works really well. Keen to hear if anyone else finds this useful.

Marko

  • zl

    Thank you very much for your script. I am a newbie to PHP. I run the script on my macbook (os x 10.7.5) and got following error.

    PHP Warning: require_once(includes/tools.inc.php): failed to open stream: No such file or directory in /Users/czhiling/Downloads/wpinstall.php on line 25
    PHP Fatal error: require_once(): Failed opening required ‘includes/tools.inc.php’ (include_path=’.:’) in /Users/czhiling/Downloads/wpinstall.php on line 25

    ===

    > PHP -V

    PHP 5.3.15 with Suhosin-Patch (cli) (built: Jul 31 2012 14:49:18)
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

    Would you please advice me the solution?

    Zhiling

    • http://www.markomedia.com.au Marko Tomic

      @zhiling, it looks like you are missing “includes” directory. Did you clone the code from GitHub? The GitHub repo contains all the files you need to run this script successfully.

      • zhiling

        Thank you very much for the help. Now it works like a charm.