在运营中使用MeiliSearch:将你的网站性能提升到一个新的水平

在运营中使用MeiliSearch:将你的网站性能提升到一个新的水平

快速介绍

希望您已经知道美利搜索是一个内置的强大而快速的开源搜索引擎.它旨在为用户提供非常有用且可自定义的搜索体验,包括开箱即用的功能,例如拼写错误容错、过滤和任何类型的项目的同义词。

运行用于测试目的的 Meilisearch 实例非常容易,可以通过多种方式完成:使用 Docker、Homebrew、Aptitude、二进制文件、简单 ,甚至是源代码。如果您是Meilisearch的新手,我们建议您浏览一下文档。curl

在您自己的机器上使用Meilisearch进行周末项目很有趣,让我们同意这一点。但是,您可能希望上线并在生产中部署项目,以将其提升到一个新的水平。您需要执行哪些步骤和详细信息才能在生产中部署 Meilisearch 并确保其安全且随时可用

让您的美利搜索为生产做好准备

在本教程中,我们将使用运行在 DigitalOcean 上的 Debian 10 服务器。您可以轻松地自己尝试,计划起价为每月 5 美元。如果您想要一些积分来开始运行您的 Meilisearch,并且尚未在 DigitalOcean 上注册,您可以使用此推荐链接.

先决条件

  • 运行 Debian 10 的最新服务器
  • 用于连接到该计算机的 ssh 密钥对
提示

了解如何通过 SSH 连接到您的数字海洋液滴或任何Linux 或 Windows 服务器

步骤 1:安装美利搜索

安装和运行美利搜索既简单又直接。为了使本教程尽可能简单,让我们使用一个将执行安装过程的脚本。它会将Meilisearch的二进制文件复制到您的计算机,并使您能够立即使用它。

通过 SSH 登录到计算机后,请确保系统及其依赖项是最新的,然后再继续安装。

砰砰��

# 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

本指南详细介绍了不同的 Meilisearch 安装选项。

有许多不同的方法可以让Meilisearch在您的机器上运行。作为一个开源项目,您始终可以从其源代码编译 Meilisearch 的最新稳定版本,以确保二进制文件以最佳方式使用您的架构。

您可以随时查看最新的Meilisearch稳定版本,并通过访问以下链接获取您选择的操作系统的Meilisearch:

最新美利搜索稳定版

使用以下方法授予二进制执行权限:

砰砰��

chmod +x meilisearch

美利搜索终于安装完毕,可以使用了。要使其可从系统中的任何位置访问,请将二进制文件移动到系统二进制文件夹中:

砰砰��

# Move the Meilisearch binary to your system binaries
mv ./meilisearch /usr/local/bin/

步骤 2:创建系统用户

以 root 身份运行应用程序可能会在系统中引入安全漏洞。为了防止这种情况发生,请创建一个用于运行 Meilisearch 的专用系统用户:

砰砰��

useradd -d /var/lib/meilisearch -b /bin/false -m -r meilisearch

步骤 3:创建配置文件

将默认配置下载到:/etc

砰砰��

curl https://raw.githubusercontent.com/meilisearch/meilisearch/latest/config.toml > /etc/meilisearch.toml

更新以下行,以便 Meilisearch 将其数据存储在新创建的用户的主文件夹中:

.INI

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

Step 4: Run Meilisearch as a service

In Linux environments, a is a process that can be launched when the operating system is booting and which will keep running in the background. One of its biggest advantages is making your program available at any moment. Even if some execution problems or crashes occur, the service will be restarted and your program will be run again.service

NOTE

If you are new to services and , you can learn more about the basics of Linux services systemdhere.

In Debian and other Linux distributions, allows you to create and manage your own custom services. In order to make sure that Meilisearch will always respond to your requests, you can build your own service. This way, you will ensure its availability in case of a crash or in case of system reboot. If any of these occur, will automatically restart Meilisearch.systemdsystemd

4.1. Create a service file

Service files are text files that tell your operating system how to run your program, and when. They live in the directory, and your system will load them at boot time. In this case, let’s use a very simple service file that will run Meilisearch on port ./etc/systemd/system7700

To run Meilisearch in a production environment, use the flag. Set a master key of at least 16 bytes using the option. When you launch an instance for the first time, Meilisearch creates two default API keys: Default Search API Key and Default Admin API Key. With the , you can control who can access or create new documents, indexes, or change the configuration.--env--master-keyDefault Admin API Key

Remember to choose a safe and random key and avoid exposing it in publicly accessible applications. You can change the master key with the following command:

BASH

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
提示

有关 Meilisearch 安全性和 API 密钥的更多信息,请参阅安全文档。您可以查看我们的快速入门指南,了解有关如何启动和运行Meilisearch的更多信息。

至于现在,现在还不是将您的美利搜索实例暴露给外部世界的时候。要继续在您自己的环境中安全地运行它,请在 上使其在本地可用。这意味着只允许在您的计算机上运行的程序向您的 Meilisearch 实例发出请求。local

4.2. 启用和启动服务

您刚刚构建的服务文件是创建服务所需的全部内容。现在你必须告诉操作系统我们希望它在每次启动时运行Meilisearch。然后,您可以使服务立即运行。通过检查服务确保一切顺利进行。enablestartstatus

砰砰��

# 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 执行的所有请求都是由位于同一台机器上的另一个应用程序完成的,则可以在此处停止。

但你可能想向外界开放你的美利搜索,目前,它是孤立的。让我们以安全的方式解决这个问题。

第 5 步:保护并完成设置

是时候安全地将您的全新 Meilisearch 提供给外界请求了。为此,您将使用 Web 上可用的两种主要技术:反向代理和 SSL/TLS。

5.1. 创建反向代理恩金克斯

反向代理基本上是一个应用程序,它将处理外部世界和内部应用程序之间的每次通信。Nginx将接收外部HTTP请求并将其重定向到Meilisearch。当Meilisearch完成其出色的工作时,它会将其响应传达给Nginx,然后将后者传输给最初发送请求的用户。这是通过添加强大、安全和快速的看门人(例如 Nginx)来隔离和保护任何应用程序的常用方法,Nginx 是在线最安全、最有效的工具之一,当然还有开源!

提示

反向代理在安全性、性能、可伸缩性和日志记录问题方面非常有用。如果您不熟悉反向代理,您可能会喜欢这篇文章,解释为什么和如何反向代理.

将Nginx配置为代理服务器非常简单。首先,将其安装在您的计算机上。

砰砰��

# Install Nginx on Debian
apt-get install nginx -y

首先,删除默认配置文件作为HTTP的默认端口很重要,默认情况下,Nginx使用。因此,尝试将其用于Meilisearch会产生冲突。将默认文件替换为您自己的配置文件。你也可以通过在 Nginx 配置文件中指定 Meilisearch 监听另一个端口,但我们在本教程中不会介绍此选项。port 80

砰砰��

# 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

Finally, enable and start the Nginx service again to make sure it is still available.

BASH

# Reload the operating system daemons / services
systemctl daemon-reload

# Enable and start Nginx service
systemctl enable nginx
systemctl restart nginx

Meilisearch现已启动,部署在生产环境中,使用安全的API密钥,并由反向代理Nginx提供服务。您现在应该能够从外部世界向服务器发送请求。打开您的网络浏览器并访问:(http://your-ip-address).IP 地址与您在步骤 1 中用于通过 SSH 连接到计算机的 IP 地址相同。

注意

如果您想了解有关使用 Nginx 作为反向代理的更多信息,请参阅此专用文档.

唯一剩下的问题是Meilisearch通过HTTP处理请求,没有任何额外的安全性。通过HTTP传输的内容很容易被攻击者读取或修改,并且有人可以完全或部分访问您的数据。为了防止这种情况发生,使用 HTTPS 非常重要,这将使您能够使用 SSL/TLS 证书并安全地传输数据。

5.2. 为您的美利搜索设置 SSL/TLS

SSL将允许用户或客户端与Meilisearch建立经过身份验证的连接。通过这种方式,用户可以在发送敏感数据或向其发出任何请求之前验证服务器的身份。然后,数据以只有Meilisearch服务器才能解密的加密方式发送,为您提供快速,可靠和自动的安全层。

在大多数情况下,启用 SSL 时,您可能希望使用自己的域名(或子域)。您需要遵循的第一步是注册自己的域名并更改 DNS 记录。要使您的域名指向新安装的Meilisearch服务器,您只需添加一个指向用于连接到您自己的服务器的IP地址的指向即可。此过程简单快捷,但可能因每个域名提供商而异。因此,我们不会在本文中介绍该过程。A record

提示

当您注册域名并添加 时,您应该能够使用该域名直接自动请求 Meilisearch。 为了说明这一点,如果您已经注册了域名,则请求索引将在A recordexample.comhttp://example.com/indexes

设置域名后,您就可以配置 SSL/TLS 并使用 HTTPS。您有两种不同的选择来实现此目标。第一个是使用塞特博特,一个惊人的,免费的,非常易于使用的工具。如果您已经为您的域名颁发了 SSL 证书,则第二个选项涵盖了您需要遵循的步骤。然后,您就可以在生产中安全地使用美利搜索了!Certificate Authority or CA

5.2.1. 选项 A:认证机器人

在Linux服务器中使用certbot非常简单。此工具将为您的域名生成免费的SSL / TLS证书,并自动处理其在服务器上的安装。certbot 文档包含许多操作系统和服务器的详细说明,但我们将按照以下说明进行操作Certbot on Debian with Nginx.

首先,在您的系统上安装软件包:

砰砰��

sudo apt-get install certbot python-certbot-nginx -y

让我们运行 Certbot 脚本以指导完成安装过程:

砰砰��

certbot --nginx

输入您的电子邮件地址,同意条款和条件,然后输入您的域名。然后,系统将提示您以下选项:

砰砰��

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.

我们建议您选择选项 2,将 HTTP 重定向到 HTTPS 并始终使用安全连接。您应该能够使用 SSL 请求您的域名,如 或 .https://example.comhttps://example.com/indexes

5.2.2. 选项 B:自定义 SSL/TLS 证书

当 为您颁发 SSL 证书时,您至少会收到两个包含加密密钥的文件:Certificate Authority

  • 您的证书(通常命名或your_domain_name.pemexample.pem)
  • 您的密钥(通常命名为 或your_domain_name.keyexample.key)

example.pem并将在以下示例中使用。请确保替换为您自己的证书文件的名称。example.keyexample

您需要做的就是将证书文件存储在安全位置,并使用适当的文件系统安全权限。然后,在 Nginx 配置中设置证书的位置。还强烈建议将所有 HTTP 请求重定向到 HTTPS(端口 80 到 443)。

首先,让我们将证书文件复制到其常规目录中,以便服务器可以找到它们:

砰砰��

# Create a directory /etc/ssl/example to store the certificate files
mkdir -p /etc/ssl/example

# Move your files to /etc/ssl/example. We will suppose that your
# files are called example.pem and example.key

mv path-to-your-files/example.pem /etc/ssl/example/
mv path-to-your-files/example.key /etc/ssl/example/

最后,我们创建一个新的 Nginx 配置文件,并重新启动守护进程和 Nginx 服务

请记住将两个字段中的域名替换为您自己的域名example.comserver_name

砰砰��


# Replace example.com in both `server_name` fields with your own domain name

cat << EOF > /etc/nginx/sites-enabled/meilisearch
server {
      listen 80 default_server;
      listen [::]:80 default_server;

      server_name example.com;

      return 301 https://\$server_name\$request_uri;
}
server {
    server_name example.com;

    location / {
        proxy_pass  http://localhost:7700;
    }

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;

    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    ssl_certificate /etc/ssl/example/example.pem;
    ssl_certificate_key /etc/ssl/example/example.key;
}
EOF

systemctl restart nginx

您的SSL证书应该可以工作,Nginx应该能够找到它们。现在,每个请求都将重定向到http://example.comhttps://example.com

结论

您已按照主要步骤提供安全稳定的服务。您的 Meilisearch 实例应在安全的环境中启动并运行,并且即使在出现最常见问题时也能随时可用。此外,它受具有您自己的域名和 API 密钥的反向代理保护,因此您的数据和配置只能由受信任的客户端访问。与服务器的通信现已加密。此外,在以快速和自动的方式发送敏感数据之前,每次都会验证其身份。

您现在可以开始使用生产就绪的 Meilisearch 实例了!

无错源码所有资源来自会员发布以及互联网收集,不代表本站立场,如有侵犯你的权益请联系管理员,站内发信联系 我们会第一时间进行审核删除。站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请在下载24小时内删除!


如果遇到付费才可观看的文章,建议升级传奇VIP。全站所有资源任意下免费看”。本站资源少部分采用7z压缩,为防止有人压缩软件不支持7z格式,7z解压,建议下载7-zip,zip、rar解压,建议下载WinRAR如遇解压需要密码,请尝试使用www.wucuoym.com来解压,如若仍有问题,请联系站长。

给TA打赏
共{{data.count}}人
人已打赏
使用指南

2023年如何搭建干净的DNS服务

2023-10-16 8:56:53

使用指南

使用cdnfly自建CDN并配置CC防护(更新了漏洞)

2023-10-24 19:25:15

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索