Posted on

Installing WordPress via the command line

I found myself having to create more and more WordPress websites after clients would specifically ask for ‘a WordPress website’. Although WordPress is built as a blogging tool, it can be used as a very simple content management system (without too much customisation in the back end).

After installing WordPress manually for the seventh time, I thought it would be an idea to make a quick shell script that I would be able to use over SSH, as servers are able to download files via wget much quicker than I can downloading to my local machine and then uploading via FTP or copying it over via scp or rsync. Some hosting providers have automated scripts already installed within Plesk, cPanel and other web host management systems that allow you to install WordPress with a click of a button. It will install the latest version, set up the database and you’re set. The only issue here is when you’re working on dedicated or cloud servers where these tools are not readily available.

I’ve always been the one to start something from scratch so I know the exact the process, how long it should take and what to expect, as well as the ability to easily customise so it can meet my needs.

This simple shell script will:

  • download and unzip the latest version of WordPress
  • remove the sample configuration file
  • generate random salts (taken directly from the WordPress website):
  • create a custom configuration file
  • define the SITE_URL and HOME_URL variables as per your handle name
  • copy over any predefined themes and plugins
  • change permissions and ownership of the installed files (otherwise, if you’re running this as root, you’ll end up with internal server errors)

I’ve added a load of comments against each part of the code so it should be easy enough to follow. Anyway, here it is:

# Install WordPress
# Installation: Copy this script into /usr/local/bin/wordpress
# Useage: [email protected] [~]# wordpress

# Get the handle name
echo 'Please enter the handle name'

# Get the group and username
group=$(ls -la | head -3 | awk 'END {print $3}')
user=$(ls -la | head -3 | awk 'END {print $4}')

# Install the latest version of WordPress
tar xfz latest.tar.gz
mv ./wordpress/* ./
rmdir ./wordpress/
rm -f latest.tar.gz

# Remove the sample configuration file
rm -f ./wp-config-sample.php

# Get random salts from the WordPress website
salts=$(wget -O- -q

# Create the configuration file
cat <./wp-config.php
* The base configurations of the WordPress.
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {@link Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
* @package WordPress

define('SITE_URL', 'http://$HANDLE.local');
define('HOME_URL', 'http://$HANDLE.local');

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', '$HANDLE_wordpress');

/** MySQL database username */
define('DB_USER', 'dev');

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

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

* Authentication Unique Keys and Salts.
* Change these to different unique phrases!
* You can generate these using the {@link secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
* @since 2.6.0


* WordPress Database Table prefix.
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
\$table_prefix = 'wp_';

* WordPress Localized Language, defaults to English.
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
define('WPLANG', '');

* For developers: WordPress debugging mode.
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
define('WP_DEBUG', false);

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

# Insert custom blank theme (comment out the unfavored copying process)
mkdir ./wp-content/themes/$HANDLE
#cp -r /path/to/my/custom-theme/* ./wp-content/themes/$HANDLE
scp -r [email protected]:/path/to/my/custom-theme/* ./wp-content/themes/$HANDLE

# Replace placeholder content (this was to replace the comments within the custom theme but I have yet to get it working, maybe something to fix in the near future)
#perl -pi -w -e 's/Company Name/$HANDLE/g;' ./wp-content/themes/$HANDLE/*.php
#perl -pi -w -e 's/since 2014/since $(date +%Y)/g;' ./wp-content/themes/$HANDLE/*.php

# Install plugins (comment out the unfavored copying process)
#cp -r /path/to/my/plugins/* ./wp-content/plugins/
scp -r [email protected]:/path/to/my/plugins/* ./wp-content/plugins/

# Change file permissions and ownership
sudo chown -R $group:$user ./
find ./ -type d -exec chmod 755 {} \;
find ./ -type f -exec chmod 655 {} \;

I think the only bits I am missing, that I will eventually get round to doing is:

  • Install a new database for the WordPress website
  • Set up new database credentials and insert them into the configuration file
  • Add an environment variable (so it will use scp instead of cp for copying over files if you’re not installing locally, change the predefined URL names and other useful bits)

I hope this script helps someone installing WordPress on their server or local machine (or at least makes the process a little quicker). If there’s anything that I have missed that might be useful to you, please mention them in the comments and I will update the installation script.

This code is also available on GitHub so feel free to download or even contribute:

Posted on

Watermarking images using PHP

My girlfriend recently started a photography business. I found her uploading her photos to Facebook or her website with her logo in the bottom right of the image. She would use Photoshop to do this manually but the work really started when there were quite a few photos to do this to.

I know there are batch processes in Photoshop but this can be quite slow depending on what you’re doing. Photoshop seems to show you the running batch process which uses up a lot of CPU rending the changes on screen. As she’s lucky enough to have me for a boyfriend, I thought I would help out by writing a little script she could use to do this for her.

The class takes a predefined logo and sticks it on the image, saves it and moves on to the next one. There are two versions of the logo, one to be used on a light background, the other to be used on a dark background:

View the class on GitHub:

To use the code:

// Create a new instance of the Watermark class
$watermark = new Watermark();

// Set the input directory (where the images are located)

// Set the output directory (where the files will be saved)

// Run the script