How to Install PHP on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install PHP on Ubuntu 20.04 | 16.04.
PHP is a popular scripting language suited for web development. Many of the popular content management systems like WordPress, Joomla and Drupal are PHP based platform.
If you’re going to be developing PHP based applications, you’ll need it installed on the systems you’re developing on.
For students and new users, this post will come in handy if they want to learn how to install it on Ubuntu, with either Apache or Nginx HTTP servers.
PHP is usually combined with a web server and/or database server to be usable. Many times, you’ll see PHP being used with Apache or Nginx web servers.
Using PHP with Apache
Below is how one uses PHP with Apache web server on Ubuntu.
Install the commands below to use PHP with Apache web server.
sudo apt update sudo apt install php libapache2-mod-php
The commands above will install the latest stable version of PHP along with Apache on Ubuntu. After installing, you can restart Apache using the commands below:
sudo systemctl restart apache2
Using PHP with Nginx
For most people, PHP with Nginx is a better suite then Apache. For those, they can run the commands below to use PHP with Nginx.
Unlike Apache2, Nginx doesn’t have built-in support for processing PHP files, so they both need to be installed and configured separately.
To install Nginx with PHP, run the commands below:
sudo apt update sudo apt install nginx php-fpm
The commands above will install the latest stable version of PHP-FPM along with Nginx web server on Ubuntu.
At the time of this writing, the latest PHP version on Ubuntu 20.04 is php7.4-fpm. On Ubuntu 18.04, it’s php7.2-fpm.
After installing the packages above, restart Nginx and PHP-FPM.
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
To validate PHP status, run the commands below:
sudo systemctl status php7.4-fpm
You should get something similar to the lines below:
php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr> Active: active (running) since Sat 2020-05-02 14:48:13 CDT; 11s ago Docs: man:php-fpm7.4(8) Process: 10501 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru> Main PID: 10480 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req> Tasks: 3 (limit: 4657) Memory: 6.9M CGroup: /system.slice/php7.4-fpm.service ├─10480 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─10499 php-fpm: pool www └─10500 php-fpm: pool www
Because Nginx doesn’t have built-in support for PHP, you’ll need to manually edit the default site server block to enable PHP support.
Open the default server block on Ubuntu by running the commands below:
sudo nano /etc/nginx/sites-available/default
Then edit the lines below to enable PHP support.
# pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one
Save the file and exit.
Then reload Nginx and PHP.
sudo systemctl reload nginx sudo systemctl reload php7.4-fpm
Testing PHP Processing
Now that you know how to install PHP with Apache and Nginx processing by running the commands below.
First, create a test PHP file called info.php in the web server default home directory.
sudo nano /var/www/html/info.php
Then copy and paste the line into the file and save it.
<?php
phpinfo();
Save the file and exit.
After that, open your browser and browse to the server hostname or IP address followed by the name of the file you created above.
http://localhost/info.php
That should display a PHP configuration settings are shown below:

That’s it!
Conclusion:
This post showed you how to install PHP with Apache and Nginx support on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report it.
Thanks,
You may also like the post below: