Deployment ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


Changing optimization level

By default, Zig modules are compiled at the optimization level "Debug". You should change it to "ReleaseSmall" the before your application is deployed. Actually, you should do this as soon as the Zig code is ready and so you can better gauge your code's performance in a production environment.

"ReleaseSmall" is generally the setting that offers the best overall performance, as smaller code means better cache efficiency.

Creating a build script

If your development environment does not match the eventual server environment, you need to create a separate script that compiles the module for that environment. To maximize deployment flexibility, you might choose to build for multiple CPU architectures:

<?php

$targets = [
    [ 'platform' => 'linux', 'arch' => 'x64' ],
    [ 'platform' => 'linux', 'arch' => 'arm64' ],
    [ 'platform' => 'win32', 'arch' => 'x64' ],
];
foreach ($targets as $options) {
    zigar_compile(__DIR__ . '/../zig/hello.zig', $options);
}

Note: possible values for platform and arch are listed in the documentation for Node.js's os.platform() and os.arch().

Installing php-zigar on the server

First, you need to compile the extension for the server environment. Open build.ini in the directory containing the extension's source code and uncomment the desired compilation targets:

; versions[] = 8.1
; versions[] = 8.2
; versions[] = 8.3
; versions[] = 8.4
versions[] = 8.5

; debug = on
debug = off

targets[] = x86_64-linux-gnu
targets[] = aarch64-linux-gnu
; targets[] = x86_64-macos
; targets[] = aarch64-macos
targets[] = x86_64-windows
; targets[] = x86_64-windows-ts

; optimize = Debug
; optimize = ReleaseSafe
optimize = ReleaseSmall
; optimize = ReleaseFast

Then run php build.php.

If you're using Linux or MacOS, you make the changes using the extension build script's menu screen. Activate it by running php build.php menu. When you're done, select Build and press [ENTER].

You'll find the binaries for the extension in the sub-directory extensions. Upload the matching version to the server and move it to the directory where PHP extensions are stored. You can learn where that is by running php -r "echo ini_get('extension_dir');".

Once the file is in place, open php.ini and activate the extension:

extension=php_zigar

And add a section for Zigar:

[zigar]
zigar.recompile = Off

Code adjustments

In a situation where you're distributing the PHP application unaccompanied by the Zig source code, you might want to change the path given to zigar_use() from that of the Zig source file to that of the module:

<?php

$m = zigar_use(__DIR__ . './lib/hello.zigar');

Doing so prevents potential confusion due to your code referencing a non-existing file. It also removes dependency on the module_rel_path setting in php.ini.