Nginx and PHP

Nginx and PHP

  • mdo  DigitalBox
  •   Dev
  •   February 10, 2025

A few weeks ago, we have reviewed how to use a lighttpd server with PHP under Haiku.

Another alternative is to use the Nginx web server.

Let's review how to configure it.

Pre-requisites

You will need two applications before starting :

  • Nginx installed
  • PHP installed

In a terminal, type the below commands to install both Nginx and PHP : 

pkgman install nginx
pkgman install php8

We are now ready to start:)

Configuration

Before launching the Nginx server, we need to indicate where the web server files will be located.

For that, let's a create the "www" directory :

mkdir /boot/home/www

Then edit the "nginx.conf" config file : 

lpe /system/settings/nginx/nginx.conf &

With the below lines we indicate that :

  • the "/boot/home/www" directory contains the pages for the website
  • "index.php" page is one of the main pages to read
location / {
            root   /boot/home/www;
            index  index.php index.nginx.html index.html index.htm;
}


In order to use fastcgi, paste the below lines : 

location ~ \.php$ {
        include        fastcgi_params;
        root           /boot/home/www;
        fastcgi_pass   unix:/tmp/php-cgi.sock;
#fastcgi_pass unix:/tmp/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }

Save your changes.

Finally, let's create the below script to use the "php-cgi" command :

lpe /boot/home/config/non-packaged/bin/start-php-fcgi.sh

In this file put the line :

/bin/php-cgi -b /tmp/php-cgi.sock

Then make the script executable :

chmod +x /boot/home/config/non-packaged/bin/start-php-fcgi.sh

Please note, the other possibility instead of using "php-cgi" is the "php-fpm" command.

Haiku is not multi-users, however when installing nginx the group/user "nginx" are created.

By default, there's a check in "php-fpm" preventing to launch this command with the root user:

So let's specify in the "php-fpm.conf" to use "nginx" user to launch the daemon.

lpe /system/settings/php8/php-fpm.conf &

Paste the below content :

[global]
pid = /system/var/run/php-fpm.pid

[www]
listen = /tmp/php-fpm.sock
user = nginx
listen.owner = nginx
listen.group = nginx
listen.mode = 0666

pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 5

Ok, let's review how to launch the nginx server with PHP.

Server launch

Before running the Nginx server, let's finalize a few things.

Copy the default index.html page for our server :

cp /boot/system/var/www/nginx/index.nginx.html /boot/home/www/index.html

Now, launch WebPositive and check that the server is running fine :

Everything is in order :)

Now, first option, launch the "php-cgi" command : 

/boot/home/config/non-packaged/bin/start-php-fcgi.sh

Edit a simple php page : 

lpe /boot/home/www/info.php

And paste the below content : 

<?php
phpinfo();
?>

Save the file.

Then type in WebPositive : 

http://localhost/info.php

Excellent !

Now PHP is recognized under the Nginx web server via php-cgi.

Second option, launch the "php-fpm" daemon :

php-fpm -D

In the nginx "nginx.conf" setup, replace the "fastcgi_pass" by the php-fpm version :

location ~ \.php$ {
        include        fastcgi_params;
        root           /boot/home/www;
        #fastcgi_pass   unix:/tmp/php-cgi.sock;
fastcgi_pass unix:/tmp/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }

Stop/start the server :

kill -9
nginx

Then type in WebPositive : 

http://localhost/info.php

Wonderful !

Now PHP is recognized under the Nginx web server via php-fpm.

This setup can be useful when using Haiku as a development platform with PHP.

Last point : as nginx server will use the user/group "nginx", it's better to change all the files/directories ownership to this user :

chown -R nginx:nginx *

Any feedback you would like to share ? 

Let put a comment below.


Powered by Bludit - Hosted by Planet Hoster
© 2025 Haiku Insider