Install and enable PHP for Apache on Ubuntu 18.04 LTS - jbilander/HowTos GitHub Wiki

Install and enable PHP for Apache on Ubuntu 18.04 LTS

In this guide I presume that you have Apache server up and running, otherwise take a look here first:

How to install Apache and configure virtualhosts with http and https on Ubuntu 18.04 LTS

Having Apache installed and running, let's install PHP 7.2 on Ubuntu 18.04 LTS...

jbilander@zeus:~$ sudo -s
[sudo] password for jbilander:
root@zeus:~#

root@zeus:~# apt update

Install PHP 7.2 with apache module and support for MySQL:

root@zeus:~# apt install php libapache2-mod-php php-mysql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php7.2 libsodium23 php-common php7.2 php7.2-cli php7.2-common php7.2-json php7.2-mysql php7.2-opcache
  php7.2-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php libapache2-mod-php7.2 libsodium23 php php-common php-mysql php7.2 php7.2-cli php7.2-common php7.2-json
  php7.2-mysql php7.2-opcache php7.2-readline
0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,118 kB of archives.
After this operation, 18.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Checking version:

root@zeus:~# php -v
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
root@zeus:~#

Install PHP 7.2 modules

These are the most common PHP 7.2 modules often used in php applications. Install the ones you plan to use in your applications:

  • php-pear
  • php-fpm
  • php-dev
  • php-zip
  • php-curl
  • php-xmlrpc
  • php-gd
  • php-mbstring
  • php-xml

I will not install any of these yet, but if you need any of them just install with apt install, e.g.

$ sudo apt install php-gd php-mbstring php-xml

Now, let's enable the php module for apache, check the name in mods-available:

root@zeus:~# ls -al /etc/apache2/mods-available/php*
-rw-r--r-- 1 root root 855 Sep 13 15:45 /etc/apache2/mods-available/php7.2.conf
-rw-r--r-- 1 root root 102 Sep 13 15:45 /etc/apache2/mods-available/php7.2.load
root@zeus:~#

Enable the module php7.2:

root@zeus:~# a2enmod php7.2
Considering dependency mpm_prefork for php7.2:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.2:
Module php7.2 already enabled
root@zeus:~#

Now create a info.php file in our site directory /var/www/example

root@zeus:~# vi /var/www/example/info.php

Put these lines to the file:

<?php
phpinfo();
?>

and save and exit.

Now point your browser to http://www.example.com/info.php

Nice!, php works fine with apache2. On this page you can easily find out how your php environment is configured and what's currently installed and supported. However, we don't want this page to be enabled all the time so let's turn it off by renaming the info.php file:

root@zeus:~# mv /var/www/example/info.php /var/www/example/info.php.off

Now if you need to access this page later on just enable it temporarily by renaming the file back to info.php.

That's all folks!