本教程适用于以下 OS 版本

  • Debian 12 (Bookworm)
  • Debian 11 (Bullseye)
  • Debian 9 (Stretch)

本页内容

  • 前提条件
  • 安装 Apache、MariaDB 和 PHP
  • 为 PrestaShop 创建数据库
  • 下载 PrestaShop
  • 为 PrestaShop 配置 Apache
  • 访问 PrestaShop Web UI
  • 使用 Let's Encrypt SSL 保护 PrestaShop
  • 结论

PrestaShop 是一款免费、开源、高效且创新的电子商务解决方案,可帮助你在线销售产品。它是一个功能强大且适应性强的电子商务 CMS,提供创建在线商店所需的全部功能。借助 PrestaShop,你可以管理库存、产品目录、配送、开票等更多内容。它提供 5000 多个模块、主题,并支持许多不同的支付网关系统,如 PayPal、Google Checkout 等。

在本文中,我们将向你展示如何在 Debian 11 上使用 Apache web server 和 Let's Encrypt SSL 安装 PrestaShop。

前提条件

  • A server running Debian 11.
  • A valid domain name is pointed to your server IP.
  • A root password is configured on the server.

安装 Apache、MariaDB 和 PHP

首先,你需要在服务器上安装 Apache、MariaDB、PHP 以及其他 PHP 扩展。你可以使用以下命令安装所有这些组件:

apt-get install apache2 mariadb-server libapache2-mod-php php php-gd php-mbstring php-mysql php-curl php-xml php-cli php-intl php-zip unzip -y

所有软件包安装完成后,编辑 php.ini 文件并调整一些设置:

nano /etc/php/7.4/apache2/php.ini

修改以下行:

memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 32M
date.timezone = UTC

保存并关闭文件,然后重启 Apache 服务以使更改生效:

systemctl restart apache2

为 PrestaShop 创建数据库

首先,你需要保护 MariaDB 安装并设置 MariaDB root 密码。你可以通过运行 mysql_secure_installation 脚本来完成:

mysql_secure_installation

按如下方式回答所有问题:

Enter current password for root (enter for none): 
Switch to unix_socket authentication [Y/n] Y
Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

接下来,使用以下命令登录 MariaDB shell:

mysql -u root -p

输入你的 MariaDB root 密码,然后使用以下命令为 PrestaShop 创建用户和数据库:

MariaDB [(none)]> CREATE DATABASE prestadb;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON prestadb.* TO 'prestauser'@'localhost' IDENTIFIED BY 'yourpass';

接下来,使用以下命令刷新权限并退出 MariaDB:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

下载 PrestaShop

接下来,前往 PrestaShop 网站并使用以下命令下载最新版本的 PrestaShop:

wget https://download.prestashop.com/download/releases/prestashop_1.7.7.7.zip

下载完成后,使用以下命令解压下载的文件:

unzip prestashop_1.7.7.7.zip

接下来,使用以下命令在 Apache web root 目录中再次解压 PrestaShop:

unzip prestashop.zip -d /var/www/html/prestashop

接下来,为 PrestaShop 目录设置正确的属主和权限:

chown -R www-data:www-data /var/www/html/prestashop/ 
chmod -R 755 /var/www/html/prestashop

完成后,你可以继续进行下一步。

为 PrestaShop 配置 Apache

接下来,你需要为 PrestaShop 创建一个 Apache 虚拟主机配置文件。你可以使用以下命令创建它:

nano /etc/apache2/sites-available/prestashop.conf

添加以下行:

<VirtualHost *:80>
      ServerAdmin 
[email protected]

      ServerName presta.example.com
      DocumentRoot /var/www/html/prestashop


     <Directory /var/www/html/prestashop>
          Options FollowSymlinks
          AllowOverride All
          Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
     CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined

</VirtualHost>

保存并关闭文件,然后使用以下命令启用 Apache rewrite 模块并激活 Apache 虚拟主机:

a2enmod rewrite
a2ensite prestashop

最后,重启 Apache 服务以使更改生效:

systemctl restart apache2

你也可以使用以下命令检查 Apache 服务:

systemctl status apache2

你应该看到以下输出:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-08-27 06:15:23 UTC; 5min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 17540 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
   Main PID: 14330 (apache2)
      Tasks: 6 (limit: 2341)
     Memory: 16.5M
        CPU: 332ms
     CGroup: /system.slice/apache2.service
             ??14330 /usr/sbin/apache2 -k start
             ??17544 /usr/sbin/apache2 -k start
             ??17545 /usr/sbin/apache2 -k start
             ??17546 /usr/sbin/apache2 -k start
             ??17547 /usr/sbin/apache2 -k start
             ??17548 /usr/sbin/apache2 -k start

Aug 27 06:15:23 debian11 systemd[1]: Starting The Apache HTTP Server...

访问 PrestaShop Web UI

至此,PrestaShop 已安装并配置完成。你现在可以使用 URL http://presta.example.com 访问它。你应该会看到以下页面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

选择你的语言并点击 Next 按钮。你应该会看到以下页面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

接受条款和条件,然后点击 Next 按钮。你应该会看到以下页面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

填写你的商店名称、管理员账户信息,然后点击 Next 按钮。你应该会看到以下页面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

填写你的数据库设置并点击 Next 按钮以开始安装。安装完成后,你应该会看到以下页面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

点击 Manage your store 按钮。你应该会看到以下页面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

接下来,打开你的终端并删除安装文件夹:

rm -rf /var/www/html/prestashop/install

现在,使用上一个屏幕中显示的 URL 访问 PrestaShop 管理界面。你应该会看到 PrestaShop 登录界面:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

输入你的管理员用户名、密码,然后点击 Log in 按钮。你应该在下一页看到 PrestaShop 仪表盘:

在 Debian 11 系统上完整安装 PrestaShop 网站并配置 Let's Encrypt 证书

使用 Let's Encrypt SSL 保护 PrestaShop

首先,你需要安装 Certbot Let's Encrypt 客户端,以便为你的域名下载并安装 SSL 证书。

你可以使用以下命令安装它:

apt-get install python3-certbot-apache -y

安装完成后,你可以运行以下命令为你的域名 prestashop.example.com 安装 Let's Encrypt 证书。

certbot --apache -d presta.example.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): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 prestashop.example.com
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/prestashop-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/prestashop-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/prestashop-le-ssl.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 并按回车键,为你的域名下载并安装免费的 SSL 证书。安装成功完成后,你应该会得到以下输出:

Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/prestashop.conf to ssl vhost in /etc/apache2/sites-available/
prestashop-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://prestashop.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=prestashop.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

结论

在上述指南中,我们讲解了如何在 Debian 11 上使用 Apache 和 Let's Encrypt SSL 证书安装 PrestaShop。你现在可以轻松地使用 PrestaShop 托管你的在线商店。如果你有任何疑问,欢迎随时向我提问。