Meilisearch多实例运行,在同一台服务器上实现多站点搜索
要使用Meilisearch实现多个应用的搜索支持有两个方案:
- 单实例,每个实例下分配规则供多站点/应用使用
- 多个实例,一个实例一个应用
单实例优势是统一的服务管理,其中它有两个实现方案
- 多索引,为每个站点分配一个索引
- 单索引,使用Tenant Token和searchRules为不同的站点分配搜索规则
由于笔者近期使用medusa,目前还不支持单实例的实现(官方写的是可以,但是会出现无法索引内容的清空),所以只能使用多实例来实现
1.每个应用/站点下建立meilisearch目录
2.下载toml配置文件
curl https://raw.githubusercontent.com/meilisearch/meilisearch/latest/config.toml > config.toml
如果无法访问可以直接复制下面内容保存为config.toml
# This file shows the default configuration of Meilisearch. # All variables are defined here: https://www.meilisearch.com/docs/learn/configuration/instance_options#environment-variables # Designates the location where database files will be created and retrieved. # https://www.meilisearch.com/docs/learn/configuration/instance_options#database-path db_path = "./data.ms" # Configures the instance's environment. Value must be either `production` or `development`. # https://www.meilisearch.com/docs/learn/configuration/instance_options#environment env = "development" # The address on which the HTTP server will listen. http_addr = "localhost:7700" # Sets the instance's master key, automatically protecting all routes except GET /health. # https://www.meilisearch.com/docs/learn/configuration/instance_options#master-key # master_key = "YOUR_MASTER_KEY_VALUE" # Deactivates Meilisearch's built-in telemetry when provided. # Meilisearch automatically collects data from all instances that do not opt out using this flag. # All gathered data is used solely for the purpose of improving Meilisearch, and can be deleted at any time. # https://www.meilisearch.com/docs/learn/configuration/instance_options#disable-analytics # no_analytics = true # Sets the maximum size of accepted payloads. # https://www.meilisearch.com/docs/learn/configuration/instance_options#payload-limit-size http_payload_size_limit = "100 MB" # Defines how much detail should be present in Meilisearch's logs. # Meilisearch currently supports six log levels, listed in order of increasing verbosity: `OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE` # https://www.meilisearch.com/docs/learn/configuration/instance_options#log-level log_level = "INFO" # Sets the maximum amount of RAM Meilisearch can use when indexing. # https://www.meilisearch.com/docs/learn/configuration/instance_options#max-indexing-memory # max_indexing_memory = "2 GiB" # Sets the maximum number of threads Meilisearch can use during indexing. # https://www.meilisearch.com/docs/learn/configuration/instance_options#max-indexing-threads # max_indexing_threads = 4 ############# ### DUMPS ### ############# # Sets the directory where Meilisearch will create dump files. # https://www.meilisearch.com/docs/learn/configuration/instance_options#dump-directory dump_dir = "dumps/" # Imports the dump file located at the specified path. Path must point to a .dump file. # https://www.meilisearch.com/docs/learn/configuration/instance_options#import-dump # import_dump = "./path/to/my/file.dump" # Prevents Meilisearch from throwing an error when `import_dump` does not point to a valid dump file. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ignore-missing-dump ignore_missing_dump = false # Prevents a Meilisearch instance with an existing database from throwing an error when using `import_dump`. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ignore-dump-if-db-exists ignore_dump_if_db_exists = false ################# ### SNAPSHOTS ### ################# # Enables scheduled snapshots when true, disable when false (the default). # If the value is given as an integer, then enables the scheduled snapshot with the passed value as the interval # between each snapshot, in seconds. # https://www.meilisearch.com/docs/learn/configuration/instance_options#schedule-snapshot-creation schedule_snapshot = false # Sets the directory where Meilisearch will store snapshots. # https://www.meilisearch.com/docs/learn/configuration/instance_options#snapshot-destination snapshot_dir = "snapshots/" # Launches Meilisearch after importing a previously-generated snapshot at the given filepath. # https://www.meilisearch.com/docs/learn/configuration/instance_options#import-snapshot # import_snapshot = "./path/to/my/snapshot" # Prevents a Meilisearch instance from throwing an error when `import_snapshot` does not point to a valid snapshot file. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ignore-missing-snapshot ignore_missing_snapshot = false # Prevents a Meilisearch instance with an existing database from throwing an error when using `import_snapshot`. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ignore-snapshot-if-db-exists ignore_snapshot_if_db_exists = false ########### ### SSL ### ########### # Enables client authentication in the specified path. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-authentication-path # ssl_auth_path = "./path/to/root" # Sets the server's SSL certificates. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-certificates-path # ssl_cert_path = "./path/to/certfile" # Sets the server's SSL key files. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-key-path # ssl_key_path = "./path/to/private-key" # Sets the server's OCSP file. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-ocsp-path # ssl_ocsp_path = "./path/to/ocsp-file" # Makes SSL authentication mandatory. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-require-auth ssl_require_auth = false # Activates SSL session resumption. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-resumption ssl_resumption = false # Activates SSL tickets. # https://www.meilisearch.com/docs/learn/configuration/instance_options#ssl-tickets ssl_tickets = false ############################# ### Experimental features ### ############################# # Experimental metrics feature. For more information, see: <https://github.com/meilisearch/meilisearch/discussions/3518> # Enables the Prometheus metrics on the `GET /metrics` endpoint. experimental_enable_metrics = false # Experimental RAM reduction during indexing, do not use in production, see: <https://github.com/meilisearch/product/discussions/652> experimental_reduce_indexing_memory_usage = false
3.启动meilisearch服务
./meilisearch --config-file-path="./config.toml"
需要注意的是,多个应用的端口一定要区分开。
测试无误后我们可以正式部署到生产环境,需要修改config.toml
中的env
参数
Ubuntu中的部署
注意:多个应用中的meilisearch文件要避免相同名称,以下为单一文件的示例操作
安装Meilisearch
# Update the list of available packages and their versions apt update # Install curl which is required to install Meilisearch in the next step apt install curl -y # Install Meilisearch latest version from the script curl -L https://install.meilisearch.com | sh
设置权限
chmod +x meilisearch
移动到系统bin目录中
# Move the Meilisearch binary to your system binaries mv ./meilisearch /usr/local/bin/
创建系统用户
以 root 身份运行应用程序可能会在系统中引入安全漏洞。为了防止这种情况发生,请创建一个专门的系统用户来运行Meilisearch:
useradd -d /var/lib/meilisearch -b /bin/false -m -r meilisearch
创建配置文件
将toml
文件创建到etc
目录中
curl https://raw.githubusercontent.com/meilisearch/meilisearch/latest/config.toml > /etc/meilisearch.toml
配置参数
env = "production" master_key = "YOUR_MASTER_KEY_VALUE" db_path = "/var/lib/meilisearch/data" dump_dir = "/var/lib/meilisearch/dumps" snapshot_dir = "/var/lib/meilisearch/snapshots"
最后设置权限
mkdir /var/lib/meilisearch/data /var/lib/meilisearch/dumps /var/lib/meilisearch/snapshots chown -R meilisearch:meilisearch /var/lib/meilisearch chmod 750 /var/lib/meilisearch
设置Meilisearch作为系统服务
服务文件是告诉操作系统如何以及何时运行程序的文本文件。它们位于/etc/systemd/system
目录中,您的系统将在启动时加载它们。在本例中,我们使用一个非常简单的服务文件来在端口7700
上运行 Meilisearch
。
要在生产环境中运行 Meilisearc
h,请使用该--env
标志。使用该选项设置至少 16 字节的主密钥--master-key
。当您第一次启动实例时,Meilisearch 会创建两个默认 API 密钥:Default Search API Key
和Default Admin API Key
。使用Default Admin API Ke
y,您可以控制谁可以访问或创建新文档、索引或更改配置。
请记住选择一个安全且随机的密钥,并避免将其暴露在可公开访问的应用程序中。您可以使用以下命令更改主密钥:
创建服务文件
cat << EOF > /etc/systemd/system/meilisearch.service [Unit] Description=Meilisearch After=systemd-user-sessions.service [Service] Type=simple WorkingDirectory=/var/lib/meilisearch ExecStart=/usr/local/bin/meilisearch --config-file-path /etc/meilisearch.toml User=meilisearch Group=meilisearch [Install] WantedBy=multi-user.target EOF
就目前而言,还不是将您的美丽搜索实例暴露给外部的时候。为了在您自己的环境中保持安全运行,请使其在本地可用local
。这意味着只有在您的计算机上运行的程序才被允许向您的Meilisearch 实例发出请求。
启用服务
# Set the service meilisearch systemctl enable meilisearch # Start the meilisearch service systemctl start meilisearch # Verify that the service is actually running systemctl status meilisearch
服务状态
-# --- Expected output --- ● meilisearch.service - MeiliSearch Loaded: loaded (/etc/systemd/system/meilisearch.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-04-10 14:27:49 UTC; 1min 8s ago Main PID: 14960 (meilisearch)
至此,Meilisearch已安装并运行。它可以免受最终崩溃、系统重新启动以及运行时可能发现的大多数问题的影响。但它仍然隐藏在计算机的本地并受到保护,并且外部无法访问。如果您对Meilisearch所做的所有请求都是由同一台计算机上的应用程序完成的,您可以到此为止。
但你可能想向外界开放你的搜索,让我们以安全的方式解决这个问题。
配置外网访问
安装Nginx反向代理
# Install Nginx on Debian apt-get install nginx -y
首先,删除默认配置文件很重要,因为 Nginx 默认使用 HTTP 的默认端口 80
。因此,尝试将其用于 Meilisearch 会产生冲突。用你自己的配置文件替换默认文件。你也可以在 Nginx 配置文件中指定另一个端口,让 Meilisearch 监听该端口,但本教程将不涉及这一选项。
# Delete the default configuration file for Nginx rm -f /etc/nginx/sites-enabled/default # Add your configuration file specifying the Reverse Proxy settings cat << EOF > /etc/nginx/sites-enabled/meilisearch server { listen 80 default_server; listen [::]:80 default_server; server_name _; location / { proxy_pass http://localhost:7700; } } EOF
修改server_name
绑定对应域名,修改7700
与实际应用端口一致
最后,再次启用并启动 Nginx 服务
# Reload the operating system daemons / services systemctl daemon-reload # Enable and start Nginx service systemctl enable nginx systemctl restart nginx
安装SSL证书
安装Certbot
sudo apt-get install certbot python-certbot-nginx -y
生成证书
certbot --nginx -d yourdomain.com
最后自动续期
sudo systemctl status certbot.timer
防火墙放行规则等更多细节请参考在Ubuntu中为Nginx部署自动续期的Let's Encrypt服务一文。
有用的链接:
https://www.meilisearch.com/docs/learn/cookbooks/running_production