×
Community Blog Build a Magento E-commerce Website on an ECS Instance Manually

Build a Magento E-commerce Website on an ECS Instance Manually

This article provides a step-by-step guide on how to build a Magento e-commerce website on an Elastic Compute Service (ECS) instance running the CentOS 7 operating system.

By Alibaba Cloud ECS Team

Magento is an open-source e-commerce platform written in PHP. Its architecture, which is extensible and modular, makes it an excellent choice for building medium to large-sized websites. Magento supports PHP versions ranging from 5.6 to 7.1 and utilizes MySQL databases for data storage. This article provides a step-by-step guide on how to build a Magento e-commerce website on an Elastic Compute Service (ECS) instance running the CentOS 7 operating system.

Prerequisites

• Create an Alibaba Cloud account. To create an Alibaba Cloud account, go to the Sign up to Alibaba Cloud page.

• Add inbound rules to the security group of the ECS instance to allow traffic on ports 80 and 3306. For detailed instructions on how to add a security group rule, refer to the Add a security group rule.

Note: In most cases, clients are located within LANs and can map their private IP addresses to public IP addresses for communication 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 are unable to access the Magento website after it is built, please 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 Magento website, specify 0.0.0.0/0 as an authorization object.

Background Information

In this article, we will use an ECS instance with the following configurations:

• Instance type: ecs.c6.large
• Operating system: CentOS 7.2 64-bit public image
• CPU: 2 vCPUs
• Memory: 4 GiB
• Network type: Virtual Private Cloud (VPC)
• IP address: public IP address

Note: If you intend to build a Magento server, the selected instance type must have a minimum memory of 2 GiB.

For 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 are using different software versions, you may need to adjust commands and parameter settings accordingly.

Procedure

Follow these steps to build a Magento e-commerce website on an Alibaba Cloud ECS instance:

• Step 1: Install and configure Apache HTTP Server
• Step 2: Install and configure MySQL
• Step 3: Install and configure PHP
• Step 4: Create a Magento database
• Step 5: Install and configure Composer
• Step 6: Install and configure Magento
• Step 7: Configure the Magento client
• Step 8: Add a cron Job

Step 1: Install and Configure Apache HTTP Server

1.  Install Apache.

a) Run the following command to install Apache:

yum install httpd -y

b) Run the following command to check whether Apache is installed.

httpd -v

The following command output indicates that Apache is installed.

1

2.  Configure Apache.

a) Run the following command to open the configuration file of Apache:

vim /etc/httpd/conf/httpd.conf

b) Add LoadModule rewrite_module modules/mod_rewrite.so below Include conf.modules.d/*.conf.

Perform the following steps:

i) Move the pointer to the beginning of the line below the Include conf.modules.d/*.conf line.
ii) Press the i key to enter the edit mode.
iii) Enter LoadModule rewrite_module modules/mod_rewrite.so.

The following figure shows the added content.

2

c) 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.

3

d) 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 the 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.

a) Run the following command to add a MySQL YUM repository.

rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

b) 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 upon system startup.

systemctl enable mysqld

4.  Configure MySQL.

a) Run the following command to view the /var/log/mysqld.log file 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.

b) Run the following command to configure the security settings of MySQL.

mysql_secure_installation

Perform the following operations:

i) Set the password of the root account.

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

ii) 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.

iii) 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 # Enter Y to deny remote access by the root account.
Success.

iv) 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.

v) Enter Y to reload the privilege table.

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, see MySQL documentation.

Step 3: Install and configure PHP

1.  Install PHP.

a) 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

b) Run the following command to add the Webtatic repository.

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

c) Run the following command to install PHP7 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

d) 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.

a) Run the following command to open the PHP configuration file.

vim /etc/php.ini

b) Move the pointer to the end of the last line. Perform the following operations:

i) Enter :$ and press the Enter key to move the pointer to the last line of the file.

ii) Press the $ to move the pointer to the end of the line.

c) Press the I key to enter the edit mode.

d) Add the configurations of 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.

4

e) Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

f) Restart Apache.

systemctl restart httpd

Step 4: Create a Magento database

1.  Run the following command to log on to MySQL with the account and password of the root user.

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 user account are available. Perform the following steps:

a) 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.

b) Run the following command to view the new magento database:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| magento            |
+--------------------+
2 rows in set (0.00 sec)

c) 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 dependent 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 for global use.

mv /root/composer.phar /usr/bin/composer

3.  Run the command composer -v 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

Note: The latest Composer version is not compatible with Magento. Therefore, you must install Composer of a version that is compatible with Magento. Run the following command to convert the latest Composer version to a version that is compatible with Magento, such as Composer 1.8.5.

composer self-update 1.8.5

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 only for test purposes, you can install sample data.

• If you install Magento for production purposes, 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.

a) Run the following command to install Git:

yum -y install git

b) Go to the default root directory of the web server.

cd /var/www/html/

c) 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 command 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 https://<ECS Public IP address>. Otherwise, you can access your Magento website only by using https://<ECS Public IP address>/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 Magento Client

1.  Open your browser.

2.  In the browser address bar, enter http://<ECS Public IP address>. The following page indicates that Magento is installed.

5

3.  Click Agree and Setup Magento to start configuring Magento. Perform the following steps:

a) Check readiness.

i) Click Start Readiness Check.

ii) After the check is completed, click Next.

6

b) Add the database.

i) 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.

ii) Enter the name of the created database. In this example, the database name is magento.

iii) Click Next.

7

c) Complete the settings for web access and click Next.

8

d) Enter the custom store information and click Next.

e) Enter the administrator account information and click Next.

f) Click Install Now to install Magento.

The following page indicates that Magento is configured.

9

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 file.

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

FAQ

When you enter http://<ECS Public IP address>/admin in your browser to connect to the Magento website, if the error message "One or more indexers are invalid. Make sure your Magento cron job is running." is returned, perform the following operations:

10

1.  Connect to the Magento server. For more information, see Connection methods.

2.  Run the following command to create a symbolic link from the PHP installation path to the /usr/sbin/php directory:

ln -s /usr/local/php/bin/php /usr/sbin/php

3.  Run the following command to refresh indexes.

cd /var/www/html
php bin/magento indexer:reindex

A command output similar to the following one indicates that the indexes are refreshed.

[root@iZbp1h2mquu8nb0jz99**** wwwroot]# php bin/magento indexer:reindex
Design Config Grid index has been rebuilt successfully in 00:00:00
Customer Grid index has been rebuilt successfully in 00:00:00
Category Products index has been rebuilt successfully in 00:00:00
Product Categories index has been rebuilt successfully in 00:00:00
Product Price index has been rebuilt successfully in 00:00:00
Product EAV index has been rebuilt successfully in 00:00:00
Stock index has been rebuilt successfully in 00:00:00
Catalog Rule Product index has been rebuilt successfully in 00:00:00
Catalog Product Rule index has been rebuilt successfully in 00:00:00
Catalog Search index has been rebuilt successfully in 00:00:00

4.  Refresh the page and click Cache Management.

11

5.  Select the Cache Types in the INVALIDATED state and click Submit.

12

A command output similar to the following one indicates that the issue is fixed.

13

What to Do Next

• Access http://<ECS Public IP address> to view the default homepage, as shown in the following figure.

14

• Access http://<ECS Public IP address>/admin and enter the username and password that you set during installation. The following page appears after you log on to the management console.

15

Reference

Magento official documentation

0 1 0
Share on

Alibaba Cloud Community

1,042 posts | 256 followers

You may also like

Comments

Alibaba Cloud Community

1,042 posts | 256 followers

Related Products