Use two versions of PHP on Ubuntu 20?
Can you have two versions of PHP on Ubuntu 20.04 LTS?
I currently have PHP 7.4. WordPress now recommends PHP 8.x. But Piwigo.org (picture gallery) recommends we stay with PHP 7.4.
Can I install PHP 8 and keep 7? If so, how do I specify a certain PHP (probably in the .htaccess file?)
Also, is there a simple way to list all the PHP modules I currently have installed via some apt or dpkg command?
Thanks.
30 Replies
Can I install PHP 8 and keep 7? If so, how do I specify a certain PHP (probably in the .htaccess file?)
You should be able to do this -- esp if you use php-fpm…
You would have 2 copies of php-fpm running -- one for 7.4 and one for 8.0 (obviously, each would be started with a corresponding unique configuration and set of loadable modules).
If you use a global apache2 configuration for php-fpm (in /etc/apache2/conf-available), you would have to split that into two -- one for 7.4 and one for 8.0 -- and disable the global one.
You would put the 7.4 php-fpm configuration directives into the <VirtualHost> for Piwigo.org and the 8.0 php-fpm configuration into the <VirtualHost> for WP.
Before doing all that, I would try running your picture gallery on 8.0 and see what happens… Unless your picture gallery uses some really obtuse features or relies on something equally obtuse that was deprecated in 8.0, my guess is that it will probably work -- the differences are pretty minor. This will save a lot of (what may be left of) your hair…
Also, is there a simple way to list all the PHP modules I currently have installed via some apt or dpkg command?
apt list --installed | grep php
-- sw
I did a lot of research on this and decided to simply upgrade to PHP 8.0 as I am sure all my apps will run with the new PHP version.
Let me ask this. My sites are small and get very little traffic. One is the EspoCRM database that is only used by me. Is there any advantage to running FPM vs. the traditional 'mod' install?
PHP 7.4 is reaching EOL in a seven months. See:
Let me ask this. My sites are small and get very little traffic. One is the EspoCRM database that is only used by me. Is there any advantage to running FPM vs. the traditional 'mod' install?
The reasons for using php-fpm is to future-proof your installation and thread-safety. mod_php is not thread-safe so you can only use it with mpm_prefork. Since mpm_prefork uses multiple processes to handle requests, it requires the most system resources. mpm_worker & mpm_event use multiple threads (and only a few processes).
As far as future-proofing goes, mod_php’s days are numbered.
— sw
PS. php_fpm is multithreaded too…and gives you the ability to assign different PHP requests to different thread pools based on <VirtualHost> configuration or whatever criteria you choose (as codified in the web server config). php_fpm works with nginx too…not so with mod_php. If you use nginx, php_fpm is the only game in town (besides the uber-slow CGI).
PPS. You can’t do what I suggested with mod_php…since apache will only load one mod_php at a time. Imagine what your performance would be like if you had to unload mod_php, load a different mod_php & reload apache for every single request!
I don't believe I am running mpm_prefork. How do I know if it is loaded? As I remember when I installed the LAMP stack on Linode mpm_prefork was an option and I don't think I installed it.
Can I add php-fpm to my current PHP 7.4 install? I believe I can and have seen some sites that instruct how. I think it is just a download and a couple of Apache "disable/enable/stop/start' commands using systemctl.
Do you really think mod_php is close to EOL?
I DID see how you can assign different version of PHP to different domains via php-fpm via adding some code the virtual-host file in Apache.
I don't believe I am running mpm_prefork. How do I know if it is loaded? As I remember when I installed the LAMP stack on Linode mpm_prefork was an option and I don't think I installed it.
Do this:
stevewi $ apache2ctl -V # beware of PATH here...
Server version: Apache/2.4.51
Server built: unknown
Server's Module Magic Number: 20120211:118
Server loaded: APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: event <<--- This is it -- prefork, worker or event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
<server compiler options omitted>
Can I add php-fpm to my current PHP 7.4 install? I believe I can and have seen some sites that instruct how. I think it is just a download and a couple of Apache "disable/enable/stop/start' commands using systemctl.
Yes…it's possible to run php-fpm with an mpm_prefork configuration that uses mod_php. However, which method you use to interpret php pages, has to be configured at least as low as the <VirtualHost> level. If you enable, php-fpm at the server level (using /etc/apache2/conf-available/php-fpm.conf), you will be using php-fpm for everything. If you enable php-fpm, you also need to enable mod_proxy & mod_proxy_fcgi.
Do you really think mod_php is close to EOL?
Yes. It's not part of apache2…it's part of php.
One of these days (sooner rather than later, IMHO), some new feature of the Zend Engine (the thing that actually interprets php), is going to break it forever and it'll be cast aside faster than a politician involved in a sex scandal.
mod_php is not thread safe and forces you to stick with mpm_prefork (multi process, no threads), which is the slowest possible configuration.
mod_php is a relic of the 1980s…when OSs and processors didn't support threads. It also will never support any evolving http protocol improvements.
You probably have a multi-processor Linode…why not let your web server take advantage of that fact? The canned apache2 config for using php-fpm (at the server level), /etc/apache2/conf-available/php-fpm.conf, has been part of Ubuntu since at least 12.04 (for php5)…so it's not like this is a new thing…
Again, for nginx and Open LiteSpeed users, php-fpm and good ol' CGI are the only games in town to run php sites.
-- sw
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2022-03-16T16:52:53
Server's Module Magic Number: 20120211:88
Server loaded: APR 1.6.5, APR-UTIL 1.6.1
Compiled using: APR 1.6.5, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
I guess I have it!
OK, I will install PHP 8.0 and php-fpm on my small test server. Do you have a favorite 'how to' site you would point me to? There are a zillion of them out there.
Do I install both php8.1 AND the fpm module? It is confusing the more you Google and the more you read.
I did read that fpm is not as secure as mod. Comment?
I guess I have it!
You can also tell by doing ps -ef | grep apache and counting the number of apache2 processes you have. With mpm_prefork it'll be a lot. With mpm_event/mpm_worker, the number will be small (2 or 3).
OK, I will install PHP 8.0 and php-fpm on my small test server. Do you have a favorite 'how to' site you would point me to? There are a zillion of them out there.
This should be relevant:
https://askubuntu.com/questions/1319861/how-to-configure-apache-http-to-php-fpm-on-ubuntu-20-10
All you should have to do is (all with sudo):
a2dismod mpm_prefork
a2enmod mpm_event # or mpm_worker
# (tuning parameters are in
# /etc/apache2/mods-available/mod_{event,worker}.conf)
a2enmod proxy
a2enmod proxy_fcgi
a2dismod php8.0 # ...or whatever it's called
a2enconf php-fpm
systemctl start php8.0_fpm # ...or whatever it's called
systemctl restart apache2
and you should be in business. The configuration for php8.0-fpm is under /etc/php somewhere (I don't remember where).
I did read that fpm is not as secure as mod. Comment?
Only because it uses a socket to communicate with apache2… However, since it uses local-domain sockets, communication never crosses a system boundary. If you got some bad actor installed on your system (e.g. a rootkit or some such), it could access php-fpm's socket.
However, if that happened, you'd have much bigger problems than hacked web pages. IMHO, wherever you read that was giving you a hefty dose of FUD. It's no less secure than anything else that does the same thing, say, systemd…your Ubuntu installation won't run without that!
-- sw
Let me ask this. When PHP 7.4 goes EOL in November of this year what happens with Ubuntu 20.04 LTS (or any distro)? Will they put PHP 8.1 in their repository for people to install on their own or will they leave it on the 3rd party repository and not include PHP 8.1 until their next release of an LTS version? What does the history of EOL programs with Ubuntu (and distros) tell you?
If Ubuntu 20 LTS is going to include PHP 8.1 come November perhaps I should just wait seeing as 7.4 works just fine?
Let me ask this. When PHP 7.4 goes EOL in November of this year what happens with Ubuntu 20.04 LTS (or any distro)?
Canonical doesn't observe or sync with Zend’s release schedule for php. This is true for any distro out there. To do anything else would be suicide…
20.04 will keep 7.4 for the duration of it's support life. 22.04 will keep 8.0.
Canonical doesn't put the latest/greatest anything on past LTS versions (22.04 is/will be the current LTS version). The first chance you'll see something > 8.0 in an LTS version will be 24.04.
If Ubuntu 20 LTS is going to include PHP 8.1 come November perhaps I should just wait seeing as 7.4 works just fine?
If history is a guide, if you want 8.x on 20.04, you'll have to install it yourself using a PPA. Ditto for 7.4 or 8.1 on 22.04. The only choices you'll have are 7.4 on 20.04 and 8.0 on 22.04. Anything else will require a PPA.
7.4 works just fine
If you stick with 20.04, you won't have any choice…7.4 will be the Canonical-supported version. If you upgrade to 22.04, 8.0 will be the Canonical-supported version.
EOL !== stops working…it just means that Zend won't support it anymore. Since php is open-source, that means Canonical will have to do that for the duration of their LTS window(s).
However, you can hear Canonical's response to a catastrophic 7.4 bug now…"upgrade to 22.04 and use 8.0".
-- sw
What if I install PHP 8.0 with php-fpm and for some reason it does not work. How do I go back to 7.4 which has the 'mod' install? I Googled it and didn't find an answer… but there has to be a way.
I ran 8.0 on my MAMP install on my iMac and both Piwigo (pix gallery) and EspoCRM ran fine. I didn't run WordPress (too lazy to install it) but I'm told by qualified people that it runs with 8.0.
Next, I'll install it on my small Linode test server and then on my big production server.
What if I install PHP 8.0 with php-fpm and for some reason it does not work. How do I go back to 7.4 which has the 'mod' install? I Googled it and didn't find an answer… but there has to be a way.
- The hard way:
- Make sure you have a large bottle of whiskey on hand.
- Shut down your web server.
- Deinstall 8.0 & all it's artifacts.
- Install 7.4 & all it's artifacts.
- Configure.
- Start your web server.
- If it doesn't work, go to 5.
- Configure your apps.
- If it doesn't work, go to 7.
- Pray that you did everything correctly…or take a big pull off the bottle of whiskey and hold your breath.
- The easy way:
Take a snapshot of your 7.4 server. Install 8.0, configure and test. If it doesn't work, restore the snapshot.
-- sw
Hmmm… since 7.4 will still be resident after php-fpm is installed and enabled, wouldn't it be possible to simply disable it via
a2disconf
a2dismod
Or maybe just:
sudo update-alternatives --config php
see:
https://stackoverflow.com/questions/42619312/switch-php-versions-on-commandline-ubuntu-16-04
php-fpm is version specific too…on Ubuntu, there is php7.4-fpm and php8.0-fpm. This is because the Zend Engine (the thing that does the work of php language interpretation) is different for each.
Ditto for mod_php.
7.4 & 8.0 can coexist on the same machine but you’ll have to change the apache2 config for each & for mod_php vs php-fpm. This may help:
https://thishosting.rocks/install-php-on-ubuntu/
Frankly I’d just test your preferred configuration really well on your test server. If the tests pan out, move forward, don’t look back and don't second-guess yourself.
— sw
Hmmm. No simple way to backtrack from 8.0 FPM to 7.4 mod?
That is surprising.
I tested 8.0 with applications on local MAMP but it is not using FPM and I don't think there is a way to add it.
I'll have to try FPM on the small Linode with installs of both EspoCRM and Piwigo.
I don't know how to take a snapshot, so I'll research that. Is it same as using the Linode backup facility which cost $2.95 a month? I don't use it because I have my own backup scripts with stuff sent to a 3rd party company… rsync.net … 72 GB for $19/yr.
Hmmm. No simple way to backtrack from 8.0 FPM to 7.4 mod?
That is surprising.
Really? We're talking about Canonical here…a marketing company. Would you really expect that a marketing company would think something like this through? Of course not…
I tested 8.0 with applications on local MAMP but it is not using FPM and I don't think there is a way to add it.
Of course there is…php8.0-fpm is a supported part of php8.0. The problem is Ubuntu's goofy packaging… See:
https://www.linuxcapable.com/how-to-install-php-8-0-on-ubuntu-20-04-lts-focal-fossa/
Skip down to the section labeled Install Apache with PHP-FPM. This assumes you're using 20.04 and have already established access to the PPA that's referenced.
Is it same as using the Linode backup facility which cost $2.95 a month?
Yes, you get one snapshot that you can overwrite whenever you want (it's API scriptable too).
I don't use it because I have my own backup scripts with stuff sent to a 3rd party company… rsync.net … 72 GB for $19/yr.
OK, use that then…however it is you do that…
-- sw
Of course there is…php8.0-fpm is a supported part of php8.0.
You misunderstood. I tested with the free MAMP utility on my local iMac. MAMP comes bundled with 7.4 and 8.0. However it does not run FPM
https://www.mamp.info/en/mamp/mac/
However the paid MAMP PRO version does have FastCGI which as I understand is 'php-fpm' and since they had a free trial I ran all the apps with that and while I got some warnings in Piwigo (pix gallery) the first time, they didn't reoccur and I don't know why.
I don't know if there would be any difference between running LAMP (with php-fpm on macOS vs. Ubuntu but I will find out when I first install on my test server and then on my big production server.
I will take your suggestion and sign up for the Linode backup (snapshot) service and keep it just for a short time. If after you drop the service do your backups disappear?
Thanks for your help and assistance. You are a H U G E resource to us on this board.
You misunderstood. I tested with the free MAMP utility on my local iMac.
…
However the paid MAMP PRO version does have FastCGI which as I understand is 'php-fpm'
You can build this yourself using the same open-source components as on Linux using Homebrew. See: https://brew.sh
With a few perl hacks, you can port the Linux apache2 administration tree (/etc/apache2) and admin utilities (a2{en, dis}{mod, conf, site}) to the Mac for free.
I know this because I did it once… Unfortunately, I don't have it anymore…I replaced it with a much more elegant solution using a ruby program (a2m…replaces a2{en, dis}*) and a shell script (a2q…basically a friendlier front-end to apachectl) when I migrated off Linux to FreeBSD.
I don't know if there would be any difference between running LAMP (with php-fpm) on macOS vs. Ubuntu but I will find out when I first install on my test server and then on my big production server.
There shouldn't be… Darwin (the Mac OS kernel) has a FreeBSD API (the kernel architecture is very different than Linux/Unix but, to a programmer, it looks like Unix). I port things among the three systems all the time…esp shell scripts (well, only two now).
If after you drop the service do your backups disappear?
Yes.
One other thing…
I don't use it because I have my own backup scripts with stuff sent to a 3rd party company… rsync.net … 72 GB for $19/yr.
You really should check out borg or restic for this instead of maintaining your own stuff. rsync.net supports them both.
I have a 200-line shell script wrapping borg that runs as a cron job that backs up hourly (well…every 4 hours actually), daily, weekly and monthly…with exclusions and custom retentions for each. On my home FreeBSD system, the same script does the same thing to a 128GiB thumb drive (I rotate the thumb drive yearly on New Years' Day).
Thanks for your help and assistance. You are a H U G E resource to us on this board.
Thanks for the kudos. I try…
-- sw
Well, to wrap this up, I installed PHP 8.0 with php-fpm on my small test server and then on my big production server. I followed this site step by step:
https://itslinuxfoss.com/how-to-install-php-eight-on-ubuntu/
The only issue I use was a zillion PHP warnings running Piwigo (picture gallery) from my iPhone. After a bit of research I fixed it by creating a…. /local/config/config.inc.php file in my Piwigo install and adding this code to it:
<?php
$conf['show_php_errors'] = E_ALL & ~E_DEPRECATED & ~E_NOTICE &
~E_WARNING;
?>
I was thinking of going all the way up to PHP 8.1 but since it is so new I decided I can do that later on in the year.
Thanks for all the help.
<?php
$conf['show_php_errors'] = E_ALL & ~E_DEPRECATED & ~E_NOTICE &
~E_WARNING;
?>
I would probably keep E_WARNING enabled:
<?php
$conf['show_php_errors'] = E_ALL & ~E_DEPRECATED & ~E_NOTICE
?>
Do you get a lot of warnings? I would also apply DeMorgan's Law and change this to:
<?php
$conf['show_php_errors'] = E_ALL & ~(E_DEPRECATED | E_NOTICE)
?>
or in your case
<?php
$conf['show_php_errors'] = E_ALL & ~(E_DEPRECATED | E_NOTICE | E_WARNING)
?>
potato potahto
I was thinking of going all the way up to PHP 8.1 but since it is so new I decided I can do that later on in the year.
Chicken! ;-)
Glad you got it working!
-- sw
I notice a distinct speed increase in my sites, especially the picture gallery (https://answer123.com/piwigopix/) software which is a major 'hog.' Is php-fpm supposed to speed things up such that you actually notice it?
I thought php-fpm just made handling high traffic better. (I don't get high traffic!)
I installed this because you said this is the future of PHP. Plus, I'd have to update from 7.4 in Nov. when it goes EOL. (I have a year and six months left with 8.0). If there is a bump in speed, that is just a nice bonus.
I notice a distinct speed increase in my sites, especially the picture gallery (https://answer123.com/piwigopix/) software which is a major 'hog.' Is php-fpm supposed to speed things up such that you actually notice it?
Here are some observations:
php-fpm allows you to use mpm_event or mpm_worker which are much more efficient at handling system resources than mpm_prefork. Look at the number of apache2 processes you have -- 2 or 3 compared to double that with prefork.
With php-fpm you've offloaded the job of interpreting php pages from apache. php-fpm has its own pool of threads (and caching system) for doing that efficiently. With mod_php, the Zend interpreter lived inside apache. With php-fpm, it lives inside php-fpm. apache is free to handle other requests while it's waiting for php-fpm interpret a page pursuant to a particular request.
I'm assuming this picture gallery has a database of some sort (prob the same MySQL/MariaDB database your Wordpress site uses). With prefork/mod_php, every apache2 process needed a dedicated connection to the database (6 or 8 active connections at any one time). With php-fpm, there are maybe 2 connections that are shared in the php-fpm thread pool. Setting up and tearing down those database connections is a major performance hit that apache no longer takes.
php-fpm manages the cache of shared libraries required by php extensions.
So, that's four things (and probably counting) that apache no longer has to do…
I thought php-fpm just made handling high traffic better. (I don't get high traffic!)
low traffic + more efficient use of resources = better performance
-- sw
To the best of your knowledge are there any tweaks to 8.1 that will make it faster than 8.0? I saw that they added "fibers" and enums, neither of which I have need for.
I know it is a major pain to downgrade from 8.0 using FPM to 7.4 using mod. What about if I upgrade my test server to 8.1 FPM and stuff does not work. Is it the same pain to go back to 8.0 FPM.
(I noticed that when I added the new repository that it brought down ALL the current version of PHP… including 8.1 which I didn't ask for.)
To the best of your knowledge are there any tweaks to 8.1 that will make it faster than 8.0? I saw that they added "fibers" and enums, neither of which I have need for.
See: https://www.php.net/manual/en/migration81.php
I know it is a major pain to downgrade from 8.0 using FPM to 7.4 using mod. What about if I upgrade my test server to 8.1 FPM and stuff does not work. Is it the same pain to go back to 8.0 FPM.
Since you now have php-fpm, you only have to tell your web server to use the 8.0 version of php-fpm. Nothing about php lives in your web server (as it did with mod_php). Another nice thing about php-fpm…different versions of php can coexist more peaceably.
-- sw
Why does it say 8.1 when I'm running 8.0 (according to phpinfo()
$ php -v
PHP 8.1.5 (cli) (built: Apr 21 2022 10:14:45) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies
You have 8.1 CLI (/usr/bin/php) and 8.0 php-fpm (/usr/sbin/php-fpm).
Check to see if you have both /usr/bin/php81 and /usr/bin/php80 and you need to change your alternatives alias.
-- sw
I do have /usr/bin/php8.1 and 8.0 (and 7.4)
I do have (/usr/sbin/php-fpm8.0
I did the following
sudo update-alternatives --config php
There are 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php.default 100 auto mode
1 /usr/bin/php.default 100 manual mode
2 /usr/bin/php7.4 74 manual mode
3 /usr/bin/php8.0 80 manual mode
4 /usr/bin/php8.1 81 manual mode
Press <enter> to keep the current choice[*], or type selection number:
I chose #3
I got:
update-alternatives: using /usr/bin/php8.0 to provide /usr/bin/php (php) in manual mode
I then restarted Apache and tried the command again:
$php -v
PHP 8.0.18 (cli) (built: Apr 21 2022 10:14:55) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.18, Copyright (c) Zend Technologies
with Zend OPcache v8.0.18, Copyright (c), by Zend Technologies
Did I screw anything up?
What is the CLI anyway? Is it the command line PHP… if you were to run php commands at the terminal.
How did I end up with 8.1 CLI when I installed 8.0?
What is the CLI anyway?
The thing that does php language interpretation is the Zend Engine. It's packaged 3 ways:
- php-fpm (which we've discussed here ad nauseam);
- mod_php (which we've also discussed here ad nauseam); and
- php…the CLI program.
Is it the command line PHP… if you were to run php commands at the terminal.
Yes, like perl or ruby… composer is an example of a general-purpose program not involving web services that uses the php interpreter this way. composer is like apt except for pre-packaged php components (and the components are usually installed per project but can be installed system-wide).
The CLI version of php can also be used used with web servers as a CGI program.
How did I end up with 8.1 CLI when I installed 8.0?
You wrote previously:
(I noticed that when I added the new repository that it brought down ALL the current version of PHP… including 8.1 which I didn't ask for.)
…or you didn't add --dryrun when you should have.
-- sw
Everything is working great.
If I want to try out 8.1 on my small test server, do I just use the 'alternatives' command and restart Apache… after installing all of the 8.1 php modules that I need?
If I want to try out 8.1 on my small test server, do I just use the 'alternatives' command and restart Apache… after installing all of the 8.1 php modules that I need?
No. You need to shut down php8.0-fpm and start php8.1-fpm using systemctl…and maybe change the config in /etc/apache2/conf-enabled/php-fpm.conf to use the local-domain socket php8.1-fpm creates (you may not need to do this if php8.1-fpm and php8.0-fpm name their sockets the same…my suspicion is that this is the case -- unless you changed the configs for each of the php-fpms to do this). Then you restart apache.
'alternatives' has nothing to do with it…it only creates aliases for programs you use in the shell (e.g., /usr/bin/php8.0 vs /usr/bin/php8.1).
-- sw
P.S. If you have 7.4 and php7.4-fpm installed, you can do the same with 7.4 too. This was the gist of my original response to your question.
7.4 and php7.4-fpm installed, you can do the same with 7.4 too
I'll never go back to 7.4.
8.0 with FPM works just fine.
Since I have all my major apps (Wordpress, Espocrm, Piwigo) on my test server, why not try 8.1?
With 8.0 I get 6 months of active support and then 18 months of security support.
With 8.1 I get 18 months of active support and then 30 months of security support.
See: https://www.php.net/supported-versions.php
This looks like a good step-by-step site to follow:
https://www.linuxcapable.com/how-to-install-php-8-1-on-ubuntu-22-04-lts/