PHP Installation on Ubuntu

What is PHP?

PHP is a very popular web or mobile programming language used widely to create mobile and web applications. If you want to design a beautiful website you need to first learn about a programming language.

A programming language converts developers language into computer language. Once computer understands the language it will display it on a command prompt or a web browser.

Using php you can create dynamic web pages and can also send and receive cookies. It can do a lot more things we will learn them in PHP course.

Where to use PHP?

There are three main areas where you can use php:

  • Server Side Scripting - mainly used as a backend langauge for web development
  • Command Line Scripting - To write command line scripts or cron jobs
  • Desktop Applications - It is not a best language for desktop programming but does a job

Where can it be installed?

You can use php on any popular operating systems like:

  • Mac OS
  • Windows OS
  • Linux OS

How to Install PHP on Ubuntu?

PHP can be installed on any linux based operating system like Ubuntu. In this tutorial we will learn how to install php, php5 and php7 on ubuntu operating system.

Install PHP < 5.0 on Ubuntu

To install php version < 5.0 you need to follow the steps below on your ubuntu terminal window. Open your terminal window and follow the steps below:

sudo apt-get update 
sudo apt-get install python-software-properties 
sudo apt install php 
sudo apt install libapache2-mod-php php-cli php-mysql​

Install PHP >= 5.0 on Ubuntu

To install php version >= 5.0 on ubuntu you need to run following commands on your ubuntu terminal window:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install python-software-properties
sudo apt-get install php5.6
sudo apt-get install libapache2-mod-php php5.6-mysql 

Install PHP7 on Ubuntu

To upgrade php5 to php7 or to install php7 on ubuntu machine you need to follow the steps below:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install python-software-properties
sudo apt-get install php7.0
sudo apt-get install php7.0-mysql
sudo apt-get install php7.0-fpm​

This way you can install php on ubuntu machine. Please remember this installation only contains php however in some cases you might want to install php extensions.

How to switch between php versions?

To switch from one php version to another on ubuntu you need to follow the commands below keeping in mind that you should have both versions of php available before you make switch:

# switch from php5.6 to php 7.0
sudo a2dismod php5.6
sudo a2enmod php7.0 
sudo service apache2 restart

# switch from php7.0 to php 5.6
sudo a2dismod php7.0
sudo a2enmod php5.6 
sudo service apache2 restart​

What are php extensions?

PHP extensions are special compiled libraries for php language which enable specific functions to be used along with php.

When you install php you do not have all the functionality available to you. Depends on your web development process you need to enable some features of php.

To install extensions on top of your php installation on ubuntu you need to follow the steps below:

# enable cli feature for different php versions
sudo apt-get install php-cli
sudo apt-get install php5.6-cli
sudo apt-get install php7.0-cli

# enable xml feature for differnet php versions
sudo apt-get install php-xml
sudo apt-get install php5.6-xml
sudo apt-get install php7.0-xml

# enable php mbstring
sudo apt-get install php-mbstring
sudo apt-get install php5.6-mbstring
sudo apt-get install php7.0-mbstring

# enable json extension
sudo apt-get install php-json
sudo apt-get install php5.6-json
sudo apt-get install php7.0-json

# enable pdo extension
sudo apt-get install php-pdo
sudo apt-get install php5.6-pdo
sudo apt-get install php7.0-pdo

# install imagick
sudo apt-get install php-imagick​