Updated on 2024-11-04 GMT+08:00

Manually Setting Up a Magento E-Commerce Website (Linux)

Overview

The section guides you through the manual setup of a Magento e-commerce website on a Linux ECS. Magento is an open source e-commerce system that features flexible design, modular architecture, and rich functions. It is suitable for building medium- and large-sized sites. Magento is written in PHP and employs the MySQL database management system for data storage.

Prerequisites

  • You have purchased an ECS and bound an EIP to it.
  • The rules listed in the following table have been added to the security group which the target ECS belongs to. For details, see Adding a Security Group Rule.
    Table 1 Security group rules

    Direction

    Priority

    Action

    Type

    Protocol & Port

    Source Address

    Inbound

    1

    Allow

    IPv4

    TCP: 22

    IP address of the client that is allowed to remotely connect to Linux ECSs using SSH. If the source IP is set to 0.0.0.0/0, access from all IP addresses is allowed. For security purposes, 0.0.0.0/0 is not recommended.

    Inbound

    1

    Allow

    IPv4

    TCP: 80

    IP address of the client that is allowed to access Magento. If the source IP is set to 0.0.0.0/0, access from all IP addresses is allowed.

    Inbound

    1

    Allow

    IPv4

    TCP : 3306

    IP address of the client that is allowed to remotely access MySQL databases. If the source IP is set to 0.0.0.0/0, access from all IP addresses is allowed. For security purposes, 0.0.0.0/0 is not recommended.

Resource Planning

Table 2 lists the resource configuration and software versions used in this practice. The commands and parameters may vary according to the hardware specifications or software versions you would use.

Table 2 Resource planning

Resource

Type

Specification/Version

ECS configuration

Flavor

c6s.large.2

vCPUs

2 vCPUs

Memory

4 GiB

OS

CentOS 7.2

Software resources

Apache

2.4.6

MySQL

5.7

Download URL:

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

PHP

7.0.33

Download URL:

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

Composer

1.10.19

Download URL:

https://getcomposer.org/installer

Magento

2.1.0

Download URL:

https://github.com/magento/magento2.git

To make sure that the website works properly, use an ECS whose memory is 2 GiB or higher.

Step 1: Install and Configure Apache

  1. Remotely log in to the ECS by referring to Logging In to a Linux ECS.
  2. Run the following commands as user root to update the software package and install Apache:

    yum -y update
    yum -y install httpd

    If an error message is displayed, indicating that the domain name cannot be resolved, add a DNS server to the /etc/resolv.conf file.

  3. Open the Apache configuration file.

    vim /etc/httpd/conf/httpd.conf

    If vim is not installed, run the yum install -y vim* command to install it.

  4. Press i to enter insert mode and modify the file as follows:

    • Change AllowOverride None to AllowOverride all.

    • Add the following parameters to the end of the configuration file:
      LoadModule rewrite_module modules/mod_rewrite.so

  5. Press Esc to exit insert mode. Then, enter :wq to save the settings and exit.
  6. Run the following commands in sequence to start Apache and enable it to start automatically upon ECS startup:

    systemctl start httpd
    systemctl enable httpd

Step 2: Install and Configure MySQL

  1. Run the following command as the root user to add a 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

  3. Run the following commands in sequence to start MySQL and enable it to start automatically upon ECS startup:

    systemctl start mysqld
    systemctl enable mysqld

  4. Run the following command to obtain the root user's password that is automatically set during MySQL installation:

    grep 'temporary password' /var/log/mysqld.log
    Information similar to the following is displayed, in which (n?K7jP#cirM is the temporary password.
    2019-05-09T11:29:42.365419Z 1 [Note] A temporary password is generated for root@localhost: (n?K7jP#cirM

  5. Run the following command to harden MySQL:

    mysql_secure_installation

    Perform operations as prompted.

    Securing the MySQL server deployment.
    
    Enter password for user root:    #Enter the obtained password of user root.
    The existing password for the user account root has expired. Please set a new password.
    
    New password:                    #Set a new password for user root.
    
    Re-enter new password:           #Enter the new password again.
    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   #Press Y to change the password of user root.
    
    New password:    #Enter a new password that consists of 8 to 30 characters, including letters, digits, and special characters (` ~!@#$%^&*-+=|(){}[]:;'<>,.?/).
    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    #Press Y.
    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   #Press Y to remove anonymous users.
    Success.
    
    Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y   #Press Y to disallow remote logins of user root.
    Success.
    
    By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y   #Press Y to delete the test database and remove access to it.
     - Dropping test database...
    Success.
    
     - Removing privileges on test database...
    Success.
    
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y   #Press Y to reload privilege tables.
    Success.
    
    All done!

  6. Log in to the MySQL database as the root user.

    mysql -u root -p

  7. Run the following command to create a database named magento.

    CREATE DATABASE magento;

  8. Run the following command to create a user for the database and assign full permissions to the user:

    GRANT ALL ON magento.* TO magentouser@localhost IDENTIFIED BY 'xxxxx';

    In this command, magento is the name of the database created in the previous step, magentouser is the name of the database user, and xxxxx is the password of the database user.

  9. Run the following command to exit the MySQL CLI:

    exit

  10. (Optional) Perform the following operations to check whether the database and account have been created and then exit the MySQL CLI:

    1. Run the following command to log in to the MySQL CLI as user magentouser:
      mysql -u magentouser -p
    2. Run the following command to view the created database:
      SHOW DATABASES;

      In the displayed information, magento is the newly created database.

      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | magento            |
      +--------------------+
      2 rows in set (0.00 sec)
    3. Run the following command to exit the MySQL CLI:
      exit

Step 3: Install and Configure PHP

  1. Run the following command to add the IUS and EPEL repositories:

    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 a webtatic repository:

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

  3. Run the following command to install PHP7 and required PHP extensions for Magento:

    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 version of the installed PHP:

    php -v

    Information similar to the following is displayed:

    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

  5. Run the following command to open the PHP configuration file php.ini:

    vim /etc/php.ini

  6. Press i to enter insert mode and modify the file as follows:

    • Set memory_limit to a proper value.

    • Uncomment and set date.timezone.

  7. Press Esc to exit insert mode. Then, enter :wq to save the settings and exit.
  8. Run the following command to restart the web service process:

    systemctl restart httpd

Step 4: Install Composer

Composer is a package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.

  1. Install composer 1.x for the dependency of Magento 2.

    Run the following commands to install the Composer of the specified version and set the installation path to /usr/bin/ for global use:
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php --install-dir=/usr/bin/ --filename=composer --version=1.10.19

    The command output is as follows:

    All settings correct for using Composer
    Downloading... 
    
    Composer (version 1.10.19) successfully installed to: /usr/bin/composer
    Use it: php /usr/bin/composer

  2. Run the following command to check whether the Composer is successfully installed:

    composer -v

    The command output is as follows:

       ______
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version 1.10.19 2020-12-04 09:14:16
    ...

Step 5: Install Magento

When installing Magento, you can determine whether to install sample data. If Magento is only used for testing, it is optional for you to install sample data. If Magento is used in production environments, you are advised to perform initial configuration instead of installing sample data.

  1. Run the following command to install git:

    yum -y install git

  2. Run the following commands to go to the default root directory /var/www/html/ of the web server and use git to download Magento:

    cd /var/www/html/
    git clone https://github.com/magento/magento2.git

  3. Switch Magento to a stable version.

    By default, Magento of the latest version is installed. In the production environment, you are advised to switch to a stable version.

    cd magento2 && git checkout tags/2.1.0 -b 2.1.0

    The command output is as follows:

    Switched to a new branch '2.1.0'

  4. Run the following command to move the installation file to the root directory /var/www/html/ of the web server. /var/www/html/magento2/ is the directory where Magento is installed.

    shopt -s dotglob nullglob && mv /var/www/html/magento2/* /var/www/html/ && cd ..

    Then, you can access the Magento site from http://magento server IP address. If you do not move the installation file to the root directory, you can access the Magento site only from http://magento server IP address/magento2.

  5. Run the following commands to set file permissions for Magento:

    chown -R apache: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

  6. Run the following command to install unzip and zip.

    yum install -y unzip zip

  7. Run the following commands to go to the default root directory /var/www/html/ of the web server and use Composer to install Magento:

    cd /var/www/html/
    composer install

  8. After the installation is complete, enter http://magento server IP address in the address bar of a browser to visit Magento. If the following page is displayed, Magento is installed successfully.

Step 6: Configure Magento

  1. Visit http://magento server IP address and click Agree and Setup Magento to start the configuration.
  2. Click Start Readiness Check to check the environment. After the check is passed, click Next.

  3. Enter the MySQL database user magentouser, the password of the database user, and the database magento created in Step 2: Install and Configure MySQL, and click Next.

  4. Set the website access address and background management address, and click Next.

    The background management address can be customized.

  5. Set the time zone and language, and click Next.

  6. Set the admin account and click Next.

  7. Click Install Now and wait until the installation is complete.

    If the following information is displayed, the installation is successful.

  8. Log in to the Magento server and set up cron jobs.

    crontab -u apache -e

  9. Press i to enter insert mode and add the following content:

    */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

    Press Esc to exit insert mode. Then, enter :wq to save the settings and exit.

    For more information about running Magento cron jobs, see Magento Documentation.

  10. Enter http://magento server IP address in the address bar of a browser. The following page is displayed by default.

  11. Enter http://magento background management address in the address bar of a browser and use the admin account to log in. After the login is successful, the following page is displayed.

    If the message "One or more indexers are invalid. Make sure your Magento cron job is running" is displayed, run the php bin/magento indexer:reindex command in Magento root directory /var/www/html.

    For more information about Magento configuration, see Magento Documentation.

Step 7: Other Operations

  1. Purchase a domain name.

    Configure a unique domain name for website access. You need to obtain an authorized domain name from the domain name registrar first.

  2. Configure DNS records.

    Your website can be visited using the registered domain name only after DNS records are configured. For details, see Routing Internet Traffic to a Website.

    For example, if the domain name is www.example.com, enter http://www.example.com in the address bar of the browser to access the website.