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.

Thomas Ward
  • 72,494
  • 30
  • 173
  • 237
David Wright
  • 711
  • 1
  • 6
  • 3
  • 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 '16 at 20:10
  • 2
    For missing mbstring do: `sudo apt-get install php-mbstring` (who would have thought, right?) – oerdnj Apr 24 '16 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. – Andrea Lazzarotto Jun 05 '16 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. – Andrew Ensley Jun 06 '16 at 20:59

7 Answers7

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.

Thomas Ward
  • 72,494
  • 30
  • 173
  • 237
  • 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.` – Doug Smythies Apr 22 '16 at 23:51
  • @DougSmythies I assume you didn't read where I said you have to enable the module... – Thomas Ward Apr 23 '16 at 00: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. – Doug Smythies Apr 23 '16 at 03: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 '16 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 '16 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.

Videonauth
  • 33,045
  • 16
  • 104
  • 120
  • 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 '16 at 01:12
  • 1
    Revised my answer according to your input. – Videonauth Apr 23 '16 at 01: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 '16 at 20:13
  • @oerdnj: making an omelet may break eggs... **;-)** – Fabby Apr 24 '16 at 20:22
  • I really appreciate your response and I have performed all of the above and everything looks as you have suggested, however apache2 is still not allowing me to run php scripts. – David Wright May 03 '16 at 04:58
  • EXCELLENT! I had literally a 'bastard of 3 days hell'; first mySql-server etc gave up, so my Drupal projects wouldn't work, some 3 working days later, I had to re-install, upgrade 15.10 to 16.04, then no localhost/mySql connections. I worked through your above steps, and got apache talking to my mySql DBs again. Thanks very much for taking the time for sharing it with us, just what askubuntu.com should be all about. Good karma to you. – Paul B Jun 15 '16 at 16:08
  • 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 http://devplant.net/2010/05/04/linux-php-not-working-in-userdir-public_html/ – d586 Aug 03 '16 at 14:04
  • Doing just: sudo a2enmod php7.0 - did the trick for me – Petar Vasilev Oct 26 '16 at 11:02
  • Thank you very much. This line: `sudo apt install libsnmp-dev` was missing element in my case. I was searching this a lot of time. You are awesome! – Daniel Dec 19 '16 at 07:44
  • First class!! I tried many other solutions all over the net, with a fresh install of Ubuntu 16 and Viritual min. This worked! Thank you! – petergus May 04 '17 at 07:02
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
patrick
  • 225
  • 2
  • 7
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

storm
  • 4,943
  • 6
  • 35
  • 48
Maciej
  • 81
  • 1
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.

patrick
  • 225
  • 2
  • 7
  • 1
    OMMFG! Of course it would be the very last comment with upvotes that finally worked for me! Thank you so mf much, @patrick!!! – hyp3rg3om3tric Mar 24 '17 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).

David Foerster
  • 35,754
  • 55
  • 92
  • 145
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.