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

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)
  • Ubuntu 18.04 (Bionic Beaver)

本页内容

  • 先决条件
  • 安装 Nginx 和 MariaDB
  • 安装 PHP 及其他组件
  • 配置 MariaDB 数据库
  • 下载 Shopware
  • 为 Shopware 配置 Nginx
  • 访问 Shopware 安装向导
  • 使用 Let's Encrypt 保护 Shopware 安全
  • 总结

Shopware 社区版是一款免费且开源的购物车平台,可让你在 Web 上创建自己的在线商店。它基于 Symfony 和 Vue.js 编写,并采用现代化的技术栈。它是 Magento 等其他电商应用的替代方案。它提供美观且用户友好的 Web 界面,用于管理客户和订单。它允许你管理产品价格、更改或更新主题、设计用于产品营销的电子邮件模板,并生成统计结果。

在本教程中,我们将向你展示如何在 Ubuntu 22.04 上使用 Nginx 和 Let's Encrypt 安装 Shopware CE。

先决条件

  • 一台运行 Ubuntu 22.04 且至少配备 4 GB 内存的服务器。
  • 一个已指向你服务器的有效域名。
  • 服务器上已配置 root 密码。

安装 Nginx 和 MariaDB

首先,使用以下命令安装 Nginx Web 服务器和 MariaDB 数据库服务器:

apt-get install nginx mariadb-server -y

两个软件包安装完成后,启动 Nginx 和 MariaDB 服务,并设置为在系统启动时自动启动:

systemctl start nginx
systemctl start mariadb
systemctl enable nginx
systemctl enable mariadb

安装 PHP 及其他组件

Shopware 6 支持 7.2 到 7.3 版本的 PHP。因此你需要在系统中安装 PHP 及其他相关库。

首先,使用以下命令将 PHP 仓库添加到你的系统:

apt-get install software-properties-common -y
add-apt-repository ppa:ondrej/php

添加仓库后,使用以下命令安装 PHP 及其他库:

apt-get install php7.2 php7.2-cli php7.2-fpm php7.2-common php7.2-mysql php7.2-curl php7.2-json php7.2-zip php7.2-gd php7.2-xml php7.2-mbstring php7.2-intl php7.2-opcache git unzip socat curl bash-completion -y

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

nano /etc/php/7.2/fpm/php.ini

修改以下行:

memory_limit = 512M
upload_max_filesize = 20M
max_execution_time = 300

修改完成后保存并关闭文件。

接下来,你需要在系统中安装 IonCube loader。

首先,使用以下命令下载它:

wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz

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

tar xfz ioncube_loaders_lin_x86-64.tar.gz

接下来,定位 PHP 扩展目录的路径:

php -i | grep extension_dir

你应该看到以下输出:

extension_dir => /usr/lib/php/20170718 => /usr/lib/php/20170718

接下来,将 IonCube loader 复制到 PHP 扩展目录:

cp ioncube/ioncube_loader_lin_7.2.so /usr/lib/php/20170718/

接下来,编辑 php.ini 文件并定义 IonCube loader:

nano /etc/php/7.2/fpm/php.ini

在 [PHP] 部分内添加以下行:

zend_extension = /usr/lib/php/20170718/ioncube_loader_lin_7.2.so

保存并关闭文件,然后重启 PHP-FPM 服务以使更改生效。

systemctl restart php7.2-fpm

配置 MariaDB 数据库

首先,使用以下脚本保护 MariaDB 安装并设置 root 密码:

mysql_secure_installation

按如下所示回答所有问题:

Enter current password for root (enter for none): 
Set 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 密码,然后为 Shopware 创建数据库和用户:

MariaDB [(none)]> CREATE DATABASE shopwaredb;
MariaDB [(none)]> GRANT ALL ON shopwaredb.* TO 'shopware'@'localhost' IDENTIFIED BY 'password';

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

MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

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

下载 Shopware

首先,在 Nginx Web 根目录下为 Shopware 创建一个目录:

mkdir /var/www/html/shopware

接下来,切换到 shopware 目录,并使用以下命令下载最新版本的 Shopware:

cd /var/www/html/shopware
wget http://releases.s3.shopware.com.s3.amazonaws.com/install_5.4.5_6847c0845f0f97230aa05c7294fa726a96dda3ff.zip?_ga=2.133696968.774684214.1529926951-1771999509.1528830594 -O shopware.zip

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

unzip shopware.zip

接下来,使用以下命令更改 shopware 目录的所有权并赋予适当的权限:

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

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

为 Shopware 配置 Nginx

首先,为 Shopware 创建一个新的 Nginx 虚拟主机配置文件:

nano /etc/nginx/sites-available/shopware.conf

添加以下行:

server {
    listen 80;

    server_name shopware.example.com; # Check this
    root /var/www/html/shopware; # Check this

    index shopware.php index.php;

    location / {
        try_files $uri $uri/ /shopware.php$is_args$args;
    }

    location /recovery/install {
      index index.php;
      try_files $uri /recovery/install/index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # Check this
    }
}

保存并关闭文件,然后使用以下命令启用 Shopware 虚拟主机文件:

ln -s /etc/nginx/sites-available/shopware.conf /etc/nginx/sites-enabled/

接下来,使用以下命令检查 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 服务以使更改生效:

systemctl reload nginx

要检查 Nginx 状态,运行以下命令:

systemctl status nginx

你应该得到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-09-12 05:03:41 UTC; 5min ago
       Docs: man:nginx(8)
    Process: 29668 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
   Main PID: 17628 (nginx)
      Tasks: 3 (limit: 4579)
     Memory: 5.7M
        CPU: 63ms
     CGroup: /system.slice/nginx.service
             ??17628 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??29669 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ??29670 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Sep 12 05:03:41 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 12 05:03:41 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
Sep 12 05:09:28 ubuntu2204 systemd[1]: Reloading A high performance web server and a reverse proxy server...
Sep 12 05:09:28 ubuntu2204 systemd[1]: Reloaded A high performance web server and a reverse proxy server.

访问 Shopware 安装向导

至此,Shopware 已安装在你的系统中。现在,打开你的 Web 浏览器并访问 URL http://shopware.example.com。你应该会看到 Shopware 的 Web 安装向导:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

选择你的语言并点击“下一步”按钮。你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

确保所有必需的依赖项都已安装,然后点击“下一步”按钮。你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

接受条款和条件,然后点击“下一步”按钮。你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

填写你的数据库详细信息,然后点击“开始安装”按钮。安装成功完成后,你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

现在,点击“开始安装”按钮。你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

点击“下一步”按钮。你应该会看到以下页面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

选择你所需的选项,然后点击“下一步”按钮。你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

填写你的商店名称、电子邮件、国家/地区、管理员电子邮件、管理员用户名、密码,然后点击“下一步”按钮。你将被重定向到 Shopware 仪表板界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

点击“进入商店后台”按钮。你应该会看到以下界面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

输入你的登录用户名、密码,然后点击“登录”按钮。你应该会看到以下页面:

在 Ubuntu 系统上从零安装配置 Shopware 电商系统

现在,完成你的商店设置,并使用 Shopware 平台开始在线销售。

使用 Let's Encrypt 保护 Shopware 安全

在开始之前,你需要在系统中安装 Certbot 客户端来安装和管理 Let's Encrypt SSL。你可以使用以下命令安装它:

apt-get install certbot python3-certbot-nginx -y

Certbot 客户端安装完成后,运行以下命令为你的网站下载并安装 Let's Encrypt SSL:

certbot --nginx -d shopware.example.com

按如下所示提供你的电子邮件地址并接受服务条款:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
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 shopware.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/shopware.conf

选择是否将 HTTP 流量重定向到 HTTPS:

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 并按回车开始该过程。安装完成后,你应该会看到以下输出:

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/shopware.conf

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

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

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/shopware.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/shopware.example.com/privkey.pem
   Your cert will expire on 2022-09-12. 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"
 - 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

至此,你的 Shopware 网站已通过 Let's Encrypt SSL 保护。你现在可以使用 URL https://shopware.example.com 安全地访问你的网站。

总结

恭喜!你已成功在 Ubuntu 22.04 上使用 Nginx 和 Let's Encrypt SSL 安装了 Shopware。你现在可以开始使用 Shopware 搭建自己的在线业务了。如果你有任何问题,欢迎随时向我提问。