如何在 Debian 11 上使用 Nginx 和 SSL 安装 AbanteCart
在此页
- 先决条件
- 第 1 步 - 配置防火墙
- 第 2 步 - 安装 PHP
- 第 3 步 - 安装 MySQL
- 第 4 步 - 安装 Nginx
- 第 5 步 - 为 AbanteCart 配置 MySQL
- 第 6 步 - 安装 AbanteCart
- 第 7 步 - 安装 SSL
- 第 8 步 - 配置 Nginx 和 PHP
- 配置PHP-FPM
- 配置 Nginx
Abantecart 是一款免费、开源且功能丰富的电子商务应用程序。它是用 PHP 编写的,由 HTML5、Bootstrap、MVC 等最新技术提供支持。它可以使用单一安装创建多个在线商店,支持数字和实体产品,与多种支付网关和货币、多种语言、优惠券、商店积分和客户仪表板集成。您可以通过它运行电子邮件营销活动或创建横幅以增加用户参与度。此外,它还针对友好的 URL 进行了 SEO 优化,与多个分析平台集成并支持插件。
在本教程中,您将学习如何在基于 Debian 11 的服务器上安装 Abantecart。
先决条件
-
A server running Debian 11.
-
A non-root user with sudo privileges.
-
Everything is updated.
$ sudo apt update && sudo apt upgrade
-
Few packages that your system needs.
$ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release unzip debian-archive-keyring -y
Some of these packages may already be installed on your system.
第 1 步 - 配置防火墙
第一步是配置防火墙。 Debian 带有 ufw(简单防火墙)。
检查防火墙是否正在运行。
$ 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。要安装它,我们将使用 Ondrejs 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 中有一个错误,您需要单独运行 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 密码。选择一个强密码。
系统将要求您确认密码。接下来,您将看到一个描述新身份验证系统的屏幕。选择确定继续。
最后,系统会要求您选择身份验证方法。选择强密码加密并选择确定以完成设置。
第 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 密码。接下来,系统会询问您是否要设置验证密码插件,您可以使用它来测试 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 外壳。输入您的根密码以继续。
$ 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';
退出外壳。
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
要使用 Lets 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 -d abantecart.example.com
上面的命令会将证书下载到服务器上的
/etc/letsencrypt/live/abantecart.example.com
目录。生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
为 Lets Encrypt 自动更新创建一个挑战 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 $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
,您将看到以下屏幕。选中该框以同意许可,然后按继续继续。接下来,安装程序将检查是否满足所有要求。
如果一切正常,请按“继续”进入下一页。
系统将要求您填写数据库凭据。您可以在此处创建管理员帐户和安全密钥以访问控制面板。如果您想要演示数据,请保持选中该框,否则请不要选中它,如果您想从头开始。
完成后按继续继续。然后安装程序将继续设置所有内容,完成后将显示以下屏幕。
将指向您的控制面板的链接添加为书签,因为您将需要它。运行以下命令删除安装程序文件,因为它们不需要并且会带来安全风险。
$ sudo rm -rf /var/www/html/abantecart/install
登录到控制面板,系统会要求您完成商店设置。
您可以关闭快速向导并从如下所示的控制面板对其进行配置,或者继续使用向导来设置基本设置。
结论
我们关于在基于 Debian 11 的服务器上设置 AbanteCart 的教程到此结束。如果您有任何问题,请在下面的评论中发表。