Apache2 with PHP 7.1 Support on Ubuntu 18.04 LTS Server

I have been testing Ubuntu 18.04 LTS recently. It is installed on my Windows 10 machine via VMware Workstation 14 Pro… There are lots of fun things in store and upcoming. Here’s how to install Apache2 with PHP support on Ubuntu 18.04 LTS.

Oh, Ubuntu 18.04 LTS also comes with the latest version of PHP… which is 7.1

If you’re going to be building any PHP based application, you’re mostly going to need PHP and related modules installed on your systems. PHP is an open source, server scripting language use for creating dynamic web applications and websites.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.

For those wanting to test Apache2 with PHP support on Ubuntu 18.04 LTS, the steps below show you how to do that. Majority of the popular content management systems like WordPress, Joomla, Drupal use it, so PHP is a big deal!

When you’re ready to set up Apache2 environment with PHP 7.1 support, follow the steps below:

Step 1: Install Apache2 Web Server

sudo apt install apache2

After installing Apache2, the commands below can be used to stop, start, reload and enable Apache2 service to always startup when the server boots up.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl reload apache2.service
sudo systemctl enable apache2.service

Step 2: Install PHP and Related Modules

PHP 7.1 may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1

sudo apt update

Finally, run the commands below to install PHP 7.1 and related modules..

sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-mcrypt php7.1-cli php7.1-xml php7.1-zip php7.1-mysql php7.1-gd php7.1-imagick php7.1-recode php7.1-tidy php7.1-xmlrpc

The line above will allow PHP to function with many popular PHP based websites and applications.

Step 3: Configure Apache2 PHP Settings

Now that Apache2 and PHP are installed, you may want to configure Apache2 to use PHP properly. The default Apache2 PHP configuration file for PHP  is located at /etc/php/7.1/apache2/php.ini

Open PHP Apache2 configuration file by running the commands below

sudo nano /etc/php/7.1/apache2/php.ini

Then edit the file to suit your environments. Scroll down the file line by line and modify the lines for the settings below… always configure an appropriate setting for your environment.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

Next, lookup Apache2 dir.conf file and confirm the highlighted line below… If you see index.php defined, then you’re good.

<IfModule mod_dir.c>
     DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

If you don’t see the index.php definition on the line, please add it and save the file.

Restart Apache2 and PHP 7.1 services

sudo systemctl restart apache2.service

Step 4: Test PHP Setup

At this point, Apache2 and PHP should be installed and ready.. to test your Apache2 PHP settings, create a blank file with the line below:

sudo nano /var/www/html/phpinfo.php

Then add the line in the file and save.

<?php phpinfo( ); ?>

Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php

You should see something similar to the image below… if you do, then you’re all good!

lighttpd php ubuntu

That’s it!

You may also like the post below:

Nginx HTTP Server on Ubuntu 18.04 LTS {Beta} Server

11 Replies to “Apache2 with PHP 7.1 Support on Ubuntu 18.04 LTS Server

  1. mcrypt was deprecated in PHP 7.1 and dropped in PHP 7.2
    Alternatives include:
    – Sodium
    – OpenSSL
    I am testing OpenSSL just now, so I do not know whether there are caveats.

    1. Kinda a noob here. I followed the steps changing php 7.1 to 7.2 and got this:
      $ sudo apt-get install php7.2 libapache2-mod-php7.2 php7.2-mcrypt php7.2-cli php7.2-xml php7.2-zip php7.2-mysql php7.2-gd php7.2-imagick php7.2-recode php7.2-tidy php7.2-xmlrpc
      (…returns)
      Reading package lists… Done
      Building dependency tree
      Reading state information… Done
      Note, selecting ‘php-imagick’ instead of ‘php7.2-imagick’
      E: Unable to locate package php7.2-mcrypt
      E: Couldn’t find any package by glob ‘php7.2-mcrypt’
      E: Couldn’t find any package by regex ‘php7.2-mcrypt’
      And, http://192.168.1.21 to the server in my browser simply returns the content of the file .

      Is this the mcrypt problem? Any suggestions to troubleshoot and get this working certainly appreciated.

    2. you can:
      – sudo apt install php-bz2 php-dba php-imap php-intl php-ldap libmcrypt-dev
      – sudo pecl install mcrypt-1.0.1
      and put the name of the mod you like:
      php-mcript
      and use as:
      extension=php-mcrypt.so

  2. Ubuntu server 18
    sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-mcrypt php7.1-cli php7.1-xml php7.1-zip php7.1-mysql php7.1-gd php7.1-imagick php7.1-recode php7.1-tidy php7.1-xmlrpc
    gives
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Note, selecting ‘php-imagick’ instead of ‘php7.1-imagick’
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    php7.1-mcrypt : Depends: libmcrypt4 but it is not installable
    php7.1-zip : Depends: libzip4 (>= 1.0) but it is not installable
    E: Unable to correct problems, you have held broken packages.

    1. you can:
      – sudo apt install php-bz2 php-dba php-imap php-intl php-ldap libmcrypt-dev
      – sudo pecl install mcrypt-1.0.1
      and put the name of the mod you like:
      php-mcript
      and use as:
      extension=php-mcrypt.so

  3. I tried all the above (had the same messages) and can’t seem to get anything to work with php file.
    my terminal says: sudo: pecl: command not found
    when i tried to install? Not sure why
    so the actual code on the info.php says:
    <?php
    phpinfo();

    Been working for over a day to get localhost/ or http://127.0.0.1/ to host a wordpress site and it will not render the php it just shows code. Any help would be appreciated. I am using 7.2 and ubuntu 18.04 When I go to my wordpress version it just shows code as well: <?php
    /**
    * Front to the WordPress application. This file doesn't do anything, but loads
    * wp-blog-header.php which does and tells WordPress to load the theme.
    *
    * @package WordPress
    */

    /**
    * Tells WordPress to load the WordPress theme and output it.
    *
    * @var bool
    */
    define('WP_USE_THEMES', true);

    /** Loads the WordPress Environment and Template */
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );

    Sigh

    1. run:

      sudo updatedb
      locate dir.conf

      /etc/apache2/mods-available/dir.conf
      /etc/apache2/mods-enabled/dir.conf

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.