本页内容

  • 先决条件
  • 第 1 步 - 配置防火墙
  • 第 2 步 - 安装 PHP
  • 第 3 步 - 安装 MySQL
  • 第 4 步 - 安装 Nginx
  • 第 5 步 - 为 AbanteCart 配置 MySQL
  • 第 6 步 - 安装 AbanteCart
  • 第 7 步 - 安装 SSL
  • 第 8 步 - 配置 Nginx 和 PHP 配置 PHP-FPM 配置 Nginx
  • 第 9 步 - 完成安装
  • 结论
  • 配置 PHP-FPM
  • 配置 Nginx

Abantecart 是一款免费、开源且功能丰富的电子商务应用程序。它使用 PHP 编写,并采用 HTML5、Bootstrap、MVC 等最新技术驱动。通过单次安装即可创建多个在线商店,支持数字和实体产品、集成多种支付网关和货币、多语言、优惠券、商店积分以及客户仪表板。你可以通过它开展电子邮件营销活动,或创建横幅以提高用户参与度。此外,它针对 SEO 进行了优化,支持友好的 URL,集成了多个分析平台,并支持插件。

在本教程中,你将学习如何在基于 Debian 11 的服务器上安装 Abantecart。

先决条件

  • 一台运行 Debian 11 的服务器。
  • 一个具有 sudo 权限的非 root 用户。
  • 所有内容均已更新。$ 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 unzip debian-archive-keyring -y 其中一些软件包可能已经安装在你的系统上。

一台运行 Debian 11 的服务器。

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

所有内容均已更新。

$ 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 unzip debian-archive-keyring -y

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

第 1 步 - 配置防火墙

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

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

$ sudo ufw status

你应该会得到以下输出。

Status: inactive

允许 SSH 端口,以便防火墙在启用时不会中断当前连接。

$ sudo ufw allow OpenSSH

同时允许 HTTP 和 HTTPS 端口。

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp

启用防火墙

$ 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/tcp                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

第 2 步 - 安装 PHP

Abantecart 支持 PHP 8.0。要安装它,我们将使用 Ondrej 的 PHP 仓库。运行以下命令添加该仓库。

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list

添加与该仓库关联的 GPG 密钥。

$ wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -

更新 Debian 仓库。

$ sudo apt update

安装 PHP 8.0 及所需的扩展。

$ sudo apt install php8.0-cli php8.0-fpm php8.0-mysql php8.0-gd php8.0-common php8.0-curl php8.0-xml php8.0-mbstring

第 3 步 - 安装 MySQL

要安装 MySQL 服务器,第一步是为该软件包添加 GPG 密钥

Debian 中存在一个 bug,你需要单独运行 GPG 命令来建立 .gpg 目录。

$ sudo gpg

按 Ctrl + C 退出上述命令。导入 GPG 密钥并将其保存在 /usr/share/keyrings 目录中。

$ sudo gpg --no-default-keyring --keyring /usr/share/keyrings/mysql8.0-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --brecv-keys 3A79BD29

添加官方 MySQL 仓库。

$ echo "deb [arch=amd64 signed-by=/usr/share/keyrings/mysql8.0-archive-keyring.gpg] http://repo.mysql.com/apt/debian/ `lsb_release -cs` mysql-8.0" \
    | sudo tee /etc/apt/sources.list.d/mysql-8.list

更新 Debian 仓库。

$ sudo apt update

安装 MySQL。

$ sudo apt install mysql-server

你将看到一个配置界面,要求你设置 root 密码。请选择一个强密码。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

你将被要求确认密码。接下来,将出现一个介绍新认证系统的界面。选择 OK 继续。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

最后,你将被要求选择认证方式。选择 Strong Password Encryption,然后选择 Ok 完成设置。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

第 4 步 - 安装 Nginx

Debian 附带的是较旧版本的 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/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

更新 Debian 仓库。

$ sudo apt update

安装 Nginx。

$ sudo apt install nginx

验证安装。确保在 Debian 上每次运行 Nginx 命令时都使用 sudo,否则将无法生效。

$ sudo nginx -v
nginx version: nginx/1.20.2

启用 Nginx 服务。

$ sudo systemctl enable nginx

第 5 步 - 为 AbanteCart 配置 MySQL

加固 MySQL 安装。

$ sudo mysql_secure_installation

第一步,你将被要求输入 root 密码。接下来,将询问你是否要设置 Validate Password Plugin,可用它来测试 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

在下一步中,将询问你是否要更改 root 密码。按 N 继续。

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) : N

对于以下所有提示,按 Y 然后按 ENTER 键,以移除匿名用户和测试数据库、禁用 root 登录并加载新设置的规则。

...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.
...
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!

进入 MySQL shell。输入你的 root 密码以继续。

$ mysql -u root -p

创建 abcart 用户。请确保密码满足前面设置的要求。

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

创建 abantecart 数据库。

mysql> CREATE DATABASE abantecart;

为该用户授予 abantecart 数据库上的权限。

mysql> GRANT ALL PRIVILEGES ON abantecart.* TO 'abcart'@'localhost';

退出 Shell。

mysql> exit

第 6 步 - 安装 AbanteCart

从 Github 下载 AbanteCart 的最新版本。

$ wget https://github.com/abantecart/abantecart-src/archive/master.zip

解压该文件。

$ unzip master.zip

为 Abantecart 创建公共 web 根目录。

$ sudo mkdir /var/www/html/abantecart -p

将解压出的 abantecart-src-master/public_html 目录复制到 webroot 目录。注意以下命令末尾的正斜杠( / )。

$ sudo cp -r abantecart-src-master/public_html /var/www/html/abantecart/

为 webroot 目录设置适当的权限。

$ sudo chown -R nginx:nginx /var/www/html/abantecart

至此,基本安装已完成。接下来,在完成安装之前,我们需要设置 SSL 和 Nginx。

第 7 步 - 安装 SSL

要使用 Let's Encrypt 安装 SSL 证书,我们需要安装 Certbot 工具。

为此我们将使用 Snapd 软件包安装器。由于大多数 Debian 服务器不附带它,请安装 Snap。

$ 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 --version
certbot 1.22.0

生成 SSL 证书。

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

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

生成 Diffie-Hellman 组证书。

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

为 Let's Encrypt 自动续期创建一个 challenge webroot 目录。

$ sudo mkdir -p /var/lib/letsencrypt

创建一个 Cron 作业来续期 SSL。它将每天运行以检查证书并在需要时续期。为此,首先创建文件 /etc/cron.daily/certbot-renew 并打开进行编辑。

$ sudo nano /etc/cron.daily/certbot-renew

粘贴以下代码。

#!/bin/sh
certbot renew --cert-name abantecart.example.com --webroot -w /var/lib/letsencrypt/ --post-hook "systemctl reload nginx"

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

更改任务文件的权限使其可执行。

$ sudo chmod +x /etc/cron.daily/certbot-renew

第 8 步 - 配置 Nginx 和 PHP

配置 PHP-FPM

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

$ sudo nano /etc/php/8.0/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.
user = nginx
group = nginx
...

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

listen.owner = nginx
listen.group = nginx

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

下一步是禁用 PHP opcache 扩展。

打开文件 /etc/php/8.0/fpm/conf.d/10-opcache.ini 进行编辑。

$ sudo nano /etc/php/8.0/fpm/conf.d/10-opcache.ini

在末尾粘贴以下行。

opcache.enable=0

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

重启 PHP-fpm 进程。

$ sudo systemctl restart php8.0-fpm

配置 Nginx

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

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

在其中粘贴以下代码。

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

    access_log  /var/log/nginx/abantecart.access.log;
    error_log   /var/log/nginx/abantecart.error.log;
    
    # SSL
    ssl_certificate      /etc/letsencrypt/live/abantecart.example.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/abantecart.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/abantecart.example.com/chain.pem;
    ssl_session_timeout  5m;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    resolver 8.8.8.8;
    
    root /var/www/html/abantecart;
    index index.php;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    # Pass PHP Scripts To FastCGI Server
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock; # Depends On The PHP Version
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
        try_files $uri =404;
    }
    
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    
    # Make sure files with the following extensions do not get loaded by nginx because nginx would
    # display the source code, and these files can contain PASSWORDS!
    location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
        deny all;
    }
    
    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    
    ### Retina images handler. Check cookie and looking for file with @2x at the end of name
    location ~* ^(.*)\.(jpg|jpeg|png|gif|webp)$ {
        set $hidpi_uri $1@2x.$2;
        if ($http_cookie !~ 'HTTP_IS_RETINA=1') {
          break;
        }
        try_files $hidpi_uri $uri =404;
    }

    location ~*  \.(jpg|jpeg|png|gif|css|js|ico|webp)$ {
        expires max;
        log_not_found off;
    }
    
    location ~ /(system/logs|resources/download) {
      deny all;
      return 403;
    }
    
    location /admin/ {
        location ~ .*\.(php)?$ {
          deny all;
          return 403;
        }
    }
    
    #rewrite for images for retina-displays
    location ~ / {
        if (!-e $request_filename){
            rewrite ^/(.*)\?*$ /index.php?_route_=$1 last;
        }
    }
}
# enforce HTTPS
server {
    listen       80;
    listen       [::]:80;
    server_name  abantecart.example.com;
    return 301   https://$host$request_uri;
}

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

打开文件 /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 以保存文件。

验证 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 start nginx

第 9 步 - 完成安装

在浏览器中打开 https://abantecart.example.com,你将看到以下界面。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

勾选复选框以同意许可协议,然后按 Continue 继续。接下来,安装程序将检查是否满足所有要求。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

如果一切正常,按 Continue 进入下一页。

你将被要求填写数据库凭据。在这里你可以创建一个管理员账户和一个用于访问控制面板的安全密钥。如果你想要演示数据,请保持复选框为勾选状态;否则,如果你想从头开始,请保持不勾选。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

完成后按 Continue 继续。安装程序随后将进行所有设置,完成后会向你显示以下界面。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

请将控制面板的链接加入书签,因为你会用到它。运行以下命令删除安装程序文件,因为它们不再需要且会带来安全风险。

$ sudo rm -rf /var/www/html/abantecart/install

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

登录控制面板,你将被要求完成商店的设置。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

你可以关闭快速向导,从下方所示的 Control panel 中进行配置,或者继续使用该向导来设置基本选项。

在 Debian 11 系统上通过 Nginx 与 SSL 安装 AbanteCart 电商程序教程

结论

本教程关于在基于 Debian 11 的服务器上设置 AbanteCart 的内容到此结束。如果你有任何问题,请在下方评论中发布。