如何在 Ubuntu 22.04 上使用 Nginx 和 Free Lets Encrypt SSL 安装 Shopware
本教程适用于这些操作系统版本
- Ubuntu 22.04(果酱水母)
- Ubuntu 18.04(仿生海狸)
在此页
- 先决条件
- 安装 Nginx 和 MariaDB
- 安装 PHP 和其他组件
- 配置 MariaDB 数据库
- 下载商店软件
- 为 Shopware 配置 Nginx
- 访问 Shopware 安装向导
- 使用 Lets Encrypt 保护 Shopware
- 结论
Shopware 社区版是一个免费的开源购物车平台,可让您在网络上开设自己的在线商店。它是用 Symfony 和 Vue.js 编写的,基于现代技术堆栈。它是其他电子商务应用程序(如 Magento)的替代解决方案。它提供了一个漂亮且用户友好的网络用户界面,用于管理客户和订单。它允许您管理产品价格、更改或更新主题、设计用于营销您的产品的电子邮件模板以及生成统计结果。
在本教程中,我们将向您展示如何在 Ubuntu 22.04 上使用 Nginx 和 Lets Encrypt 安装 Shopware CE。
先决条件
- 一台运行 Ubuntu 22.04 且内存至少为 4 GB 的服务器。
- 您的服务器指向一个有效的域名。
- 在您的服务器上配置了根密码。
安装 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 加载程序。
首先,使用以下命令下载它:
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 加载器复制到 PHP 扩展目录:
cp ioncube/ioncube_loader_lin_7.2.so /usr/lib/php/20170718/
接下来,编辑 php.ini 文件并定义 IonCube 加载器:
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;
完成后,您可以继续下一步。
下载商店软件
首先,在 Nginx 网站根目录中为 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 安装向导:

选择您的语言,然后单击“下一步”按钮。您应该看到以下屏幕:

确保安装了所有必需的依赖项,然后单击“下一步”按钮。您应该看到以下屏幕:

接受条款和条件,然后单击下一步按钮。您应该看到以下屏幕:

提供您的数据库详细信息,然后单击“开始安装”按钮。安装成功完成后,您应该会看到以下屏幕:

现在,点击开始安装按钮。您应该看到以下屏幕:

单击下一步按钮。您应该会看到以下页面:

选择所需的选项,然后单击“下一步”按钮。您应该看到以下屏幕:

提供您的商店名称、电子邮件、国家/地区、管理员电子邮件、管理员用户名、密码,然后单击“下一步”按钮。您将被重定向到 Shopware 仪表板屏幕:

点击去商店后端按钮。您应该看到以下屏幕:

提供您的登录用户名、密码,然后单击“登录”按钮。您应该会看到以下页面:

现在,完成您的店铺设置并开始使用 Shopware 平台在线销售。
使用 Lets Encrypt 保护 Shopware
在开始之前,您需要在系统中安装 Certbot 客户端以安装和管理 Lets Encrypt SSL。您可以使用以下命令安装它:
apt-get install certbot python3-certbot-nginx -y
安装 Certbot 客户端后,运行以下命令为您的网站下载并安装 Lets 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):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 网站已使用 Lets Encrypt SSL 进行保护。您现在可以使用 URL https://shopware.example.com 安全地访问您的网站。
结论
恭喜!您已经在 Ubuntu 22.04 上成功安装了带有 Nginx 和 Lets Encrypt SSL 的 Shopware。您现在可以开始使用 Shopware 建立自己的在线业务。如果您有任何问题,请随时问我。