edit-icon download-icon

Build a Magento website on ECS

Last Updated: Apr 14, 2018

Magento is an open-source e-commerce platform written in PHP. Many customers use it to build their B2B or B2C e-commerce platforms. This tutorial explains how to build a Magento platform on a single ECS instance.

In this tutorial, we will install the following tools:

  • MySQL version: 5.7
  • PHP version: 7.0
  • Magento version: 2.2

Prerequisites

Create an ECS instance. Make sure the instance meets the following requirements:

  • Operating system: CentOS 7.2 64bit.

  • Minimum specifications:

    • 2 Core CPU
    • 4 GiB RAM
    • A 40 GiB Ultra Cloud Disk as the system disk
  • VPC-connected. If you do not have a VPC network, one will be created when you create an ECS instance.

  • A public IP address is assigned to the instance.

  • Inbound Internet traffic to the TCP Port 80 of the ECS instance is allowed. For more information, see Add a security group rule.

Step 1. Install LAMP (Linux, Apache, MySQL, and PHP) on ECS

  1. Connect to the ECS instance and install Apache and MySQL.

    1. [ECS]$ yum update -y
    2. [ECS]$ yum install httpd y
    3. [ECS]$ rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
    4. [ECS]$ yum -y install mysql-community-server
  2. Start Apache and MySQL service and enable them at startup.

    1. [ECS]$ systemctl start httpd
    2. [ECS]$ systemctl enable httpd
    3. [ECS]$ systemctl start mysqld
    4. [ECS]$ systemctl enable mysqld
  3. Configure the Apache configuration file: /etc/httpd/conf/httpd.conf.

    1. Run vim /etc/httpd/conf/httpd.conf.
    2. Press the i key.
    3. Add the LoadModule rewrite_module modules/mod_rewrite.so line below Include conf.modules.d/*.conf, and replace AllowOverride None with AllowOverride all in the following section.
      1. Options Indexes FollowSymLinks
      2. #
      3. # AllowOverride controls what directives may be placed in .htaccess files.
      4. # It can be "All", "None", or any combination of the keywords:
      5. # Options FileInfo AuthConfig Limit
      6. #
      7. AllowOverride all
    4. Press the Esc key and type :wq to save and exit the file.
  4. Run grep 'temporary password' /var/log/mysqld.log to obtain the temporary password of the root account at the installation of MySQL. The password returns in the result.

    Note: Record this password. You will need it during the next step.

    1. 2018-03-16T02:23:32.142427Z 1 [Note] A temporary password is generated for root@localhost: pj?eyd6nH!:B
  5. Finish the MySQL security configuration, including:

    • Resetting the root account password
    • Disabling remote root login
    • Removing anonymous users
    • Removing test database and test database access

      Note: In this step, you are asked several questions to enable or disable features. Answer Y to all of them.

      1. [ECS]$ mysql_secure_installation
      2. Securing the MySQL server deployment.
      3. Enter password for user root: # Enter your temporary root password that is recorded in the previous step
      4. The existing password for the user account root has expired. Please set a new password.
      5. New password: # Enter a new strong password, which must be a minimum of 8 characters in length and must include a special character
      6. Re-enter new password: # Repeat the new password to confirm it
      7. Estimated strength of the password: 100
      8. Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
      9. By default, a MySQL installation has an anonymous user,
      10. allowing anyone to log into MySQL without having to have
      11. a user account created for them. This is intended only for
      12. testing, and to make the installation go a bit smoother.
      13. You should remove them before moving into a production
      14. environment.
      15. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
      16. Success.
      17. Normally, root should only be allowed to connect from
      18. 'localhost'. This ensures that someone cannot guess at
      19. the root password from the network.
      20. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
      21. Success.
      22. By default, MySQL comes with a database named 'test' that
      23. anyone can access. This is also intended only for testing,
      24. and should be removed before moving into a production
      25. environment.
      26. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
      27. - Dropping test database...
      28. Success.
      29. - Removing privileges on test database...
      30. Success.
      31. Reloading the privilege tables will ensure that all changes
      32. made so far will take effect immediately.
      33. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
      34. Success.
      35. All done!
  6. Install PHP 7.

    1. [ECS]$ yum install y http://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-14.ius.centos7.noarch.rpm
    2. [ECS]$ yum -y update
    3. [ECS]$ yum -y install php70u php70u-pdo php70u-mysqlnd php70u-opcache php70u-xml php70u-gd php70u-mcrypt php70u-devel php70u-intl php70u-mbstring php70u-bcmath php70u-json php70u-iconv
  7. Validate PHP installation.

    1. [ECS]$ php v
    2. PHP 7.0.28 (cli) (built: Mar 1 2018 10:03:25) ( NTS )
    3. Copyright (c) 1997-2017 The PHP Group
    4. Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    5. with Zend OPcache v7.0.28, Copyright (c) 1999-2017, by Zend Technologies
  8. Edit the /etc/php.ini file to set your time zone:

    1. Run vim /etc/php.ini.
    2. Press the i key.
    3. Find the line starting with date.timezone, which is commented out by default, and add the correct time zone. If your site is in China, add date.timezone = Asia/Shanghai.
    4. Press the Esc key and type :wq to save and exit the file.
  9. Run systemctl restart httpd to restart httpd.

Step 2. Configure the database

  1. Create a database and a user. Run the following commands, including those typed in the mysql> prompt.

    Note: You must replace YourRootPass with a password. Make sure that you have recorded the password you set here. You need it later. Once you have run FLUSH PRIVILEGES;, type exit; and then press the Enter key to quit the MySQL shell.

    1. [ECS]$ mysql -u root -p
    2. Enter password:
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 5
    5. Server version: 5.7.21 MySQL Community Server (GPL)
    6. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql> CREATE DATABASE magento;
    12. Query OK, 1 row affected (0.00 sec)
    13. mysql> GRANT ALL ON magento.* TO test@localhost IDENTIFIED BY 'YourRootPass';
    14. Query OK, 0 rows affected, 1 warning (0.00 sec)
    15. mysql> FLUSH PRIVILEGES;
    16. Query OK, 0 rows affected (0.01 sec)
  2. Test the new user.

    1. [ECS]$ mysql -u test p
    2. Enter password: # Enter the password you set for the `YourRootPass` in the preceding step
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 6
    5. Server version: 5.7.21 MySQL Community Server (GPL)
    6. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql>
  3. Test the new database: At the mysql> prompt, type show databases;, and you can view the following database listing.

    1. mysql> show databases;
    2. +--------------------+
    3. | Database |
    4. +--------------------+
    5. | information_schema |
    6. | magento |
    7. +--------------------+
    8. 2 rows in set (0.00 sec)
    9. mysql>
  4. Type “exit;” and then press the Enter key to quit the MySQL shell.

Step 3. Install and configure Composer

  1. Install Composer.

    1. [ECS]$ curl -sS https://getcomposer.org/installer | php
  2. Configure Composer.

    1. [ECS]$ mv /root/composer.phar /usr/bin/composer
  3. Test Composer.

    1. [ECS]$ composer -V

    You will obtain the following result, if the steps are successfully completed.

    1. Composer version 1.6.3 2018-01-31 16:28:17

Step 4. Install and configure Magento

  1. Download Magento from github.

    1. [ECS]$ yum -y install git
    2. [ECS]$ cd /var/www/html/
    3. [ECS]$ git clone https://github.com/magento/magento2.git
  2. Switch the version of Magento to the stable production version.

    1. [ECS]$ cd magento2 && git checkout tags/2.1.0 -b 2.1.0
  3. Move the installation files to the Apache root directory.

    Note: If you skip this step, you will only be able to access your Magento service at http://your-server-ip /magento2.

    1. [ECS]$ shopt -s dotglob nullglob && mv /var/www/html/magento2/* /var/www/html/ && cd ..
  4. Set Magento file permissions.

    1. [ECS]$ chown -R :apache /var/www/html
    2. [ECS]$ find /var/www/html -type f -print0 | xargs -r0 chmod 640
    3. [ECS]$ find /var/www/html -type d -print0 | xargs -r0 chmod 750
    4. [ECS]$ chmod -R g+w /var/www/html/{pub,var}
    5. [ECS]$ chmod -R g+w /var/www/html/{app/etc,vendor}
    6. [ECS]$ chmod 750 /var/www/html/bin/magento
  5. Install Magento.

    1. [ECS]$ composer install

Step 5. Test the installation

  1. Use your browser to access your server at http://public IP address of your ECS instance. You will see a welcome screen like this one.
    Magento

  2. Click Agree and Setup Magento and fill in the database information, web configuration, and accounts as follows.

    1. Add a database.
      Magento_Add a database

    2. Configure the web. You can customize your Magento admin address in this step. If not, a default address is assigned. For example, admin_1x13y5 in this tutorial.
      Magento_Web Configuration

    3. Customize your store. Set the time zone of the site.
      Magento_Customize your store

    4. Create an administrator account.
      Magento_Create an admin account

    5. Install the store.
      Magento_Install
      When you get a page like this, the store is installed successfully.
      Magento_Success

  3. Click Launch Magento Admin to enter the Dashboard of the store.
    Magento_Admin Dashboard

As an administrator of the store, you can access the Dashboard at the Magento admin address to manage it. For example, http://public IP address of your ECS instance/admin_1xl3y5/ in this tutorial. And your users can access your site at the http://public IP address of your ECS instance.

Step 6. Configure the cron job

  1. Run crontab -u apache -e.

  2. Add the following in the /etc/crontab file.

    1. */10 * * * * php -c /etc /var/www/html/bin/magento cron:run
    2. */10 * * * * php -c /etc /var/www/html/update/cron.php
    3. */10 * * * * php -c /etc /var/www/html/bin/magento setup:cron:run

Now you have a functional e-commerce site.

For more information about Magento configuration, see the official documentation.

Thank you! We've received your feedback.