Pratik Devkota
Pratik Devkota
I am a software engineering student interested in UNIX (GNU/Linux, *BSD, etc.), security as well as free and open-source software.

Serving PHP Files in Nginx

- Advertisement -

This post is about Serving PHP Files in Nginx


Nginx has been one of the most widely deployed web servers today. However, at its core it is merely just a reverse proxy. It can only serve static pages, and all of the dynamic content needs to be handled by other processes. In this article, we’re going to look into setting up our web server so that we can also serve PHP web pages.
Whenever nginx receives a request for a PHP file, it should pass it to another process. In the case of PHP, it is commonly the php-fpm program. We can do this using a number of different protocols. However, the most commonly used one turns out to be FastCGI. FastCGI is a binary protocol created for interfacing dynamic applications with a web server. It improves upon the earlier Common Gateway Interface (CGI) protocol with better performance.

Serving PHP Files — Installing Packages

In my previous article, we have already gone through the process of installing nginx. So now we only need to install the following additional programs. I’m using OpenBSD hence the use of pkg_add.

doas pkg_add -z php

Choose the PHP version and that’s all we need. Note that the phpXX_fpm service (where XX is the version) will automatically be installed on OpenBSD; we might have to install it separately on other systems.

Now enable (or just start) the php-fpm service. In my case, I installed the latest version of PHP (8.1).

doas rcctl enable php81_fpm
doas rcctl start php81_fpm

Let’s keep note of where the socket is created.

Socket location of PHP-FPM process

So it’s at /var/www/run/php-fpm.sock which means it will probably be running in a chrooted environment. One thing we’ll have to note is that the php-fpmprocess will be running as the www user while our nginx processes will be running as nobody, so it won’t be able to access the files of the www user.

We can configure nginx to run under the www user by adding a user directive in the main context. Then whenever nginx encounters a request for a PHP file, we’ll need to have it passed to the php-fpm socket. Before doing that we’ll have to include a few configurations. Fortunately, it is already included with our installation in the fastcgi.conf file located at the directory where nginx.conf also lives.
We’ll simply have to include that file.

user www;

events {}

http {

    include mime.types;
    server {
        listen 80;
        server_name openbsd.local;
        root /var/www/html/;
        # try to serve index.php under root, index.html otherwise
        index index.php index.html;
        location / {
            # try the given URI as is, otherwise try it with a slash.
            # If that failes, then return a 404 error
            try_files $uri $uri/ =404;
        # regex match
        # Match any request with a .php at the end
        location ~\.php$ {
            # include configuration file for FastCGI
            include fastcgi.conf;
            # now proxy the request to the socket
            fastcgi_pass unix:/var/www/run/php-fpm.sock;

With this configuration, save the file, check the syntax with nginx -t and
reload the service with doas nginx -s reload.

Now we can try to load a simple PHP script. I have the following under/var/www/html/.

Sample PHP file

Let’s try to access our web server at a local domain openbsd.local.


And it works. So that’s all I have for today’s article. Hope you learned something. Thank you for reading.

- Advertisement -


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook