Magento 2 Setup Script

Summary

The main set up file, will set up the database and source a couple scripts to help with the process. Make sure you set up all the scripts correctly in the same working directory.

The scripts assume that they are placed in a shellscripts directory within the Magento 2 root directory (not pub).

Instructions

Make a directory call shellscripts in the Magento 2 root and create each of the following files in the new directory.

To run the scripts cd in to the directory and run setup.bash.

If a database already exists then it will be dropped and recreated.

setup.bash

#!/usr/bin/env bash
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
cd $DIR;

source ./_top.inc.bash;

detailsFile="$DIR/mageDetails.txt"
authJson="$DIR/../auth.json";



if [[ -f $detailsFile ]]
then
    echo "Found existing details file, aborting";
    exit 1;
fi

if [ -f $authJson ]
then
    echo "Auth json detected, continuing";
else
    echo "auth.json is missing, it is required to download composer dependencies";
    exit 1;
fi;

echo "Details file not found, continuing install process...";

echo "Cleaning var directories";
rm -rf $dirVar/*;

repoPublicKey="YOUR_MAGENTO2_PUBLIC_KEY";
repoPrivateKey="YOUR_MAGENTO2_PRIVATE_KEY";

#Defaults
adminFName="admin_firstname_here";
adminLName="admin_lastname_here";
adminUName="myadminusername";
adminPass="2p$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c7;echo;)";
adminEmail="info@yourdomain.com";

dbPrefix="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6;echo;)_";

storeLanguage="en_GB";
storeCurrency="GBP";
storeTimezone="Europe/London";

urlSecure="0";
urlRewrites="1";

phpCommand="php";

source ./env.inc.bash

#Set up the database
echo "Recreating database $dbName ...";
mysql -e "DROP DATABASE IF EXISTS $dbName" && mysql -e "CREATE DATABASE $dbName";

echo "Composer dependencies ...";
cd ../;
composer install;
cd $DIR;


echo "Installing Magento 2 ...";
$phpCommand ../bin/magento setup:install \
    --admin-firstname "$adminFName" \
    --admin-lastname "$adminLName" \
    --admin-email "$adminEmail" \
    --admin-user "$adminUName" \
    --admin-password "$adminPass" \
    --db-host $dbHost \
    --db-name $dbName \
    --db-user $dbUser \
    --db-password $dbPass \
    --db-prefix "$dbPrefix" \
    --base-url "$urlBase" \
    --use-secure "$urlSecure" \
    --backend-frontname "$urlAdmin";

echo "Username: $adminUName Password: $adminPass" > "$DIR/mageDetails.txt";

bash "$DIR/fixpermissions.bash";

fixpermissions.bash

#!/usr/bin/env bash

if [[ -z "$DIR" ]]
then
    readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
fi

cd $DIR;
source ./_top.inc.bash;


mkdir -p "$dirVar/page_cache";

echo "Finding files to update permissions ...";

function fixMagentoPermissionsForDir()
{
    local dirChange=$1;
    chmod -R 777 "$dirChange"; #TODO Replace with chown and env file for web user
}

arrDirToChange=(
"$dirVar/page_cache"
"$dirVar/cache"
"$dirVar/cache/*"
"$dirVar/generation"
"$dirVar/generation/*"
"$dirVar/log"
"$dirVar/log/*.log"
);

for d in "${arrDirToChange[@]}"
do
    echo "Fixing: $d";
    fixMagentoPermissionsForDir "$d";
done
 

_top.inc.bash

#!/usr/bin/env bash

set -e
set -u
set -o pipefail
standardIFS="$IFS"
IFS=$'\n\t'
echo "
===========================================
$(hostname) $0 $@
===========================================
"


#### VARIABLES ####
dirProjectRoot="$(realpath $DIR/../)";
dirVar="$dirProjectRoot/var";

env.inc.dist (rename to env.inc and add overrides for environments where needed)

#!/usr/bin/env bash

# Copy this and rename .dist to .bash
dbHost="localhost"
dbUser="root"
dbPass=""
dbName="magento";
urlBase="";
urlAdmin="admin";

Tags: magento2 bash