Magento is an open source e-commerce solution that has a modular architecture and varied expansion features. Magento supports PHP versions from 5.6 to 7.1. It uses MySQL databases to store data. This topic describes how to build a Magento e-commerce website on an Elastic Compute Service (ECS) instance that runs a CentOS 7 operating system

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the Sign up to Alibaba Cloud page.
  • Inbound rules are added to the security group of the ECS instance to allow traffic on ports 80 and 3306. For more information, see Add a security group rule.
    Note Most clients are located in LANs and can map their private IP addresses to public IP addresses to communicate with external resources. Therefore, the IP addresses returned by the ipconfig or ifconfig command may not be the actual public IP addresses of the clients. If clients cannot access the Magento website after it is built, verify the public IP addresses of the clients.
    Rule direction Action Protocol type Port range Authorization type Authorization object
    Inbound Allow HTTP (80) 80/80 IPv4 CIDR block The CIDR blocks containing the public IP addresses of all clients that need to access the Magento website. Separate the CIDR blocks with commas (,).

    To allow all clients to access the Magento website, specify 0.0.0.0/0 as an authorization object.

    Inbound Allow MySQL (3306) 3306/3306 IPv4 CIDR block The CIDR blocks containing the public IP addresses of all clients that need to access the MySQL database services. Separate the CIDR blocks with commas (,).

    To allow all clients to access the services, specify 0.0.0.0/0 as an authorization object.

Background information

In this topic, an ECS instance that has the following configurations is used:
  • Instance type: ecs.c6.large
  • Operating system: CentOS 7.2 64-bit public image
  • CPU: 2 vCPUs
  • Memory: 4 GiB
    Note If you want to build a Magento server, the memory of the selected instance type must be at least 2 GiB.
  • Network type: Virtual Private Cloud (VPC)
  • IP address: public IP address

In the sample procedure, the following software versions are used:

  • Apache HTTP Server: 2.4.6
  • MySQL: 5.7
  • PHP: 7.0
  • Composer: 1.8.5
  • Magento: 2.1

If you use software versions different from the preceding ones, you may need to adjust commands and parameter settings.

Procedure

Perform the following steps to build a Magento e-commerce website on an Alibaba Cloud ECS instance:

Step 1. Install and configure Apache HTTP Server

  1. Install Apache.
    1. Run the following command to install Apache:
      yum install httpd -y
    2. Run the following command to check whether Apache is installed:
      httpd -v
      The following command output indicates that Apache is installed. magento_1
  2. Configure Apache.
    1. Run the following command to open the configuration file of Apache:
      vim /etc/httpd/conf/httpd.conf
    2. Add LoadModule rewrite_module modules/mod_rewrite.so below Include conf.modules.d/*.conf. Perform the following operations:
      1. Move the pointer to the beginning of the line below the Include conf.modules.d/*.conf line.
      2. Press the I key to enter the edit mode.
      3. Enter LoadModule rewrite_module modules/mod_rewrite.so.
      The following figure shows the added content. local_module
    3. Replace AllowOverride None in the following content with AllowOverride All.
      # AllowOverride controls what directives may be placed in .htaccess files.
      # It can be "All", "None", or any combination of the keywords:
      # Options FileInfo AuthConfig Limit
      #
      # Add a number sign (#) at the beginning of the line to comment out this line.
      #AllowOverride None
      
      # Add the following content:
      AllowOverride All
      The following figure shows the replacement result. allowoverride_all
    4. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.
  3. Run the following command to start Apache:
    systemctl start httpd
  4. Run the following command to configure Apache to run upon system startup:
    systemctl enable httpd

Step 2. Install and configure MySQL

  1. Install MySQL on the ECS instance.
    1. Run the following command to add a MySQL YUM repository:
      rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
    2. Run the following command to install MySQL:
      yum -y install mysql-community-server --nogpgcheck
  2. Run the following command to start MySQL:
    systemctl start mysqld
  3. Run the following command to enable MySQL to run on system startup:
    systemctl enable mysqld
  4. Configure MySQL.
    1. Run the following command to check the /var/log/mysqld.log file and obtain and record the initial password of the root user:
      grep 'temporary password' /var/log/mysqld.log

      The following command output is returned:

      2016-12-13T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD
      Note This initial password is used when you reset the password of the root user.
    2. Run the following command to configure the security settings of MySQL:
      mysql_secure_installation
      Perform the following operations:
      1. Set the password of the root user.
        Enter password for user root: # Enter the initial password that you obtained in the previous step.
        The 'validate_password' plugin is installed on the server.
        The subsequent steps will run with the existing configuration of the plugin.
        Using existing password for root.
        Estimated strength of the password: 100 
        Change the password for root ? (Press y|Y for Yes, any other key for No) : Y # Enter Y to change the password of the root user.
        New password: # Enter a new password that is 8 to 30 characters in length. The password must contain uppercase letters, lowercase letters, digits, and special characters. Supported special characters include ( ) `  ~ ! @ # $ % ^ & * - + =  { } [ ] : ; ‘ < > , . ? /
        Re-enter new password: # Enter the new password again.
        Estimated strength of the password: 100 
        Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
      2. Enter Y to delete the anonymous user account.
        By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
        Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  # Enter Y to delete the anonymous user.
        Success.
      3. Enter Y to deny remote access by the root account.
        Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y # Enter Y to deny remote access by the root account.
        Success.
      4. Enter Y to delete the test database and the access permissions on the database.
        Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y # Enter Y to delete the test database and the access permissions on the database.
        - Dropping test database...
        Success.
      5. Enter Y to reload privilege tables.
        Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y # Enter Y to reload privilege tables.
        Success.
        All done!

      For more information, visit MySQL documentation.

Step 3. Install and configure PHP

  1. Installed PHP.
    1. Run the following command to add the IUS repository:
      yum install \
      https://repo.ius.io/ius-release-el7.rpm \
      https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    2. Run the following command to add the Webtatic repository:
      rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    3. Run the following command to install PHP 7 and all required extensions:
      yum -y install php70w php70w-pdo php70w-mysqlnd php70w-opcache php70w-xml php70w-gd php70w-mcrypt php70w-devel php70w-intl php70w-mbstring php70w-bcmath php70w-json php70w-iconv
    4. Run the following command to check the PHP version:
      php -v
      The following command output indicates that PHP is installed:
      PHP 7.0.33 (cli) (built: Dec  6 2018 22:30:44) ( NTS )
      Copyright (c) 1997-2017 The PHP Group
      Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
          with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies                        
  2. Configure PHP.
    1. Run the following command to open the PHP configuration file:
      vim /etc/php.ini
    2. Move the pointer to the end of the last line. Perform the following operations:
      1. Enter :$ and press the Enter key to move the pointer to the last line of the file.
      2. Press the $ key to move the pointer to the end of the line.
    3. Press the I key to enter the edit mode.
    4. Add configurations for the memory limit and time zone at the end of the file.
      ; The maximum memory value allowed for the PHP script. You can increase or decrease the memory limit.
      memory_limit = 1024M
      ; Set the time zone to Shanghai.
      date.timezone = Asia/Shanghai
      The following figure shows the result. magento_2
    5. Press the Esc key, enter :wq, and press the Enter key to save and close the configuration file.
    6. Restart Apache.
      systemctl restart httpd

Step 4. Create a Magento database

  1. Run the following command and enter the password of the root user to log on to MySQL:
    mysql -u root -p
  2. Run the following command to create a magento database:
    mysql> CREATE DATABASE magento; # Replace magento with the name of database that you want to create.
  3. Run the following commands in sequence to create a user for the magento database:
    mysql> GRANT ALL ON magento.* TO <YourUser>@localhost IDENTIFIED BY '<YourPass>'; # Replace <YourUser> with the account that you want to create and <YourPass> with the password that you want to set.
    mysql> FLUSH PRIVILEGES;
    For example, to create an account named magentoUser and set its password to magentoUser1@3, run the following command:
    mysql> GRANT ALL ON magento.* TO magentoUser@localhost IDENTIFIED BY 'magentoUser1@3';
    mysql> FLUSH PRIVILEGES;
  4. Enter exit and press the Enter key to exit MySQL.
  5. Optional: Check whether the new Magento database and account are available. Perform the following operations:
    1. Run the following command to log on to MySQL with the new account and its password:
      mysql -u <YourUser> -p   # Replace <YourUser> with the account that you created.
    2. Run the following command to view the new magento database:
      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | magento            |
      +--------------------+
      2 rows in set (0.00 sec)
    3. Run the following command and press the Enter key to exit MySQL:
      mysql> exit

Step 5. Install and configure Composer

Composer is a dependency management tool of PHP. Composer can identify the code repository that is used as the basis for the project. It can also be used to install the depended code repository for the project.

  1. Run the following command to install Composer:
    curl -sS https://getcomposer.org/installer | php
  2. Run the following command to configure Composer.
    mv /root/composer.phar /usr/bin/composer
  3. Run the composer -v command to check the Composer version.
    The following command output indicates that Composer is installed:
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version 1.8.5 2019-04-09 17:46:47
                            

Step 6. Install and configure Magento

You can install Magento by using different methods and determine whether to install sample data.
  • If you install Magento for test purpose only, you can install sample data.
  • If you install Magento for production purpose, we recommend that you install Magento and configure it from the start.

In this example, Git is used to download Magento and Composer is used to install Magento.

  1. Download Magento.
    1. Run the following command to install Git:
      yum -y install git
    2. Go to the default root directory of the web server.
      cd /var/www/html/
    3. Download Magento.
      git clone https://github.com/magento/magento2.git
  2. Optional: Run the following command to switch Magento to a stable version:
    cd magento2 &&  git checkout tags/2.1.0 -b 2.1.0

    The following output is returned:

    Switched to a new branch '2.1.0'
    Note By default, Git downloads and installs the latest Magento version. If you use Magento in a production environment, we recommend that you switch Magento to a stable version. Otherwise, issues may arise when you upgrade and install Magento in the future.
  3. Run the following command to move the installation file to the root directory of the web server:
    shopt -s dotglob nullglob && mv /var/www/html/magento2/* /var/www/html/ && cd ..
    Note After you run this command, you can access your Magento website by using http://<Public IP address of the ECS instance>. Otherwise, you can access your Magento website only by using http://<Public IP address of the ECS instance>/magento2.
  4. Run the following commands in sequence to configure appropriate permissions for the Magento file:
    chown -R :apache /var/www/html
    find /var/www/html -type f -print0 | xargs -r0 chmod 640
    find /var/www/html -type d -print0 | xargs -r0 chmod 750
    chmod -R g+w /var/www/html/{pub,var}
    chmod -R g+w /var/www/html/{app/etc,vendor}
    chmod 750 /var/www/html/bin/magento
  5. Run the composer install command to install Magento.

Step 7. Configure the client of Magento

  1. Open your browser.
  2. In the browser address bar, enter http://<Public IP address of the ECS instance>.
    The following page indicates that Magento is installed. magento_3
  3. Click Agree and Setup Magento to start configuring Magento. Perform the following operations:
    1. Check readiness.
      1. Click Start Readiness Check.
      2. After the check is complete, click Next.
      magento-check
    2. Add the database.
      1. Enter the account and password of the database that you created. In this example, the user account is magentoUser and the password is magentoUser1@3.
      2. Enter the name of the created database. In this example, the database name is magento.
      3. Click Next.
    3. Complete the settings for web access and click Next.
    4. Fill in the custom store and click Next.
    5. Enter the administrator account information and click Next.
    6. Click Install Now to install Magento.
The following page indicates that Magento is configured. magento_4

Step 8. Add a cron job

Perform the following steps to add a cron job:

  1. Run the crontab -u apache -e command to configure the jobs to be operated by cron.
  2. Press the I key to enter the edit mode.
  3. Enter the following configurations:
    */10 * * * * php -c /etc /var/www/html/bin/magento cron:run
    */10 * * * * php -c /etc /var/www/html/update/cron.php
    */10 * * * * php -c /etc /var/www/html/bin/magento setup:cron:run
  4. Press the Esc key, enter :wq, and press the Enter key to save and close the configuration.

For more information about how to use cron jobs in Magento, visit Configure and run cron.

What to do next

  • Access http://<Public IP address of the ECS instance> to go to the following default homepage. luma
  • Access http://<Public IP address of the ECS instance>/admin and enter the username and password that you set during the installation. Log on to the management panel to go to the following page.