Automating a WordPress install I do not have a whole lot to do over winter break, so I wrote this little script to automate a Wordpress install (currently can only install once instance) on a fresh Debian server (tested, working with Wheezy). It may be pretty sloppy because it's the first thing I've actually tried, but it's a start I guess. I was not too worried about security with this script, but I tried to handle the passwords as best as possible, and they are not printed out at any time (except in .my.cnf, which gets deleted).

I heard somewhere that it is better to print variables like ${DOCUMENT_ROOT} instead of just $DOCUMENT_ROOT. Are there any other recommended tips like this to make scripts perform better / easier to maintain?

#!/bin/bash
#auto wordpress installer

DOCUMENT_ROOT="/var/www/wordpress"
MYSQL_ROOT_PASS="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)"

## uses this server email to set up apache's config file
echo "Enter in the email for the server administrator:"
read SERVER_ADMIN_EMAIL

apt-get update
apt-get upgrade

## Set up passwords so mysql-server install doesn't have password prompt
debconf-set-selections &lt;&lt;&lt; "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASS"
debconf-set-selections &lt;&lt;&lt; "mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASS"

## install the required packages to run
apt-get -y install apache2 install libapache2-mod-php5 install libapache2-mod-auth-mysql install php5-mysql
apt-get -y install mysql-server

## download and extract wordpress
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

## sets up variables for wordpress installation
MYSQL_DB=wordpress$(echo "$RANDOM")
MYSQL_USER=wordpress$(echo "$RANDOM")
MYSQL_USER_PASS="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)"

## creates a .my.cnf so you can run mysql from the command line without password prompt
printf "[mysql]\nuser=root\npassword=\""$MYSQL_ROOT_PASS"\"\n" &gt; ~/.my.cnf

## adds a wordpress user with own password and creates database for wordpress
mysql --defaults-file=~/.my.cnf -e "create database $MYSQL_DB; create user "$MYSQL_USER"@localhost; set password for "$MYSQL_USER"@localhost = PASSWORD(\""$MYSQL_USER_PASS"\"); GRANT ALL PRIVILEGES ON "$MYSQL_DB".* TO "$MYSQL_USER"@localhost IDENTIFIED BY '"$MYSQL_USER_PASS"'; flush privileges;"

## removes the .my.cnf file which contains mysql's root password
rm -r ~/.my.cnf

## sets up wordpress to use the newly created user and password
cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php
sed -i s/database_name_here/$MYSQL_DB/ ~/wordpress/wp-config.php
sed -i s/username_here/$MYSQL_USER/ ~/wordpress/wp-config.php
sed -i s/password_here/$MYSQL_USER_PASS/ ~/wordpress/wp-config.php

## puts wordpress in the appropriate place and changes permissions
mv wordpress /var/www/
sudo chown www-data:www-data /var/www/wordpress -R

## configures apache to serve wordpress as the site root
cp /etc/apache2/sites-available/default ./default.bak
sed -i s/webmaster@localhost/$SERVER_ADMIN_EMAIL/ /etc/apache2/sites-available/default
sed -i s@/\var\/www@${DOCUMENT_ROOT}@ /etc/apache2/sites-available/default
service apache2 reload

## removes the password used to do an unattended install of mysql-server
echo PURGE | debconf-communicate mysql-server

## browse to this URL to configure the wordpress install
echo "browse to the url /wp-admin/install.php to configure wordpress"
This line is long and hard to read with many commands jammed inside:


mysql --defaults-file=~/.my.cnf -e "create database $MYSQL_DB; create user "$MYSQL_USER"@localhost; set password for "$MYSQL_USER"@localhost = PASSWORD(\""$MYSQL_USER_PASS"\"); GRANT ALL PRIVILEGES ON "$MYSQL_DB".* TO "$MYSQL_USER"@localhost IDENTIFIED BY '"$MYSQL_USER_PASS"'; flush privileges;"



A more readable way to write this:

cat &lt;&lt; EOF | mysql --defaults-file=~/.my.cnf 
create database $MYSQL_DB;
create user "$MYSQL_USER"@localhost;
set password for "$MYSQL_USER"@localhost = PASSWORD(\""$MYSQL_USER_PASS"\");
GRANT ALL PRIVILEGES ON "$MYSQL_DB".* TO "$MYSQL_USER"@localhost IDENTIFIED BY '"$MYSQL_USER_PASS"';
flush privileges;
EOF


Drop the -r here, as that's useful for recursively removing directories, but you have a simple file here:

rm -r ~/.my.cnf


It seems the script is designed to setup a single WordPress site per system. It would be useful to extract the logic of conducting a WordPress site, so that you could setup multiple sites per system easily if needed.