Fast Local WordPress Setup

By: Ryan Kienstra on: October 16, 2014  in: Programming

Laptop IconHere’s a quick way to start a new¬†WordPress instance on a local server.

The script below:

  • Installs the latest version of WordPress
  • Asks for the database name, user, and password
  • Uses these to set up the database and wp-config.php file
  • Puts randomly generated salts in the wp-config.php file
  • Optionally downloads a .gitignore file

Copy this to a file named wp.sh, and put it in the directory where you want to install WordPress.
At the command line, cd to that directory, and type ./wp.sh

#!/bin/bash

wget -O latest.tar.gz http://wordpress.org/latest.tar.gz 
tar -zxvf latest.tar.gz
cd wordpress
cp -rf . ..
cd ..
rm -R wordpress
mkdir wp-content/uploads
rm latest.tar.gz

# wp-config setup
mv wp-config-sample.php wp-config.php
sed -i "/define('AUTH_KEY'/d" wp-config.php
sed -i "/define('SECURE_AUTH_KEY/d" wp-config.php
sed -i "/define('LOGGED_IN_KEY'/d" wp-config.php
sed -i "/define('NONCE_KEY'/d" wp-config.php
sed -i "/define('AUTH_SALT'/d" wp-config.php
sed -i "/define('SECURE_AUTH_SALT'/d" wp-config.php
sed -i "/define('LOGGED_IN_SALT'/d" wp-config.php

# Get randomly generated salt keys and put them in the right place 
wget -O salts.php https://api.wordpress.org/secret-key/1.1/salt/ 
sed -i -e "/define('NONCE_SALT[^\n]*/{r salts.php" -e "d}" wp-config.php
rm salts.php 

echo -n "Enter the database user: " 
read database_user 
echo -n "Enter the name of your database: " 
read database_name 
echo -n "Enter your database password: " 
read database_password 

# Enter the database details in the wp-config.php file
sed -i "s/define('DB_USER',[^\n]*/define('DB_USER', '"$database_user"' );/g" wp-config.php 
sed -i "s/define('DB_NAME',[^\n]*/define('DB_NAME', '"$database_name"' );/g" wp-config.php 
sed -i "s/define('DB_PASSWORD',[^\n]*/define('DB_PASSWORD', '"$database_password"' );/g" wp-config.php 
sed -i "s/define('DB_HOST',[^\n]*/define('DB_HOST', 'localhost' );/g" wp-config.php 

# Database setup
mysql --user=$database_user --password=$database_password -e "CREATE DATABASE "$database_name" DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" 

touch .htaccess
chown -R www-data:www-data .
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

echo "Download a gitignore file? y/n "
read do_install_gitignore 
if [ $do_install_gitignore = 'y'  ] ;
  then wget -O .gitignore http://cdn.wpengine.com/wp-content/uploads/2013/10/recommended-gitignore-no-wp.txt ;
fi ;

echo "Setup complete. Enjoy your new WordPress installation."
  • This field is for validation purposes and should be left unchanged.

Leave a comment

Get Free Updates

  • This field is for validation purposes and should be left unchanged.