diff --git a/NEWS b/NEWS index 44fb719c6982..45206cd0226f 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,8 @@ PHP NEWS - FPM: . Use recommended TYPE, HELP order for OpenMetrics metadata. (Marcel Hernandez) + . php-fpm now includes the CLI SAPI when it is built, and runs as the CLI + when invoked as php (e.g. through a php symlink). (Matthieu Napoli) - Hash: . Fixed mhash_keygen_s2k() truncating a $length above INT_MAX instead of diff --git a/UPGRADING b/UPGRADING index 46cf4cafab12..75181d6eb1b7 100644 --- a/UPGRADING +++ b/UPGRADING @@ -52,6 +52,16 @@ PHP 8.7 UPGRADE NOTES 3. Changes in SAPI modules ======================================== +- FPM: + . When the CLI SAPI is built, it is also linked into php-fpm. php-fpm then + runs as the CLI when invoked under the name "php", optionally followed by + a digit (e.g. php, php8.7 or php87), typically through a symlink. Under + any other name, php-fpm behaves as before. + To ship a single executable for both SAPIs, install the php-fpm binary as + php and make php-fpm a symlink to it: PHP_BINARY is resolved to the real + file, so scripts that run PHP_BINARY (e.g. to restart themselves) keep + running the CLI. + ======================================== 4. Deprecated Functionality ======================================== diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 2f6ea7d9ea84..0b9465891bd8 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -57,6 +57,8 @@ PHP 8.7 INTERNALS UPGRADE NOTES - Unix build system changes: . Autoconf minimum required version upgraded to 2.71. + . When the CLI SAPI is built, PHP_FPM_OBJS includes PHP_CLI_SHARED_OBJS and + PHP_FPM_WITH_CLI is defined, so that php-fpm can run do_php_cli(). ======================== 3. Module changes diff --git a/sapi/fpm/config.m4 b/sapi/fpm/config.m4 index 89c53a0c4d28..d6ea21ea807b 100644 --- a/sapi/fpm/config.m4 +++ b/sapi/fpm/config.m4 @@ -476,6 +476,14 @@ if test "$PHP_FPM" != "no"; then [$PHP_FPM_FILES $PHP_FPM_TRACE_FILES $PHP_FPM_SD_FILES], [-I$abs_srcdir/sapi/fpm -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1]) + dnl Link the CLI objects into php-fpm for do_php_cli(), so that php-fpm runs as + dnl the CLI when invoked under a "php" name (e.g. a php symlink to php-fpm). + AS_VAR_IF([PHP_CLI], [no],, [ + AC_DEFINE([PHP_FPM_WITH_CLI], [1], + [Define to 1 if the CLI SAPI is linked into the FPM binary.]) + PHP_FPM_OBJS="$PHP_FPM_OBJS $PHP_CLI_SHARED_OBJS" + ]) + AS_CASE([$host_alias], [*aix*], [ BUILD_FPM="echo '\#! .' > php.sym && echo >>php.sym && nm -BCpg \`echo \$(PHP_GLOBAL_OBJS) \$(PHP_BINARY_OBJS) \$(PHP_FPM_OBJS) | sed 's/\([A-Za-z0-9_]*\)\.lo/\1.o/g'\` | \$(AWK) '{ if (((\$\$2 == \"T\") || (\$\$2 == \"D\") || (\$\$2 == \"B\")) && (substr(\$\$3,1,1) != \".\")) { print \$\$3 } }' | sort -u >> php.sym && \$(LIBTOOL) --tag=CC --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) -Wl,-brtl -Wl,-bE:php.sym \$(PHP_RPATHS) \$(PHP_GLOBAL_OBJS) \$(PHP_BINARY_OBJS) \$(PHP_FASTCGI_OBJS) \$(PHP_FPM_OBJS) \$(EXTRA_LIBS) \$(FPM_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_FPM_PATH)" diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index 3d35e942a06e..23ee912b1f21 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -78,6 +78,9 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS; #include #include "fpm.h" +#ifdef PHP_FPM_WITH_CLI +#include "sapi/cli/cli.h" +#endif #include "fpm_main_arginfo.h" #include "fpm_request.h" #include "fpm_status.h" @@ -1559,6 +1562,19 @@ int main(int argc, char *argv[]) bool old_rc_debug; #endif +#ifdef PHP_FPM_WITH_CLI + /* Invoked as "php", "php8.7", "php87"... (e.g. a php symlink to php-fpm): + * run the CLI SAPI instead. */ + if (argc > 0 && argv[0]) { + const char *name = strrchr(argv[0], '/'); + name = name ? name + 1 : argv[0]; + if (strncmp(name, "php", strlen("php")) == 0 + && (name[3] == '\0' || isdigit((unsigned char) name[3]))) { + return do_php_cli(argc, argv); + } + } +#endif + #if defined(SIGPIPE) && defined(SIG_IGN) signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so that sockets created via fsockopen() diff --git a/sapi/fpm/php-fpm.8.in b/sapi/fpm/php-fpm.8.in index 6a46c9419e67..4beffad2d209 100644 --- a/sapi/fpm/php-fpm.8.in +++ b/sapi/fpm/php-fpm.8.in @@ -11,6 +11,10 @@ php-fpm \- PHP FastCGI Process Manager 'PHP-FPM' Web development and can be embedded into HTML. This is a variant of PHP that will run in the background as a daemon, listening for CGI requests. Output is logged to @php_fpm_localstatedir@/log/php-fpm.log. .LP Most options are set in the configuration file. The configuration file is @php_fpm_sysconfdir@/php-fpm.conf. By default, php-fpm will respond to CGI requests listening on localhost http port 9000. Therefore php-fpm expects your webserver to forward all requests for '.php' files to port 9000 and you should edit your webserver configuration file appropriately. +.LP +When PHP was built with the CLI SAPI, php-fpm runs as +.BR php (1) +instead when invoked under the name php, optionally followed by a digit (e.g. php8.7), typically through a symbolic link. .SH OPTIONS .TP 15 .B \-C diff --git a/sapi/fpm/tests/main-cli.phpt b/sapi/fpm/tests/main-cli.phpt new file mode 100644 index 000000000000..55ba4d3108c8 --- /dev/null +++ b/sapi/fpm/tests/main-cli.phpt @@ -0,0 +1,41 @@ +--TEST-- +FPM: runs as the CLI when invoked under a php name +--SKIPIF-- + +--FILE-- +&1 | head -1")), "\n"; +} + +echo "php-fpm: ", trim(shell_exec(escapeshellarg($fpm) . " -n -r 'echo PHP_SAPI;' 2>&1 | head -1")), "\n"; + +?> +--CLEAN-- + +--EXPECTF-- +php: cli +php8: cli +php-cgi: Usage: php-cgi [%s +php-fpm: Usage: php-fpm [%s