How to install php without Apache webserver?

Solution 1:

Ubuntu package details says php (php7.2) depends on libapache2-mod-php7.2 OR php7.2-fpm OR php7.2-cgi.

It seems to default to the first package, which itself depends on apache2. But if you install one of the latter first, and php afterwards, apache2 will not be installed.

sudo apt install php-cgi
sudo apt install php

or manually put the resolved dependency:

sudo apt install php php7.2-cgi

Then you can run

php -S localhost:8000

Solution 2:

For the benefit of anyone still looking up this question: To install PHP without Apache (either to use on its own or with nginx) you have to install FPM directly rather than installing just "php". So just use this:

apt install php-fpm php-cli

(Yes, it's obnoxious that the "php" package assumes Apache.)

And be careful installing some of the PHP module packages; they sometimes try to install Apache as well. (Astonishing that after all these years Ubuntu's PHP packages still try to force the use of Apache mod-php instead of using php-fpm!)

If Apache does get installed against your will, you can purge it with the following (yes, oddly, you have to purge both of these packages):

apt purge apache2 apache2-bin

Also, after installing PHP, always be sure to set the date.timezone option in both /etc/php/7.4/fpm/php.ini and /etc/php/7.4/cli/php.ini (adjust according to the version you've installed).