All Products
Search
Document Center

Elastic Compute Service:Build a Magento2 e-commerce website on an ECS instance

Last Updated:Feb 26, 2025

Magento2 is a powerful open source e-commerce platform that provides a flexible architecture and rich features to support the construction of complex online stores. Magento2 optimizes performance, improves user experience, and simplifies management operations. Magento2 is suitable for small and medium-sized business and large enterprises. This topic describes how to build a Magento2 e-commerce website on an Elastic Compute Service (ECS) instance that runs Ubuntu.

Note

For more information about Magento2, visit the Adobe Commerce official website.

Background information

In this topic, an ECS instance that has the following configurations is used:

  • Instance type: ecs.c7.large

  • Operating system: Ubuntu 20.04 64-bit public image

  • CPU: 2 vCPUs

  • Memory: 4 GiB

    Note

    To build a Magento2 server, the memory of the selected instance type must be at least 4 GiB.

In this topic, you must use the following software versions based on the software dependencies described in the Magento2 official website:

  • Composer 2.7: Composer is used to install and manage the code library of Magento2 and all required third-party libraries.

  • OpenSearch 2.12: OpenSearch provides product search features, including quick search for products, filtering options, and relevance sorting.

  • MySQL 8.0: MySQL is used to store all business data, such as product information, orders, and customer information.

  • PHP 8.3: PHP is used to execute all PHP code logic and communicate with databases and other services to obtain data.

  • NGINX 1.24: NGINX is the frontend server that processes static file requests and serves as a reverse proxy to forward dynamic content requests to the backend PHP service.

Prerequisites

  1. An access key pair is obtained for Magento2 from the Adobe Commerce website. The following figure shows an example of an access key pair. For information about how to obtain an access key pair for Magento2 from the Adobe Commerce website, visit the Adobe Commerce website.

    image

  2. A public IP address is automatically assigned to the ECS instance. Alternatively, an elastic IP address (EIP) is associated with the ECS instance. For instructions on how to enable public bandwidth, see Enable public bandwidth.

  3. Port 22 is open in an inbound rule of a security group to which the ECS instance belongs. For information about how to add a security group rule, see Add a security group rule.

  4. Docker is installed on the ECS instance. For information about how to install Docker, see Install Docker.

  5. An LNMP stack (Linux, NGINX, MySQL, and PHP) is deployed. For information about how to deploy an LNMP stack, see the Deploy an LNMP stack section of the "Deploy an LNMP stack" topic.

    Note
    • The LNMP stack uses the following software versions: NGINX 1.24, MySQL 8.0, and PHP 8.3.

    • Check and change the version numbers in the following steps.

Procedure

Step 1: Install PHP dependencies

  1. Install the core PHP package and specific extensions.

    1. Install the core PHP package, which contains several extensions.

      sudo apt-get install php8.3-cli php8.3-common php8.3-fpm php8.3-mysql php8.3-zip php8.3-gd php8.3-curl php8.3-intl php8.3-mbstring php8.3-soap php8.3-xml php8.3-bcmath php8.3-sqlite3 php8.3-opcache
    2. Install specific extensions that require separate installation.

      sudo apt-get install php8.3-bcmath php8.3-curl php8.3-gd php8.3-intl php8.3-mbstring php8.3-soap php8.3-xml php8.3-zip php8.3-sqlite3
    3. Restart the web server for the changes to take effect.

      sudo systemctl restart nginx  
  2. Configure the php.ini files.

    1. Open the php.ini files in the Vim editor.

      sudo vim /etc/php/8.3/fpm/php.ini
      sudo vim /etc/php/8.3/cli/php.ini
    2. Modify the following content in the php.ini files. Then, save and close the files.

      memory_limit = 2G
      max_execution_time = 1800
      zlib.output_compression = On
    3. Restart the PHP FastCGI Process Manager (PHP-FPM) service.

      sudo systemctl restart php8.3-fpm

Step 2: Create a Magento2 database

  1. Connect to MySQL.

    mysql -u root -p

    When you are prompted to enter a password, enter the password of the MySQL root user.

  2. Execute the following MySQL statements in sequence to create and configure a database. In this example, a database named magento is created, and the username used to log on to the database is magento.

    CREATE DATABASE magento;
    CREATE USER 'magento'@'localhost' IDENTIFIED BY 'magento';
    GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
  3. Verify the database.

    mysql -u magento -p
    Note

    If the MySQL monitor is displayed, the database is created. If an error is displayed, execute the preceding MySQL statements again.

Step 3: Download and install OpenSearch

  1. Before you use Docker to install OpenSearch, perform the following operations:

    1. Disable memory paging and swapping on the ECS instance to improve performance.

      sudo swapoff -a
    2. Increase the number of memory mappings available for OpenSearch.

      1. Modify the sysctl.conf file.

        sudo vi /etc/sysctl.conf
      2. Add the vm.max_map_count=262144 configuration.

      3. Check whether the configuration is added.

        sudo sysctl -p
        cat /proc/sys/vm/max_map_count

        image

  2. Run OpenSearch in a Docker container.

    1. Pull an OpenSearch image.

      sudo docker pull opensearchproject/opensearch:2
    2. Deploy OpenSearch in a container to check whether Docker runs as expected.

      sudo docker run -d \
       -p 9200:9200 \
       -p 9600:9600 \
       -e "discovery.type=single-node" \
       -e "OPENSEARCH_INITIAL_ADMIN_PASSWORD=admin" \
       -e "plugins.security.disabled=true" \
       opensearchproject/opensearch:latest
      Note

      In this example, the plugins.security.disabled parameter is set to true, which disables SSL for HTTP and the transport layer. Change the value of the parameter based on your business scenario.

    3. Send a request to port 9200. The default username and password are admin.

      sudo curl -k http://localhost:9200 -ku admin:admin

      The following command output is returned:

      {
        "name" : "a937e018****",
        "cluster_name" : "docker-cluster",
        "cluster_uuid" : "GLAjAG6bTeWE****_d-CLw",
        "version" : {
          "distribution" : "opensearch",
          "number" : <version>,
          "build_type" : <build-type>,
          "build_hash" : <build-hash>,
          "build_date" : <build-date>,
          "build_snapshot" : false,
          "lucene_version" : <lucene-version>,
          "minimum_wire_compatibility_version" : "7.10.0",
          "minimum_index_compatibility_version" : "7.0.0"
        },
        "tagline" : "The OpenSearch Project: https://opensearch.org/"
      }
  1. Display all containers that are running and copy the container ID of the OpenSearch node that you are testing.

    sudo docker container ls
  2. Configure NGINX for the search engine.

    1. Make sure that the global /etc/nginx/nginx.conf file contains the include /etc/nginx/conf.d/*.conf; configuration. This allows NGINX to load all .conf configuration files in the /etc/nginx/conf.d/ directory.

      vi /etc/nginx/nginx.conf

      image

    2. Configure NGINX as a proxy.

      1. Use the Vim editor to create a file that contains the following content.

        sudo vim /etc/nginx/conf.d/magento_es_auth.conf
        server {
           listen 8080;
           location /_cluster/health {
              proxy_pass http://localhost:9200/_cluster/health;
           }
        }
      2. Restart NGINX.

        sudo service nginx restart
      3. Run the following command to check whether the proxy runs as expected:

        sudo curl -u admin:admin -i http://localhost:8080/_cluster/health?pretty

        The following command output is returned.

        image

Step 4: Download and install Composer

  1. Install a decompression tool.

    To extract files from the Composer installation package, you must install the unzip or p7zip decompression tool. Run one of the following commands to install a decompression tool:

    sudo apt-get install unzip
    sudo apt-get install p7zip-full
  2. Run the following commands to install Composer:

    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"
  3. Move the composer.phar file to the directory specified in the following command and rename the file to composer. This allows you to start Composer from any directory.

    sudo mv composer.phar /usr/local/bin/composer
  4. View the version of Composer.

    composer -version

Step 5: Download and install Magento2

  1. Create an editor project by using the access key pair that you obtained for Magento2 from the Adobe Commerce website. Set the project name to magento.

    cd /var/www/html/
    sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento
  2. When you are prompted to enter a password, enter your identity authentication key.

    Note
    • The identity authentication key is the private key of the access key pair that you obtained in the Prerequisites section.

    • Downloading the Magento2 software package requires 5 minutes to 10 minutes to complete.

    image

  3. Configure the read and write permissions.

    cd /var/www/html/magento
    find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
    find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
    chown -R :www-data
    chmod u+x bin/magento
  4. Install Magento2.

    You must install Magento2 by using the CLI. In this example, the db-host parameter is set to localhost, and the db-name, db-user, and db-password parameters are set to magento.

    sudo bin/magento setup:install \
      --base-url=http://196.****.*.1/ \ # The public IP address of the ECS instance.
      --db-host=localhost \ # The address of the database.
      --db-name=magento \  # The name of the database.
      --db-user=magento \ # The username used to log on to the database.
      --db-password=magento \ # The password used to log on to the database.
      --admin-firstname=admin \ # The name of the backend administrator.
      --admin-lastname=admin \ 
      --admin-email=cy****sper@email.com \ # The email address of the administrator.
      --admin-user=admin \ # The username of the backend logon account.
      --admin-password=admin*** \ # The password of the backend logon account.
      --language=en_US \ # The language of the website.
      --currency=USD \
      --timezone=America/Chicago \
      --use-rewrites=1 \
      --search-engine=opensearch \ 
      --opensearch-host=localhost \
      --opensearch-port=9200 \
      --opensearch-enable-auth=1 \
      --opensearch-username=admin \
      --opensearch-password=admin \
      --opensearch-index-prefix=magento2 \
  5. After the installation is complete, the content shown in the following figure is displayed.

    image

    The Magento Admin URI parameter indicates the Uniform Resource Identifier (URI) that you can access as an administrator. After the NGINX configuration is complete, you can access the URL that contains the URI as an administrator. Example: http://47.****.**.72/admin_46i****.

  6. Configure NGINX for forwarding.

    1. Create a file named magento.conf that is dedicated to Magento2.

      sudo vim /etc/nginx/conf.d/magento.conf
    2. Copy and paste the following code to the file, save the file, and then exit the Vim editor.

      upstream fastcgi_backend {
        server  unix:/run/php/php8.3-fpm.sock;
      }
      server {
        listen 80;
        server_name ip;
        set $MAGE_ROOT /var/www/html/magento;
        include /var/www/html/magento/nginx.conf.sample;
      }
      
    3. Check whether the syntax is correct.

      nginx -t
    4. Restart NGINX.

      sudo systemctl restart nginx

Visit the Magento2 website

  1. On your on-premises computer, enter http://<Public IP address of the ECS instance> in the address bar of a browser. The following default homepage appears.

    image

  2. On your on-premises computer, enter http://<Public IP address of the ECS instance>/admin_46i**** in the address bar of a browser. On the logon page that appears, enter the username of the backend logon account that you specified when you installed Magento2, which is admin. Enter the password of the backend logon account that you specified when you installed Magento2, which is admin***.

    image

FAQ

Q1: What do I do if I cannot download the Magento2 software package?

To resolve the issue, you must update the image repository.

image

  1. View the image repository that comes with Composer.

    composer config -l -g

    image

  2. Change the global image repository.

    sudo composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

Q2: What operations can I perform on a domain name?

If you want to build a website, but you do not have your own domain name, purchase a domain name. After you purchase a domain name, if your website is deployed on an ECS instance that resides in the Chinese mainland, apply for an Internet Content Provider (ICP) filing for the domain name and resolve the domain name to the public IP address of the ECS instance. Perform the following operations:

  • Purchase a domain name.

    You can specify a unique domain name for your website. This way, users who want to visit your website can use a domain name that is easy to remember instead of a complex IP address.

    We recommend that you purchase a domain name from Alibaba Cloud. For more information, see Register a domain name on Alibaba Cloud.

  • Apply for an ICP filing for the domain name.

    Apply for an ICP filing for the domain name that is associated with a website deployed on an ECS instance that resides in the Chinese mainland. Your website can provide services only after you obtain an ICP filing number for the domain name.

  • Resolve the domain name.

    Use Alibaba Cloud DNS to resolve the domain name to the IP address of the ECS instance on which the website is deployed. This way, users can visit your website by using the domain name. For more information, see Get started.

References