Bash script to setup WordPress for Mamp
#!/bin/bash
clear
# ----------------------------------------------------------------
# Variables
# ----------------------------------------------------------------
# Get current directory name
# ---------------------------------
currentPath=${PWD##*/}
# Generate random 12 character password
# ---------------------------------
defaultPassword=$(LC_CTYPE=C tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c 24)
# Operating System
# ---------------------------------
case "$OSTYPE" in
darwin*) OS="OSX" ;;
msys*) OS="WINDOWS" ;;
*) OS="unknown: $OSTYPE" ;;
esac
# OSX
if [ "$OS" = "OSX" ]; then
dbPath="/Applications/MAMP/Library/bin/"
fi
# Windows
if [ "$OS" = "WINDOWS" ]; then
dbPath="C:\\MAMP\\bin\\mysql\\bin\\"
fi
# Colors
# ---------------------------------
yellow=`tput setaf 3`
cyan=`tput setaf 6`
red=`tput setaf 1`
nc=`tput sgr0`
bell=`tput bel`
echo "$yellow=================================================================================$cyan"
echo "WordPress Installer for MAMP!!"
echo "$yellow=================================================================================$nc"
echo ""
# ----------------------------------------------------------------
# Functions
# ----------------------------------------------------------------
# Prompt function
# ---------------------------------
yesNoPrompt() {
while :
do
read -p "$1" answer
case "${answer}" in
[nN]|[nN][oO]) exit 0 ;;
[yY]|[yY][eE][sS]) exit 1 ;;
esac
done
}
# ----------------------------------------------------------------
# Site Info
# ----------------------------------------------------------------
# Set the site's name
# ---------------------------------
read -p "Site Name ( $currentPath ) : " siteName
# If $siteName is left blank then use current directory name
if [ -z "$siteName" ]; then
siteName=$currentPath
siteNameC=`echo "$currentPath" | awk '{print tolower($0)}' | sed 's/ /-/g'`
else
siteNameC=`echo "$siteName" | awk '{print tolower($0)}' | sed 's/ /-/g'`
fi
# Set the site domain. Default is $siteName
# ---------------------------------
read -p "Site Domain ( $siteNameC.dev ) : " siteDomain
# If $siteDomain is left blank then use $sitenameC
if [ -z "$siteDomain" ]; then
siteDomain=$siteNameC.dev
fi
# ----------------------------------------------------------------
# Database Info
# ----------------------------------------------------------------
# Set database name
# ---------------------------------
read -p "Database Name. Enter 1 to skip database creation ( $siteNameC ) : " databaseName
# If $databaseName is left blank then use $sitenameC
if [ -z "$databaseName" ]; then
databaseName=$siteNameC
fi
if [ "$databaseName" != '1' ]; then
# Set database username
# ---------------------------------
read -p "Database User ( root ) : " databaseUser
# If $databaseUser is left blank then use "root"
if [ -z "$databaseUser" ]; then
databaseUser="root"
fi
# Set database password
# ---------------------------------
read -p "Database Password ( root ) : " databasePassword
# If $databasePassword is left blank then use $defaultPassword
if [ -z "$databasePassword" ]; then
databasePassword=root
fi
# Set database host
# ---------------------------------
read -p "Database Host ( localhost ) : " databaseHost
# If $databaseHost is left blank then use "localhost"
if [ -z "$databaseHost" ]; then
databaseHost="localhost"
fi
fi
# ----------------------------------------------------------------
# WordPress Info
# ----------------------------------------------------------------
# Set WordPress version
# ---------------------------------
read -p "WordPress Version ( latest ) : " wpVersion
# If $wpVersion is left blank then use "latest"
if [ -z "$wpVersion" ]; then
wpVersion="latest"
fi
if [ $wpVersion != "latest" ]; then
wpVersion="wordpress-"$wpVersion""
fi
# Set WordPress table prefix
# ---------------------------------
read -p "WordPress Table Prefix ( wp_ ) : " wpPrefix
# If $wpPrefix is left blank then use "latest"
if [ -z "$wpPrefix" ]; then
wpPrefix="wp_"
fi
# Set Enable WP_DEBUG
# ---------------------------------
if $( yesNoPrompt "Enable WP_DEBUG? ( y/n ): " ); then
wpDebug="no"
else
wpDebug="yes"
fi
if [ "$databaseName" != '1' ]; then
# Set Database Import
# ---------------------------------
if $( yesNoPrompt "Would you like to import a database? ( y/n ): " ); then
dbImport="no"
else
dbImport="yes"
fi
if [ "$dbImport" = yes ]; then
# Import File
read -p "Enter the path to the .sql file : " dbImportFile
# If $wpPrefix is left blank then use "latest"
if [ -z "$dbImportFile" ]; then
dbImportFile="No File Added!"
fi
fi
fi
# Set Content Import
# ---------------------------------
if $( yesNoPrompt "Would you like to import a custom content? ( y/n ): " ); then
contentImport="no"
else
contentImport="yes"
fi
if [ "$contentImport" = yes ]; then
# Import File
read -p "Enter the git repo address : " contentImportFile
# If $wpPrefix is left blank then use "latest"
if [ -z "$contentImportFile" ];
then
contentImportFile="No File Added!"
fi
fi
# ----------------------------------------------------------------
# Conformation Output
# ----------------------------------------------------------------
# Echo the user inputs for verification
echo ""
echo "$red=================================================================================$nc"
echo "Site Name: "$siteName""
echo "Domain: "$siteDomain""
# If database not skipped
# ---------------------------------
if [ "$databaseName" != '1' ]; then
echo "Database Name: "$databaseName""
echo "Database User: "$databaseUser""
echo "Database Password: "$databasePassword""
echo "Database Host: "$databaseHost""
fi
echo "WordPress Version: "$wpVersion""
echo "Table Prefix: "$wpPrefix""
echo "Debug: "$wpDebug""
# If database not skipped
# ---------------------------------
if [ "$databaseName" != '1' ]; then
if [ "$dbImport" = yes ]; then
echo "Database Import File: "$dbImportFile""
fi
fi
if [ "$contentImport" = "yes" ]; then
echo "Git repo address: "$contentImportFile""
fi
echo "$red=================================================================================$nc"
echo ""
# If no restart the script
# If yes continue
# ---------------------------------
if $( yesNoPrompt "Is This Correct? (y/n): " ); then
exec "wpinstall.sh"
fi
# If no exit
# If yes continue
# ---------------------------------
if $( yesNoPrompt "Run the Install? (y/n): " );then
echo "Exit"
echo "$bell"
exit
fi
# ----------------------------------------------------------------
# Begin Install
# ----------------------------------------------------------------
# Download WordPress
# ---------------------------------
echo "Downloading WordPress..."
curl -O https://wordpress.org/$wpVersion.tar.gz
# Unzip WordPress
# ---------------------------------
echo "Unzipping..."
tar -zxvf $wpVersion.tar.gz
# Change dir to wordpress
# ---------------------------------
cd wordpress
# Copy files to parent dir
# ---------------------------------
echo "Moving WordPress Files..."
cp -rf . ..
# Change dir to parent
# ---------------------------------
cd ..
# Create wp-config.php file
# ---------------------------------
echo "Creating wp-config.php..."
cp wp-config-sample.php wp-config.php
rm -R wp-config-sample.php
# ----------------------------------------------------------------
# wp-config.php Configuration
# ----------------------------------------------------------------
# Configure wp-config.php
# ---------------------------------
echo "Configuring wp-config.php..."
# Database Details
# ---------------------------------
perl -pi -e "s'database_name_here'"$databaseName"'g" wp-config.php
perl -pi -e "s'username_here'"$databaseUser"'g" wp-config.php
perl -pi -e "s'password_here'"$databasePassword"'g" wp-config.php
perl -pi -e "s/\'wp_\'/\'$wpPrefix\'/g" wp-config.php
# WP Salts
# ---------------------------------
perl -i -pe'
BEGIN {
@chars = ("a" .. "z", "A" .. "Z", 0 .. 9);
push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|";
sub salt { join "", map $chars[ rand @chars ], 1 .. 64 }
}
s/put your unique phrase here/salt()/ge' wp-config.php
# Debug
# ---------------------------------
debugFind="define\('WP_DEBUG', false\);"
debugReplace="define( 'WP_DEBUG', true ); \nif ( WP_DEBUG ) {\n\tdefine( 'WP_DEBUG_DISPLAY', false );\n\tdefine( 'WP_DEBUG_LOG', true );\n\tdefine( 'SCRIPT_DEBUG', true );\n\tdefine( 'JETPACK_DEV_DEBUG', true );\n\tdefine( 'DEVELOPMENT', true );\n}"
if [ $wpDebug = "yes" ]; then
perl -pi -e "s{$debugFind}{$debugReplace}" wp-config.php
fi
# ----------------------------------------------------------------
# htaccess
# ----------------------------------------------------------------
# Add .htaccess file
# ---------------------------------
echo "Adding .htaccess file..."
# Write to .htaccess
# ---------------------------------
cat > .htaccess <<'EOL'
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EOL
# ----------------------------------------------------------------
# Database Creation and Import
# ----------------------------------------------------------------
if [ "$databaseName" != '1' ]; then
echo "Creating Database..."
# Variables
# ---------------------------------
dbsetup="CREATE DATABASE IF NOT EXISTS \`"$databaseName"\` /*\!40100 DEFAULT CHARACTER SET utf8 */; GRANT ALL PRIVILEGES ON \`"$databaseName"\`.* TO "$databaseUser"@"$databaseHost"; FLUSH PRIVILEGES;"
filename="${dbImportFile##*/}"
# Create the database
# ---------------------------------
$dbPath/mysql -u $databaseUser -p$databasePassword -e "$dbsetup"
# Import file
# ---------------------------------
if [ "$dbImport" = yes ]; then
echo "Importing File: "$dbImportFile""
# Copy file to current directory
cp "$dbImportFile" .
# Import the file
case "$dbImportFile" in
*.gz ) gunzip < $filename | $dbPath/mysql -u $databaseUser -p$databasePassword $databaseName ;;
*.tgz | *.tar.gz ) tar xvzf < $filename | $dbPath/mysql -u $databaseUser -p$databasePassword $databaseName ;;
*) $dbPath/mysql -u $databaseUser -p$databasePassword $databaseName < "$dbImportFile" ;;
esac
rm -r $dbImportFile
fi
fi
# ----------------------------------------------------------------
# Clone repo
# ----------------------------------------------------------------
# Import Git Repo
# ---------------------------------
if [ "$contentImport" = yes ]; then
rm -r wp-content
git clone $contentImportFile wp-content
fi
# ----------------------------------------------------------------
# Cleanup
# ----------------------------------------------------------------
# Remove files from wordpress folder
# ---------------------------------
echo "Cleaning Up Install..."
rm -r wordpress
rm -r $wpVersion.tar.gz
# End
# ---------------------------------
echo "$yellow=================================================================================$cyan"
echo "WordPress Installed!!"
echo "$yellow=================================================================================$nc"