Hướng dẫn how to enable php in apache2 ubuntu - cách bật php trong apache2 ubuntu

Hướng dẫn how to enable php in apache2 ubuntu - cách bật php trong apache2 ubuntu

Last updated on May 24th, 2020

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.

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.

/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.

Hướng dẫn how to enable php in apache2 ubuntu - cách bật php trong apache2 ubuntu

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 if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

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

Leave a reply

Your email address will not be published.