目录
前言
外贸独立站是当今外贸行业的一个热点,应一个外贸创业朋友的要求来帮忙搭建一个独立站。主流的开源独立站有Saleor、Magento等,它们都基于MVC框架开发。前者使用python开发,后者使用PHP开发,忙活了一两周后,还是没有搭建起Saleor独立站,于是转向Magento,经过几天摸索后终于搭建完成。
搭建Magent的软硬件环境如下:
操作系统:Ubuntu 18.04 x64
Web应用:Apache 2.4.29
数据库:MySQL 5.7.30
脚本语言:PHP 7.2.24
管理工具:Composer 1.6.3
商务系统:Magento2 2.3.4
一、Magento是什么?
Magento是一套专业开源的电子商务系统。设计得非常灵活,具有模块化架构体系和功能。易于与第三方应用系统无缝集成。其面向企业级应用,可处理各方面的需求,以及建设一个多种用途和适用面的电子商务网站。 包括购物、航运、产品评论等等,充分利用开源的特性,提供代码库的开发,非常规范的标准,易于与第三方应用系统无缝集成。
二、Magento框架
Magento2在Magento1的基础上进行了显著优化,它具有MVVM框架,在模型层和视图层之间提供了更强大的分离,主要分为“表示层”、“服务层”、“域层”和“持久层”这四层。
2.1 表示层
Magento的Web界面,它包含所有控制器和视图元素,例如布局,模板,块和css.js等等
2.2 服务层
表示层和域层之间的中间层,服务层实现使用 PHP接口定义的服务协定。这些服务协定允许我们在不破坏系统的情况下添加或更改业务逻辑资源模型。它通过依赖关系注入配置文件(di.xml)来实现。
2.3 域层
域层不包含特定于数据库或特定于资源的信息,它仅定义了包含业务逻辑的通用Magento数据对象或模型。可对特定类型的数据执行的操作。
2.4 持久层
持久层描述了资源模型,该模型负责使用CRUD(创建、读取、更新和删除)请求在数据库中提取和修改数据。
三、Magento搭建
3.1 准备工作
3.1.1 版本匹配要求
Magento版本 | PHP版本 |
Magento 2.4.x | ≥ PHP 7.4.x |
Magento 2.3.x | ≥ PHP 7.2.x |
Magento 2.2.x | ≥ PHP 7.0.2 |
3.1.2 更新数据源
使用Ubunut系统默认的数据源,下载数据时可能会很慢,需要手动更新,编辑sources.list文件。
# file backup
cp /etc/apt/sources.list /etc/apt/sources.list.bak
# clear sources.list
echo "" > /etc/apt/sources.list
# open and edit files
vim /etc/apt/sources.list
清空sources.list,并添加阿里数据源,内容如下。
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
数据源内容粘贴完成后,执行update命令,从服务器获取软件列表,并在本地保存为文件。
sudo apt-get update
3.1.3 删除系统更新锁
当再次使用apt命令,可能会出现错误,如下:
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
原因是程序会占用apt-get install进程,需要手动解锁。
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*
sudo dpkg --configure -a
3.2 安装&配置Apache
3.2.1 安装apache2
# download apache2
sudo apt install -y apache2
安装完成后,查看其版本
sudo apachectl -v
sudo service apache2 status
#sudo apachectl -v
Server version: Apache/2.4.29 (Ubuntu)
Server built: 2023-03-08T17:34:33# sudo service apache2 status
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Fri 2023-09-29 04:20:15 PDT; 1min 42s ago
Main PID: 4566 (apache2)
Tasks: 55 (limit: 4633)
CGroup: /system.slice/apache2.service
├─4566 /usr/sbin/apache2 -k start
├─4567 /usr/sbin/apache2 -k start
└─4568 /usr/sbin/apache2 -k startSep 29 04:20:15 ubuntu systemd[1]: Starting The Apache HTTP Server...
Sep 29 04:20:15 ubuntu apachectl[4552]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Sep 29 04:20:15 ubuntu systemd[1]: Started The Apache HTTP Server.
3.2.2 配置apache2
apache2安装完成后,需要设置/var/www/节点的重写权限,将<Directory /var/www/>的AllowOverride None修改为AllowOverride All,否则,安装Magento2后,首页不能加载图标和链接信息,后台管理页面启动时报404错误(Not Found, The requested URL was not found on this server),显示为空白页。解决的办法是启用rewrite模块,修改apache2配置,并重启apache2。
执行命令,开启rewrite模块。
a2enmod rewrite
修改apache2配置,允许对/var/www目录的重写权限。
sudo vim /etc/apache2/apache2.conf
#sudo vim /etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
重启apache2服务。
service apache2 restart
3.3 安装&配置MySQL
3.3.1 安装mysql
使用apt安装mysql并查看其版本信息。
# download MySQL
sudo apt install mysql-server
# mysql -V
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
3.3.2 配置MySQL数据库
安装完成后,进入MySQL并配置数据库,创建账号为magento2
、密码为magento2
的用户。
CREATE DATABASE magento2;
GRANT ALL ON magento2.* TO magento2@localhost IDENTIFIED BY 'magento2';
FLUSH PRIVILEGES;
终端详细操作如下:
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE DATABASE magento2;
Query OK, 1 row affected (0.00 sec)mysql> GRANT ALL ON magento2.* TO magento2@localhost IDENTIFIED BY 'magento2';
Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)mysql> quit
Bye
3.4 安装PHP及扩展
3.4.1 安装php
使用apt安装PHP,并查看其版本信息。
# download php
sudo apt install -y php
# php -v
PHP 7.2.24-0ubuntu0.18.04.17 (cli) (built: Feb 23 2023 13:29:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.17, Copyright (c) 1999-2018, by Zend Technologies
3.4.2 安装php扩展
PHP扩展是一种用C语言编写的动态链接库,可以增强PHP的功能。PHP扩展通常包含在PHP的安装包中,也可以作为独立的插件进行安装。可以使用apt安装php扩展。
sudo apt-get install -y php7.2-fpm php7.2-cli php7.2-mysql php7.2-gd php7.2-imagick php7.2-recode php7.2-tidy php7.2-xmlrpc php7.2-common php7.2-curl php7.2-mbstring php7.2-xml php7.2-bcmath php7.2-bz2 php7.2-intl php7.2-json php7.2-readline php7.2-zip php7.2-dom php7.2-soap
3.5 安装Composer
Composer是PHP中用来管理依赖关系的工具。你可以在自己的项目中声明所依赖的外部工具库,Composer会帮你安装这些依赖的库文件。可以使用apt安装composer,并查看其版本信息。
# download composer
sudo apt install -y composer
# composer -V
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer 1.6.3 2018-01-31 16:28:17
若apache运行成功,在浏览器中输入localhost,可以查看到apache默认页面。
3.6 安装&配置Magento2
3.6.1 下载magento2源码
从我的gitee下载magento2源码分支2.3.4到/var/www/html目录,并使用composer进行安装。
cd /var/www/html/
# Pull code from specified branch
git clone -b 2.3.4 https://gitee.com/lingdan_2008/magento2
cd /var/www/html/magento2
查看magent2目录下的内容清单,如下:
# ls /var/www/html/magento2/
app COPYING.txt lib pub
auth.json.sample dev LICENSE_AFL.txt README.md
bin generated LICENSE.txt SECURITY.md
CHANGELOG.md grunt-config.json.sample nginx.conf.sample setup
composer.json Gruntfile.js.sample package.json.sample var
composer.lock index.php phpserver vendor
3.6.2 配置magento2权限
magento2运行目录需要配置访问权限,否则,访问magento2应用时会出现404错误。
chown -R 777 /var/www/html/magento2
find /var/www/html/magento2 -type f -print0 | xargs -r0 chmod 647
find /var/www/html/magento2 -type d -print0 | xargs -r0 chmod 757
chmod -R g+w /var/www/html/magento2/{pub,var}
chmod -R g+w /var/www/html/magento2/{app/etc,vendor}
chmod 750 /var/www/html/magento2/bin/magento
3.6.3 使用composer安装magento2
sudo composer install
经过一段长时间的下载,magento2所有依赖安装完毕。
重启apache2服务,否则,magento2的主页会显示为空白页。
service apache2 restart
在浏览器上输入IP/magent2进入magent2安装配置页面。
同意启动Magento后,进入安装步骤界面,如下:
点击“下一步”按钮, 进行准备就绪检查,如下:
点击“下一步”,进入数据库配置页面,按3.3章节的配置MySQL数据库详情,填入相应的信息如,如下。
继续“下一步”,配置WEB地址页面,如下:
继续“下一步”,使用默认配置,如下:
继续“下一步”,创建管理员账号,如下:
继续“下一步”, 进入安装页面,并启动安装,如下:
安装完成,分别点击“我的商店”和“管理员地址”地址链接,查看其页面。
主页效果图,如下图:
后台管理员界面,如下图:
总结
magento安装比较容易,但仍需要注意它与特定PHP版本的匹配要求,以及apache2对网站应用程序目录的读写权限配置。