Making a bash script to install WordPress – automated installation


In this post, we will make a custom Script in bash to install WordPress  automatically.

With only run the script , one line in the command shell, and entering the root and mysql passwords  you will get a wordpress installation running up.

Bash , is a language to run in the GNUlinux command line.

We will put the code in a box , then we will detail the code .

We made this test on UBUNTU 14.04

Create the script

With your favorite text editor , create a text file suffixed .SH . Example


Write this content into the

#!  /bin/bash

First line indicates that is a bash script and must be interpreted in this language.

Running your scripts

When the script will be you need to run your script , you need to execute it from the command line in this way

shell> sudo bash mysitename
# script will expect this parameter

The # sign is used to make comments in the code.

$1 indicates the first parameter passed to the script when this is invocated from the command line . It is the sitename .

In this case the first script parameter  is assigned to the variable SITENAME .

#before installing you must check if this values are right
#user with permissions on wordpress folder
#database config
#subfolder for wordpress
#apache config

We will define some variables to use during the script . User must change some of them according to their needs.

USEROWNER: Which user will have permissions on wp folders.

HOSTFOLDER: where files will be physically stored.

USERNAME: This is for my sql. will be “wp_”+ sitename , you can change it. The same for DBNAME, the mysql database name.

The form “wp_1″$1 makes an addition of a string (“wp_1″) and $1 the first argument in the command line.

SITEFOLDER= the subfolder wher WordPress will be stores. Path will be  /MYSITE/BLOG In this example. Then you can use /MYSITE/CRM , etc.

SITHEPATH =  is an addition of variables $HOSTFOLDER$SITENAME”/” . The serial is an addition of variables and strings . Identifies the full route of the site.

SITEPATH2 = Is the same as SITEPATH minus the last slash.

APACHEPATH= the route of apache . It is Needed to setup apache.

ADMINMAIL = Administrator email required for apache setup.

SERVERNAME =  Your server name for apache.

#check command line parameter
if [ -n "$1" ]
echo "Starting WordPress installation... "
echo "Site name is empty"

If is a condition . In this case, if our fist parameter is not null (-n) then shows a message “Starting WordPress installation…” with ECHO.

Else shows “Site name is empty” and finish the script with exit. Finnaly end with FI

Setting up apache

#setting up apache config file

sed -i '4i\\ ServerName '$SERVERNAME $APACHEPATH$SITENAME.conf
sed -i s/"webmaster@localhost"/$ADMINMAIL/g $APACHEPATH$SITENAME.conf
sed -i s/'AllowOverride None'/'AllowOverride All'/g $APACHEPATH$SITENAME.conf
sed -i '1,11s|'$ORIGPATH2'|'$SITEPATH2'|g' $APACHEPATH$SITENAME.conf
sed -i s/'error.log'/$SITENAME$SITEFOLDER'.error.log'/g $APACHEPATH$SITENAME.conf
sed -i s/'access.log'/$SITENAME$SITEFOLDER'.access.log'/g $APACHEPATH$SITENAME.conf

ORIGPATH1 and ORIGIPATH2 = are variables to change in the original apache configuration. This file is /etc/apache2/sites-availabe/default  (or 000-default.conf)

with cp $APACHEPATH”default” $APACHEPATH$SITENAME.conf  we make a a copy of the default for customizations adding the sitename.

sed  is for search a string in a file and change its value.

sed -i ‘4i\\ ServerName ‘$SERVERNAME $APACHEPATH$SITENAME.conf  adds a line between line 3 and four . The line contains the directive ServerName adding the server name from the variable $SERVERNAME (when we refer a variable , we must start with $ sign.) and saves in the file referenced  by $APACHEPATH and $SITENAME.conf . The -i parameter saves the changes in the same file. and 4i  sets the row to insert the new line.

sed -i ‘1,5s|’$ORIGPATH1’|’$SITEPATH’|g’ $APACHEPATH$SITENAME.conf  . The parameter 1,5s  sets the change between lines 1 and 5 . changes the original path \var\www  with $SITEPATH , the new route of the site.

sed -i s/”webmaster@localhost”/$ADMINMAIL/g $APACHEPATH$SITENAME.conf . Changes the default admin mail with the variable $ADMINMAIL

sed -i s/’AllowOverride None’/’AllowOverride All’/g $APACHEPATH$SITENAME.conf

sed -i ‘1,11s|’$ORIGPATH2’|’$SITEPATH2’|g’ $APACHEPATH$SITENAME.conf  Also changes the default directory with the new path ($SITEPATH2)

sed -i s/’error.log’/$SITENAME$SITEFOLDER’.error.log’/g $APACHEPATH$SITENAME.conf
sed -i s/’access.log’/$SITENAME$SITEFOLDER’.access.log’/g $APACHEPATH$SITENAME.conf   . This two lines changes the sites log files .

#activating site and restarting service
a2ensite $SITENAME.conf
service apache2 reload

This two lines make : 1) activate the new site ($SITENAME) and 2) reload the apache services

#creating new site folder
#cp templates

mkdir $HOSTFOLDER$SITENAME  , creates a new directory for the site with the $SITENAME

chown -R $USEROWNER:www-data $HOSTFOLDER$SITENAME  –> change permissions for the user who will have access ($USEROWNER) to the folder ($HOSTFOLDER$SITENAME)

cp .htaccess $HOSTFOLDER$SITENAME  –> make a copy of .htaccess to the new site
cp wppatch.php $HOSTFOLDER$SITENAME –> also copy the file wppatch.php .This file will be used later.

tar -xvzf $HOSTFOLDER$SITENAME/latest.tar.gz
mv wordpress $SITEFOLDER
rm latest.tar.gz

wget  , go to the site folder and download with wget the latest WordPress install file.

tar -xvzf $HOSTFOLDER$SITENAME/latest.tar.gz
mv wordpress $SITEFOLDER
rm latest.tar.gz

tar -xvzf $HOSTFOLDER$SITENAME/latest.tar.gz
mv wordpress $SITEFOLDER   –> tar uncompress the file and change the name of the folder to the value of $SITEFOLDER

rm latest.tar.gz –>  removes the downloaded  file
chown -R $USEROWNER:www-data $HOSTFOLDER$SITENAME  –>  change the folder permissions .


cp $SITEPATH"wp-config-sample.php" $SITEPATH"wp-config.php"
chown -R $USEROWNER:www-data $HOSTFOLDER$SITENAME"wp-config.php"
chmod 775 $SITEPATH"wp-config.php"

cp $SITEPATH”wp-config-sample.php” $SITEPATH”wp-config.php”  –> copy the wp-config-sample.php to wp-config.php (this file, doesnt exist by default. So we need to copy to modify)

chown -R $USEROWNER:www-data $HOSTFOLDER$SITENAME”wp-config.php”  –> Change ownership , so WP can make changes .

#create mysql database
echo "MySql password please:"
read -s MYSQLPSW
mysqladmin -uroot --password=$MYSQLPSW create $DBNAME
echo "database name:"$DBNAME > $SITENAME.log

read -s MYSQLPSW –> read in the MYSQLPSW the password for MYSQL. The -s parameter reads the string without show the password.

mysqladmin -uroot –password=$MYSQLPSW create $DBNAME –> creates the database with the name specified before ($DBNAME)

echo “database name:”$DBNAME > $SITENAME.log –> puts the database info in a logfile (MYSITE.LOG)


#grant permissions with a new random password
NEWPASS=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8`
mysql -uroot --password=$MYSQLPSW $DBNAME --execute="grant all on $DBNAME.* to $USERNAME\@localhost identified by '"$NEWPASS"'"
echo "database user:"$USERNAME >> $SITENAME.log
echo "database pass:"$NEWPASS >> $SITENAME.log

NEWPASS=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8`  –>  stores a random password in the var NEWPASS , use the left apostrophes  `

mysql -uroot –password=$MYSQLPSW $DBNAME –execute=”grant all on $DBNAME.* to $USERNAME\@localhost identified by ‘”$NEWPASS”‘” –> grant permissions to username with the random password ($NEWPASS)

#update wp-config.php
sed -i s/database_name_here/$DBNAME/g $SITEPATH"wp-config.php"
sed -i s/username_here/$USERNAME/g $SITEPATH"wp-config.php"
sed -i s/password_here/$NEWPASS/g $SITEPATH"wp-config.php"

sed -i s/database_name_here/$DBNAME/g $SITEPATH”wp-config.php”  –>  like in previous steps, In this 3 lines , changes the file wp-config.php , replaces default values with customized.

#add patch to download themes
cat $SITEPATH"wp-config.php" $HOSTFOLDER$SITENAME"/wppatch.php" > $SITEPATH"wp-config2.php"
mv $SITEPATH"wp-config.php" $SITEPATH"wp-config.bak"
mv $SITEPATH"wp-config2.php" $SITEPATH"wp-config.php"

cat $SITEPATH”wp-config.php” $HOSTFOLDER$SITENAME”/wppatch.php” > $SITEPATH”wp-config2.php” –> concatenates to wp-config.php the patchfile (wppatch.php) and saves it in wp-config2.php

mv $SITEPATH”wp-config.php” $SITEPATH”wp-config.bak”
mv $SITEPATH”wp-config2.php” $SITEPATH”wp-config.php”– > puts the content of wp-config2.php to wp-config.php



Written by

Ariel Campos

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>