手动搭建Magento电商网站

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

先决条件

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

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

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

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

2. 配置 Apache。

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

vim /etc/httpd/conf/httpd.conf

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

执行以下步骤:

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

添加的内容如下图所示。

2

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

替换结果如下图所示。

3

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

3. 运行以下命令以启动 Apache。

systemctl start httpd

4. 运行以下命令,将 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

2. 运行以下命令以启动 MySQL。

systemctl start mysqld

3. 运行以下命令,使MySQL能够在系统启动时运行。

systemctl enable mysqld

4. 配置 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

2. 配置 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

结果如下图所示。

4

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

f) 重新启动 Apache。

systemctl restart httpd

步骤 4:创建 Magento 数据库

1. 执行以下命令,使用root用户的账号和密码登录MySQL。

mysql -u root -p

2. 运行以下命令以创建数据库:magento

mysql> CREATE DATABASE magento; # Replace magento with the name of database that you want to create.

3. 依次执行以下命令,为数据库创建用户。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;

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

mysql> GRANT ALL ON magento.* TO magentoUser@localhost IDENTIFIED BY 'magentoUser1@3';
mysql> FLUSH PRIVILEGES;

4. 输入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

2. 运行以下命令以配置 Composer 以供全局使用。

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

3. 运行命令 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

2. 可选:执行以下命令,将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时可能会出现问题。

3. 运行以下命令,将安装文件移动到 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

4. 依次执行以下命令,为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

5. 运行命令以安装 Magento。composer install

步骤 7:配置 Magento 客户端

1. 打开浏览器。

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

5

3. 单击“同意并设置Magento”以开始配置Magento。执行以下步骤:

a) 检查准备情况。

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

ii) 检查完成后,单击“下一步”。

6

b) 添加数据库。

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

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

iii) 单击“下一步”。

7

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

8

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

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

f) 单击“立即安装”以安装 Magento。

以下页面指示已配置Magento。

9

步骤 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

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

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

常见问题

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

10

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

2. 运行以下命令以创建从 PHP 安装路径到 /usr/sbin/php 目录的符号链接:

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

3. 运行以下命令以刷新索引。

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

4. 刷新页面,然后单击缓存管理

11

5. 选择处于“INVALIDATED”状态的“缓存类型”,然后单击“提交”。

12

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

13

下一步做什么

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

14

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

15

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值