Skip to main content
 —  James Oakley
Lighttpd and remi-php

I recently wrote a post explaining how to set up lighttpd (a light-weight, simple web server) on a RHEL-derived OS (such as AlmaLinux or Rocky Linux), so it can use SSL, on multiple virtual hosts (i.e., individual domains), and to use PHP over fpm.

In that post, I said this:

If you want to install a specific version of PHP, rather than the LTS version provided by the package manager for the life of your AlmaLinux (etc.) version, that can be done, but it’s beyond the scope of this blog post. That’s a PHP question, not a lighttpd configuration question. (Hint: Either compile from source, or use the remi repositories)

And then I thought: “That would be useful to do. Oh, go on then!” I sometimes put my own documentation notes on here, so they can help me and others later. So having solved how to do this, I thought I'd put my notes here. If you read the previous post and treated this as an exercise for the reader, here’s the solution;

Install the versions of PHP you want

First, enable the REMI PHP repository:

dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm

(Obviously, if you're on RHEL 8 or 10, you’d change the 9)

Then you install the version of PHP you want, with the capabilities and extensions you want. Change 83 to 84 or 85 (or whatever version you want). Add or remove from the list of extensions, and PECL extensions, as you wish. Here's a sampler for PHP 8.3.

dnf install php83 php83-php-{fpm,bcmath,cli,gd,gmp,intl,ldap,mysqlnd,opcache,process,soap,xml} php83-php-pecl-{imagick,zip}

You can then repeat this for other versions of PHP:

dnf install php85 php85-php-{fpm,bcmath,cli,gd,gmp,intl,ldap,mysqlnd,opcache,process,soap,xml} php85-php-pecl-{imagick,zip}

If you want to specify a version of PHP to use over CLI (at the command prompt) you can install the package php83-syspaths (replacing 83 with the version you want). You’d need to uninstall the regular system installation of php to get this to install.

If you’re just trying to get more modern PHP to work on a website, you don’t need this step. You can still call your custom version of PHP from the command line without installing this extra package; you just need to call the binary for the version you want, such as /opt/remi/php83/root/bin/php.

Set up php-fpm for the newly installed version

As long as you included php-fpm in the packages you just installed, each newly installed version of PHP comes with its own php-fpm daemon. We need to configure this, as we configured the standard system php-fpm package in the previous post. I suggested configuring php-fpm to use a tcp port rather than a unix socket. The standard port for php-fpm (9000) will already be in use, so we’ll need to assign a different port number.

The configuration file for the php-fpm services for the newly installed versions of PHP are in files with names like /etc/opt/remi/php83/php-fpm.d/www.conf. I’ll be brief here, so if you want more details please consult the previous post. In short, change the user and group to lighttpd, and the listen line to listen to localhost port 9003 rather than a socket (the default) or port 9000 (as per the previous post instructions).

At this point you can start the php-fpm process: systemctl start php83-php-fpm.

If you’re system is selinux enforcing, you will hit the snag that port 9003 is not allocated to http use (see semanage port -l | grep http_port_t). So you need to allocate it: semanage port -a -t http_port_t -p tcp 9003. Now try starting php83-php-fpm again, and it should now start if it didn’t before.

You can run a php-fpm service for each version you’ve installed. I suggested port 9003 because you could then repeat the instructions above for php85-php-fpm, and use port 9005 instead. Numbering the ports after the PHP versions will make it easier to remember what you’ve just done.

Tell lighttpd to use your new version of PHP

The last step is to tell the fastcgi part of lighttpd to use your newly running php-fpm, not the one you installed before. This is really easy.

If you want a new version of PHP for your whole webserver (or a new default version), edit /etc/lighttpd/conf.d/fastcgi.conf and change the port number from 9000 to 9003 (or whichever port you chose for the version you want).

If you want a specific virtual host (website domain) to use a different version of PHP, go into its configuration file (such as /etc/lighttpd/vhosts.d/subdomain.example.com.conf). Inside the braces that filter the configuration for a specific HTTP host, set the fastcgi.server lines you used in fastcgi.conf, but with the port number you want. You need to set the whole fastcgi.server attribute, with host and port; you can’t just set the port to a different value. So it might look like this:

$HTTP["host"] == "subdomain.example.com" {
  fastcgi.server = ( ".php" => ((
    "host" => "127.0.0.1",
    "port" => "9003"
  )))
}

Restart lighttpd, and you should have the PHP versions where you want them.

Add new comment
The content of this field is kept private and will not be shown publicly.