手动搭建Magento电商网站

Magento是一个用PHP编写的开源电子商务平台。它的架构是可扩展和模块化的,使其成为构建大中型网站的绝佳选择。Magento支持从5.6到7.1的PHP版本,并利用MySQL数据库进行数据存储。本文将为您介绍如何在CentOS 7操作系统的ECS实例上搭建Magento电商网站。

先决条件

• 创建阿里云账号。如需创建阿里云账号,请进入注册阿里云页面

• 在ECS实例的安全组中添加入方向规则,允许80和3306端口的流量。关于如何添加安全组规则,请参考添加安全组规则

注意 :在大多数情况下,客户端位于LAN内,可以将其专用IP地址映射到公共IP地址,以便与外部资源进行通信。因此,ipconfigifconfig 命令返回的 IP 地址可能不是客户端的实际公网 IP 地址。如果客户端在构建后无法访问Magento网站,请验证客户端的公网IP地址。

|----------|--------|-----------------|-----------|----------|----------------------------------------------------------------------------------------------|
| 规则方向 | 行动 | 协议类型 | 端口范围 | 授权类型 | 授权对象 |
| 入境 | 允许 | HTTP的 (80) | 80/80 | IPv4 网段 | CIDR 块包含需要访问 Magento 网站的所有客户端的公共 IP 地址。网段之间用逗号(,)分隔。 要允许所有客户端访问Magento网站,请指定0.0.0.0/0作为授权对象。 |
| 入境 | 允许 | MySQL数据库 (3306) | 3306/3306 | IPv4 网段 | 包含需要访问MySQL数据库服务的所有客户端的公网IP地址的网段。网段之间用逗号(,)分隔。 要允许所有客户端访问Magento网站,请指定0.0.0.0/0作为授权对象。 |

背景信息

在本文中,我们将使用具有以下配置的 ECS 实例:

• 实例规格:ecs.c6.large
• 操作系统:CentOS 7.2 64位公共镜像
• CPU:2 个 vCPU• 内存:4GiB
• 网络类型:VPC•

IP 地址:公网 IP 地址

**注意:**如果您打算构建 Magento 服务器,则所选实例类型的最小内存必须为 2 GiB。

对于示例过程,使用以下软件版本:

• Apache HTTP 服务器:2.4.6
• MySQL:5.7• PHP:7.0

• 作曲家:1.8.5
• Magento:2.1

如果您使用的是不同的软件版本,则可能需要相应地调整命令和参数设置。

程序

按照以下步骤在阿里云ECS实例上搭建Magento电子商务网站:

• 第 1 步:安装和配置 Apache HTTP 服务器
• 第 2 步:安装和配置 MySQL
• 第 3 步:安装和配置 PHP
• 第 4 步:创建 Magento 数据库
• 第 5 步:安装和配置 Composer
• 第 6 步:安装和配置 Magento• 第 7 步:配置 Magento
客户端
• 第 8 步:添加 cron 作业

步骤 1:安装和配置 Apache HTTP 服务器

  1. 安装 Apache。

a) 运行以下命令以安装 Apache:

复制代码
`yum install httpd -y`

b) 执行以下命令,查看Apache是否安装成功。

复制代码
`httpd -v`

以下命令输出结果表示已安装 Apache。

  1. 配置 Apache。

a) 执行以下命令,打开Apache的配置文件。

复制代码
`vim /etc/httpd/conf/httpd.conf`

b) 在下面添加。LoadModule rewrite_module modules/mod_rewrite.so``Include conf.modules.d/*.conf

执行以下步骤:

i) 将指针移动到该行下方的行首。
ii) 按 i 键进入编辑模式。
iii) 输入 。Include conf.modules.d/*.conf``LoadModule rewrite_module modules/mod_rewrite.so

添加的内容如下图所示。

c) 将以下内容替换为 。AllowOverride None``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`

替换结果如下图所示。

d) 按Esc 键,输入:wq,然后按Enter键保存并关闭配置文件。

  1. 运行以下命令以启动 Apache。
复制代码
`systemctl start httpd`
  1. 运行以下命令,将 Apache 配置为在系统启动时运行:
复制代码
`systemctl enable httpd`

步骤 2:安装和配置 MySQL

  1. 安装 MySQL。

a) 运行以下命令以添加 MySQL YUM 存储库。

复制代码
`rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm`

b) 执行以下命令,安装MySQL。

复制代码
`yum -y install mysql-community-server --nogpgcheck`
  1. 运行以下命令以启动 MySQL。
复制代码
`systemctl start mysqld`
  1. 运行以下命令,使MySQL能够在系统启动时运行。
复制代码
`systemctl enable mysqld`
  1. 配置 MySQL。

a) 运行以下命令查看 /var/log/mysqld.log 文件并记录 root 用户的初始密码。

复制代码
`grep 'temporary password' /var/log/mysqld.log`

返回结果如下:

复制代码
`2016-12-13T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD`

注意:重置 root 用户的密码时,将使用此初始密码。

b) 执行以下命令,配置MySQL的安全设置。

复制代码
`mysql_secure_installation`

具体操作如下:

i) 设置root账户的密码。

复制代码
``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) 输入以删除匿名用户帐户。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  # Enter Y to delete the anonymous user.
Success.`

iii) 输入以拒绝 root 用户的远程访问。Y

复制代码
`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) 输入以删除测试数据库和对数据库的访问权限。Y

复制代码
`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) 输入以重新加载权限表。Y

复制代码
`Reload privilege tables now?(Press y|Y for Yes, any other key for No): Y # Enter Y to reload privilege tables.
Success.
All done!`

有关更多信息,请参阅 MySQL 文档

第 3 步:安装和配置 PHP

  1. 安装 PHP。

a) 运行以下命令以添加 IUS 存储库。

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

b) 运行以下命令以添加 Webtatic 存储库。

复制代码
`rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm`

c) 运行以下命令以安装 PHP7 和所有必需的扩展。

复制代码
`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) 执行以下命令,查看PHP版本。

复制代码
`php -v`

以下命令输出表示PHP已安装:

复制代码
`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`
  1. 配置 PHP。

a) 运行以下命令以打开 PHP 配置文件。

复制代码
`vim /etc/php.ini`

b) 将指针移动到最后一行的末尾。具体操作如下:

i) 输入并按 Enter 键将指针移动到文件的最后一行。:$

ii) 按 将指针移动到行尾。$

c) 按 I 键进入编辑模式。

d) 在文件末尾添加内存限制和时区的配置。

复制代码
`; 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`

结果如下图所示。

e) 按 Esc 键,回车,然后按 Enter 键保存并关闭配置文件。:wq

f) 重新启动 Apache。

复制代码
`systemctl restart httpd`

步骤 4:创建 Magento 数据库

  1. 执行以下命令,使用root用户的账号和密码登录MySQL。
复制代码
`mysql -u root -p`
  1. 运行以下命令以创建数据库:magento
复制代码
`mysql> CREATE DATABASE magento; # Replace magento with the name of database that you want to create.`
  1. 依次执行以下命令,为数据库创建用户。magento
复制代码
`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;`

例如,若要创建名为 的帐户并将其密码设置为 ,请运行以下命令:magentoUser``magentoUser1@3

复制代码
`mysql> GRANT ALL ON magento.* TO magentoUser@localhost IDENTIFIED BY 'magentoUser1@3';
mysql> FLUSH PRIVILEGES;`
  1. 输入exit ,然后按Enter键退出MySQL。

5.**可选:**检查新的Magento数据库和用户帐户是否可用。执行以下步骤:

a) 执行以下命令,使用新账号和密码登录MySQL。

复制代码
`mysql -u <YourUser> -p   # Replace <YourUser> with the account that you created.`

b) 运行以下命令以查看新的 magento 数据库:

复制代码
`mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| magento            |
+--------------------+
2 rows in set (0.00 sec)`

c) 运行以下命令,然后按Enter键退出MySQL:

复制代码
`mysql> exit`

步骤 5:安装和配置 Composer

Composer 是 PHP 的依赖管理工具。Composer 可以识别用作项目基础的代码存储库。它还可用于安装项目的依赖代码存储库。

  1. 运行以下命令以安装 Composer。
复制代码
`curl -sS https://getcomposer.org/installer | php`
  1. 运行以下命令以配置 Composer 以供全局使用。
复制代码
`mv /root/composer.phar /usr/bin/composer`
  1. 运行命令 composer -v 以检查 Composer 版本。以下命令输出结果表示已安装 Composer:
复制代码
``/ ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.8.5 2019-04-09 17:46:47``

注意:最新的 Composer 版本与 Magento 不兼容。因此,您必须安装与 Magento 兼容的版本的 Composer。执行以下命令,将最新的Composer版本转换为兼容Magento的版本,例如Composer 1.8.5。

复制代码
`composer self-update 1.8.5`

步骤6:安装和配置Magento

您可以使用不同的方法安装Magento,并确定是否安装示例数据。

• 如果仅出于测试目的安装Magento,则可以安装示例数据。

• 如果您出于生产目的安装Magento,我们建议您安装Magento并从头开始配置它。

在此示例中,Git 用于下载 Magento,Composer 用于安装 Magento。

  1. 下载Magento。

a) 运行以下命令以安装 Git:

复制代码
`yum -y install git`

b) 转到 Web 服务器的默认根目录。

复制代码
`cd /var/www/html/`

c) 下载Magento。

复制代码
`git clone https://github.com/magento/magento2.git`
  1. 可选:执行以下命令,将Magento切换到稳定版本。
复制代码
`cd magento2 &&  git checkout tags/2.1.0 -b 2.1.0`

返回结果如下:

复制代码
`Switched to a new branch '2.1.0'`

注意:默认情况下,Git 会下载并安装最新的 Magento 版本。如果您在生产环境中使用Magento,建议您将Magento切换到稳定版本。否则,将来升级和安装Magento时可能会出现问题。

  1. 运行以下命令,将安装文件移动到 Web 服务器的根目录。
复制代码
`shopt -s dotglob nullglob && mv /var/www/html/magento2/* /var/www/html/ && cd ..`

注意 :运行此命令后,可以使用 访问Magento网站。否则,您只能使用 .https://<ECS Public IP address>``https://<ECS Public IP address>/magento2

  1. 依次执行以下命令,为Magento文件配置相应的权限。
复制代码
`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`
  1. 运行命令以安装 Magento。composer install

步骤 7:配置 Magento 客户端

  1. 打开浏览器。

  2. 在浏览器地址栏中,输入 。以下页面指示已安装Magento。http://<ECS Public IP address>

  1. 单击**"同意并设置**Magento"以开始配置Magento。执行以下步骤:

a) 检查准备情况。

i) 单击**"Start Readiness Check"(开始准备情况检查**)。

ii) 检查完成后,单击**"下一步**"。

b) 添加数据库。

i) 输入您创建的数据库的账号和密码。在此示例中,用户帐户是 ,密码是 。magentoUser``magentoUser1@3

ii) 输入已创建数据库的名称。在此示例中,数据库名称为 。magento

iii) 单击**"下一步**"。

c) 完成 Web 访问的设置,然后单击下一步

d) 输入自定义商店信息,然后单击下一步

e) 输入管理员帐户信息,然后单击下一步

f) 单击**"立即**安装"以安装 Magento。

以下页面指示已配置Magento。

步骤 8:添加 cron 作业

执行以下步骤以添加 cron 作业。

  1. 执行命令,配置cron操作的作业。crontab -u apache -e

  2. i 键进入编辑模式。

  3. 输入以下配置。

复制代码
`*/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`
  1. Esc 键,回车键,然后按 Enter 键保存并关闭配置文件。:wq

有关如何在 Magento 中使用 cron 作业的详细信息,请参阅配置和运行 cron

常见问题

当您在浏览器中输入连接到Magento网站时,如果错误消息"一个或多个索引器无效。请确保您的 Magento cron 作业正在运行",请执行以下操作:http://<ECS Public IP address>/admin

1.连接到Magento服务器。有关详细信息,请参阅连接方法

  1. 运行以下命令以创建从 PHP 安装路径到 /usr/sbin/php 目录的符号链接:
复制代码
`ln -s /usr/local/php/bin/php /usr/sbin/php`
  1. 运行以下命令以刷新索引。
复制代码
`cd /var/www/html
php bin/magento indexer:reindex`

类似于以下命令输出表示索引已刷新。

复制代码
`[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`
  1. 刷新页面,然后单击缓存管理
  1. 选择处于"INVALIDATED "状态的"缓存类型 ",然后单击**"提交"。**

类似于以下命令输出表示问题已修复。

下一步做什么

• 访问查看默认主页,如下图所示。http://<ECS Public IP address>

• 访问并输入您在安装过程中设置的用户名和密码。登录管理控制台后,将显示以下页面。http://<ECS Public IP address>/admin

相关推荐
唐豌豆2 分钟前
MySQL 数据库迁移方法汇总
数据库·mysql
深光标准技术13 分钟前
OpenHarmony开源鸿蒙兼容性测试常见问题解答分享
华为·开源·harmonyos
曼岛_1 小时前
[架构之美]从PDMan一键生成数据库设计文档:Word导出全流程详解(二十)
数据库·word
GIS之路1 小时前
shp2pgsql 导入 Shp 到 PostGIS 空间数据库
数据库
惊起白鸽4501 小时前
安装PostgresSQL
数据库
数据库幼崽2 小时前
MySQL 8.0 OCP 1Z0-908 161-170题
数据库·mysql·ocp
数据库幼崽2 小时前
MySQL 8.0 OCP 1Z0-908 151-160题
数据库·mysql·ocp
消失在人海中2 小时前
Oracle资源管理器
数据库·oracle
文牧之2 小时前
Oracle RAC 中的 RBAL 进程
运维·数据库·oracle
zfj3217 小时前
H2数据库源码学习+debug, 数据库 sql、数据库引擎、数据库存储从此不再神秘
java·数据库·sql·学习·数据库底层原理