Manually build a Magento 2 e-commerce website

更新时间:
复制 MD 格式

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 22.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. Obtain your personal keys from Adobe Commerce, as shown below. For instructions, see Adobe Commerce.

    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 additional PHP extensions.

      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 to apply the changes.

      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 vm.max_map_count=262144

      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

      This example sets plugins.security.disabled to true to disable SSL for the HTTP and Transport layers for testing.

    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. Ensure that your global /etc/nginx/nginx.conf contains include /etc/nginx/conf.d/*.conf; to load the other configuration files in the following sections.

      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.

    Composer depends on the unzip or p7zip command-line tools to decompress files. Run the following command to install them.

    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. By placing composer.phar in a directory that is in your PATH, you can call 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 use the command line to install Adobe Commerce. In this example, db-host is on the same computer (localhost), and db-name, db-user, and db-password are all magento.

    sudo bin/magento setup:install \
      --base-url=http://196.****.*.1/ \ # Public IP address of the ECS instance
      --db-host=localhost \ # Database host
      --db-name=magento \  # Database name
      --db-user=magento \ # Database username
      --db-password=magento \ # Database password
      --admin-firstname=admin \ # First name of the backend administrator
      --admin-lastname=admin \ 
      --admin-email=cy****sper@email.com \ # Administrator's email address
      --admin-user=admin \ # Backend login username
      --admin-password=admin*** \ # Backend login password
      --language=en_US \ # Website language
      --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 is the URL for the administrator backend. This URL will be accessible after you configure Nginx. 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. In a browser, navigate to http://<Public IP address of your ECS instance> to view the default home page, as shown in the following figure.

    image

  2. Access http://<Public IP address of the ECS instance>/admin_46i****, and enter the backend logon account admin and login password admin*** to log in.

    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 default image repository.

    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.

    An ICP filing is mandatory for any domain name that resolves to a server in a Chinese mainland region. The website cannot be publicly accessible until it receives an ICP filing number. For more information, see ICP filing process and ICP filing process.

  • 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