How to setup Magento on AWS (Amazon Web Services)

How to setup Magento on AWS (Amazon Web Services)

|Commerce Codes

Please note: this guide was written in 2019 for Magento 2.3 on Ubuntu 18.04 with PHP 7.2. Newer Magento (Adobe Commerce) 2.4 releases need a newer PHP version and a search engine (OpenSearch or Elasticsearch), and are installed from the command line rather than the web setup wizard. Always check the current Magento system requirements before you start.

AWS (Amazon Web Services) is one of the most reliable and scalable cloud hosting services available. If your store gets heavy traffic, or struggles during busy periods like festivals, holidays and sales, it's worth considering a move to AWS.

Here are the steps to set up Magento on AWS.

In this guide:

Part 1: Launch an instance in the AWS console

  1. Create an AWS account, then open the AWS Management Console.
  2. Select EC2. If you can't see it, search for it in Find Services.
  3. Click Launch Instance and choose a machine image with an operating system, such as Ubuntu, another Linux distribution or Windows.
  4. Select Ubuntu Server. Most developers prefer Linux for this kind of work, so we use Ubuntu here. If you'd rather use Windows, you can; the configuration is very similar.
  5. Choose an instance type. Magento's official minimum is 2 GB of RAM, but we recommend at least 4 GB, so you don't run out of memory when debugging or running heavier tasks. Select t2.medium (4 GB RAM), or a newer equivalent such as t3.medium.
  6. Click Configure Instance Details. The defaults are fine here, so continue to Add Storage. The default storage size is usually enough, but you can increase it if you need more.
  7. Click Add Tags. You don't need any tags for now, so continue to Configure Security Group. This step is important.
  8. Add the rules that control who can reach your server:
    • SSH (port 22): set the source to My IP, so only you can log in to the server.
    • HTTP (port 80) and HTTPS (port 443): set the source to Anywhere, so customers can visit your store.
  9. Click Review and Launch, check your configuration, then click Launch.
  10. You'll be asked to create a key pair. This is what lets you connect to your server securely. It has a public key, which AWS stores, and a private key file, which you keep. For Linux instances, the private key lets you log in over SSH; for Windows instances, you use it to get the login password. Download the key pair and keep it safe: without it, you won't be able to access your server. Then click Launch Instances.
  11. Click View Instances. Under Status checks, you'll see the instance initializing. To name it, hover over the empty Name field, click the pencil icon and enter a name. Wait a few minutes, refresh the page and check that the status checks have passed.
  12. Select your new instance and click Connect to see the connection instructions.

Security tip: don't open your security group to "All traffic" from anywhere, even temporarily. An open server can be scanned and attacked within minutes. Only open the ports you need.

Connect to your server

The easiest way to connect from Windows is with Git Bash (you'll need Git installed). On macOS or Linux, use the Terminal.

  1. Go to the folder where you saved your key pair file.
  2. Right-click in an empty space and choose Git Bash Here.
  3. Copy the example SSH command from the AWS Connect screen. It starts with ssh -i and looks like this:
    ssh -i "your-key.pem" ubuntu@your-server-public-dns
  4. Press Enter. When asked whether you're sure you want to connect, type yes.

Congratulations, you're now logged in to your server! Next, you'll set up the PHP environment and the other tools Magento needs.

Part 2: Install Magento on your Ubuntu server

Run the following commands in your Git Bash or PuTTY console.

  1. List all the files and folders in the current directory:
    ls -al
  2. Switch to the root user:
    sudo su
  3. Update the server's package list:
    apt-get update
  4. Install Apache (the web server), PHP with the extensions Magento needs, MySQL (the database server) and Composer. Press Y if asked whether you want to continue.
    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
  5. Install the PHP GD extension (the version number may differ on your server):
    apt-get install php7.2-gd
  6. Enable Apache's rewrite module:
    sudo a2enmod rewrite
  7. Restart Apache:
    systemctl restart apache2
  8. Allow Magento's .htaccess files to work. Open the Apache configuration file:
    cd /etc/apache2/
    sudo nano apache2.conf
    Press Ctrl + W and search for /var/www/. In that <Directory /var/www/> block, change AllowOverride None to AllowOverride All. Press Ctrl + X, then Y, then Enter to save.
  9. Restart Apache again:
    systemctl restart apache2.service
  10. Go to the web root and download Magento 2 from GitHub:
    cd /var/www/html
    git clone https://github.com/Magento/Magento2.git
    cd Magento2
  11. Install Magento's dependencies with Composer:
    composer install
    If you get an error about a missing extension, install it and run Composer again. The most common ones are bcmath and soap:
    apt-get install php7.2-bcmath
    sudo apt-get install php-soap
    composer install
  12. Set the folder permissions so the Magento setup can write the files it needs, and give the web server (www-data) ownership:
    cd /var/www/html/Magento2
    sudo chown -R www-data:www-data .
    sudo find var generated pub/static pub/media app/etc -type d -exec chmod 775 {} +
    sudo find var generated pub/static pub/media app/etc -type f -exec chmod 664 {} +
  13. Restart Apache:
    sudo service apache2 restart
  14. Create the database. Log in to MySQL (if no root password is set, just press Enter when asked):
    mysql -u root -p
    Then, in the MySQL terminal, run:
    show databases;
    create database magento;
    show databases;
    exit;
  15. Open the setup wizard in your browser by visiting your server's public IP address followed by /Magento2/setup, for example http://xx.xxx.xxx.xx/Magento2/setup, and follow the steps.

Security tip: avoid setting folders to 777 (writable by everyone), which is a common shortcut but leaves your store open to attack. The ownership and 775/664 permissions above give Magento the access it needs without that risk.

Part 3: Install and update phpMyAdmin

phpMyAdmin gives you a web interface for managing your database.

Install phpMyAdmin

  1. Update the package list and install phpMyAdmin:
    sudo apt update
    sudo apt install phpmyadmin php-mbstring php-gettext
  2. The installation adds phpMyAdmin's Apache configuration file to the /etc/apache2/conf-enabled/ directory, where it's read automatically. The only thing left to do is enable the mbstring PHP extension and restart Apache:
    sudo phpenmod mbstring
    sudo systemctl restart apache2

If phpMyAdmin shows a "Not Found" error after installing, link its configuration to Apache and reload:

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

Set up a MySQL user with password access

Create a MySQL user with a password that you can use to log in to phpMyAdmin. Replace your_username and your_strong_password with your own values:

sudo mysql
SELECT user,authentication_string,plugin,host FROM mysql.user;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' WITH GRANT OPTION;

Upgrade phpMyAdmin to the latest version

1. Back up phpMyAdmin

  1. 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. Go to the new folder:
    cd /usr/share/phpmyadmin/

2. Download and extract phpMyAdmin

Get the link to the latest version from phpmyadmin.net. This example uses version 4.9.0.1:

  1. Download it:
    sudo wget https://files.phpmyadmin.net/phpMyAdmin/4.9.0.1/phpMyAdmin-4.9.0.1-all-languages.tar.gz
  2. Extract it:
    sudo tar xzf phpMyAdmin-4.9.0.1-all-languages.tar.gz
    ls
  3. You'll see a new folder named phpMyAdmin-4.9.0.1-all-languages. Move its contents to /usr/share/phpmyadmin:
    sudo mv phpMyAdmin-4.9.0.1-all-languages/* /usr/share/phpmyadmin
  4. Log back in to phpMyAdmin and check the version. You may see two errors, which the next step fixes.

3. Edit vendor_config.php

If you see the error "The $cfg['TempDir'] (./tmp/) is not accessible. phpMyAdmin is not able to cache templates and will be slow because of this.":

  1. Open vendor_config.php:
    sudo nano /usr/share/phpmyadmin/libraries/vendor_config.php
  2. Press Ctrl + W, search for TEMP_DIR and change the line to:
    define('TEMP_DIR', '/var/lib/phpmyadmin/tmp/');
  3. Press Ctrl + W, search for CONFIG_DIR and change the line to:
    define('CONFIG_DIR', '/etc/phpmyadmin/');
  4. Save the file and exit (Ctrl + X, then Y, then Enter). Log back in to phpMyAdmin and check that the errors are gone.

4. Clean up

  1. Delete the downloaded archive and the empty folder:
    sudo rm /usr/share/phpmyadmin/phpMyAdmin-4.9.0.1-all-languages.tar.gz
    sudo rm -rf /usr/share/phpmyadmin/phpMyAdmin-4.9.0.1-all-languages
  2. Once you're sure the new phpMyAdmin is working correctly, you can delete the backup folder:
    sudo rm -rf /usr/share/phpmyadmin.bak

Security tip: phpMyAdmin is a common target for attackers. Once you've finished setting up, consider restricting access to it by IP address or password-protecting its URL.

Part 4: Set up email (SMTP and Postfix)

Install the Mageplaza SMTP extension

Run these commands from your Magento root folder.

Install the Mageplaza core module:

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

To upgrade it later:

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

If your store is in production mode, also run:

php bin/magento setup:di:compile

Install the SMTP module:

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

Install and configure Postfix

In this example, the server's fully qualified domain name (FQDN) is mail.example.com and its hostname is mail. Replace these, the IP address and the email details with your own.

  1. Set the hostname. Open the file and replace its contents with mail:
    sudo nano /etc/hostname
  2. Update the hosts file:
    sudo nano /etc/hosts
    Add these lines, using your server's IP address:
    127.0.0.1    localhost
    127.0.1.1    mail.example.com mail
    198.51.100.3 mail.example.com mail
  3. Update the package list and install the mail utilities (this also installs Postfix):
    sudo apt-get update
    sudo apt-get install mailutils
  4. Open the Postfix configuration file:
    sudo nano /etc/postfix/main.cf
    Set the hostname and relay host, and add the authentication settings:
    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
  5. Add your SMTP login details:
    sudo nano /etc/postfix/sasl/sasl_passwd
    Add this line, replacing the email address and password with your own. For Gmail, use an app password, not your normal password:
    [smtp.gmail.com]:587 your-email@yourdomain.com:your_app_password
  6. Create the lookup table from the password file:
    sudo postmap /etc/postfix/sasl/sasl_passwd
  7. Secure the password files, so only the root user can read them:
    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
  8. Set the CA certificate file (use the second command if your system uses ca-bundle.crt):
    sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt'
    sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt'
  9. Restart Postfix:
    sudo postfix stop
    sudo postfix start
    sudo postfix reload
    sudo service postfix restart
  10. To check the mail log:
    sudo nano /var/log/mail.log
  11. To test that Postfix is working, send yourself an email:
    echo "This email confirms that Postfix is working" | mail -s "Testing Postfix" you@yourdomain.com

Useful commands

Delete a file or folder:

rm -r file_or_folder_name

List the folders in a directory:

ls

Create a folder:

mkdir folder_name

Clear the Magento cache and reindex:

cd /var/www/html/Magento2
php bin/magento cache:clean
php bin/magento indexer:reindex

Enable a Magento extension (replace Vendor_Module with the extension's module name):

cd /var/www/html/Magento2
php bin/magento module:enable Vendor_Module
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f

Disable and remove a Magento extension:

cd /var/www/html/Magento2
php bin/magento module:status
php bin/magento module:disable Vendor_Module --clear-static-content
php bin/magento setup:upgrade
rm -rf app/code/Vendor/Module

Find and edit your php.ini file:

php --ini
sudo nano /etc/php/7.2/cli/php.ini

Zip or unzip files:

sudo apt-get install zip unzip
sudo unzip photos.zip
sudo zip -r Magento2-backup.zip Magento2

Restart Apache:

sudo service apache2 restart

Set up Magento's cron jobs:

cd /var/www/html/Magento2
php bin/magento cron:install

This adds Magento's cron jobs to the crontab automatically. You can check them with crontab -l.

Manage the firewall (UFW):

# check status, and enable or disable the firewall
sudo ufw status
sudo ufw enable
sudo ufw disable

# allow the web and SSH ports (do this before enabling the firewall)
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# allow mail ports, if your server sends or receives email
sudo ufw allow 25/tcp
sudo ufw allow 465/tcp
sudo ufw allow 587/tcp

# remove a rule
sudo ufw delete allow 25/tcp

Check which ports are listening:

netstat -l