文档首页> 弹性云服务器 ECS> 最佳实践> 手工搭建Magento电子商务网站(Linux)
更新时间:2024-04-17 GMT+08:00
分享

手工搭建Magento电子商务网站(Linux)

简介

本文主要介绍了如何使用弹性云服务器的Linux实例手工搭建Magento电子商务系统。Magento是一款开源电子商务系统,设计灵活,具有模块化架构体系和丰富的功能,可为大中型站点提供解决方案。Magento采用PHP开发,数据存储使用MySQL。

您还可以选择Solution as Code一键式部署方式自动部署Magento电子商务网站,详细内容请参见基于Magento快速构建电商网站

前提条件

  • 已购买一台弹性云服务器,且已为其绑定弹性公网IP。
  • 弹性云服务器所在安全组添加了如下表所示的安全组规则,具体步骤参见为安全组添加安全组规则
    表1 安全组规则

    方向

    协议/应用

    端口/范围

    是否必须

    源地址

    入方向

    TCP(22)

    22

    允许SSH远程连接Linux弹性云服务器的客户端IP地址。如允许所有IP访问,请设置为0.0.0.0/0,出于安全考虑,不建议您设置为允许所有IP访问。

    入方向

    HTTP(80)

    80

    允许访问Magento网站的客户端IP地址。如允许所有IP访问,请设置为0.0.0.0/0。

    入方向

    MySQL(3306)

    3306

    允许远程访问MySQL数据库的客户端IP地址。如允许所有IP访问,请设置为0.0.0.0/0,出于安全考虑,不建议您设置为允许所有IP访问。

资源规划

本次实践所用的资源配置及软件版本如表2中所示。当您使用不同的硬件规格或软件版本时,本指导中的命令及参数可能会发生改变,需要您根据实际情况进行调整。

表2 资源规划

资源

类别

规格/版本

ECS配置

实例规格

c6s.large.2

vCPUs

2vCPUs

内存

4GiB

操作系统

CentOS 7.2

软件资源

Apache

2.4.6

MySQL

5.7

获取方式:

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

PHP

7.0.33

获取方式:

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

Composer

1.10.19

获取方式:

https://getcomposer.org/installer

Magento

2.1.0

获取方式:

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

为保证所搭建网站正常运行,所选的ECS实例规格建议为2GiB及以上。

步骤一:安装配置Apache

  1. 远程登录弹性云服务器,操作方法请参考登录Linux弹性云服务器
  2. 为了更好的获取和更新系统和软件,建议您更新镜像源为华为云镜像源,详细操作,请参见如何使用自动化工具配置华为云镜像源(x86_64和ARM)?
  3. 使用root用户执行以下命令更新软件包,并安装Apache。

    yum -y update
    yum -y install httpd

    如果执行命令过程中报错,提示无法解析域名时,可以在“/etc/resolv.conf”配置文件中增加一个DNS服务器。

  4. 打开Apache配置文件。

    vim /etc/httpd/conf/httpd.conf

    如果没有安装vim,可使用yum install -y vim*命令进行安装。

  5. i键进入编辑模式,修改如下两处内容:

    • 将下图所示位置的配置参数“AllowOverride None ”改为“ AllowOverride all ”:

    • 在配置文件末新增一行配置参数:
      LoadModule rewrite_module modules/mod_rewrite.so

      如下图所示:

  6. Esc键退出编辑模式,并输入:wq保存后退出。
  7. 依次执行以下命令,启动Apache服务并设置开机自启动。

    systemctl start httpd
    systemctl enable httpd

步骤二:安装配置MySQL

  1. 使用root用户执行以下命令添加yum源。

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

  2. 安装MySQL。

    yum -y install mysql-community-server --nogpgcheck

  3. 依次执行以下命令,启动MySQL服务并设置开机自启动。

    systemctl start mysqld
    systemctl enable mysqld

  4. 执行以下命令,获取安装MySQL时自动设置的root用户密码。

    grep 'temporary password' /var/log/mysqld.log
    回显如下类似信息,其中“(n?K7jP#cirM”即为临时密码。
    2019-05-09T11:29:42.365419Z 1 [Note] A temporary password is generated for root@localhost: (n?K7jP#cirM

  5. 执行以下命令加固MySQL。

    mysql_secure_installation

    按照回显提示信息进行操作,具体操作如粗体所示:

    Securing the MySQL server deployment.
    
    Enter password for user root:    #输入上一步骤中获取的安装MySQL时自动设置的root用户密码
    The existing password for the user account root has expired. Please set a new password.
    
    New password:                    #设置root用户密码
    
    Re-enter new password:           #再次输入密码
    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   #是否更改root用户密码,输入Y
    
    New password:    #输入密码,长度为8至30个字符,须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
    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    #输入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   #是否删除匿名用户,输入Y
    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   #禁止root远程登录,输入Y
    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   #是否删除test库和对它的访问权限,输入Y
     - 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   #是否重新加载授权表,输入Y
    Success.
    
    All done!

  6. 以root用户登录MySQL数据库。

    mysql -u root -p

  7. 创建一个新的数据库,其中“magento”为自定义的数据库名称。

    CREATE DATABASE magento;

  8. 执行以下命令,为数据库创建用户并为用户分配数据库的完全访问权限。

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

    其中,“magento”为上一步骤中创建的数据库名,“magentouser”为新建的数据库用户名,“xxxxx”为对应的账户密码,可以自行设置。

  9. 执行以下命令,退出MySQL命令行。

    exit

  10. (可选)依次执行以下步骤,验证数据库和用户是否已成功创建,并退出MySQL命令行。

    1. 以新建的数据库用户“magentouser”登录MySQL命令行。
      mysql -u magentouser -p
    2. 查看数据库。
      SHOW DATABASES;

      回显如下,“magento”即为新建的数据库,说明数据库已成功创建。

      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | magento            |
      +--------------------+
      2 rows in set (0.00 sec)
    3. 执行以下命令,退出MySQL命令行。
      exit

步骤三:安装配置PHP

  1. 添加ius源和epel源。

    yum install \
    https://repo.ius.io/ius-release-el7.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

  2. 添加Webtatic源。

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

  3. 安装PHP7和用于Magento安装的一些必需的PHP扩展。

    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. 执行以下命令,验证PHP的安装版本。

    php -v

    回显如下类似信息:

    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. 打开PHP配置文件“php.ini”

    vim /etc/php.ini

  6. i键进入编辑模式,修改如下两处内容:

    • 根据实际情况修改参数“memory_limit”的值,设置内存限制,如下图所示:

    • 取消注释并设置参数“date.timezone”的值,指定PHP的时区,如下图所示:

  7. Esc键退出编辑模式,并输入:wq保存后退出。
  8. 执行以下命令,重启Web服务进程。

    systemctl restart httpd

步骤四:安装Composer工具

Composer是PHP编程语言的包管理器,提供管理PHP软件依赖和所需库的标准格式。

  1. 由于Magento2的依赖为Composer1.x版本,所以需确保安装的Composer版本的1.x版本。

    执行以下命令安装指定版本的Composer,并将安装路径指定为“/usr/bin/”以供全局使用。
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php --install-dir=/usr/bin/ --filename=composer --version=1.10.19

    回显如下:

    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. 执行以下命令,验证Composer否成功安装。

    composer -v

    回显如下:

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

步骤五:安装Magento组件

安装Magento时可以选择是否安装示例数据。如果安装Magento仅用于测试,可以选择安装示例数据。如果是在生产环境中安装Magento,建议安装全新的Magento,并从初始进行配置。

  1. 执行以下命令,安装git。

    yum -y install git

  2. 依次执行以下命令,进入Web服务器的默认根目录“/var/www/html/”并使用git下载Magento。

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

  3. 执行以下命令,将Magento切换到稳定版本。

    默认情况下前一步骤的命令会安装最新开发版本的Magento。在生产环境中使用时,建议切换到稳定版本,否则将来将无法进行升级。

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

    回显如下类似信息:

    Switched to a new branch '2.1.0'

  4. 执行以下命令,将安装文件移到Web服务器的根目录“/var/www/html/”下,其中“/var/www/html/magento2/”为Magento的当前安装目录。

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

    迁移后可以通过“http://magento服务器的IP地址”访问Magento站点。否则,只能通过“http://magento服务器的IP地址/magento2”访问Magento站点。

  5. 依次执行以下命令,设置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. 安装unzip和zip。

    yum install -y unzip zip

  7. 进入Web服务器的默认根目录“/var/www/html/”,然后使用composer工具安装Magento。

    cd /var/www/html/
    composer install

  8. 安装完成后,可通过浏览器访问“http://magento服务器IP地址”,显示如下页面,说明Magento安装成功。

步骤六:配置Magento

  1. 通过浏览器访问“http://magento服务器IP地址”,单击“Agree and Setup Magento”,开始配置Magento。
  2. 单击“Start Readiness Check”,进行环境检测。检测通过后单击“Next”

  3. 填写步骤二:安装配置MySQL中创建的MySQL数据库账户“magentouser”和密码,以及对应的数据库“magento”,并单击“Next”

    如下图所示:

  4. 设置网站的访问地址和后台管理地址,并单击“Next”

    如下图所示:

    后台管理地址可自定义,请牢记设置的后台管理地址。

  5. 设置语言和时区信息,并单击“Next”

    如下图所示:

  6. 设置管理账号和密码,并单击“Next”

    如下图所示:

  7. 单击“Install Now”,等待系统安装完成。

    显示如下界面时,表示系统安装成功。

  8. 登录Magento服务器,设置cron来运行预定的作业。

    crontab -u apache -e

  9. i键进入编辑模式,添加如下内容。

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

    Esc键退出编辑模式,并输入:wq保存后退出。

    更多关于Magento使用cron作业的内容,请参见Magento官方文档

  10. 打开客户端的计算机,使用浏览器访问“http://magento服务器IP地址”,显示如下图所示的默认界面。

  11. 使用浏览器访问“http://magento后台管理地址”,并使用配置Magento过程中设置的管理账号和密码登录,登录成功后显示如下图所示的界面。

    如果登录后界面提示“One or more indexers are invalid. Make sure your Magento cron job is running.”的错误信息,可在Magento根目录“/var/www/html”执行php bin/magento indexer:reindex命令。

    更多关于Magento配置信息,请参见Magento官方文档

步骤七:其余操作

  1. 购买域名。

    为了便于网站的访问和使用,可以给网站设置一个单独的域名,使用域名访问网站。首先,需要在域名注册商处获得授权的域名。

  2. 进行备案。

    如果网站未进行备案,且需要使用华为云进行托管,则需要通过华为云备案系统进行备案。具体操作请参见如何进行备案

  3. 配置域名解析。

    配置域名解析后才能使用注册的域名访问网站。具体操作请参见配置域名解析

    例如,配置的域名为“www.example.com”,配置成功后,可在浏览器地址栏中输入“http://www.example.com”访问部署的网站。

分享:

    相关文档

    相关产品