本页内容

  • 准备工作
  • 第 1 步 - 配置防火墙
  • 第 2 步 - 安装 PHP 及其扩展
  • 第 3 步 - 安装 Composer
  • 第 4 步 - 安装 MariaDB
  • 第 5 步 - 配置 MariaDB
  • 第 6 步 - 安装 Nginx
  • 第 7 步 - 安装 SSL
  • 第 8 步 - 安装 Elasticsearch
  • 第 9 步 - 安装 Redis 服务器
  • 第 10 步 - 下载 Magento
  • 第 11 步 - 安装 Magento
  • 第 12 步 - 配置 PHP-FPM
  • 第 13 步 - 配置 Nginx
  • 第 14 步 - 禁用双因素认证
  • 第 15 步 - 访问管理门户
  • 第 16 步 - 启用并配置双因素认证
  • 结语

Magento 是一个用 PHP 编写的开源电子商务平台。它于 2018 年被 Adobe 收购。它也作为商业和基于云的产品提供。你可以使用 Magento 创建高容量的专业购物网站。它同时提供单店铺和多店铺两种模式。它附带大量模块以扩展其功能。

在本教程中,我们将安装 Magento 开源社区版。它提供了搭建专业在线商店所需的全部功能。我们还将安装 Elasticsearch 用于商品目录搜索,安装 Redis 用于会话和文件缓存,并使用 Nginx 服务器来提供服务。

准备工作

  • 一台运行 Debian 12 的服务器,至少 2GB 内存。根据你的需求,可能需要更多内存。
  • 一个具有 sudo 权限的非 root 用户。
  • 服务器的完全限定域名 (FQDN),magento.example.com
  • 确保已更新所有软件包。 $ sudo apt update $ sudo apt upgrade
  • 系统所需的少量软件包。$ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release debian-archive-keyring unzip -y 其中部分软件包可能已安装在你的系统上。

一台运行 Debian 12 的服务器,至少 2GB 内存。根据你的需求,可能需要更多内存。

一个具有 sudo 权限的非 root 用户。

服务器的完全限定域名 (FQDN),magento.example.com

确保已更新所有内容。

$ sudo apt update
$ sudo apt upgrade

系统所需的少量软件包。

$ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release debian-archive-keyring unzip -y

其中部分软件包可能已安装在你的系统上。

第 1 步 - 配置防火墙

第一步是配置防火墙。Debian 默认自带 ufw (Uncomplicated Firewall)。

检查防火墙是否正在运行。

$ sudo ufw status

你应该会看到类似以下的输出。

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

同时放行 HTTP 和 HTTPS 端口。

$ sudo ufw allow http
$ sudo ufw allow https

再次检查防火墙的状态。

$ sudo ufw status

你应该会看到类似的输出。

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

第 2 步 - 安装 PHP 及其扩展

Debian 12 默认附带 PHP 8.2 版本。你可以通过运行以下命令安装它以及 Magento 所需的扩展。

$ sudo apt install php-fpm php-cli php-mysql php-mbstring php-xml php-gd php-bcmath php-zip php-curl php-tidy php-intl php-soap php-xsl libsodium-dev libsodium23 libssl-dev libcurl4-openssl-dev

如果想始终使用最新版本的 PHP,或者想安装多个版本的 PHP,请添加 Ondrej 的 PHP 仓库。

首先,导入 Sury 仓库的 PHP GPG 密钥。

$ sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg

添加 Ondrej Sury 的 PHP 仓库。

$ sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

更新系统仓库列表。

$ sudo apt update	

接下来,安装 PHP 以及 Magento 所需的扩展。

$ sudo apt install php8.2-fpm php8.2-mysql php8.2-bcmath php8.2-xml php8.2-zip php8.2-curl php8.2-mbstring php8.2-gd php8.2-tidy php8.2-intl php8.2-cli php8.2-soap php8.2-xsl libsodium-dev libsodium23 libssl-dev libcurl4-openssl-dev

验证安装。

$ php --version
PHP 8.2.8 (cli) (built: Jul 16 2023 11:00:43) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.8, Copyright (c), by Zend Technologies

第 3 步 - 安装 Composer

Composer 是 PHP 的依赖管理工具,是安装 Magento 所必需的。

运行以下命令下载 Composer 二进制文件。Magento 需要 Composer 2.2 LTS,因此我们相应地修改了命令。

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php composer-setup.php --2.2
$ php -r "unlink('composer-setup.php');"

将二进制文件移动到 /usr/local/bin 目录来安装 Composer。

$ sudo mv composer.phar /usr/local/bin/composer

通过检查其版本来验证安装。

$ composer --version
Composer version 2.2.21 2023-02-15 13:07:40

第 4 步 - 安装 MariaDB

Debian 12 默认不自带 MySQL,且官方也尚未发布对应的软件包。因此,我们将使用 MariaDB 替代。MariaDB 同样没有针对 Debian 12 的官方软件包,但 Debian 自带了该软件。因此,请使用以下命令安装它。

$ sudo apt install mariadb-server

检查 MySQL 的版本。

$ mysql --version
mysql  Ver 15.1 Distrib 10.11.3-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

目前,Magento 不支持此版本的 MariaDB,且 Debian 12 的官方 MariaDB 软件包暂时不可用。因此我们将继续安装,并在稍后使用变通方法绕过该限制。

运行 MariaDB 安全安装脚本。

$ sudo mysql_secure_installation

系统会要求输入 root 密码。直接按 Enter,因为我们尚未为其设置任何密码。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):

接下来,系统会询问是否切换到 Unix socket 认证方式。unix_socket 插件允许你使用操作系统凭据连接 MariaDB 服务器。由于你已经拥有一个受保护的 root 账户,请输入 n 继续。

OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n

接下来,系统会询问是否要更改 root 密码。在 Debian 12 上,root 密码与自动化系统维护密切相关,因此应保持原样。输入 n 继续。

 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n

接下来,系统会提出一些用于提升 MariaDB 安全性的问题。输入 Y 以删除匿名用户、禁止远程 root 登录、删除测试数据库并重新加载权限表。

 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] 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? [Y/n] y
 ... Success!

By default, MariaDB 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? [Y/n] 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? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

你可以在命令行输入 sudo mysql 或 sudo mariadb 进入 MariaDB shell。

第 5 步 - 配置 MariaDB

登录到 MariaDB shell。

$ sudo mysql

为 Magento 创建数据库。

mysql> CREATE DATABASE magento;

创建一个 SQL 用户账户。

mysql> CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'Your_password2';

将该数据库的全部权限授予该用户。

mysql> GRANT ALL PRIVILEGES ON magento.* TO 'magentouser'@'localhost';

由于我们不修改 root 用户,你应该创建另一个用于执行管理任务、采用密码认证的 SQL 用户。请为其设置一个强密码。

MariaDB> GRANT ALL ON *.* TO 'navjot'@'localhost' IDENTIFIED BY 'Yourpassword32!' WITH GRANT OPTION;

刷新用户权限。

mysql> FLUSH PRIVILEGES;

退出 shell。

mysql> exit

第 6 步 - 安装 Nginx

Debian 12 自带较旧版本的 Nginx。要安装最新版本,你需要下载官方 Nginx 仓库。

导入 Nginx 的签名密钥。

$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

添加 Nginx 稳定版的仓库。

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

更新系统仓库。

$ sudo apt update

安装 Nginx。

$ sudo apt install nginx

验证安装。在 Debian 系统上,以下命令只能配合 sudo 使用。

$ sudo nginx -v
nginx version: nginx/1.24.0

启动 Nginx 服务器。

$ sudo systemctl start nginx

检查服务状态。

? nginx.service - nginx - high performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Wed 2023-08-02 06:45:52 UTC; 14s ago
       Docs: https://nginx.org/en/docs/
    Process: 18326 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
   Main PID: 18327 (nginx)
      Tasks: 2 (limit: 2315)
     Memory: 1.8M
        CPU: 11ms
     CGroup: /system.slice/nginx.service
             ??18327 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
             ??18328 "nginx: worker process"

第 7 步 - 安装 SSL

我们需要安装 Certbot 来生成 SSL 证书。你可以使用 Debian 的仓库安装 Certbot,或者通过 Snapd 工具获取最新版本。我们将使用 Snapd 版本。

Debian 12 默认未安装 Snapd。安装 Snapd 软件包。

$ sudo apt install snapd

运行以下命令,确保你的 Snapd 版本是最新的。

$ sudo snap install core && sudo snap refresh core

安装 Certbot。

$ sudo snap install --classic certbot

通过创建指向 /usr/bin 目录的符号链接,使用以下命令确保可以运行 Certbot 命令。

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

验证 Certbot 是否正常工作。

$ certbot --version
certbot 2.6.0

运行以下命令生成 SSL 证书。

$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m 
[email protected]
 -d magento.example.com

上述命令会将证书下载到你服务器上的 /etc/letsencrypt/live/magento.example.com 目录。

生成 Diffie-Hellman 组证书。

$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

检查 Certbot 续期调度服务。

$ sudo systemctl list-timers

你会发现 snap.certbot.renew.service 是计划运行的服务之一。

NEXT                        LEFT          LAST                        PASSED        UNIT                      ACTIVATES
.....
Wed 2023-08-02 08:09:00 UTC 9h left       Wed 2023-08-02 07:39:06 UTC 2h 59min ago  snap.certbot.renew.timer  snap.certbot.renew.service
Thu 2023-08-03 06:41:11 UTC 9h left       Wed 2023-08-02 06:24:33 UTC 10h ago       apt-daily-upgrade.timer   apt-daily-upgrade.service
Wed 2023-08-02 14:06:00 UTC 11h left      Sun 2021-11-14 02:03:02 UTC 5min ago      apt-daily.timer           apt-daily.service

对该过程进行一次试运行,以检查 SSL 续期是否正常工作。

$ sudo certbot renew --dry-run

如果没有看到错误,说明配置完成。你的证书将自动续期。

第 8 步 - 安装 Elasticsearch

Magento 使用 Elasticsearch 进行商品搜索。我们将通过其官方仓库安装 Elasticsearch 7.x,因为这是与 Magento 兼容的版本。

导入 Elasticsearch 的 GPG 密钥。

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

添加 Elasticsearch 仓库。

$ echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list

更新系统的仓库列表。

$ sudo apt update

安装 Elasticsearch。

$ sudo apt install elasticsearch

Elasticsearch 会占用大量内存。你需要根据服务器规模限制其使用量。创建文件 /etc/elasticsearch/jvm.options.d/memory.options 并打开进行编辑。

$ sudo nano /etc/elasticsearch/jvm.options.d/memory.options

在其中粘贴以下代码。根据你的服务器规模修改相应数值。第一个值表示初始内存,第二个值表示最大可用内存。对于 1GB 及以上,请使用 -Xms1g 格式。

-Xms512m
-Xmx784m

按 Ctrl + X 保存文件,并在提示时输入 Y。这将配置 Elasticsearch 使用 1GB 内存。你可以根据需要设置任意数值。

启动并启用该服务。

$ sudo systemctl enable elasticsearch --now

检查 Elasticsearch 是否正常运行。

$ curl http://localhost:9200

你应该会看到以下输出。

{
  "name" : "magento",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "LNFRevgvQIOGeWCdtvc7bA",
  "version" : {
    "number" : "7.17.12",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "e3b0c3d3c5c130e1dc6d567d6baef1c73eeb2059",
    "build_date" : "2023-07-20T05:33:33.690180787Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

第 9 步 - 安装 Redis 服务器

Magento 使用 Redis 进行会话和缓存存储。这完全是可选的,你也可以使用数据库来存储会话。但 Redis 的表现更好。最新版本的 Magento 可与 Redis 7.0 配合使用。Debian 自带 Redis 6.0,因此我们将使用 Redis 仓库进行安装。

导入官方 Redis GPG 密钥。

$ curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

将 APT 仓库添加到你的源列表。

$ echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

更新系统仓库列表。

$ sudo apt update

执行以下命令安装 Redis 服务器。

$ sudo apt install redis

确认 Redis 版本。

$ redis-server -v
Redis server v=7.0.12 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=d706905cc5f560c1

让我们使用以下命令验证服务连接。

$ redis-cli

你将被切换到 Redis shell。

第一步是为 Redis 默认用户设置密码。将 Your_Redis_Password 替换为你自己设置的强密码。请务必在密码前加上 > 字符。

127.0.0.1:6379> acl setuser default >Your_Redis_Password

测试 Redis 认证。

127.0.0.1:6379> AUTH Your_Redis_Password
OK

Ping 该服务。

127.0.0.1:6379> ping
PONG

输入 exit 退出该服务。

第 10 步 - 下载 Magento

为 Magento 创建 Web 根目录。

$ sudo mkdir /var/www/magento -p

将 Magento 目录的权限赋予当前用户。

$ sudo chown $USER:$USER /var/www/magento/ -R

在继续之前,你需要获取 Magento 仓库所需的认证密钥。访问网站 https://account.magento.com/,你将看到如下页面,要求你使用 Adobe ID 登录。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

点击 Sign in with Adobe ID 按钮进入如下页面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

如果你已有 Adobe ID,请输入凭据以继续,或者也可以在此创建一个账户。创建并登录账户后,打开 URL https://marketplace.magento.com/customer/accessKeys/ 。你也可以通过访问个人资料并点击 Access Keys 链接来进入该页面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

点击 Create A New Access Key 按钮创建你的认证密钥。为你的密钥起一个用于标识的名称。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

记下公钥和私钥,供下一步使用。

创建 ~/.config/composer/auth.json 文件并打开进行编辑。

$ nano ~/.config/composer/auth.json

在其中粘贴以下代码。使用公钥作为用户名,私钥作为密码。

{
"http-basic": {
        "repo.magento.com": {
                   "username": "<public_key>",
                   "password": "<private_key>"
              }

}
}

按 Ctrl + X 保存文件,并在提示时输入 Y。

切换到 /var/www/magento 目录。

$ cd /var/www/magento

创建 Magento 项目。注意命令末尾的句点。它表示运行该命令的当前目录。

$ composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

你应该会看到类似的输出。

Creating a "magento/project-community-edition" project at "./"
Installing magento/project-community-edition (2.4.6-p1)
  - Downloading magento/project-community-edition (2.4.6-p1)
  - Installing magento/project-community-edition (2.4.6-p1): Extracting archive
Created project in /var/www/magento/.
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Lock file operations: 565 installs, 0 updates, 0 removals
  - Locking 2tvenom/cborencode (1.0.2)
  - Locking adobe-commerce/adobe-ims-metapackage (2.2.0)
  - Locking allure-framework/allure-codeception (v2.3.0)
  - Locking allure-framework/allure-php-commons (v2.3.1)
  - Locking allure-framework/allure-phpunit (v2.1.0)
...............................................

Adobe 提供的示例 Nginx 配置文件存在一个问题。运行以下命令进行修复。

$ sed -i 's/php-fpm:9000/fastcgi_backend/g' /var/www/magento/nginx.conf.sample

运行以下命令设置文件权限,并使 Magento 二进制文件可执行。同时,将 Magento 目录的所有者设为 Nginx 用户,以便其能够访问网站。

$ sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
$ sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
$ sudo chown -R :nginx .
$ sudo chmod u+x bin/magento

第 11 步 - 安装 Magento

在进行安装之前,我们需要修改安装程序,使其允许我们使用当前 Magento 尚不支持的 MariaDB 10.11.3。迄今为止,Magento 支持 MariaDB 10.2-10.6 版本。

打开文件 /var/www/magento/app/etc/di.xml 进行编辑。

$ sudo nano /var/www/magento/app/etc/di.xml

搜索以下行。

<item name="MariaDB-(10.2-10.6)" xsi:type="string">^10\.[2-6]\.</item>

将其替换为以下代码。

<item name="MariaDB-(10.2-10.11)" xsi:type="string">^10\.([2-9]|10|11)\.</item>

按 Ctrl + X 保存文件,并在提示时输入 Y。

确保你位于 Magento 目录中。

$ cd /var/www/magento

运行以下命令安装 Magento。

$ bin/magento setup:install \
--base-url=http://magento.example.com \
--use-secure=1 \
--base-url-secure=https://magento.example.com \
--use-secure-admin=1 \
--db-host=localhost \
--db-name=magento \
--db-user=magentouser \
--db-password=Your_password2 \
--admin-firstname=Navjot \
--admin-lastname=Singh \

[email protected]
 \
--admin-user=navjot \
--admin-password=admin_password \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--elasticsearch-host=http://127.0.0.1 \
--elasticsearch-port=9200 \
--session-save=redis \
--session-save-redis-db=0 \
--session-save-redis-password=redis_password \
--cache-backend=redis \
--cache-backend-redis-db=2 \
--cache-backend-redis-password=redis_password \
--page-cache=redis \
--page-cache-redis-db=4 \
--page-cache-redis-password=redis_password

一旦过程完成,你将得到类似的输出。

.......
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_11xb2x
Nothing to import.

记下 Admin URI,稍后访问管理面板时会用到。

创建 Magento 定时任务 (cron jobs)。

$ php bin/magento cron:install

验证定时任务。

$ crontab -l

你应该会看到以下输出。

#~ MAGENTO START d1957f62aa710cc367525c9ec68dd7456d4311756b5aa37d2143c4a98b25318c
* * * * * /usr/bin/php8.2 /var/www/magento/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/magento/var/log/magento.cron.log
#~ MAGENTO END d1957f62aa710cc367525c9ec68dd7456d4311756b5aa37d2143c4a98b25318c

第 12 步 - 配置 PHP-FPM

打开文件 /etc/php/8.2/fpm/pool.d/www.conf 。

$ sudo nano /etc/php/8.2/fpm/pool.d/www.conf

我们需要将 PHP 进程的 Unix 用户/组设置为 nginx 。在文件中找到 user=www-data 和 group=www-data 这两行,并将它们改为 nginx 。

...
; Unix user/group of the child processes. This can be used only if the master
; process running user is root. It is set after the child process is created.
; The user and group can be specified either by their name or by their numeric
; IDs.
; Note: If the user is root, the executable needs to be started with
;       --allow-to-run-as-root option to work.
; Default Values: The user is set to master process running user by default.
;                 If the group is not set, the user's group is used.
user = nginx
group = nginx
...

在文件中找到 listen.owner = www-data 和 listen.group = www-data 这两行,并将它们改为 nginx 。

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. The owner
; and group can be specified either by name or by their numeric IDs.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = nginx
listen.group = nginx

按 Ctrl + X 保存文件,并在提示时输入 Y。

将 PHP-FPM 和 PHP-CLI 的执行时间提高到 180 秒。

$ sudo sed -i 's/max_execution_time = 30/max_execution_time = 180/' /etc/php/8.2/fpm/php.ini
$ sudo sed -i 's/max_execution_time = 30/max_execution_time = 180/' /etc/php/8.2/cli/php.ini

将 PHP-FPM 的内存限制从 128MB 提高到 256MB 。你可以根据服务器规模和需求提高该限制。

$ sudo sed -i 's/memory_limit = 128M/memory_limit = 256M/' /etc/php/8.2/fpm/php.ini

Magento 默认将媒体库的文件大小限制设为 2MB。运行以下命令将文件大小限制提高到 25MB。

$ sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 25M/g' /etc/php/8.2/fpm/php.ini
$ sudo sed -i 's/post_max_size = 8M/post_max_size = 25M/g' /etc/php/8.2/fpm/php.ini

开启 Zlib 压缩。

$ sudo sed -i 's/zlib.output_compression = Off/zlib.output_compression = On/g' /etc/php/8.2/fpm/php.ini

重启 PHP-FPM 服务。

$ sudo systemctl restart php8.2-fpm

将 PHP 会话目录的所属组改为 Nginx。

$ sudo chgrp -R nginx /var/lib/php/sessions

第 13 步 - 配置 Nginx

打开文件 /etc/nginx/nginx.conf 进行编辑。

$ sudo nano /etc/nginx/nginx.conf

在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下行。

server_names_hash_bucket_size  64;

按 Ctrl + X 保存文件,并在提示时输入 Y。

创建并打开文件 /etc/nginx/conf.d/magento.conf 进行编辑。

$ sudo nano /etc/nginx/conf.d/magento.conf

在其中粘贴以下代码。

upstream fastcgi_backend {
  server  unix:/run/php/php8.2-fpm.sock;
}

server {
  # Redirect any http requests to https
  listen 80;
  listen [::]:80;
  server_name magento.example.com;
  return 301 https://$host$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name magento.example.com;

  set $MAGE_ROOT /var/www/magento;
  include /var/www/magento/nginx.conf.sample;
  client_max_body_size 25m;

  access_log /var/log/nginx/magento.access.log;
  error_log  /var/log/nginx/magento.error.log;

  # TLS configuration
  ssl_certificate /etc/letsencrypt/live/magento.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/magento.example.com/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/magento.example.com/chain.pem;
  ssl_protocols TLSv1.2 TLSv1.3;

  ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:50m;
  ssl_session_timeout 1d;

  # OCSP Stapling ---
  # fetch OCSP records from URL in ssl_certificate and cache them
  ssl_stapling on;
  ssl_stapling_verify on;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
}

完成后,按 Ctrl + X 保存文件,并在提示时输入 Y。

Magento 自带一个位于 /var/www/magento/nginx.conf.sample 的 Nginx 配置模板,我们已在配置中包含了它。$MAGE_ROOT 变量指向我们已在文件中设置的 Magento Web 根目录,并在示例配置文件中使用。

验证 Nginx 配置文件语法。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 Nginx 服务。

$ sudo systemctl restart nginx

通过 URL https://magento.example.com 打开 Magento 网站。你应该会看到如下页面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

如果你的 CSS 和 JS 无法加载,则运行以下命令。

$ cd /var/www/magento
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento indexer:reindex

第 14 步 - 禁用双因素认证

在访问管理面板之前,我们需要禁用默认启用的双因素认证。Magento 在安装过程中会尝试通过 sendmail 发送邮件来启用双因素认证,但由于我们并未配置该服务,访问仪表盘的唯一方法是先禁用该功能。

如果你已在服务器上配置了 sendmail 来发送邮件,则可以跳过此步骤。要禁用双因素认证,我们需要使用以下命令禁用 Magento 的两个模块。

$ php /var/www/magento/bin/magento module:disable Magento_AdminAdobeImsTwoFactorAuth
$ php /var/www/magento/bin/magento module:disable Magento_TwoFactorAuth

运行以下命令创建类。

$ php /var/www/magento/bin/magento setup:di:compile

同时清理缓存。

$ php /var/www/magento/bin/magento c:c

第 15 步 - 访问管理门户

你需要使用 Magento 安装脚本提供给你的 URI 打开管理门户。如果你不慎忘记记下或丢失了它,可以使用以下命令再次获取该 URI。

$ php /var/www/magento/bin/magento info:adminuri
Admin URI: /admin_11xb2x

在浏览器中打开 URL https://magento.example.com/admin_11xb2x,你将看到如下界面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

输入安装期间提供的管理员凭据,然后点击 Sign in 按钮继续。你将看到如下界面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

你会看到一个弹窗,请求允许 Adobe 收集使用数据。点击 Don't Allow 按钮继续。

下一步是配置用于邮件的 SMTP,以便我们可以重新启用双因素认证。访问 Stores >> Configuration 菜单。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

从左侧展开 Advanced 菜单,并点击 Systems 选项以打开邮件设置页面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

取消勾选 Transport、Host 和 Port 选项前的 Use system value。点击 Transport 的下拉菜单并从中选择 SMTP。在本教程中,我们使用 Amazon SES 作为邮件发送服务。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

在相应字段中输入你的 SMTP 主机、端口 587、用户名和密码,将 Auth 设为 LOGIN,将 SSL 设为 TLS。完成后点击 Save Config 按钮。既然我们已经配置好邮件设置,下一步是配置店铺邮件 ID,以便我们能够对其进行测试。

向上滚动并在同一页面展开 General 菜单,然后选择 Store Email Addresses 选项。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

取消勾选默认的 Sender Email 字段,并输入你店铺的邮件 ID。完成后点击 Save Config 按钮。同样地,打开 Contacts 界面,进行相同的修改,并点击 Save Config 按钮完成。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

更改管理员选项可能会影响缓存,你会收到一条警告。运行以下命令手动清除缓存。

$ php /var/www/magento/bin/magento c:c

要测试邮件,请访问店铺前台页面并打开 Contact Us 页面。你可以直接使用 URL https://magento.example.com/contact/ 访问它。发送一条测试消息并点击 Submit 按钮。你应该会收到类似的邮件。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

第 16 步 - 启用并配置双因素认证

既然我们已经启用了 SMTP 邮件发送服务,现在是重新启用双因素认证的时候了。运行以下命令启用双因素认证。

$ php /var/www/magento/bin/magento module:enable Magento_AdminAdobeImsTwoFactorAuth
$ php /var/www/magento/bin/magento module:enable Magento_TwoFactorAuth

升级这些模块的安装配置。

$ php /var/www/magento/bin/magento setup:upgrade

运行以下命令创建类。

$ php /var/www/magento/bin/magento setup:di:compile

同时清理缓存。

$ php /var/www/magento/bin/magento c:c

如果你无法访问管理区域,也请运行以下命令。

强制部署静态内容。

$ php /var/www/magento/bin/magento setup:static-content:Deploy -f

设置文件权限。

$ cd /var/www/magento
$ sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
$ sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
$ sudo chown -R :nginx .

访问管理门户,你将看到如下界面。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

我们将使用 Google Authenticator 方式。如果你有硬件密钥,也可以使用。Google Authenticator 方式可与任何 TOTP 应用配合使用,包括 Authy、1Password、Bitwarden、Microsoft Authenticator 等。点击 Apply 按钮继续。

在 Debian 12 上基于 Nginx 与 Elasticsearch 安装 Magento 电商套件

在下一页,你将获得一个可用你的 2FA 应用扫描的二维码。将详细信息输入到你的应用中,并将生成的代码复制到 Authenticator code 字段。点击 Confirm 按钮进入管理仪表盘。

结语

至此,本篇关于在 Debian 12 服务器上使用 Nginx 和 Elasticsearch 安装 Magento 电商网站的教程就结束了。如果你有任何问题,请在下方评论区留言。