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 installerDOCUMENT_ROOT=/var/www/wordpressMYSQL_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 fileecho Enter in the email for the server administrator:read SERVER_ADMIN_EMAILapt-get updateapt-get upgrade## Set up passwords so mysql-server install doesn't have password promptdebconf-set-selections <<< mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSdebconf-set-selections <<< mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASS## install the required packages to runapt-get -y install apache2 install libapache2-mod-php5 install libapache2-mod-auth-mysql install php5-mysqlapt-get -y install mysql-server## download and extract wordpresswget http://wordpress.org/latest.tar.gztar -xzvf latest.tar.gz## sets up variables for wordpress installationMYSQL_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 promptprintf [mysql]\nuser=root\npassword=\$MYSQL_ROOT_PASS\\n > ~/.my.cnf## adds a wordpress user with own password and creates database for wordpressmysql --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 passwordrm -r ~/.my.cnf## sets up wordpress to use the newly created user and passwordcp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.phpsed -i s/database_name_here/$MYSQL_DB/ ~/wordpress/wp-config.phpsed -i s/username_here/$MYSQL_USER/ ~/wordpress/wp-config.phpsed -i s/password_here/$MYSQL_USER_PASS/ ~/wordpress/wp-config.php## puts wordpress in the appropriate place and changes permissionsmv wordpress /var/www/sudo chown www-data:www-data /var/www/wordpress -R## configures apache to serve wordpress as the site rootcp /etc/apache2/sites-available/default ./default.baksed -i s/webmaster@localhost/$SERVER_ADMIN_EMAIL/ /etc/apache2/sites-available/defaultsed -i s@/\var\/www@${DOCUMENT_ROOT}@ /etc/apache2/sites-available/defaultservice apache2 reload## removes the password used to do an unattended install of mysql-serverecho PURGE | debconf-communicate mysql-server## browse to this URL to configure the wordpress installecho browse to the url /wp-admin/install.php to configure wordpress
Automating a WordPress install
mysql;bash;linux;wordpress;installer
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 << 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;EOFDrop the -r here, as that's useful for recursively removing directories, but you have a simple file here:rm -r ~/.my.cnfIt 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.