How to setup Magento on AWS (Amazon Web Services)

how-to-setup-magento-on-aws

How to setup Magento on AWS (Amazon Web Services)

AWS is one of the most reliable and scalable hosting services on cloud. If you have huge traffic and face special challenges during high traffic events like festivals, holidays, sales etc then you should consider migrating to AWS.

Following are the steps to setup Magento on AWS.

1- Steps for Setup Instance on AWS Console

1. Go to Amazon and setup an account. Now, navigate to AWS Management console.

2. Select EC2. Don’t worry if you can’t see it. Search it in Find Services and you will get it. Click on it.

3. Click on Launch Instance. Select a machine with default operating system (OS), such as Linux, Ubuntu, or Windows etc.

4. Select the Ubuntu server. Most developers tend to use Linux operating system to carry out their tasks, therefore I prefer Ubuntu. If you are a Windows user, and you want to select a Windows operating system, you are more welcome to do so. The configuration is almost identical.

According to the official Magento 2 system requirements you need at least 2G of RAM. However, I consider and advise to have 4GB RAM. Just to be on safe side, if you want to debug or execute a block of code, you probably won’t want to exhaust your RAM . Therefore, you must:

Select Type t2 medium

5. Click on Configure Instance Details. You can configure your machine (i.e. instance). Since, there is nothing much to do, proceed ahead with add storage.

Now, the system will ask you to add storage. By default, the storage size is enough, but if you want to increase, then just increase the size.

Once you complete the process, it time to click on Add Tags.
For now, we don’t need any tags, therefore, click on configure security group button. This is important.

6. Now, you can add rules about which people from across the globe will access your server. Do they have to follow a protocol to reach to a server? For now, I am making my server accessible to all traffic. Later, we can put security rules. Navigate to Type and select All traffic. In the source, click anywhere.

7. Click review and launch. Then you will have the opportunity to review your configuration again. Click Launch.

8. You might be wondering what is a key pair? In simple words, its a key which lets you connect to your server machine securely. It consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely. For Windows AMIs, you must use the private key file to obtain the login password for your instance. For Linux AMIs, the private key file allows you to login to your instance with active SSH.

Download the key pair and keep it secure, as without it, you won’t be able to access your server. Now click on launch instance.

A new screen will open up.

9. Click View Instance. In status check, you can see that it’s initializing
Ignore the old server. You might not see the name of your server. Now, navigate to the empty space and you will notice a pencil write icon. Click on it and name your server. You must wait for a while and then refresh the screen. Check your system status

Now, you must connect to your server. Before, you do that make sure you have git installed, because we will use it to connect to our server. Now, select your new server

10. Click Connect.

Now that you know about how to connect to your new server, we will opt the easy way. Remember the key pair you downloaded earlier? Navigate to that folder. Assuming, you have git installed, right click in empty space and select git bash here option. Now copy the line, I underline just below the example which starts with ssh -I.....

Press Enter. You will get a question: Are you sure you want to connect? Enter yes. Congrats, now you are on your server. But we must fulfill the requirement to make Magento work. Therefore, it is time to set up a PHP environment. Next, we will run a few commands on git to install the prerequisite tools.

2- How to Install Magento on AWS (Amazon Web Services) Server Ubuntu through Putty or git bash console

1. ls -al (This will show all the directories on the server)

2. sudo su (This will direct you to the home/ubuntu directory. It means that you have logged in as a root user)

3. apt-get update (This will update your server with the latest tools)

4. sudo apt-get install apache2 php libapache2-mod-php mysql-server php-mysql php-dom php-simplexml php-curl php-intl php-xsl php-mbstring php-zip php-xml composer

(This code will install Apache (a web server software), PHP (latest version, currently its 7.2) along with necessary extra tools required for Magento, and MySQL server which will serve as a database)

Press Y if asked do you want to continue? Once the installation is complete, you must create a database.

5. Now, install PHP gd extension

apt-get install php7.2-gd (can vary with PHP version)

6. Install PHP mcrypt extension

apt-get install php7.2-mcrypt (can vary with PHP version)

7. sudo a2enmod rewrite

8. Now Restart the Apache:

systemctl restart apache2

9. And then navigate

cd /etc/apache2/

10. sudo nano apache2.conf

Press ctrl+w and search indexes
Replace ‘None’ word with ‘all’
Press ctrl+x, press y and hit Enter

11. Again Restart the Apache services

systemctl restart apache2.service

12. Now go to the public root directory and clone Magento 2 from Github

cd /var/www/html
git clone https://github.com/Magento/Magento2.git
cd Magento2

13. Now just install composer

composer install
(If you come across an error)

14. To resolve it, run these commands:
apt-get install php7.2-bcmath
composer install

15. Now if the composer is still not getting installed , run

sudo apt-get install php-soap
composer install

16. Now you have to change the permissions of the folder so that Magento setup can installed required files: chmod -R 755 /var/www/html/Magento2/
chmod -R 777 /var/www/html/Magento2/var/
chmod -R 777 /var/www/html/Magento2/pub/
chmod -R 777 /var/www/html/Magento2/app/etc
chmod -R 777 /var/www/html/Magento2/generated

17. sudo service apache2 restart

18. Setup Database mysql -u root -p
It will ask you a password. By default it’s none, so just press Enter. Now you are in MySQL terminal
show databases;
create database magento;
show databases;
exit;

19. Copy and paste it your browser like xx.xxx.xxx.xx/Magento2/setup

20. To install phpmyadmin
1. sudo apt update
2. sudo apt install phpmyadmin php-mbstring php-gettext
3. The installation process adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/ directory, where it is read automatically. The only thing you need to do is explicitly enable the mbstring PHP extension, which you can do by typing:
sudo phpenmod mbstring
sudo systemctl restart apache2

21. Configuring Password Access for the MySQL Root Account
sudo mysql
SELECT user,authentication_string,plugin,host FROM mysql.user;
CREATE USER 'james'@'localhost' IDENTIFIED BY 'james@999';
GRANT ALL PRIVILEGES ON *.* TO 'james'@'localhost' WITH GRANT OPTION;

22. Upgrade phpmyadmin to latest version

Back up phpMyAdmin

1. You should back up your current phpMyAdmin folder by renaming it.
sudo mv /usr/share/phpmyadmin/ /usr/share/phpmyadmin.bak

2.Create a new phpMyAdmin folder
sudo mkdir /usr/share/phpmyadmin/

3. Change to directory
cd /usr/share/phpmyadmin/

Download and Extract phpMyAdmin

1. sudo wget https://files.phpmyadmin.net/phpMyAdmin/4.9.0.1/phpMyAdmin-4.9.0.1-all-languages.tar.g?

2. extract
sudo tar xzf phpMyAdmin-4.9.0.1-all-languages.tar.gz
ls

3. You should see a new folder phpMyAdmin-4.9.0.1-all-languages
We want to move the contents of this folder to /usr/share/phpmyadmin
sudo mv phpMyAdmin-4.9.0.1-all-languages/* /usr/share/phpmyadmin

4. You can now log back into phpMyAdmin and check the current version. You may also see two errors.

Edit vendor_config.php

1. If you are seeing an error The $cfg[‘TempDir’] (./tmp/) is not accessible. phpMyAdmin is not able to cache templates and will be slow because of this.
Open vendor_config.php
sudo nano /usr/share/phpmyadmin/libraries/vendor_config.php

2. Press CTRL + W and search for TEMP_DIR
Change line to
define('TEMP_DIR', '/var/lib/phpmyadmin/tmp/');
( /usr/share/phpmyadmin/libraries/vendor_config.php )

3. Press CTRL + W and search for CONFIG_DIR
Change line to
define('CONFIG_DIR', '/etc/phpmyadmin/');
(/usr/share/phpmyadmin/libraries/vendor_config.php)
save file and exit. (Press CTRL + X, press Y and then press ENTER) Now log back in to phpMyAdmin and ensure the errors are gone.

CleanUp

You can now delete the tar.gz file and the empty folder.

1. sudo rm /usr/share/phpmyadmin/phpMyAdmin-4.9.0.1-all-languages.tar.gz

2. sudo rm -rf /usr/share/phpmyadmin/phpMyAdmin-4.9.0.1-all-languages

3. And if you’re certain your new phpMyAdmin install is working correctly you can delete the backup folder.

sudo rm -rf /usr/share/phpmyadmin.bak

If after installing PHPMYADMIN it says not found then :

1. sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf

2. sudo a2enconf phpmyadmin

3. sudo service apache2 reload

If after installing PHPMYADMIN it says not found then :

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin
sudo service apache2 reload


Install SMTP Extension :

1.1 Install
composer require mageplaza/module-core
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy

1.2 Upgrade
composer update mageplaza/module-core
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy

Run compile if your store in Product mode:
php bin/magento setup:di:compile

SMTP
composer require mageplaza/module-smtp
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy


To install SMTP , POSTFIX

1. sudo nano /etc/hostname
mail

2. sudo nano /etc/hosts
127.0.0.1 localhost
127.0.1.1 mail.example.com mail
198.51.100.3 mail.example.com mail


FQDN : mail.example.com
Hostname : mail

3. sudo apt-get update

4. sudo apt-get install mailutils

5. sudo nano /etc/postfix/main.cf

6. sudo service postfix restart

7. sudo nano /etc/postfix/main.cf
myhostname = mail.example.com
relayhost = [smtp.gmail.com]:587

# enable SASL authentication smtp_sasl_auth_enable = yes # disallow methods that allow anonymous authentication. smtp_sasl_security_options = noanonymous # where to find sasl_passwd smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd # Enable STARTTLS encryption smtp_use_tls = yes # where to find CA certificates smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

8. sudo nano /etc/postfix/sasl/sasl_passwd
[smtp.gmail.com]:587 no-reply@naturalmel.com:no@999no@999

9. sudo postmap /etc/postfix/sasl/sasl_passwd
or
sudo postmap hash:/etc/postfix/sasl/sasl_passwd

10. sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db

11. sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt'
or
sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt'

12. sudo postfix stop
sudo postfix start
sudo postfix reload
sudo service postfix restart

13. To check mail log :
sudo nano /var/log/mail.log

14. To check postfix is working :
echo "This email confirms that Postfix is working for now" | mail -s "Testing Posfix" vinod@bicsl.com

Useful Commands

1. To delete files or folder
rm -r file_name/folder_name


2. To view folders inside any directory
ls


3. To provide rights on all files under Magento2 Directory
cd /var/www
sudo chmod -R 777 html*


4. To uninstall or delete extensions in html
cd /var/www/html
php bin/magento module:status
php bin/magento module:disable Cminds_Marketplace --clear-static-content
php bin/magento module:disable Cminds_Supplierfrontendproductuploader --clear-static-content
php bin/magento setup:upgrade
cd app/code/Cminds/
rm -rf Marketplace
rm -rf Supplierfrontendproductuploader


5. To clear Magento Cache
cd /var/www/html
php bin/magento cache:clean
To reindex Magento
cd /var/www/html
php bin/magento indexer:reindex


6. To know the path in server of php.ini file and to view php.ini file
php --ini
sudo nano /etc/php/7.2/cli/php.ini


7. To zip or unzip file
sudo apt-get install zip unzip
sudo unzip photos.zip
sudo zip -r Magento2-30-05-2019.zip(new_file_name) Magento2(original_file_name)


8. To install multivendor market place extension :
cd /var/www/html/app/code/Cminds
php bin/magento module:enable Cminds_Supplierfrontendproductuploader
php bin/magento module:enable Cminds_Marketplace
php bin/magento setup:upgrade
rm -rf var/di
rm -rf var/generation
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f


9. To restart apache service :
cd /
sudo service apache2 restart


10. to save and install cron job :
cd /var/www/html
crontab -u ubuntu -e
(paste this content in file)
* * * * * /usr/bin/php /var/www/html/bin/magento cron:run | grep -v "Ran jobs by schedule" >>
/var/www/html/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/update/cron.php >> /var/www/html/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/bin/magento setup:cron:run >> /var/www/html/var/log/setup.cron.log
ls -al /var/www/html/var/.setup_cronjob_status
cd /var/www/html
php bin/magento cron:install


11. to make directory or folder_name
mkdir folder_name


13. Status of All Ports :
sudo ufw status
sudo ufw enable
sudo ufw disable
sudo ufw allow 25/tcp
sudo ufw allow 465/tcp
sudo ufw allow 587/tcp
sudo ufw delete allow 25/tcp
sudo ufw delete allow 465/tcp
sudo ufw delete allow 587/tcp
sudo ufw allow 25
sudo ufw allow 143
sudo ufw allow 993
sudo ufw allow 110
sudo ufw allow 995
To check ports listening :
netstat -l

Let us connect and talk about your project

CONTACT US

Connect to us on any of the below channels

Messenger Skype Whatsapp Call E-mail Book a Call