Skip to main content

Debian/Ubuntu

  1. Install the prerequisite packages for the OpenResty installation.
  2. Add the GPG key for the OpenResty repository with the following command for Debian and Ubuntu versions before 22.
    If you are running Ubuntu 22 or later, use the following command instead.
  3. Add the OpenResty repository to the APT package manager. This step varies depending on your system’s distribution and its version.
    • For Debian systems, use the following command:
    • For Ubuntu versions earlier than 22, use the following command:
    • For Ubuntu 22 and later, use the following command:
  4. Update the package manager’s index, then install OpenResty.
You can verify that the installation was successful by checking that the OpenResty service is running.
One of OpenResty’s most compelling features is its ability to run Lua scripts within its server configurations. The example below demonstrates this feature by creating a server with basic Lua scripting.
  1. Using your preferred text editor, open the nginx.conf file for your OpenResty instance. This file contains the default OpenResty server configuration and should be located at /usr/local/openresty/nginx/conf/nginx.conf.
  2. Remove the contents of the file, and replace them with the code shown here. (OpenResty typically comes with a duplicate of the default configuration file as nginx.conf.default, should you ever want to revert to it). File: /usr/local/openresty/nginx/conf/nginx.conf
This NGINX configuration leaves the default welcome page intact (at the / path) and adds two new paths to the server.
  • /content-test path. This path uses a block of Lua code within the configuration file to serve simple HTML content with the Hello, world! message. The example, while brief, gives an idea of how Lua script can be woven in with your OpenResty server.
  • /rest-test path. This path starts to demonstrate the broader range of OpenResty’s features. It uses a Lua script file, created in the next two steps, and serves JSON. In fact, this path’s model could be leveraged to make a simple RESTful API service fully within OpenResty.
  1. Create a directory to hold Lua script files for your OpenResty server. This example uses a scripts subdirectory within the nginx directory for OpenResty.
  2. Create a new file in that directory with the name rest.lua. Give the file the contents shown here. The in-code comments — lines beginning with -- explain what each part of this script does. File: /usr/local/openresty/nginx/scripts/rest.lua
You now have an OpenResty web server with three endpoints. In addition to the welcome page shown above, you have a simple HTML page and a JSON server.
  • Navigate to the /content-test path in your web browser to see a Hello, world! page. For example, the URL would be http://192.0.2.0/content-test if your instance’s remote IP address was 192.0.2.0.
  • Navigate to the /rest-test path in your web browser to see the JSON content being served by your OpenResty server. That URL would be http://192.0.2.0/rest-test if your server’s remote IP address was 192.0.2.0.
那系统服务启动 openresty ,默认并不会加载 -c 指定的配置文件啊 你说得对,当使用系统服务启动 OpenResty 时,它会加载默认的配置文件,而不是通过命令行参数 -c 指定的配置文件。要让 OpenResty 服务使用特定的配置文件,可以通过修改 OpenResty 的系统服务配置来实现。

修改 OpenResty 系统服务配置

你可以修改 OpenResty 的服务文件,确保它启动时加载你指定的配置文件。以下是步骤:
  1. 编辑 OpenResty 服务文件: 通常,这个文件位于 /lib/systemd/system/openresty.service/etc/systemd/system/openresty.service。使用以下命令打开编辑: sh
  2. 修改服务文件: 找到 ExecStart 行,并添加你指定的配置文件路径。例如: ini
  3. 重新加载系统服务: 保存文件并退出编辑器后,重新加载系统服务配置: sh
  4. 重启 OpenResty 服务: 重启 OpenResty 服务以应用新的配置: sh
通过这些步骤,你可以确保 OpenResty 服务启动时使用指定的配置文件。