Skip to main content

Debian/Ubuntu

  1. Install the prerequisite packages for the OpenResty installation.
    sudo apt install --no-install-recommends wget gnupg ca-certificates
    
  2. Add the GPG key for the OpenResty repository with the following command for Debian and Ubuntu versions before 22.
    wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
    
    If you are running Ubuntu 22 or later, use the following command instead.
    wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/openresty.gpg
    
  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:
      codename=`grep -Po 'VERSION="[0-9]+ \(\K[^)]+' /etc/os-release`
      echo "deb http://openresty.org/package/debian $codename openresty" | sudo tee /etc/apt/sources.list.d/openresty.list
      
    • For Ubuntu versions earlier than 22, use the following command:
      echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list
      
    • For Ubuntu 22 and later, use the following command:
      echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/openresty.gpg] http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list > /dev/null
      
  4. Update the package manager’s index, then install OpenResty.
    sudo apt update
    sudo apt install openresty
    
You can verify that the installation was successful by checking that the OpenResty service is running.
sudo systemctl status openresty
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
worker_processes 1;

events {
    worker_connections 1024;
}


http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name localhost;

        root html;
        index index.html index.htm;

        location / {
            try_files $uri $uri/ =404;
        }

        location /content-test {
            default_type text/html;
            content_by_lua_block {
                ngx.say('<html><body><p>Hello, world!</p></body></html>')
            }
        }

        location /rest-test {
            charset utf-8;
            charset_types application/json;
            default_type application/json;
            content_by_lua_file scripts/rest.lua;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}
    
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.
    mkdir /usr/local/openresty/nginx/scripts
    
  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
-- Imports a module for handling JSON; cjson is included with OpenResty
-- by default.
local cjson = require("cjson")

-- Defines an initial status for the request.
ngx.status = ngx.HTTP_OK

-- Serves the JSON content, using the cjson module to encode the Lua
-- object as a JSON one.
ngx.say(cjson.encode(
    {
        name = "world",
        message = "Hello, ",
        punctuation = "!"
    }
))

-- Exits with the success status.
return ngx.exit(ngx.HTTP_OK)
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.
    Hello, world!
    
  • 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.
    {
      "name": "world",
      "message": "Hello, ",
      "punctuation": "!"
    }
    
那系统服务启动 openresty ,默认并不会加载 -c 指定的配置文件啊 你说得对,当使用系统服务启动 OpenResty 时,它会加载默认的配置文件,而不是通过命令行参数 -c 指定的配置文件。要让 OpenResty 服务使用特定的配置文件,可以通过修改 OpenResty 的系统服务配置来实现。

修改 OpenResty 系统服务配置

你可以修改 OpenResty 的服务文件,确保它启动时加载你指定的配置文件。以下是步骤:
  1. 编辑 OpenResty 服务文件: 通常,这个文件位于 /lib/systemd/system/openresty.service/etc/systemd/system/openresty.service。使用以下命令打开编辑: sh
    sudo nano /lib/systemd/system/openresty.service
    
  2. 修改服务文件: 找到 ExecStart 行,并添加你指定的配置文件路径。例如: ini
    [Unit]
    Description=OpenResty Web Server
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /var/www/myproject/conf/nginx.conf
    ExecReload=/bin/kill -HUP $MAINPID
    ExecStop=/bin/kill -QUIT $MAINPID
    PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  3. 重新加载系统服务: 保存文件并退出编辑器后,重新加载系统服务配置: sh
    sudo systemctl daemon-reload
    
  4. 重启 OpenResty 服务: 重启 OpenResty 服务以应用新的配置: sh
    sudo systemctl restart openresty
    
通过这些步骤,你可以确保 OpenResty 服务启动时使用指定的配置文件。
I