Installing PHP for Apache on Ubuntu 18.04 Server

Installing PHP for Apache on Ubuntu 18.04 Server

In this guide we will install PHP 7 and configure it to work with the Apache web server on Ubuntu Server 18.04 (Bionic Beaver). The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic and interactive Web pages. As of 2018, PHP was used as the server-side programming language on 85% of 10 million most popular websites!

Video Tutorial


You should use a non-root user account as explained in the Ubuntu 18.04 Initial Server Setup.

You should also have Apache already installed and be able to access a test web page in your browser as explained in our guide Installing Apache on Ubuntu 18.04 Server with Virtual Hosts.

1. Install PHP

Let’s begin by updating the package lists and installing PHP on Ubuntu 18.04. Below we have two commands separated by &&. The first command will update the package lists to ensure you get the latest version and dependencies for PHP. The second command will then download and install PHP. Press y and ENTER when asked to continue.

sudo apt update && sudo apt install php

Press y and ENTER when prompted to install the PHP package.

If you want to use PHP in conjunction with MySQL, you should install libapache2-mod-php and php-mysql

sudo apt update && sudo apt install libapache2-mod-php php-mysql

2. Test PHP

Once the package has finished installing, we can test PHP in the command line.

php -version

If PHP installed correctly, you should see something similar below:

PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2018 22:03:58) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.3-1ubuntu1, Copyright (c) 1999-2018, by Zend Technologies

Great! Now, let’s test PHP for Apache.

Create a new file called info.php in your document root directory.

The default document root in Ubuntu 18.04 is /var/www/html/, or if you followed our previous Apache guide on setting up Virtual Hosts, your document root may be in somewhere like  /var/www/ where is the name of your own domain.

Once you’ve confirmed the location of you document root directory, create a new file called info.php using the nano text editor.

In this example, we will create a new file in /var/www/html/

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

Once nano editor has opened, paste in the following PHP code. (If using PuTTY, click to right mouse button to paste)


Save file and exit. (Press CTRL + X, press Y and then press ENTER)

We can now load this file in the browser by going to or http://your_ip/info.php

Tip: If you don’t know you IP, you can find out with:

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v ''

Below we can see the PHP info page is working correctly.

PHP 7 info test page on Apache and Ubuntu 18.04 Bionic Beaver

Once you’ve confirmed PHP is working correctly, it’s important to delete info.php as it contains information that could be useful to hackers.

sudo rm /var/www/html/info.php

3. Configure PHP (Optional)

If you plan on uploading files larger than 2MBs through WordPress or similar, you will need to alter the PHP config file and set the max upload size.

What Next?

Now that you have PHP up and running, you can begin setting up a MySQL database server and phpMyAdmin.

Let me know in the comments if this helped. Follow me @DevAnswers or buy me a beer 🍺


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

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces