This post shows students and new users the steps to install and use PHP server-side programming languages ​​on Ubuntu Linux with Apache or Nginx to support content management systems (CMS) written in PHP. Popular CMS frameworks like WordPress, Magento, Drupal, and many others are written in PHP.

PHP supports and works with many types of web servers. You can use PHP with Apache, Nginx, and some other open source web servers. However, the vast majority of PHP implementations are integrated with Apache or Nginx web servers. So this post will only show you how to use PHP with Apache or Nginx on Ubuntu Linux.

PHP is always constantly updated, so don’t be surprised that the versions installed here are not the latest. At the time of this writing, the latest version of PHP is PHP 8.0.

If you are a student or a new user learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.

To start installing PHP on Ubuntu Linux, follow the steps below.

How to install PHP on Ubuntu with Apache support

As mentioned above, PHP supports many types of web servers, including Apache, Nginx, and a few others. If you are using Apache web server, the following commands are used to install PHP.

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

After installing PHP, restart the Apache web server for the PHP modules to take effect. PHP is tightly integrated with Apache. Anything you make changes to PHP and want to have it applied, just restart or reload Apache.

To do that, run the compliments below.

sudo systemctl restart apache2

How to install PHP on Ubuntu Linux with Nginx support

If you are running the Nginx web server, the following commands will install PHP with Nginx support.

Nginx does not have built-in support for processing PHP files and is not tightly integrated like Apache. To add PHP support for Nginx, we will use PHP-FPM (fastCGI process manager) to handle PHP files.

To do that, run the following commands.

sudo apt update
sudo apt install php-fpm

Because PHP is not tightly integrated with Nginx, if you make changes to PHP, you must restart or reload PHP and Nginx separately for the changes to take effect.

sudo systemctl restart php-fpm
sudo systemctl restart nginx

Also, to allow Nginx to read PHP files, you must add these lines to the Nginx server block. Remember to use the installed PHP version and reference it in the highlighted line shown below.

server {


    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }
}

How to install PHP modules on Ubuntu Linux

You have installed PHP, but there are many other modules available to support and improve the performance and functionality of PHP.

Here are some common PHP extensions and modules that can be installed.

php-common php-cli php-gd php-curl php-mysql

How to install the latest version of PHP on Ubuntu Linux

As mentioned above, the current version of PHP is 8.0. However, you won’t see that version in the Ubuntu repositories. The latest in Ubuntu repositories is 7.4.

To install the latest of other versions of PHP that are not available in the Ubuntu repository, run the following commands to install a third-party PPA repository that includes multiple versions of PHP.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

After adding the above repository, you can install another version of PHP.

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql

You should do that!

Conclusion:

In this tutorial we have seen how to install PHP on Ubuntu Linux with support for Apache or Nginx web servers. If you find any errors above or have something to add, use the comment form below.

Write A Comment