PHP is a popular server-side scripting language used to build websites and other backend functionality. PHP is an important part of LAMP (Linux, Apache, MySQL, PHP) stacks as well as other AMP stacks.
This article assumes you are running a Debian or Ubuntu-based instance. Visit the following link to view current versions of PHP:
Installing PHP (default version)
PHP comes in different packages depending on what you specifically need to use it for. You can choose to install one or more of these packages to suite your needs, but the following command installs the most common and useful packages to get moving with PHP right away.
user@server:$ sudo apt-get install php libapache2-mod-php php-mysql php-curl
Install a specific version of PHP
The commands above install the default version of PHP configured with your version of Debian or Ubuntu. For example, running this command in Ubuntu 17 would install PHP 7.1. On Ubuntu 16, it would install PHP 7.0.
If you need a different version, specify that in the command. This shows how to specifically install PHP 7.0.
user@server:$ sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-mysql
How to install PHP 7.2?
Version 7.2 is the most recent version available and not available in earlier versions of Ubuntu's repositories. To install, you must first add a repository, run an update, then install version 7.2.
user@server:$ sudo apt-add-repository ppa:ondrej/php user@server:$ sudo apt-get update user@server:$ sudo apt-get install php7.2
You can view all currently installed PHP packages by running the following command:
user@server:$ dpkg -l | grep php
There are a few common packages that are normally installed with PHP. The most common are shown below.
This package provides the PHP module for the Apache 2 webserver. View the following link for further details:
If you ran the commands above, this was already installed. You can confirm by running the following command:
user@server:$ dpkg -l | grep apache2-mod-php ii libapache2-mod-php 1:7.2+60+ubuntu17.10.1+deb.sury.org+1 all server-side, HTML-embedded scripting language (Apache 2 module) (default)
This package provides the /usr/bin/php command interpreter which is used to test PHP scripts from a shell or perform general shell scripting tasks. This package is only for command line tools and is not usable for websites running on Apache. php-cli is installed when you install a PHP version. If not already installed, you can install is using the following command:
user@server:$ sudo apt-get install -y php-cli
This will install the default PHP binary in the /usr/bin/ directory. You can check if this packaged is installed by running the following:
user@server:$ dpkg -l | grep php-cli
View the following site for further details:
If you have installed 'php-cli', you can view all PHP modules currently installed by running the following command:
user@server:$ php -m
Common modules are listed below, but you can view additional modules for PHP at the following links.
PHP databases modules/extensions
MySQL is one of the most common databases used with PHP, however there are additional packages available for other database engines.
This package contains the mysql, mysqli and PDO mysql modules for PHP using the MySQL Native Driver which is newer and generally faster. This module is installed with php-mysql. You can check if it's installed by running the following:
user@server:$ php -m | grep mysqlnd
View the following links for further details:
This package contains the pgsql and pdo_pgsql modules for use with PostgreSQL.
This package contains the sqlite3 and pdo_sqlite modules for working with SQLite.
Optional PHP Modules
These modules add additional functionality to PHP.
The cURL module allows downloading from HTTP, FTP and other sources. You could install the default PHP version using php-curl or you could specify a specific version of PHP.
user@server:$ sudo apt-get install php7.1-curl
php-json is installed when you install a PHP version. It allows PHP to interact with JSON data.
Run the following to install (if not already installed).
user@server:$ sudo apt-get install php-json
Multimedia librarieslibav and ffmpeg and some of the most popular multimedia libraries for PHP. View the following article for an overview of the differences:
Libav was forked from FFmpeg in 2011. It is a program for handling multimedia data. View the following links for further details:
You can install it by running the following command:
user@server:$ sudo apt-get install libav-tools
The ffmpeg libraries and gd will be installed with this package. These allow retrieving meta-data from video and images, as well as some video frame editing.
You can install it by running the following command:
user@server:$ sudo apt-get install ffmpeg
The GD library provides image manipulation features which can be useful for some applications.
Imagick is a native PHP extension to create and modify images.
There are two configuration files for PHP that are used for the CLI and the Apache modules specifically. Change <version> to your specific version of PHP.
The primary configuration file for PHP when executed from the CLI. It has no memory_limit at all and allows all functions (including POSIX PHP functions).
The primary configuration file for PHP when ran from the Apache module. It has a memory_limit of 128MB and disallows all functions related to POSIX PHP functions for security and safety reasons.