How to Install PHP for Apache on Ubuntu 20.04 Server

How To Install PHP for Apache on Ubuntu 20.04 Server

Last updated on

In this guide we will install PHP and configure it to work with the Apache web server on Ubuntu Server 20.04.

Prerequisites

You should use a non-root user account as explained in the Ubuntu 20.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 20.04 Server with Virtual Hosts.

1. Install PHP

Let’s begin by updating the package lists and installing PHP on Ubuntu 20.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 and libapache2-mod-php, required for PHP to run under Apache.

sudo apt update && sudo apt install php libapache2-mod-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 php-mysql

sudo apt install 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.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), 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 20.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/mytest1.com/public_html where mytest1.com 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)

/var/www/html/info.ph
<?php
phpinfo();

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

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

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

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

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

PHP on Ubuntu 20.04

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.

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (3 votes)

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

Leave a Reply

avatar
  Subscribe  
Notify of