Article posted Sat Feb 25 2023 Updated Wed Mar 15 2023

Scripts: Single Script Deployment with Magento and Apache

The script will prompt you for the MySQL root password and the Magento database user password, and will automate the entire installation process, including the configuration of Apache and MySQL. Once the script has finished running, you should be able to access your new Magento installation by navigating to whatever base-url you have set.

Please ensure you update the following information before attempting to run this script

  • --base-url=http://localhost/
  • --admin-email=admin@example.com
  • --admin-user=admin
  • --admin-password=admin123
  • --language=en_US
  • --currency=USD
  • --timezone=America/Chicago

The latest version of Magento (2.4.5), requires PHP 8.1 and MySQL 8. Magento 2.4.6 supports PHP 8.2.

php -v

This command will print the current version of PHP used, however be mindful this is also used by Apache.

Please only use these scripts as a reference and starting block for your own scripts. This information is provided as an example of how someone could achieve automation using Magento 2 and Apache.
#!/bin/bash

# Install prerequisites
apt-get update
apt-get install -y apache2 mysql-server php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip unzip

# Download Magento
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
mkdir /var/www/html/magento
cd /var/www/html/magento
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

# Configure MySQL
read -p "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE magento;"
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'password';"
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "FLUSH PRIVILEGES;"

# Configure Apache
cat > /etc/apache2/sites-available/magento.conf <<EOF
<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /var/www/html/magento
  <Directory /var/www/html/magento>
    AllowOverride All
  </Directory>
</VirtualHost>
EOF

# Enable new Apache configuration
a2ensite magento.conf
systemctl restart apache2

# Install Magento
cd /var/www/html/magento
read -p "Enter MySQL password for magento user: " MAGENTO_DB_PASSWORD
bin/magento setup:install --base-url=http://localhost/ \
--db-host=localhost --db-name=magento --db-user=magento --db-password=$MAGENTO_DB_PASSWORD \
--admin-firstname=Admin --admin-lastname=User --admin-email=admin@example.com \
--admin-user=admin --admin-password=admin123 --language=en_US --currency=USD \
--timezone=America/Chicago --use-rewrites=1