本教程适用于以下操作系统版本

  • Ubuntu 24.04 (Noble Numbat)
  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)
  • Ubuntu 18.04 (Bionic Beaver)
  • Ubuntu 16.04 (Xenial Xerus)
  • Ubuntu 15.10 (Wily Werewolf)

本页内容

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

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

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

准备工作

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

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

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

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

确保系统已更新。

$ sudo apt update
$ sudo apt upgrade

你的系统需要安装的几个软件包。

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

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

第 1 步 - 配置防火墙

第一步是配置防火墙。Ubuntu 默认自带 ufw(Uncomplicated Firewall,简易防火墙)。

检查防火墙是否在运行。

$ sudo ufw status

你应该会得到如下输出。

Status: inactive

放行 SSH 端口,以免在启用防火墙时中断当前的连接。

$ sudo ufw allow OpenSSH

同时放行 HTTP 和 HTTPS 端口。

$ sudo ufw allow http
$ sudo ufw allow https

启用防火墙

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

再次检查防火墙的状态。

$ 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 及其扩展

Ubuntu 22.04 自带 PHP 8.1.2 版本,该版本略显过时。我们将通过 Ondrej 的 PHP 仓库安装最新的 PHP 8.2 版本。

$ sudo add-apt-repository ppa:ondrej/php

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

$ 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 libcurl14-openssl-dev

验证安装。

$ php --version
PHP 8.2.5 (cli) (built: Apr 14 2023 04:27:02) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.5, 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 步 - 安装 MySQL

Ubuntu 22.04 自带最新版本的 MySQL。你可以用一条命令安装它。

$ sudo apt install mysql-server

检查 MySQL 的版本。

$ mysql --version
mysql  Ver 8.0.33-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

此步骤适用于 MySQL 8.0.28 及以上版本。进入 MySQL Shell。

$ sudo mysql

运行以下命令为你的 root 用户设置密码。请确保密码包含数字、大写字母、小写字母和特殊字符的组合。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword12!';

退出 shell。

mysql> exit

运行 MySQL secure install 脚本。

$ sudo mysql_secure_installation

首先,会要求你输入 root 密码。输入它。接下来,会询问是否安装 Validate Password Component(密码验证组件)。它会检查 MySQL 中所使用密码的强度。按 Y 安装它。接着,会要求你设置密码验证策略的级别。选择 2,因为它是最强的一档。

Securing the MySQL server deployment.

Enter password for user root:

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100

接下来,输入 N 拒绝更改你的 root 密码。同时,输入 Y 以删除匿名用户、禁止 root 远程登录、删除测试数据库并重新加载权限表。

Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

 ... skipping.
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
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
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
 - 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
Success.

All done!

第 5 步 - 配置 MySQL

登录到 MySQL shell。在提示时输入你的 root 密码。

$ sudo mysql -u root -p

为 Magento 创建一个数据库。

mysql> CREATE DATABASE magento;

创建一个 SQL 用户账户。

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

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

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

刷新用户权限。

mysql> FLUSH PRIVILEGES;

退出 shell。

mysql> exit

第 6 步 - 安装 Nginx

Ubuntu 22.04 自带较旧版本的 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 arch=amd64] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

更新系统仓库。

$ sudo apt update

安装 Nginx。

$ sudo apt install nginx

验证安装。

$ nginx -v
nginx version: nginx/1.24.0

启动 Nginx 服务器。

$ sudo systemctl start nginx

第 7 步 - 安装 SSL

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

Ubuntu 22.04 默认已安装 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

运行以下命令来生成 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
.....
Sun 2023-02-26 06:32:00 UTC 9h left       Sat 2023-02-25 18:04:05 UTC 2h 59min ago  snap.certbot.renew.timer  snap.certbot.renew.service
Sun 2023-02-26 06:43:20 UTC 9h left       Sat 2023-02-25 10:49:23 UTC 10h ago       apt-daily-upgrade.timer   apt-daily-upgrade.service
Sun 2023-02-26 09:00:06 UTC 11h left      Sat 2023-02-25 20:58:06 UTC 5min ago      apt-daily.timer           apt-daily.service

对流程进行一次空运行(dry run),以检查 SSL 续期是否正常工作。

$ sudo certbot renew --dry-run

如果没看到错误,说明一切就绪。你的证书将自动续期。

第 8 步 - 安装 Elasticsearch

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

导入 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

在其中粘贴以下代码。

-Xms1g
-Xmx1g

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

启动并启用该服务。

$ sudo systemctl enable elasticsearch --now

检查 Elasticsearch 是否正常工作。

$ curl http://localhost:9200

你应该会看到如下输出。

{
  "name" : "magento",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "6yks8tZ6T4GskIwWoXuSLA",
  "version" : {
    "number" : "7.17.10",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "fecd68e3150eda0c307ab9a9d7557f5d5fd71349",
    "build_date" : "2023-04-23T05:33:18.138275597Z",
    "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。Ubuntu 自带 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.11 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=3af367a78d5e21e9

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

$ 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

切换到 /var/www 目录。

$ cd /var/www

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

点击“使用 Adobe ID 登录”(Sign in with Adobe ID)按钮,进入以下页面。

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

点击“创建新的访问密钥”(Create A New Access Key)按钮来创建你的认证密钥。为你的密钥起一个用于识别的名称。

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

创建 Magento 项目。

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

会要求你输入该仓库的用户名和密码。使用公钥作为用户名、私钥作为密码。会询问你是否要将凭据存储在 Composer 配置目录中。输入 y 以确认。

Creating a "magento/project-community-edition" project at "./magento"
    Authentication required (repo.magento.com):
      Username: 53211xxxxxxxxxxxxxxxxxxxxxxxxxxx
      Password:
Do you want to store credentials for repo.magento.com in /home/navjot/.config/composer/auth.json ? [Yn] y
Installing magento/project-community-edition (2.4.6)
  - Downloading magento/project-community-edition (2.4.6)
  - Installing magento/project-community-edition (2.4.6): 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: 564 installs, 0 updates, 0 removals
  - Locking 2tvenom/cborencode (1.0.2)
  - Locking adobe-commerce/adobe-ims-metapackage (2.2.0)
...............................................

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

$ 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 .
$ sudo chmod u+x bin/magento

第 11 步 - 安装 Magento

确保你位于 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_19uadb
Nothing to import.

记下 Admin URI(管理员 URI),稍后你将需要它来访问管理面板。

创建 Magento cron 作业。

$ php bin/magento cron:install

验证 cron 作业。

$ 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 processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
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 自带一个 Nginx 配置模板,位于 /var/www/magento/nginx.conf.sample,我们已将其包含在我们的配置中。$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

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

在 Ubuntu 22.04 服务器上使用 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_19uadb

在浏览器中打开网址 https://magento.example.com/admin_19uadb ,你会看到以下界面。

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

会弹出一个请求 Adobe 收集使用情况数据权限的窗口。点击“不允许”(Don't Allow)按钮继续。

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

向上滚动并在同一页面展开 General(常规)菜单,选择 Store Email Addresses(店铺邮件地址)选项。

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

取消勾选默认的 Sender Email(发件人邮件)字段,并输入你店铺的邮件 ID。完成后点击“保存配置”(Save Config)按钮。同样地,打开 Contacts(联系人)界面,进行同样的修改并点击“保存配置”(Save Config)按钮完成操作。

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

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

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

在 Ubuntu 22.04 服务器上使用 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 .

访问管理门户,你会看到以下界面。

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

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

在 Ubuntu 22.04 服务器上使用 Nginx 与 Elasticsearch 安装部署 Magento 电商网站

在下一页,你会看到一个供 2FA 应用扫描的二维码。将详细信息输入你的应用,并将生成的代码复制到 Authenticator code(验证码)字段中。点击 Confirm(确认)按钮进入管理仪表板。

结语

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