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.

pLumo
  • 26,947
  • What is the purpose? Installing PHP without Apache or installing PHP with something other than Apache? – Yaron Jul 23 '19 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 '19 at 14:29
  • @Yaron Sorry for not understanding the comment, we just want the same benefit windows users have. – ilias iliadis Jul 23 '19 at 14:30
  • 2
    @pLumo No. It installs Apache also. – ilias iliadis Jul 23 '19 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 '19 at 14:32
  • @Thomas Ward ...If there is an IIS Web Server. The link to php.net I gave, says nothing about an extra web server required to run on the machine. – ilias iliadis Jul 23 '19 at 14:39
  • @iliasiliadis this doesn't answer my question which is "Are you intending to use PHP without a web server or use PHP as a web backend?" This question will determine the *PROPER* answer. as to what you have to default install. For example, php-cli will give you the php command on command line to run PHP scripts, but php-fpm or libapache2-mod-php7.2 (the most commonly installed PHP package in Ubuntu for Apache PHP support, hence why just doing apt install php installs Apache and that package) will give you a PHP web backend system. We need more details to properly answer you. – Thomas Ward Jul 23 '19 at 14:40
  • As the question already says: We just want to utilize the simple built-in web server. Sorry, but cannot make that more clear. – ilias iliadis Jul 23 '19 at 14:44
  • @iliasiliadis, what about my answer ?! – pLumo Jul 23 '19 at 14:47
  • There is no inbulit webserver to PHP, unless you've written a lib/wrapper around it. For that, you'd need php-cli and then launch PHP via a similar command like in the answer below. By default, though, PHP has no Web Server set up for it - this is why you need Apache or nginx+php-fpm to get to a web backend for PHP. – Thomas Ward Jul 23 '19 at 14:58

3 Answers3

27

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
pLumo
  • 26,947
  • 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. – ilias iliadis Jul 28 '19 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).

Greenonline
  • 2,081
  • “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 '21 at 13:37
  • This should be the accepted answer. – dev_willis Jun 23 '22 at 15:15
1
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
nobody
  • 5,437
  • 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 '19 at 19:43
  • you a right in bionic is it this way. I was in xenial. – nobody Jul 23 '19 at 20:00