NGINX Unit

使用语言模块§

Unit 支持的语言分为以下两类

  • 外部(Go、Node.js):在 Unit 外部运行,并通过接口层与本机运行时通信。

  • 嵌入式(Java、Perl、PHP、Python、Ruby、WebAssembly):在 Unit 在启动时加载的运行时中执行。

对于任何特定语言及其版本,Unit 都需要一个语言模块。

外部语言模块§

外部模块是常规语言库或软件包,您可以像安装其他任何模块一样安装它们。它们提供常见的 Web 功能,从应用程序运行空间与 Unit 进行通信。

在 Go 中,Unit 支持通过一个软件包实现,您可以在应用程序中导入该软件包,以使其支持 Unit。

在 Node.js 中,Unit 受 npm 托管的软件包支持,您可以在应用程序代码中require该软件包。您可以从 npm 存储库安装该软件包;否则,可以使用 Unit 的源代码为您的 Node.js 版本构建该软件包。

对于 WebAssembly,Unit 将字节码执行委托给 Wasmtime 运行时,该运行时通过语言模块模块或在源构建期间安装。

嵌入式语言模块§

嵌入式模块是 Unit 在启动时加载的共享库。查询 Unit 以在您的系统中找到它们

$ unitd -h

       ...
      --log FILE           set log filename
                           default: "/default/path/to/unit.log"

      --modules DIRECTORY  set modules directory name
                           default: "/default/modules/path/"

$ ps ax | grep unitd
      ...
      unit: main v1.32.1 [unitd --log /runtime/path/to/unit.log --modules /runtime/modules/path/ ... ]

$ ls /path/to/modules

      java.unit.so  php.unit.so     ruby.unit.so  wasm_wasi_component.unit.so
      perl.unit.so  python.unit.so  wasm.unit.so

要明确模块版本,请查看Unit 日志,了解在启动时加载了哪些模块

# less /path/to/unit.log
      ...
      discovery started
      module: <language> <version> "/path/to/modules/<module name>.unit.so"
      ...

如果未列出语言版本,Unit 无法运行依赖它的应用程序;但是,您可以添加新模块

  • 如果可能,请使用官方语言包,以便轻松集成和维护。

  • 如果您通过第三方存储库安装了 Unit,请检查那里是否提供了合适的语言包。

  • 如果您想要一个定制但可重用的解决方案,准备您自己的包,以便在 Unit 旁边安装。

打包自定义模块§

您始终有可能需要运行尚未在官方 Unit 中提供的语言版本,但仍希望受益于打包安装的便利性。在这种情况下,您可以构建自己的包,以便与官方发行版一起安装,并添加后者作为先决条件。

在此,我们打包一个自定义 PHP 7.3 模块,以便安装在官方 Unit 包旁边;根据需要调整命令示例以适应您的场景。

注意

有关构建 Unit 语言模块的详细信息,请参阅源代码操作方法;它还描述了构建Unit本身。有关更多打包示例,请参阅我们的包

假设您正在为当前系统打包并且已安装官方 Unit 包

  1. 确保安装包的先决条件。在我们的示例中,它是 Debian 10 上的 PHP 7.3

    # apt update
    # apt install ca-certificates apt-transport-https debian-archive-keyring
    # curl --output /usr/share/keyrings/php-keyring.gpg  \
          https://packages.sury.org/php/apt.gpg
    # echo "deb [signed-by=/usr/share/keyrings/php-keyring.gpg]  \
          https://packages.sury.org/php/ buster main" > /etc/apt/sources.list.d/php.list
    # apt update
    # apt install php7.3
    # apt install php-dev libphp-embed
    
  2. 为您的包创建一个暂存目录

    $ export UNITTMP=$(mktemp -d -p /tmp -t unit.XXXXXX)
    $ mkdir -p $UNITTMP/unit-php7.3/DEBIAN
    $ cd $UNITTMP
    

    这将创建一个适合 dpkg-deb 的文件夹结构;DEBIAN 文件夹将存储包定义。

  3. 运行 unitd --version 并记下 ./configure 标志 以供以后使用,省略 --ld-opt--njs

    $ unitd --version
    
        unit version: 1.32.1
        configured as ./configure FLAGS
    
  4. 下载 Unit 的源代码,配置 并构建自定义模块,然后将其放在 Unit 可以找到的位置

    $ curl -O https://sources.nginx.org/unit/unit-1.32.1.tar.gz
    $ tar xzf unit-1.32.1.tar.gz                                 # Puts Unit's sources in the unit-1.32.1 subdirectory
    $ cd unit-1.32.1
    $ ./configure FLAGS W/O --LD-OPT & --NJS                     # Use the ./configure flags noted in the previous step
    $ ./configure php --module=php7.3 --config=php-config        # Configures the module itself
    $ make php7.3                                                # Builds the module in the build/ subdirectory
    $ mkdir -p $UNITTMP/unit-php7.3/MODULESPATH                  # Use the module path set by ./configure or by default
    $ mv build/php7.3.unit.so $UNITTMP/unit-php7.3/MODULESPATH   # Adds the module to the package
    
  5. 创建一个 $UNITTMP/unit-php7.3/DEBIAN/control 文件,列出 unit 和其他依赖项

    Package: unit-php7.3
    Version: 1.32.1
    Comment0: Use Unit's package version for consistency: 'apt show unit | grep Version'
    Architecture: amd64
    Comment1: To get current architecture, run 'dpkg --print-architecture'
    Comment2: For a list of other options, run 'dpkg-architecture -L'
    Depends: unit (= 1.32.1-1~buster), php7.3, libphp-embed
    Comment3: Specify Unit's package version to avoid issues when Unit updates
    Comment4: Again, run 'apt show unit | grep Version' to get this value
    Maintainer: Jane Doe <j.doe@example.com>
    Description: Custom PHP 7.3 language module for NGINX Unit 1.32.1
    

    保存并关闭文件。

  6. 构建并安装包

    $ dpkg-deb -b $UNITTMP/unit-php7.3
    # dpkg -i $UNITTMP/unit-php7.3.deb
    

假设您正在为当前系统打包并且已安装官方 Unit 包

  1. 确保安装包的 先决条件。在我们的示例中,它是 Fedora 30 上的 PHP 7.3

    # yum install -y php-7.3.8
    # yum install php-devel php-embedded
    
  2. 安装 RPM 开发工具并准备目录结构

    # yum install -y rpmdevtools
    $ rpmdev-setuptree
    
  3. 创建一个 .spec 文件 来存储自定义包的构建命令

    $ cd ~/rpmbuild/SPECS
    $ rpmdev-newspec unit-php7.3
    
  4. 运行 unitd --version 并记下 ./configure 标志 以供以后使用,省略 --ld-opt--njs

    $ unitd --version
    
        unit version: 1.32.1
        configured as ./configure FLAGS
    
  5. 编辑 unit-php7.3.spec 文件,添加下载 Unit 的源代码、配置 和构建自定义模块的命令,然后将其放在 Unit 可以找到的位置

    Name:           unit-php7.3
    Version:        1.32.1
    # Use Unit's package version for consistency: 'yum info unit | grep Version'
    Release:        1%{?dist}
    Summary:        Custom language module for NGINX Unit
    
    License:        ASL 2.0
    # Unit uses ASL 2.0; your license depends on the language you are packaging
    URL:            https://example.com
    BuildRequires:  gcc
    BuildRequires:  make
    BuildRequires:  php-devel
    BuildRequires:  php-embedded
    Requires:       unit = 1.32.1
    # Specify Unit's package version to avoid issues when Unit updates
    # Again, run 'yum info unit | grep Version' to get this value
    Requires:       php >= 7.3
    Requires:       php-embedded
    
    %description
    Custom language module for NGINX Unit 1.32.1 (https://unit.nginx.ac.cn).
    
    Maintainer: Jane Doe <j.doe@example.com>
    
    %prep
    curl -O https://sources.nginx.org/unit/unit-1.32.1.tar.gz
    # Downloads Unit's sources
    tar --strip-components=1 -xzf unit-1.32.1.tar.gz
    # Extracts them locally for compilation steps in the %build section
    
    %build
    ./configure FLAGS W/O --LD-OPT & --NJS
    # Configures the build; use the ./configure flags noted in the previous step
    ./configure php --module=php7.3 --config=php-config
    # Configures the module itself
    make php7.3
    # Builds the module
    
    %install
    DESTDIR=%{buildroot} make php7.3-install
    # Adds the module to the package
    
    %files
    %attr(0755, root, root) "MODULESPATH/php7.3.unit.so"
    # Lists the module as package contents to include it in the package build
    # Use the module path set by ./configure or by default
    

    保存并关闭文件。

  6. 构建并安装包

    $ rpmbuild -bb unit-php7.3.spec
    
        ...
        Wrote: /home/user/rpmbuild/RPMS/<arch>/unit-php7.3-<moduleversion>.<arch>.rpm
        ...
    
    # yum install -y /home/user/rpmbuild/RPMS/<arch>/unit-php7.3-<moduleversion>.<arch>.rpm