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
  • 78,878

7 Answers7

83

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
  • 78,878
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,815
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
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
  • 5,013
Maciej
  • 81
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
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
  • 36,890
  • 56
  • 97
  • 151
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.