How to Install PHP (5.6,7.1,7.2,7.3) on Amazon EC2 Ubuntu Server

May 12, 2020 | By Gerald | Filed in: AWS.
How to Install PHP on Ubuntu Server

In this tutorial, you will learn how to install PHP and modules on Amazon EC2 Ubuntu server.

PHP is an open-source and most popular programming language commonly used for web development (such as static websites, dynamic websites and web applications) and also deployed on most web servers on almost every operating system and platform, free of charge. PHP (stands for Hypertext Preprocessor).

If you are also planning to install a WordPress platform and Mysql server for your site, this guide will help you through the step process installing PHP.

What will you do

  1. SSH remote into your Ubuntu server.
  2. Installing PHP version (5.6, 7.1, 7.2, 7.3) and PHP extensions.
  3. List of PHP Configuration File.
  4. Finding PHP Modules
  5. Switching PHP version

Requirements

To get started, this guide will help you to install specific PHP version on Amazon EC2 Ubuntu server.

SSH Remote on Ubuntu Server

ssh -i key.pem ubuntu@172.XX.XX.XX

Big thanks to this guy Ondřej Surý who continues maintaining PPA of most of the popular PHP versions on the launchpad. 

Now add the PHP PPA package using:

sudo add-apt-repository ppa:ondrej/php

Then run update command:

sudo apt update

Next, choose the following PHP installation guide below that suitable for your web application on Ubuntu server.

Install PHP 5.6 on

If you want to install PHP 5.6 on your Ubuntu server, use the command:

sudo apt install php5.6

Then install the following extensions that are only needed for your PHP application.

sudo apt install php5.6-fpm php5.6-bcmath php5.6-cgi php5.6-cli php5.6-common php5.6-curl php5.6-dba php5.6-dev php5.6-json php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-opcache php5.6-xml php5.6-xsl php5.6-zip libapache2-mod-php5.6

Install PHP 7.1

If you want to install PHP 7.1 on your Ubuntu server, use the command:

sudo apt install php7.1

Then install the following extensions that are only needed for your PHP application.

sudo apt install php7.1-fpm php7.1-bcmath php7.1-cgi php7.1-cli php7.1-common php7.1-curl php7.1-dba php7.1-dev php7.1-json php7.1-mbstring php7.1-mysql php7.1-opcache php7.1-xml php7.1-xsl php7.1-zip libphp7.1-embed libapache2-mod-php7.1

Install PHP 7.2

If you want to install PHP 7.2 on your Ubuntu server, use the command:

sudo apt install php7.2

Then install the following extensions that are only needed for your PHP application.

sudo apt install php7.2-fpm php7.2-bcmath php7.2-cgi php7.2-cli php7.2-common php7.2-curl php7.2-dba php7.2-dev php7.2-json php7.2-mbstring php7.2-mysql php7.2-opcache php7.2-xml php7.2-xsl php7.2-zip libphp7.2-embed libapache2-mod-php7.2

Install PHP 7.3

If you want to install PHP 7.3 on your Ubuntu server, use the command:

sudo apt install php7.3

Then install the following PHP extensions that are only needed for your PHP application.

sudo apt install php7.3-fpm php7.3-bcmath php7.3-cgi php7.3-cli php7.3-common php7.3-curl php7.3-dba php7.3-dev php7.3-json php7.3-mbstring php7.3-mysql php7.3-opcache php7.3-xml php7.3-xsl php7.3-zip libphp7.3-embed libapache2-mod-php7.3

List PHP Configuration File

Your PHP configuration files can be found in /etc/php/ directory and then you can configure the following php.ini files using:

sudo vim /etc/php/7.<version>/apache2/php.ini
sudo vim /etc/php/7.<version>/cli/php.ini
sudo vim /etc/php/7.<version>/fpm/php.ini

Finding For PHP extension

If you need to install PHP modules based on your application requirements. Use the command below to find the available PHP modules in the package repository, type command:

apt search php7.*

And then install the PHP module using:

sudo apt install php7.<version>-<module_name>

Switching PHP version

Interactive switching mode:

sudo update-alternatives --config php

Manual Switching:

From PHP 5.6 => PHP 7.1

Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.

Apache:

sudo a2dismod php5.6
sudo a2enmod php7.1
sudo service apache2 restart

Command Line:

sudo update-alternatives --set php /usr/bin/php7.1

From PHP 7.1 => PHP 5.6

Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.

Apache:

sudo a2dismod php7.1
sudo a2enmod php5.6
sudo service apache2 restart

Command Line:

sudo update-alternatives --set php /usr/bin/php5.6

That’s all.

I hope this tutorial helped you and feel free to comment section below for more suggestions.

SHARE THIS ARTICLE

Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *