如何使用 Nginx 安装 OpenCart 并在 Debian 10 上加密
在此页
- 先决条件
- 开始
- 安装 LEMP 服务器
- 配置 MariaDB 数据库
- 下载 OpenCart
- 为 OpenCart 配置 Nginx
- 使用 Lets Encrypt SSL 保护 OpenCart
- 访问 OpenCart 网络界面
- 结论
Opencart 是一种流行的开源购物车解决方案,可帮助您托管自己的全功能电子商务网站。 Opencart 提供了一个简单且用户友好的界面来在线销售您的产品,例如 Amazon 和 Flipcart。它专为中小型企业设计,具有在线商店所需的所有标准电子商务功能。它提供了一组丰富的功能,包括多货币、语言、无限类别、产品、产品评论、多商店等等。
在本教程中,我们将向您展示如何在 Debian 10 上安装带有 Nginx 的 OpenCart 并使用 Lets Encrypt SSL 保护它。
先决条件
- 运行 Debian 10 的服务器。
- 在您的服务器上配置了根密码。
入门
首先,使用以下命令将系统更新到最新版本:
apt-get update -y
apt-get upgrade -y
服务器更新后,重新启动它以应用更改。
安装 LEMP 服务器
首先,通过运行以下命令安装 Nginx Web 服务器、MariaDB 数据库服务器、PHP 和其他所需的 PHP 扩展:
apt-get install nginx mariadb-server php-common php-cli php-fpm php-opcache php-gd php-mysql php-curl php-intl php-xsl php-mbstring php-zip php-bcmath php-soap unzip git -y
安装所有包后,编辑 php.ini 文件并进行一些更改:
nano /etc/php/7.3/fpm/php.ini
更改以下行:
memory_limit = 256M
upload_max_filesize = 100M
opcache.save_comments=1
max_execution_time = 300
date.timezone = Asia/Kolkata
完成后保存并关闭文件。
配置 MariaDB 数据库
接下来,您需要设置一个 MariaDB root 密码,因为它没有在 Debian 10 中设置。
为此,请使用以下命令登录到 MariaDB shell:
mysql
登录后,使用以下命令设置 MariaDB root 密码:
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("yournewrootpassword");
接下来,使用以下命令将 MariaDB 身份验证插件设置为 mysql_native_password:
MariaDB [(none)]> SET GLOBAL innodb_fast_shutdown = 0;
MariaDB [(none)]> UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User = 'root';
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
接下来,使用 root 用户登录 MariaDB:
mysql -u root -p
使用以下命令提供您的 root 密码并为 OpenCart 创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE opencartdb;
MariaDB [(none)]> GRANT ALL ON opencartdb.* TO 'opencart'@'localhost' IDENTIFIED BY 'password';
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
配置 MariaDB 后,您可以继续下一步。
下载OpenCart
您可以使用以下命令从 Git 存储库下载最新版本的 OpenCart:
wget https://github.com/opencart/opencart/releases/download/3.0.3.2/opencart-3.0.3.2.zip
下载 OpenCart 后,使用以下命令解压缩下载的文件:
unzip opencart-3.0.3.2.zip
接下来,使用以下命令将上传目录移动到 Nginx web 根目录:
mv upload /var/www/html/opencart
接下来,将目录更改为 opencart 并重命名 config-dist.php 文件:
cd /var/www/html/opencart/
mv config-dist.php config.php
mv admin/config-dist.php admin/config.php
接下来,使用以下命令为 opencart 目录授予适当的权限:
chown -R www-data:www-data /var/www/html/opencart/
chmod -R 775 /var/www/html/opencart/
完成后,您可以继续下一步。
为 OpenCart 配置 Nginx
接下来,您需要创建一个 Nginx 虚拟主机配置文件来为 OpenCart 提供服务。您可以使用以下命令创建它:
nano /etc/nginx/sites-available/opencart.conf
添加以下内容:
server {
listen 80;
server_name opencart.linuxbuz.com;
root /var/www/html/opencart;
index index.php;
access_log /var/log/nginx/opencart_access.log;
error_log /var/log/nginx/opencart_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
保存并关闭文件,然后使用以下命令检查 Nginx 是否存在任何语法错误:
nginx -t
您应该得到以下输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
接下来,使用以下命令启用 Nginx 虚拟主机文件:
ln -s /etc/nginx/sites-available/opencart.conf /etc/nginx/sites-enabled/
接下来,重新启动 Nginx 和 PHP-FPM 服务以应用更改:
systemctl restart nginx
systemctl restart php7.3-fpm
完成后,您可以继续下一步。
使用 Lets Encrypt SSL 保护 OpenCart
接下来,您需要安装 Certbot 客户端来为您的网站安装和设置 Lets Encrypt。
默认情况下,Certbot 在 Debian 10 默认存储库中不可用。因此,您需要在系统中添加一个 Certbot 存储库。
您可以使用以下命令添加它:
echo "deb http://ftp.debian.org/debian buster-backports main" >> /etc/apt/sources.list
接下来,使用以下命令更新存储库并为 Nginx 安装 Certbot 客户端:
apt-get update -y
apt-get install python3-certbot-nginx -t buster-backports
安装后,运行以下命令下载 Lets Encrypt SSL 并配置 Nginx 以使用此 SSL:
certbot --nginx -d opencart.linuxbuz.com
系统将提示您接受服务条款并提供您的有效电子邮件地址,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for opencart.linuxbuz.com
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/nginx/sites-available/opencart-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/nginx/sites-available/opencart-le-ssl.conf
Enabling available site: /etc/nginx/sites-available/opencart-le-ssl.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
接下来,您需要选择是否将 HTTP 流量重定向到 HTTPS,如下所示:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
键入 2 并按 Enter 键继续。安装完成后,您应该得到以下输出:
Redirecting vhost in /etc/nginx/sites-enabled/opencart.conf to ssl vhost in /etc/nginx/sites-available/opencart-le-ssl.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://opencart.linuxbuz.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=opencart.linuxbuz.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/opencart.linuxbuz.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/opencart.linuxbuz.com/privkey.pem
Your cert will expire on 2020-04-30. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
此时,OpenCart 使用 Lets Encrypt SSL 进行保护。
访问 OpenCart Web 界面
现在,打开您的 Web 浏览器并输入 URL https://opencart.linuxbuz.com。您将被重定向到 OpenCart 许可协议页面:

单击“继续”按钮接受许可协议。您应该会看到以下页面:


确保安装了所有必需的 PHP 扩展,然后单击“继续”按钮。您应该会看到以下页面:

提供您的数据库凭据、管理员用户名、密码,然后单击“继续”按钮。安装完成后,您应该会看到以下页面:

现在,打开您的终端并使用以下命令删除安装目录:
rm -rf /var/www/html/opencart/install/
接下来,单击“转到您的在线商店”。您应该会在以下页面上看到您的 OpenCart 商店:

接下来,单击登录到您的管理按钮。您应该会看到 OpenCart 登录页面:

提供您的管理员用户名和密码,然后单击“登录”按钮。您应该在以下页面中看到您的 OpenCart 管理面板:

结论
恭喜!您已在 Debian 10 上成功安装并保护 OpenCart。您现在可以使用 OpenCart 托管您自己的在线购物车。如果您有任何问题,请随时问我。