Skip to content

Instantly share code, notes, and snippets.

@igoralves1
Last active January 26, 2019 00:03
Show Gist options
  • Save igoralves1/62bb5723d4b5acc2df46924232948bdc to your computer and use it in GitHub Desktop.
Save igoralves1/62bb5723d4b5acc2df46924232948bdc to your computer and use it in GitHub Desktop.
Install et Setup Magento 2.3 on AWS Ubuntu 16.04, EC2 - 4CPU + 32Gb
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
echo ""
echo "0 - Variables"
echo " "
BASE_URL='http://xxx-xx-xxx-xxx-xx.us-east-2.compute.amazonaws.com/'
DB_USER='xxx'
DB_USER_PASSWORD='xxx'
DB_MAGENTO='db_magento'
MAGENTO_USER_FISTNAME='xxx'
MAGENTO_USER_LASTNAME='xxx'
MAGENTO_USER_EMAIL='[email protected]'
MAGENTO_USER='xxx'
MAGENTO_USER_PASSWORD='xxx'
SERVER_NAME='example.com'
WITHSAMPLEDATA=1 #0 or 1
PRJ_MAGENTO='mag'
PRJ_MAGENTO_PUBK=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PRJ_MAGENTO_PRVK=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
MAGENTO_SAMPLEDATA=''
if [ "$WITHSAMPLEDATA" -eq "1" ];
then
MAGENTO_SAMPLEDATA='--use-sample-data'
fi
echo " "
echo "1 - Install nginx"
echo " "
sleep 2s
sudo apt-get update
sudo apt-get install -y nginx
echo " "
echo "#2 - Create vhosts/mag into home directory."
echo " "
sleep 2s
cd ~/
mkdir -p ~/vhosts
echo " "
echo "#3 - Create a soft link from /var/www to ~/vhosts"
echo " "
sleep 2s
cd /var/www/
sudo ln -s ~/vhosts
echo " "
echo "#4 - Install php 7.1 and modules to Laravel and Magento"
echo " "
sleep 2s
sudo apt-get install -y software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.1-fpm
sudo apt-get install -y php7.1
//sudo apt-get install -y php7.1-dev
//PHP Modules:
//Laravel
sudo apt-get install -y php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-fpm php7.1-xml php7.1-xmlrpc php-token-stream
//Magento 2.3
sudo apt-get install -y php7.1-bcmath php7.1-curl php7.1-gd php7.1-intl php7.1-soap
echo " "
echo "#5 - Install MySQL "
echo " "
sleep 2s
## https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
## https://serverfault.com/questions/783527/non-interactive-silent-install-of-mysql-5-7-on-ubuntu-16-04-lts/783528
sudo apt-get update
# Install MySQL 5.7 - Non-interactive (silent)
# Suggestion from @dcarrith (http://serverfault.com/a/830352/344471):
echo debconf mysql-server/root_password password $DB_USER_PASSWORD | sudo debconf-set-selections
echo debconf mysql-server/root_password_again password $DB_USER_PASSWORD | sudo debconf-set-selections
#sudo debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password password $MYSQL_ROOT_PASSWORD"
#sudo debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD"
sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly
sudo mysql -uroot -p123 -e "CREATE SCHEMA ${DB_MAGENTO} DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_USER_PASSWORD}';GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User FROM mysql.user;"
#mysql_secure_installation //This is non interactive mode
echo " "
echo "#6 - Install and configure composer"
echo " "
sleep 2s
cd ~/
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/bin/composer
composer
echo " "
echo "#7 - Download magento 2.3 using composer"
echo " "
sleep 2s
cd ~/vhosts
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition $PRJ_MAGENTO
cd ~/vhosts/$PRJ_MAGENTO
php -f ./bin/magento setup:install \
--base-url=$BASE_URL \
--use-secure=0 \
--db-host=127.0.0.1 \
--db-name=$DB_MAGENTO \
--db-user=$DB_USER \
--db-password=$DB_USER_PASSWORD \
--admin-firstname=$MAGENTO_USER_FISTNAME \
--admin-lastname=$MAGENTO_USER_LASTNAME \
--admin-email=$MAGENTO_USER_EMAIL \
--admin-user=$MAGENTO_USER \
--admin-password=$MAGENTO_USER_PASSWORD \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--backend-frontname=admin \
$MAGENTO_SAMPLEDATA
sudo chmod -R 777 ~/vhosts/$PRJ_MAGENTO
cd ~/vhosts/$PRJ_MAGENTO
php bin/magento sampledata:deploy
##Check how to make it silent
#Authentication required (repo.magento.com):
#Username: $PRJ_MAGENTO_PUBK
#Password: $PRJ_MAGENTO_PRVK
#Do you want to store credentials for repo.magento.com in /home/ubuntu/.config/composer/auth.json ? [Yn] y
sudo chmod -R 777 ~/vhosts/$PRJ_MAGENTO
cd ~/vhosts/$PRJ_MAGENTO
php bin/magento setup:upgrade
sudo chmod -R 777 ~/vhosts/$PRJ_MAGENTO
#If you have the follow error - must increase ram memory in AWS
# mmap() failed: [12] Cannot allocate memory
#mmap() failed: [12] Cannot allocate memory
#mmap() failed: [12] Cannot allocate memory
#PHP Fatal error: Out of memory (allocated 657465344) (tried to allocate 67108872 bytes) in phar:///usr/bin/composer/src/Composer/DependencyResolver/RuleSet.php on line 84
#Fatal error: Out of memory (allocated 657465344) (tried to allocate 67108872 bytes) in phar:///usr/bin/composer/src/Composer/DependencyResolver/RuleSet.php on line 84
# From this post https://magento.stackexchange.com/questions/228651/mmap-failed-12-cannot-allocate-memory
#"Magento need 2GB means you should have 8GB ram"
#related but not tested -> https://stackoverflow.com/questions/18116261/php-composer-update-cannot-allocate-memory-error-using-laravel-4
#If no errors -> magento smaple data and magento modules will be installed.
echo " "
echo "#7 - Setting up NGINX para Magento"
echo " "
sleep 2s
echo "Display ls -l /etc/nginx/sites-available/"
cd /etc/nginx/sites-available/
ls -l
echo " "
sleep 5s
echo "Deleteing /etc/nginx/sites-available/default"
sudo rm -Rf /etc/nginx/sites-available/default
echo "Display ls -l /etc/nginx/sites-available/. No default"
cd /etc/nginx/sites-available/
ls -l
echo " "
sleep 5s
echo "Creating a new /etc/nginx/sites-available/default"
sudo touch /etc/nginx/sites-available/default
echo "Display ls -l /etc/nginx/sites-available/. New default"
cd /etc/nginx/sites-available/
ls -l
echo " "
sleep 5s
echo "NOTE: Original was /etc/nginx/sites-available/deault => -rw-r--r-- 1 root root 1534 Dec 31 15:37 default"
cat >/etc/nginx/sites-available/default << EOL
upstream fastcgi_backend {
server unix:/run/php/php7.1-fpm.sock;
}
server {
listen 80;
server_name ${SERVER_NAME};
# oRIGINAL set $MAGE_ROOT /var/www/vhosts/...;
set \$MAGE_ROOT /var/www/vhosts/${PRJ_MAGENTO};
include /var/www/vhosts/${PRJ_MAGENTO}/nginx.conf.sample;
}
EOL
sudo service nginx restart
sudo chmod -R 777 ~/vhosts/$PRJ_MAGENTO
cd ~/vhosts/$PRJ_MAGENTO
sudo php bin/magento cron:install
sudo crontab -l
sudo php bin/magento cron:run
sudo chmod -R 777 ~/vhosts/$PRJ_MAGENTO
echo "End"
echo " "
sleep 2s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment