16

It seems that the only way to do this is to install together an Apache server which is very-very unwanted (in our case).

We just want to utilize the simple built-in web server, as the lucky users of some non-ubuntu OSes (like MS Windows 10) have.

10
  • What is the purpose? Installing PHP without Apache or installing PHP with something other than Apache?
    – Yaron
    Jul 23, 2019 at 14:28
  • How comes you think that you can install php only together with Apache. Just sudo apt install php will do.
    – pLumo
    Jul 23, 2019 at 14:29
  • @Yaron Sorry for not understanding the comment, we just want the same benefit windows users have. Jul 23, 2019 at 14:30
  • 2
    @pLumo No. It installs Apache also. Jul 23, 2019 at 14:32
  • 1
    @iliasiliadis Windows and Ubuntu are different entities and operate differently. When you install PHP in Windows, it also configures IIS Web Server to deploy the PHP typically. Are you intending to use straight PHP without any web server, or PHP as a web backend?
    – Thomas Ward
    Jul 23, 2019 at 14:32

3 Answers 3

26

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
1
  • Tried this. Seems that cgi is not enough (still installs apache). So I tried both php-fpm and php-cgi which appears to do the trick. Jul 28, 2019 at 14:57
11

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).

2
  • “Yes, it's obnoxious that the "php" package assumes Apache.” Yes it is – but then again, Apache was THE one and only webserver for PHP scripts for more than 20 years now! Let's call it “for historic reasons”…
    – feeela
    Dec 17, 2021 at 13:37
  • This should be the accepted answer.
    – dev_willis
    Jun 23, 2022 at 15:15
0
sudo apt-get --no-install-recommends --dry-run install php 

First simulate. I'm not in ubuntu now. If you are happy you can run

sudo apt-get --no-install-recommends install php
2
  • This won't work as installing php will install a web server to go with it as it defaults to the Apache module first. OP would have to install either the php-cgi package or php-cli package first before trying to install php - however if they install php-cgi or php-cli they'll get all the PHP common dependencies anyways which does not include a web server.
    – Thomas Ward
    Jul 23, 2019 at 19:43
  • you a right in bionic is it this way. I was in xenial.
    – nobody
    Jul 23, 2019 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .