Deploy Magento 2 in Amazon EC2

Ayesha Jayasankha
3 min readJul 14, 2022

What is magento 2

Magento 2 is an open source E-commerce platform. It allows you to create and customize your own e-store and carry out activities on that.

What are the prerequisites to install magento 2

  • Nginx server
  • Apache server
  • Php (7.0 /7.1 /7.2 /7.3)
  • Composer
  • Mysql 5.7

In this article I am explaining how we can install magento 2 on Amazon EC2. For that you need an account in AWS. you can create an account using https://aws.amazon.com/console/. In amazon we don’t have apt or apt-get support. Therefore we can use yum as an alternative.

Install httpd in the amazon EC2 using yum

sudo yum install httpdsudo systemctl start httpd

You can run systemctl status httpd

img 1 — http server status

Install php 7.2 using

sudo amazon-linux-extras | grep php
img 2 — available php servers

If php 7.2 is showing enable that otherwise,

sudo yum clean metadatasudo yum install php72 php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap}

Verify the install php version using php -v

Install composer

You can use the following commands to install composer on EC2.

sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
sudo ln -s /usr/local/bin/composer /usr/bin/composer

Start mysql 5.7 server

You can install mysql in your EC2 or you can use an externally hosted mysql server to connect with magento 2. You need to install or use the mysql 5.7 version. If you plan to use an external mysql server you can use RDS which is provided by amazon for that.

Install magento 2

Download magento tar from https://magento.com/tech-resources/download

cd /var/www/htmlsudo mkdir magentosudo tar -zxvf ~/Downloads/<file name> -C ./magentocd magentosudo composer installsudo chmod -R 755 /var/www/html/magento

Configuration files

Nginx conf file

Create magento.conf file in the /etc/nginx/conf.d and add the following file to there. For that you need to enter your generated certificate and keys to the /etc/nginx/ssl/ path.

upstream fastcgi_backend {server unix:/run/php-fpm/magento.sock;}server {proxy_busy_buffers_size 512k;proxy_buffers 4 512k;proxy_buffer_size 256k;fastcgi_buffers 16 32k;fastcgi_buffer_size 64k;fastcgi_busy_buffers_size 64k;listen 443 ssl;server_name <your dns name>(ex: magento.test.com);ssl_certificate /etc/nginx/ssl/<cert pem file>;ssl_certificate_key /etc/nginx/ssl/<private key pem file>;set $MAGE_ROOT /var/www/html/magento;set $MAGE_MODE developer; # or productionaccess_log /var/log/nginx/magento.com-access.log;error_log /var/log/nginx/magento.com-error.log;include /var/www/html/magento/nginx.conf.sample;}

Create a new user with a user name with magento and assign it to the nginx user group. And change the ownership of the directory to the new user.

sudo chown -R magento /var/www/html/magento

php-pfm conf file

Create a magento.conf file in the /etc/php-fpm.d/ directory.

[magento]user = magentogroup = nginxlisten.owner = magentolisten.group = nginxlisten = /run/php-fpm/magento.sockpm = ondemandpm.max_children = 50pm.process_idle_timeout = 10spm.max_requests = 500

Then restart the php-fpm server using systemctl restart php-fpm.service

Restart the nginx service using systemctl restart nginx

Install the magento 2 using

cd <magento home>bin/magento setup:installbin/magento sampledata:deploybin/magento setup:upgrade

If it is asking for authentication,

Login to https://www.magentocommerce.com/magento-connect/customer/account/login/

Go to developers->Secure keys and generate new key or use existing key. You can use public key as Username and Private key as Password

img 3 — magento 2 access keys

Create a new user for magento

Execute the following command in the command line to create a new admin user.

bin/magento admin:user:create

It will prompt you to enter username, password, email, first and last name.

After that you can use your own host entry (ex: magento.example.com) to connect to your magento store.

External references

--

--