Debian/Ubuntu
-
Install the prerequisite packages for the OpenResty installation.
-
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.
-
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:
-
For Debian systems, use the following command:
-
Update the package manager’s index, then install OpenResty.
-
Using your preferred text editor, open the
nginx.conffile for your OpenResty instance. This file contains the default OpenResty server configuration and should be located at/usr/local/openresty/nginx/conf/nginx.conf. -
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
/ path) and adds two new paths to the server.
-
A
/content-testpath. This path uses a block of Lua code within the configuration file to serve simple HTML content with theHello, world!message. The example, while brief, gives an idea of how Lua script can be woven in with your OpenResty server. -
A
/rest-testpath. 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.
-
Create a directory to hold Lua script files for your OpenResty server. This example uses a
scriptssubdirectory within thenginxdirectory for OpenResty. -
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
-
Navigate to the
/content-testpath in your web browser to see aHello, world!page. For example, the URL would behttp://192.0.2.0/content-testif your instance’s remote IP address was192.0.2.0. -
Navigate to the
/rest-testpath in your web browser to see the JSON content being served by your OpenResty server. That URL would behttp://192.0.2.0/rest-testif your server’s remote IP address was192.0.2.0.
-c 指定的配置文件。要让 OpenResty 服务使用特定的配置文件,可以通过修改 OpenResty 的系统服务配置来实现。
修改 OpenResty 系统服务配置
你可以修改 OpenResty 的服务文件,确保它启动时加载你指定的配置文件。以下是步骤:-
编辑 OpenResty 服务文件: 通常,这个文件位于
/lib/systemd/system/openresty.service或/etc/systemd/system/openresty.service。使用以下命令打开编辑: sh -
修改服务文件: 找到
ExecStart行,并添加你指定的配置文件路径。例如: ini -
重新加载系统服务: 保存文件并退出编辑器后,重新加载系统服务配置:
sh
-
重启 OpenResty 服务: 重启 OpenResty 服务以应用新的配置:
sh