This post shows students and new users 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 few other open source web servers. However, the vast majority of PHP implementations are integrated with Apache or Nginx web servers. Because of that, this post will only show you how to use PHP with Apache or Nginx on Ubuntu Linux.
PHP is always constantly being updated, so don’t be surprised that the versions installed here isn’t the latest. As of this writing, the latest PHP version is PHP 8.0.
If you’re a student or new user learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.
To get started with 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 few others. If you’re using Apache web server, then the commands below is used to install PHP.
sudo apt update sudo apt install php libapache2-mod-php
After installing PHP, restart Apache web server for PHP modules to apply. PHP is tightly integrated with Apache. Anything you make changes to PHP and you want it to apply, simply restart or reload Apache.
To do that, run the commends below.
sudo systemctl restart apache2
How to install PHP on Ubuntu Linux with Nginx support
If you’re running Nginx web server, then the commands below will install PHP with support for Nginx.
Nginx doesn’t have built-in support for processing PHP files and is not tightly integrated as Apache. To add PHP support for Nginx, you will need to install and use PHP-FPM (fastCGI process manager) to handle the PHP files.
To do that, run the commands below.
sudo apt update sudo apt install php-fpm
Because PHP isn’t tightly integrated with Nginx, if you make changes to PHP, you must restart or reload PHP and Nginx separately for the changes to apply.
sudo systemctl restart php-fpm sudo systemctl restart nginx
Also, to allow Nginx to read PHP files, you must add these lines to Nginx server block. Remember to use the version of PHP installed and reference it in the highlighted line show 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 enhance PHP performance and functionality.
Below are some common PHP extensions and modules one can install.
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 will not see that version in Ubuntu repositories at the moment. The current latest in Ubuntu repositories is 7.4.
To install the latest of other versions of PHP that are not available in Ubuntu repository, run the commands below to install a third party PPA repository which includes multiple versions of PHP.
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php
After adding the repository above, you can then install other PHP version.
sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql
That should do it!
Conclusion:
This post showed you how to install PHP on Ubuntu Linux with support for Apache or Nginx web servers. If you find any error above or have something to add, please use the comment form below.