Magento is an open source e-commerce platform. It is built on flexible modular architecture and extended features to provide premium solutions for large- and medium-sized websites. 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 a CentOS 7 ECS instance.

Prerequisites

  • You must have registered an Alibaba Cloud account. If not, create a new Alibaba Cloud account first.
  • Inbound rules are added to the security group of the ECS instance and ports 80 and 3306 are allowed. For more information, see Add security group rules.
    Note Most clients reside within LANs and their private IP addresses are converted into public IP addresses when the clients access or are accessed by external devices. Therefore, the IP addresses returned by the ipconfig or the ifconfig command may not be the actual public IP addresses of the clients. If clients cannot access the Magento website after the Magento website 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 multiple CIDR blocks with commas (,).

    To allow all clients to access the Magento website, authorize 0.0.0.0/0.

    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 multiple CIDR blocks with commas (,).

    To allow all clients to access the services, authorize 0.0.0.0/0.

Background information

The ECS instance used in this topic has the following configurations:
  • Instance type: ecs.c6.large
  • Operating system: public image CentOS 7.2 64-bit
  • Number of vCPUs: 2
  • 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: VPC
  • IP address: public IP address

The following software versions are used in the sample procedure:

  • 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 ones listed, you may need to adjust the commands and parameter settings.

Procedure

Complete 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 HTTP Server.
    1. Run the following command to install Apache HTTP Server:
      yum install httpd -y
    2. Run the following command to check whether Apache HTTP Server is installed:
      httpd -v
      The following command output indicates that Apache HTTP Server is installed.magento_1
  2. Configure Apache HTTP Server.
    1. Run the following command to open the configuration file of Apache HTTP Server:
      vim /etc/httpd/conf/httpd.conf
    2. Add LoadModule rewrite_module modules/mod_rewrite.so below Include conf.modules.d/*.conf. Complete the following steps:
      1. Move the pointer to the start of the line below the Include conf.modules.d/*.conf line.
      2. Press the I key to switch to the edit mode.
      3. Enter LoadModule rewrite_module modules/mod_rewrite.so.
      The following figure shows the added content.loadmodule
    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 press the Enter key to save and close the configuration file.
  3. Run the following command to start Apache HTTP Server:
    systemctl start httpd
  4. Run the following command to configure Apache HTTP Server to run upon system startup:
    systemctl enable httpd

Step 2. Install and configure MySQL

  1. Install MySQL.
    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
  2. Run the following command to start MySQL:
    systemctl start mysqld
  3. Run the following command to configure MySQL to run upon system startup:
    systemctl enable mysqld
  4. Configure MySQL.
    1. Run the following command to check the /var/log/mysqld.log file. Then, you can 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 You can use this initial password to reset the password of the root user in subsequent operations.
    2. Run the following command to configure your MySQL databases and protect data:
      mysql_secure_installation
      Complete the following steps:
      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
        New password: # Enter a new password. The password must be 8 to 30 characters in length and contain uppercase letters, lowercase letters, digits, and special characters. Special characters include ( ) ` ~ ! @ # $ % ^ & * - + = | { } [ ] : ; ' < > , . ? /
        Re-enter new password.
        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 anonymous users.
        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
        Success.
      3. Enter Y to deny remote access by the root user.
        Disallow root login remotely? (Press y|Y for Yes, any other key for No): Y
        Success.
      4. Enter Y to delete the test database and access permissions on this database.
        Remove test database and access to it? (Press y|Y for Yes, any other key for No): Y
        - 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
        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. Complete the following steps:
      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 switch to 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.memory&time
    5. Press the Esc key, enter :wq, and press the Enter key to save and close the configuration file.
    6. Run the following command to restart Apache HTTP Server:
      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 the magento database:
    mysql> CREATE DATABASE magento; # replace magento with the database name 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 replace.<YourPass> with the password that you want to use.
    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. Type exit and press the Enter key to exit MySQL.
  5. Optional: Verify whether the new Magento database and account are available. Complete the following steps:
    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 decide 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.

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

  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 output is as follows:

    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 through http://<the public IP address of the ECS instance>. Otherwise, you can access your Magento website only through http://<the public IP address of the ECS instance>/magento2.
  4. Run the following commands in sequence to set 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://<the public IP address of the ECS instance>.
    The following page indicates Magento is installed.Magento
  3. Click Agree and Setup Magento to start configuring Magento. Complete the following steps:
    1. Check readiness.
      1. Click Start Readiness Check.
      2. After the check is complete, click Next.
      installer
    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 previously 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.success

Step 8. Add a cron job

To add a cron job, complete the following steps:

  1. Run the crontab -u apache -e command to configure the jobs to be operated by cron.
  2. Press the I key to switch to the edit mode.
  3. Enter the following configuration information:
    */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://<the public IP address of the ECS instance> to go to the following default homepage.LUMA
  • Access http://<the 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.