Read More
Menu Close

    Shell/Bash Script to Install Magento 2

    It is easy to customize things according to your need. So if you are still installing the Magento by typing commands on your terminal it’s time to quit this process.
    You can write your own shell script which will allow you to install your Magento just by running a single command on your terminal.

    Using this script you can create your Database, install your Magento with sample data. The only thing is required you must have a Magento Store account to download the files from Magento using composer.

    Steps are as follows-
    1) Write a script
    2) The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows: chmod 755
    3) Now you need to run your script using the command- ./

    Please find the script below-

    # color coding.
    red=`tput setaf 1`
    green=`tput setaf 2`
    yellow=`tput setaf 3`
    cyan=`tput setaf 6`
    reset=`tput sgr0`
    bg=`tput setab 7`
    # All Required Variables
    [email protected]
    #Creating Database
    echo "Creating Database..........."
    echo "${cyan}Enter Your DataBase name I'll create your DB${reset}"
    read db_name
    # DB existance check
    db_exists=$(mysql -u $db_user -p$db_password -e "SHOW DATABASES LIKE '"$db_name"';" | grep "$db_name" > /dev/null; echo "$?")
    if [ $db_exists -eq 0 ];then
        echo "${red}${bg}A database with the name $db_name already exists${reset}"
         echo "${yellow}Creating database $db_name.${reset}"
    mysql -u $db_user -p$db_password -e "create database $db_name;"
    echo "${green} Database $db_name created${reset}"
    sleep 1
    #Install Magento
    echo "${yellow}Installing Magento...........${reset}"
    composer create-project --repository-url= magento/project-community-edition
    sleep 1
    #Rename donloaded folder name (change required)
    echo "${cyan}Enter your magento name${reset}"
    read magento_directory
    echo "${yellow}Magento name is $magento_directory ${reset}"
    mv project-community-edition $magento_directory
    #Go inside your folder (change required)
    cd $magento_directory
    #Give permissions
    echo "${yellow}Giving permissions........${reset}"
    find . -type f -exec chmod 644 {} \;
    find . -type d -exec chmod 755 {} \;
    #Install Magento
    echo "${yellow}Installing Magento........${reset}"
    php bin/magento setup:install --base-url=http://localhost/$magento_directory/ --db-host=localhost --db-name=$db_name --db-user=$db_user --db-password=$db_password --admin-firstname=admin --admin-lastname=admin --admin-email=$email --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
    #Installing sample data
    echo "${yellow}Installing sample data and updating magento.......${reset}"
    php bin/magento sampledata:deploy; php bin/magento setup:upgrade; php bin/magento setup:di:compile; php bin/magento setup:static-content:deploy; php bin/magento cache:clean; php bin/magento indexer:reindex; php bin/magento cache:flush
    # User input to edit backend URL
    echo "${cyan}Do you want to edit your backend url? yes/no${reset}"
    read backend_url
    if [ "$backend_url" = yes -o y -o Yes -o Y ]; then
        admin_url=$(grep -E "admin_+" app/etc/env.php)
        sed -i "s/$admin_url/'frontName' => 'admin',/g" app/etc/env.php
        echo "${green}Backend url has been set. Your Magento is ready to use now...) ${reset}"
        echo "${green}Your Magento is ready to use now...) ${reset}"

    Please update some values in the script before running it. Like- I have set dummy DB username (db_user) and DB password (db_password). Some Magento related data are already set like – base URL, Db host, admin first name, last name,  email id, password etc which are required for the Magento installation.

    Searching for an experienced
    Magento 2 Company ?
    Read More

    You can add validations to your script and do required changes according to your need. Hope it will help you.

    Thank You! 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

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

    Be the first to comment.

    Back to Top