How to Set Up Magento on AWS Without Using AMI

How to Set Up Magento on AWS Without Using AMI

If you are a Magento developer, chances are you may have performed many local setups! You may have also set up Magento development services on many other scratch servers. If yes, Magento on AWS ec2 instance is something that you need to look out for. As we all know AWS has many AMIs that we can use to set up Magento on the EC2 instance but that can be expensive on the budget. 

 

In this article, we will get to know about Magento on AWS ec2 environment variables a bit and how to set up Magento from scratch without using any AMI. This also helps us create our own Magento AMI through which we can create many Magento setups for the future and that too in just 5 to 10 minutes. So let's get started with it!

 

Create an EC2 instance 

Firstly start creating an EC2 instance in your AWS account. Then, get the server up and running. Also, open port 80 for all IP addresses in the security group, or else the store won't be accessible. Now SSH into the server using the generated key file.    

 

Root user login

We need to log in to the root user to avoid any permission issues. The below command switches you to a root user:

 

sudo su  

 

Install PHP

Execute the below commands to update system packages and install PHP 7.4 

 

yum update

yum install amazon-linux-extras

amazon-linux-extras | grep php

amazon-linux-extras enable php7.4

yum clean metadata

 

Required PHP Extensions

 

Install the required PHP extensions. You can review all the required extensions here 

 

yum install php-cli php-pdo php-fpm php-json php-mysqlnd

yum install php php-{dom,simplexml,curl,intl,xsl,mbstring,zip,xml}

yum install php-{pdo,mysqlnd,opcache,gd,devel,bcmath,json,iconv,soap}

yum install php-sodium
 

 

Install Apache (httpd) 

Execute the below commands to install, enable and start the apache. Also, check the status of the apache using the status command:

 

yum install -y httpd httpd-tools mod_ssl

systemctl enable httpd

systemctl start httpd

systemctl status httpd

 

Install Mariadb

Execute the below commands to get the required version of the MariaDB. Add it to the repository list and install it

yum -y update

curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup

 

sudo bash mariadb_repo_setup --os-type=rhel  --os-version=7 --mariadb-server-version=10.4
 

sudo rm -rf /var/cache/yum
 

sudo yum makecache
 

sudo yum repolist
 

sudo yum install MariaDB-server MariaDB-client
 

sudo systemctl enable --now mariadb
 

systemctl status mariadb

 

Mariadb-secure-installation

 

Below configurations is to be done during the installation of the MariaDB:

 

Enter current password for root (enter for none):

You can just hit enter here, as this is a fresh setup of MariaDB. If you already have MariaDB installed and now if you are upgrading/ downgrading then enter the existing root password.

 

Switch to unix_socket authentication [Y/n] n

 

Next, set a new password to the root user.

Change the root password? [Y/n] y
New password:
Re-enter new password:

 

After this, the system will ask you to remove the anonymous users.

Remove anonymous users? [Y/n] Y

 

Next, it does not allow any user to login to the root user remotely. 

Disallow root login remotely? [Y/n] Y

 

The system will ask to remove test databases.

 

Remove test database and access to it? [Y/n] Y

 

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? [Y/n] Y

 

All done!  If you've completed all of the above steps, your MariaDB installation should now be secure.

 

Create a New MySQL user

Use the following command to log in to MySQL. Enter the root user password and you should be in. 

 

mysql -u root -p

 

After you log in, create the database using the following command. 

create database magento;

Next, create a new user using the following command. Please try to assign a strong password to the user.

 

create user 'magentouser'@'localhost' identified by 'mageTest';

 

Next, grant privileges to the above user.

Grant all privileges on magento.* to  'magentouser'@'localhost';

 

Composer Installation

To quickly install Composer run the following script:

 

1. Download the installer to the current directory

php -r "copy('https://getcomposer.org/installer','composer-setup.php');"

 

2. Verify the installer SHA-384

php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

 

3. Run the installer

To programmatically install specific major versions, you can use the --1 or --2 flag. You can check all the available options here

php composer-setup.php --1

 

4. Remove the installer

php -r "unlink('composer-setup.php');"

 

5. Global installation of composer

mv composer.phar /usr/local/bin/composer

 

Install Elasticsearch 

Before beginning with elastic search installation, we need to make sure JAVA is installed.

 

Install Java

firstly check if the required version of java is available in amazon Linux extras and install it. Also, export the jvm directory to the JAVA_HOME variable.  

 

amazon-linux-extras | grep java

amazon-linux-extras install java-openjdk11

java -version

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.amzn2.0.3.x86_64

echo $JAVA_HOME

 

Elasticsearch Installation

First import the Elasticsearch GPG Key. So, download and install the public signing key

rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

 

Now start installing from the RPM repository. Create a file called elasticsearch.repo in the /etc/yum.repos.d/ directory

 

nano /etc/yum.repos.d/elasticsearch.repo

 

Add the following content to the repository file (Here we are installing ElasticSearch 7)

[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

 

Now, the above-created repository is ready for use. You can now install, enable and start Elasticsearch with the following commands. Also, check if the status is starting.

 

yum install elasticsearch
systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service
sudo systemctl status elasticsearch.service

 

Test ElasticSearch Cluster using CURL. The below command should give you JSON response indicating cluster name, UUID, and other fields related to the cluster.

 

curl -X GET "localhost:9200/"

 

Httpd Configurations 

  • Set AllowOverride to All in /etc/httpd/conf/httpd.conf
  • Next restart the apache using the following command

service httpd restart

 

Install Magento

Navigate to /var/www/html/ directory

cd /var/www/html/

 

Create a project using composer. This will create a Magento2 directory with all the files and directories required for Magento setup for your Magento 2 AWS architecture.

 

/usr/local/bin/composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.3 magento2

 

Next, execute the following command to install Magento.

 

php bin/magento setup:install --base-url=domainname/magento2 --db-host=localhost --db-name=magento --db-user=magentouser --db-password=mageTest --admin-firstname=admin --admin-lastname=admin --admin-email[email protected] --admin-user=admin --admin-password=admin@123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1

 

Above installation will run for a few minutes and at the end, you will receive the following message:

 

[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_j6d74i
Nothing to import.

 

Please note the admin URL. Now you can access Magento using the above admin URL. if you want you can disable the 2FA module when the instance is in the development phase.

 

So there you go! That’s how you setup Magento on AWS without using any AMI in Magento development services. If you need any help on how to install Magento on AWS or AWS Magento 2, get in touch with our AWS cloud services experts! 

You can always contact our experts to know more about Magento Development Services!

 

Legred Vaz
Legred Vaz
Jr. Software Engineer
Why Is Customer Relationship Management So Important?

Why CRM is important

Ismail Hossain
Tips for Setting Up Cron Job in CakePHP Project

How to Set Up Cron Job in CakePHP Project

Fokrul Hasan

SJ Innovation - FACT SHEET

ADMIN