71

I just upgraded from 14.10 to 16.04 and am not sure how to configure PHP 7 in apache. I did modify the php7.0.conf file to uncomment that last lines, restarted apache2 and no change.

Do I need to change the apache2 setup to allow php?

PHP works from the command line so I am sure the php is properly installed.

Additionally, I get an error on phpadmin saying that the mbstring is missing.

4
  • 1
    Working CLI SAPI (the php command) is orthogonal to the other PHP SAPIs (apache2, FPM, CGI, ...), so working command line PHP doesn't say anything about working PHP in the HTTP server.
    – oerdnj
    Apr 24, 2016 at 20:10
  • 2
    For missing mbstring do: sudo apt-get install php-mbstring (who would have thought, right?)
    – oerdnj
    Apr 24, 2016 at 20:11
  • What does "php7 not working in browser" mean? Are you talking about Apache not starting? PHP is not a client side language. Jun 5, 2016 at 17:09
  • It is really depressing seeing all these answers directing users to enable mpm-prefork and mod-php. This is a huge step backwards. Jun 6, 2016 at 20:59

7 Answers 7

82

Just like before, you have to install PHP for Apache.

From the Server Guide:

sudo apt install php libapache2-mod-php

That command will install PHP and the Apache 2 PHP module. Simply configure Apache as before (sudo a2enmod php7.0, I think, will turn on the module).


Your mbstring issue is similar - you need to install that extension:

sudo apt install php7.0-mbstring

For other missing modules there will be similar things to have to install as well.

5
  • The php module should be enabled by default upon installation. If not, it has to be the full sudo a2enmod php7.0 (or so I determined by disabling and re-enabling on my computer.) interesting, I got this message: Considering conflict php5 for php7.0: Enabling module php7.0. Apr 22, 2016 at 23:51
  • @DougSmythies I assume you didn't read where I said you have to enable the module...
    – Thomas Ward
    Apr 23, 2016 at 0:56
  • 1
    Yes, I did, which is why I made the comment. sudo a2enmod php will not work, I tried it. However sudo a2enmod php7.0 will work. Apr 23, 2016 at 3:43
  • 1
    This should be marked as the correct answer. Ubuntu 16.04 fresh install does not include this apache2 module by default.
    – Selosindis
    Apr 27, 2016 at 20:21
  • @Selosindis Absolutely agreed! Still annoying though, that with Ubuntu 16.04 the standard installation process changed in comparison to older versions.
    – Socrates
    Jun 14, 2016 at 21:55
41

To configure php7 to run with your server you need to do some configuration:

1. Make sure you remove any traces of php/php5

Open a terminal Ctrl+Alt+T and:

cd /etc/apache2/mods-enabled
ls -la

The output should not contain any php5.conf or php5.load, but if it does, do the following:

# this is the proper way of disabling modules
sudo a2dismod php5

# run this only if the above command didn't remove the php5 sym-links
sudo rm php5.load
sudo rm php5.conf

Now add the php7.0.conf and php7.0.load instead:

# this is the proper way of enabling modules
sudo a2enmod php7.0

# run this only if the above command didn't create the php7.0 sym-links
sudo ln -s php7.0.conf ../mods-available/php7.0.conf
sudo ln -s php7.0.load ../mods-available/php7.0.load

The output of ls -la php* should look like this:

lrwxrwxrwx 1 root root 29 Apr 15 03:55 php7.0.conf -> ../mods-available/php7.0.conf
lrwxrwxrwx 1 root root 29 Apr 15 03:55 php7.0.load -> ../mods-available/php7.0.load

After dealing with the modules we now come to the /etc/apache2/conf-enabled directory. Remove any traces of php/php5 here as well by sudo rm <name>

Then, if needed do:

# the proper way of enabling configs
sudo a2enconf php7.0-cgi
sudo a2enconf php7.0-fpm

# do those commands only if the above didn't work out
sudo ln -s php7.0-cgi.conf ../conf-available/php7.0-cgi.conf
sudo ln -s php7.0-fpm.conf ../conf-available/php7.0-fpm.conf

The output of ls -la php* should look like this:

lrwxrwxrwx 1 root root 33 Apr 21 17:00 php7.0-cgi.conf -> ../conf-available/php7.0-cgi.conf
lrwxrwxrwx 1 root root 33 Apr 21 17:01 php7.0-fpm.conf -> ../conf-available/php7.0-fpm.conf

2. Restarting Apache2

Before restarting Apache make sure to clean out the Apache error.log then restart:

sudo su
> /var/log/apache2/error.log
exit
sudo service apache2 restart

Now check the error.log by issuing cat /var/log/apache2/error.log | less (piping through less enables you to easy scroll up and down, q exits the output).

If your error.log contains many (and I literally mean a heap of) some MIBS not found do the following:

sudo apt install libsnmp-dev
sudo net-snmp-config --snmpconfpath
sudo apt-get install snmp snmp-mibs-downloader
sudo su
> /var/log/apache2/error.log
exit
sudo service apache2 restart

The check again the error.log it now should only contain 3 lines:

[Sat Apr 23 01:39:07.504005 2016] [mpm_prefork:notice] [pid 1647] AH00169: caught SIGTERM, shutting down
[Sat Apr 23 01:39:08.685774 2016] [mpm_prefork:notice] [pid 9590] AH00163: Apache/2.4.18 (Ubuntu) mod_perl/2.0.9 Perl/v5.22.1 configured -- resuming normal operations
[Sat Apr 23 01:39:08.685938 2016] [core:notice] [pid 9590] AH00094: Command line: '/usr/sbin/apache2'

Your Apache with php7.0 should now be properly configured.

10
  • 2
    Use the a2enmod and a2dismod commands instead of manually tinkering with links in mods-available/mods-enabled (similarly, a2enconf and a2disconf for conf-{available,enabled}).
    – muru
    Apr 23, 2016 at 1:12
  • 1
    Revised my answer according to your input.
    – Videonauth
    Apr 23, 2016 at 1:46
  • The fiddling with links is harmful as the a2enmod and a2dismod remember the state of the modules in internal apache2 package registry, so making/removing the links manually might make your system broken for future upgrades as the upgrade scripts looks at the state of this internal database.
    – oerdnj
    Apr 24, 2016 at 20:13
  • @oerdnj: making an omelet may break eggs... ;-)
    – Fabby
    Apr 24, 2016 at 20:22
  • 1
    @David Wright - Perhaps you are trying to run PHP script from user directory - these are disabled by default. If so you need to open php7.0.conf and comment out the last part. More here devplant.net/2010/05/04/…
    – d586
    Aug 3, 2016 at 14:04
14

I got this problem as well.

It helped me to delete all PHP and reinstall php7.

All that was needed was:

sudo apt-get purge php*

php7 will be deleted, afterwards just to reinstall:

sudo apt-get install php7.0-mysql php7.0-curl php7.0-json php7.0-cgi  php7.0 libapache2-mod-php7.0
4
  • 1
    I have tried several thing before and this method work for me. Thank you!!! Feb 12, 2017 at 1:07
  • 1
    OMG. You saved me.
    – plhn
    Mar 30, 2017 at 9:25
  • this is the only solution that worked for me. cheers!
    – geevee
    Mar 11, 2018 at 8:24
  • This helped me, cause after Ubuntu upgrade I didnt found php7.0-cgi in conf-avaliable. BUT keep in mind that doing sudo apt-get purge php* will remove your phpMyAdmin also, if you have one. Mar 27, 2019 at 21:06
8

Im my case not only the php wasn't working but phpmyadmin aswell i did step by step like that

sudo apt install php libapache2-mod-php
sudo apt install php7.0-mbstring
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
service apache2 restart

And then to:

gksu gedit /etc/apache2/apache2.conf

In the last line I do add Include /etc/phpmyadmin/apache.conf

That make a deal with all problems

8

The solution I found for this problem was the default PHP.INI setting for short_open_tag. By default this is set to 'Off'. My PHP code started with

   <?

instead of

   <?php

If enabled the short_open_tag (by changing 'Off' to 'On' in my php.ini) and reset the apache server (sudo service apache2 restart) and PHP popped back alive.

1
  • 1
    OMMFG! Of course it would be the very last comment with upvotes that finally worked for me! Thank you so mf much, @patrick!!! Mar 24, 2017 at 14:47
0

https://wiki.archlinux.org/index.php/Apache_HTTP_Server

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
service apache2 restart

Note: The above is required, because libphp7.so included with php-apache does not work with mod_mpm_event, but will only work mod_mpm_prefork instead. (FS#39218)

Otherwise you will get the following error:

Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe.  You need to recompile PHP.
AH00013: Pre-configuration failed
httpd.service: control process exited, code=exited status=1

As an alternative, you can use mod_proxy_fcgi (see Using php-fpm and mod_proxy_fcgi below).

0

I, too, could run php scripts successfully from the command line on Ubuntu 16.04, but not in any browser. In an attempt to solve the problem I purged my existing copies of both php7.0 and apache2. Thereafter I reinstalled both from the command line including as well as apache2's php library. Frustration followed when I attempted to run "a2enmod php7.0," which informed me that the php7.0 module didn't exist despite the fact that once again I was able to run php scripts from the command line. The instinct kicked in. Opening up Synaptic, I discovered that when I installed the apache2 php library from the command line, I installed only libapache2-mod-php7.0, and had forgotten also to install the "wrapper," libapache2-mod-php, without which the former will not function.

1
  • 1
    What is the name of the "wrapper"?
    – EKons
    Sep 2, 2016 at 17:49

You must log in to answer this question.

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