如何在 Ubuntu 上安装Vendure 22.04
介绍
Vendure 是一个开源的无头商务框架,允许构建由 Node.js、TypeScript 和 GraphQL 提供支持的生产就绪电子商务应用程序。它的工作原理是通过 API 公开所有应用程序功能。Vendure 在 GraphQL API 上公开了所有商店前端功能,并且不提供默认的商店前端界面。这提高了开发人员的灵活性,因为它允许您使用任何前端技术创建店面。
本文介绍如何在 Ubuntu 22.04 Vultr 服务器上部署 Vendure。您需要将该框架与 Vultr Object Storage(PostgreSQL 托管数据库)和 Redis® 集成,以构建生产就绪型应用程序。
先决条件
准备工作:
- 在 Vultr 上购买 Ubuntu 22.04 服务器
- 部署 Vultr Managed Database for PostgreSQL
- 部署用于缓存的 Vultr 托管数据库
- 部署 Vultr 对象存储
- 设置指向服务器的域 A 记录
- 使用 SSH,以 root 身份登录服务器
- 创建具有 sudo 权限的非 root 用户
- 切换到新的 sudo 用户帐户
- 安装 PostgreSQL 数据库服务器
- 在服务器上安装 s3cmd:
- 配置 s3cmd 以使用 Vultr 对象存储
安装Node.js
Vendure 需要 Node.js 版本 16.x 或更高版本。按照以下步骤中的说明安装最新的 Node.js 版本。
- 更新服务器
- 创建目录
keyrings
- 下载节点源 GPG 密钥并将其添加到服务器密钥
- 将Node.js存储库添加到源列表中。替换为您想要的版本
apt
20
- 更新服务器程序包
- 安装Node.js
- 验证已安装的Node.js版本
输出:
设置本地 PostgreSQL 数据库服务器
要在服务器上安装 Vendure,您需要将应用程序数据临时保存在本地 PostgreSQL 数据库服务器上。稍后,您可以先将数据迁移到 Vultr Managed Database for MySQL,然后再将应用程序部署到生产环境。如下所述设置本地数据库。
在 PostgreSQL 上启用密码身份验证
- 验证已安装的 PostgreSQL 版本
输出:
- 根据您的 PostgreSQL 版本,使用文本编辑器编辑文件,例如
pg_hba.conf
nano
- 查找以下配置行
- 将身份验证方法更改为
peer
md5
保存并关闭文件
- 重新启动 PostgreSQL 数据库服务器
创建新的 PostgreSQL 数据库
- 登录PostgreSQL数据库服务器
- 创建新数据库
输出:
- 使用强密码创建新的数据库用户
输出:
- 授予用户对 Vendure 数据库的完全权限
输出:
- 退出 PostgreSQL 控制台
安装 Vendure
- 使用 Node.js 工具,使用该工具安装 Vendure。
npx
@vendure/create
- 回复每个安装提示,如下所述按 安装软件包Y
@vendure/create
选择为正在使用的数据库
Postgres
按下可设置为 PostgreSQL 主机名ENTER
localhost
按下可保留为 PostgreSQL 端口号ENTER
5432
输入您之前创建的 PostgreSQL 数据库名称
按下可将 PostgreSQL 架构设置为 publicENTER
输入您之前创建的 PostgreSQL 用户
输入您之前创建的 PostgreSQL 数据库用户密码
按下可保留为默认管理员用户名。将用户名更改为所需值ENTER
superadmin
输入您想要的超级管理员用户名或按下以使用默认密码ENTER
Select and press to populate the database with sample product data
yes
ENTER - When successful, the installation process should complete with the following output:
- List files in your working directory
Output:
Verify that a new directory is available on the list
vendure-app
Set Up the Vultr Managed Database for PostgreSQL
- Log in to your Vultr Managed Database for PostgreSQL. Replace , , with your actual values
vultradmin
1234
host.vultrdb.com
Or, copy and use your database connection string from your Vultr Managed Database for PostgreSQL control panel
- When logged in, create a new database
Output:
- Quit PostgreSQL console
将本地 PostgreSQL 数据库数据迁移到 Vultr PostgreSQL 托管数据库
- 使用之前创建的用户和数据库将 PostgreSQL 数据库备份到文件
venduredb.sql
出现提示时,输入您之前设置的正确的 Vendure 用户数据库密码
- 使用备份文件,将数据库还原到 Vultr PostgreSQL 托管数据库。将 ,, 替换为您的实际详细信息。
host.vultrdb.com
vendure_db
vultradmin
- 恢复完成后,访问 Vultr Managed Database for PostgreSQL
- 查看 Vendure 数据库表
输出:
Enter 退出 PostgreSQL 寻呼机Q
- 退出 PostgreSQL 控制台
- 在 Vultr 客户门户中,下载 Vultr Managed Database for PostgreSQL 签名证书文件
ca-certificate.crt
- 下载后,在终端会话中使用文件并将其上传到服务器
scp
- 上传时,请验证证书文件在用户主目录中是否可用
输出:
- 将证书文件移动到目录中。
/usr/local/share/ca-certificates/
- 编辑文件
vendure-config.ts
- 从文件顶部导入方法
readFileSync
node:fs
- 将以下声明添加到该部分
dbConnectionOptions
您编辑的部分应如下所示:
dbConnectionOptions
保存并关闭文件。
切换 Vendure PostgreSQL 数据库配置
- 切换到目录
vendure-app
- 编辑文件
.env
- 使用 Vultr Managed Database for PostgreSQL 详细信息更新以下现有变量
Save and close the file.
Store Vendure Assets in Object Storage
Create a New Bucket
- Log in to the Vultr customer portal
- Navigate to Products -> Cloud Storage -> Object Storage.
- Access your Vultr Object Storage control panel
- Navigate to Buckets
- Click the Create Bucket button, and assign the bucket a name of your choice. For example
vendure
Sync the Assets to Vultr Object Storage
- View your working directory
Verify that you're operating in the directory, or switch to the directory
vendure-app
- Using the tool, synchronize the Vendure assets directory to your Vultr Object Storage bucket
s3cmd
Verify that the file transfer completes successfully
Add the Vultr Object Storage Configuration
- Using , install the and packages
npm
@aws-sdk/client-s3
@aws-sdk/lib-storage
- Edit the file
environment.d.ts
- Add the following configurations within the function
ProcessEnv
Save and close the file.
Your edited file should look like the one below:
- Edit the file:
.env
- Add the following environment variables to the file. Replace the placeholder values with your actual Vultr Object Storage details
Save and close the file.
You can view your Vultr Object Storage details on the instance overview section
- Back up the original file
vendure-config.ts
- Create the file again
- Add the following updated contents to the file. Replace with your actual domain
example.com
Save and close the file.
The above configuration imports the and assets to Vendure. Then, it defines the Vendure URL in the section.
configureS3AssetStorage
asset-server-plugin
example.com
AssetServerPlugin.init({
Store the Job Queue and Session Cache in Redis®
Vendure keeps the job queue in the PostgreSQL database by default. To store the jobs in a Vultr Managed Database for Caching, use the BullMQ job queue plugin as described in the steps below.
Set Up the Job Queue
- Install the BullMQ job queue plugin
- Edit the file
environment.d.ts
- Add the following declarations to the interface after your directives
ProcessEnv
S3
Save and close the file.
- Edit the file
vendure-config.ts
- Add the import directive at the top of the file
BullMQJobQueuePlugin
In the section, find the initialization directive
plugins:
DefaultJobQueuePlugin
Replace it with the following declarations
BullMQJobQueuePlugin
Save and close the file.
- Edit the file
.env
- Add the following environment variables at the end of the file. Replace the placeholder values with your Vultr Managed Database for Caching details
You can find your Vultr Managed Database for Caching details on your instance overview section
Configure Session Cache
Vendure stores the session object cache in your system memory. It's fast and suitable for a single-instance deployment. However, for horizontal scaling or multi-instance deployment, you must store the session cache to an external data store such as a Vultr Managed Database for Caching. To enable Redis®, create a custom session cache strategy as described below.
- Create a new file in the directory
redis-session-cache-strategy.ts
plugins
- Add the following configurations to the file
Save and close the file
- Edit the file
vendure-config.ts
- Add the directive to the import section
RedisSessionCachePlugin
Within the section, add the following code after to initialize the
Plugins: [
AdminUiPlugin.init(...)
RedisSessionCachePlugin
Save and close the file.
Build for Vendure Production
- Edit the file
.env
- Change the value from to
APP_ENV
dev
production
Save and close the file.
- Install the Vendure harden plugin that locks down your schema and protects your shop API from malicious queries
- Edit the file
vendure-config.ts
- Add the following import directive to the section
harden plugin
import
Within the section, add the following code to initialize the harden plugin
Plugins:
Save and close the file.
The edited file should look like the one below:
vendure-config.ts
- Build the Vendure application
npm
writes the build files to the directorydist
Run the Production Vendure App
- Install the package
PM2
- Run the Vendure app server in cluster mode
- Run the Vendure app worker in cluster mode
Output:
- Generate and run a startup script to start PM2 when the server reboots
Output:
- Save all the running processes
- Using , test that your Vendure app runs on the default port
wget
3000
Output:
If Vendure fails to run and listen on port . Start it using the following command to view the runtime log and catch any errors
3000
Output:
When successful, verify that Vendure runs on port
3000
Configure Nginx as a Reverse Proxy
To securely access the Vendure app through your domain name, configure Nginx as a reverse proxy to handle connections to the backend port as described below.3000
- Install Nginx
- Disable the default Nginx configuration
- Create a new Nginx virtual host configuration file
- Add the following configurations to the file. Replace with your actual domain
example.com
Save and close the file.
- Enable the Nginx configuration file
- Test your configurations from syntax errors
- Reload Nginx configurations to save changes
Security
To secure Vendure for production use, allow Nginx to accept incoming connections on HTTP port and the HTTPS port . Then, securely redirect all HTTP requests to HTTPS by generating SSL certificates as described in the steps below.80
443
Configure the Firewall
- Allow SSH port connection to the server
- Allow the Nginx HTTP and HTTPS ports profile
- Enable the firewall
- Verify the firewall status
Output:
Secure Vendure with Let's Encrypt SSL Certificates
- Using , install the Certbot Let's Encrypt client tool
snap
- Create a symbolic link for the system wide Certbot command to
/usr/bin
- Generate your Let's Encrypt SSL certificate. Replace , with your domain, and email address respectively
example.com
hello@example.com
- Verify that Certbot auto renews the SSL certificate upon expiry
Test the Application
- Using a web browser such as Chrome, access your Vendure shop administrator page
Log in with the credentials you set earlier. When successful, the administrator dashboard should display
superadmin
- Navigate to the Inventory menu. Verify that all sample products display on the page. Try to update or delete any of them.
- Navigate to the Assets menu and upload a sample image
- 在 Vultr 客户门户会话中,查看 Vultr 对象存储桶。验证映像在存储桶中是否可用
- 除了 Vendure 管理员页面,还可以访问以下 GraphQL API 端点:
- 管理员 GraphQL API:
https://example.com/admin-api
- 购买 GraphQL API:
https://example.com/shop-api
- 管理员 GraphQL API:
当您设计 Vendure 商店时,前端界面会随着您的产品和设计而激活。
结论
在本指南中,您安装了 Vendure 应用程序并将其部署到生产环境。您还将 Vendure 配置为使用 Vultr 对象存储、用于 PostgreSQL 的 Vultr 托管数据库和用于缓存的 Vultr 托管数据库。有关 Vendure 的更多信息,请访问官方文档。
1.本站主要是为了记录工作、学习中遇到的问题,可能由于本人技术有限,内容难免有纰漏,一切内容仅供参考。
2.本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!
3.本站所有原创作品,包括文字、资料、图片、网页格式,转载时请标注作者与来源。




