WordPress Install Script – Fast Setup for Ubuntu

WordPress Install Script is a small Bash Script that will help improve the process and have your website ready and installed within seconds.

Installing WordPress locally can be a time consuming job if you have to download WordPress manually from the WordPress.org site, then create a database and configure WordPress to use that database.

And if you want to have nice url like wordpress-site.local or wordpress-site.dev you need to create VirtualHost for each site and this takes a lot of time.

Note: This script assumes that you already have Apache, MySQL and PHP installed. The script also uses WP CLI to install and configure WordPress, so you need to have that installed as well.

This same script can also be used on Macbook with small tweaks for the sites location folder.

If you don’t have WP CLI installed follow the instructions on the WP CLI site to install it.

#! /bin/bash

echo -n "Enter full domain name (e.g. example.com): "
read -e DN
echo -n "Enter a unique 'nice' name for the site (e.g. example): "
read -e SN
if [ -d /etc/apache2/sites-available/$SN ]
    then echo "Error: site already configure using the name $SN";
    exit
fi

echo "Creating Apache Site Configuration File"
sudo echo "<VirtualHost *:80>
        ServerName $DN
        ServerAlias www.$DN
        DocumentRoot /var/www/html/$SN
        <Directory /var/www/html/$SN>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
        ErrorLog  /var/log/apache2/$SN/error.log
        CustomLog /var/log/apache2/$SN/access.log combined
</VirtualHost>" | sudo tee /etc/apache2/sites-available/$SN.conf

echo "Creating Document Root"
sudo mkdir /var/www/html/$SN
sudo chmod -R 777 /var/www/html/$SN

echo "Creating Log File Directory"
sudo mkdir /var/log/apache2/$SN

echo "Enabling Site & Reloading Apache (edit /etc/apache2/sites-available/$SN for more options)"
sudo a2ensite $SN

echo "Adding $DN to hosts file"
echo "127.0.0.1  $DN" | sudo tee -a /etc/hosts

# Switch to the document root
cd /var/www/html/$SN
wp core download

echo "Creating database"
# check to see if MySQL is running, save status in a variable 'UP'
UP=$(pgrep mysql | wc -l);
echo "DB is $UP"
if [ $UP ]
then
    wp config create --dbname=$SN --dbuser=root --dbpass=pass --dbhost="localhost"
    wp db create
else
    sudo service mysql start
    wp config create --dbname=$SN --dbuser=root --dbpass=pass --dbhost="localhost"
    wp db create
fi

echo "Installing WordPress"

wp core install --url="$DN" --title=$SN --admin_user=admin --admin_password=admin123 --admin_email=martin@martincv.com

sudo chmod -R 777 /var/www/html/$SN

sudo apachectl restart
echo "$SN has been created. You can access it on http://$DN"

You can put the script in a file and name it however you want. I have named it wpinstall. Then we need to give it exec permissions so you run

sudo chmod +x /path/to/file/wpinstall

Once you do this, move the file somewhere in your PATH. Example: /usr/local/bin

Now open terminal and type wpinstall

WordPress Install Script

This asks as to enter domain name which is the following line from the script:

echo -n "Enter full domain name (e.g. example.com): "

We enter any domain we like to use. Because we work locally it is best if we use .local or .dev. Example: wordpress-site.local

WordPress Script for WordPress installation

Next we are asked to add unique name for the site. This is the name of the folder where the WordPress will be installed. I usually give the same name as the domain without the .local.

After that you will be prompted for the password to execute sudo commands.

WordPress Apache configuration

After entering the password it will create a VirtualHost in Apache

/etc/apache2/sites-available/$SN

$SN variable holds the site name we added in the 2nd step (wordpress-site).

The following command is exectuted

sudo echo "<VirtualHost *:80>
        ServerName $DN
        ServerAlias www.$DN
        DocumentRoot /var/www/html/$SN
        <Directory /var/www/html/$SN>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
        ErrorLog  /var/log/apache2/$SN/error.log
        CustomLog /var/log/apache2/$SN/access.log combined
</VirtualHost>" | sudo tee /etc/apache2/sites-available/$SN.conf

This inserts the <VirtualHost… into /etc/apache2/sites-available/wordpress-site.conf file.

WordPress host domain entry

After this the domain name is added in the /etc/hosts file

After all this is done the apache server restarts and the new site configuration is read.

Next steps are install WordPress and connecting with the database which is the by the following code which uses WP CLI (wp command)

# Switch to the document root
cd /var/www/html/$SN
wp core download

echo "Creating database"
# check to see if MySQL is running, save status in a variable 'UP'
UP=$(pgrep mysql | wc -l);
echo "DB is $UP"
if [ $UP ]
then
    wp config create --dbname=$SN --dbuser=root --dbpass=pass --dbhost="localhost"
    wp db create
else
    sudo service mysql start
    wp config create --dbname=$SN --dbuser=root --dbpass=pass --dbhost="localhost"
    wp db create
fi

echo "Installing WordPress"

wp core install --url="$DN" --title=$SN --admin_user=admin --admin_password=admin123 --admin_email=martin@martincv.com

First we enter in the folder where we want the WordPress installed and we run

wp core download

This will download WordPress in the folder.

Next we create the database. We check if mysql is running. If it is not we start it and add the wp-config.php file using the command wp config create.

We send as params the database name, user and password and run wp db create which creates the database using the configuration we added.

After all this is done we install WordPress using wp core install command

wp core install --url="$DN" --title=$SN --admin_user=admin --admin_password=admin123 --admin_email=martin@martincv.com

We pass the site url, site title, admin user, admin user password and admin email.

This finishes our installation and we can access the site on http://wordpress-site.local/

Please note that the script uses 777 permissions. This script is meant for local setup of WordPress. I DO NOT recommend using this on production / staging server. If you ever use this on a server, please remove those lines from the script.

You Might Be Interested In