Report this

What is the reason for this report?

How to setup Apache2 with an PHP7 installation?

Posted on April 19, 2015

I followed a small guide on how to install PHP7 (http://www.zimuel.it/install-php-7/). The installation went successful but could someone give some advice on how to use PHP in combination with Apache (Setting up servers isn’t my thing haha)? I couldn’t find anything about how to setup PHP7 with Apache2 which makes sense since the official release is still a few months away. But I would like to do some testing with it :)



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi Mike,

All DigitalOcean servers managed by ServerPilot have PHP 7 available by default. There’s nothing you need to customize, it’s part of the standard server configuration which uses Apache behind Nginx.

A quick search found this guide which should assist you in getting PHP 7 working with Apache on Ubuntu.

To set up Apache2 with PHP 7.x or PHP 8.x on a Linux-based system, you’ll need to follow a series of steps. Below are instructions for both PHP 7.x and PHP 8.x installations.

Installing Apache2 and PHP 7.x:

  1. Update Package Lists: Open a terminal and run the following command to update your package lists:
sudo apt update
  1. Install Apache2: Install the Apache2 web server with the following command:
sudo apt install apache2
  1. Install PHP 7.x: Install PHP 7.x and its commonly used extensions:

    For PHP 7.4:

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php7.4-curl php7.4-json php7.4-cgi php7.4-xsl
Replace `7.4` with the desired PHP 7.x version (e.g., 7.3, 7.2) if needed.
  1. Enable PHP Module for Apache: Enable the PHP module for Apache with the following command:
sudo a2enmod php7.4
  1. Restart Apache: Restart Apache to apply the changes:
sudo systemctl restart apache2
  1. Test PHP: Create a PHP test file to ensure PHP is working correctly. Use a text editor to create a file named info.php in your web server’s document root directory (usually /var/www/html/) with the following content:
<?php phpinfo(); ?>
Save the file and open it in your web browser by navigating to `http://your-server-ip/info.php` (replace `your-server-ip` with your server's IP address). You should see a page displaying PHP information.

Installing Apache2 and PHP 8.x:

  1. Update Package Lists: Open a terminal and run the following command to update your package lists:
sudo apt update
  1. Install Apache2: Install the Apache2 web server with the following command:
sudo apt install apache2
  1. Install PHP 8.x: Install PHP 8.x and its commonly used extensions:

    For PHP 8.0:

sudo apt install php8.0 libapache2-mod-php8.0 php8.0-mysql php8.0-curl php8.0-json php8.0-cgi php8.0-xsl
Replace `8.0` with the desired PHP 8.x version (e.g., 8.1, 8.2) if needed.
  1. Enable PHP Module for Apache: Enable the PHP module for Apache with the following command:
sudo a2enmod php8.0
  1. Restart Apache: Restart Apache to apply the changes:
sudo systemctl restart apache2
  1. Test PHP: Create a PHP test file to ensure PHP is working correctly. Use a text editor to create a file named info.php in your web server’s document root directory (usually /var/www/html/) with the following content:
<?php phpinfo(); ?>
Save the file and open it in your web browser by navigating to `http://your-server-ip/info.php` (replace `your-server-ip` with your server's IP address). You should see a page displaying PHP information.

That’s it! You’ve successfully set up Apache2 with PHP 7.x or PHP 8.x on your Linux system. You can now start hosting PHP-based web applications on your server.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.