Table of Contents
In this article, i will take you through 6 Easy Steps to Install PHP on Ubuntu 20.04. PHP is abbreviated as Hypertext Preprocessor language. It is the most popular Server Side web scripting language which enabled developers and Programmers to create dynamic contents that can interact with databases. You can see most of the web contents running on Server Side supports PHP Language. Hence we will go through the detailed steps to Install PHP on Ubuntu 20.04 and later will look into some of the use cases of PHP.
Steps to Install PHP on Ubuntu 20.04
Also Read: Easy Steps to Install Java on Ubuntu 20.04
Step 1: Prerequisites
a) You need to have a running Ubuntu 20.04
Server.
b) You should have apt
tool installed in your Server. You can check 20 Useful apt get examples on Ubuntu. to know more about apt-get
tool.
d) You should have sudo
access to run Privileged Commands. Please Check How to Add User to Sudoers to know more about providing sudo
access to the User.
Step 2: Update Your Server
Before going through the steps to install PHP on Ubuntu 20.04 Server, it is always recommended to update Your Server first using apt-get update -y
command as shown below.
root@localhost:~# apt-get update -y Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease Hit:3 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease Hit:4 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease Ign:2 https://pkg.jenkins.io/debian-stable binary/ InRelease Hit:5 https://pkg.jenkins.io/debian-stable binary/ Release Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease Reading package lists... Done
Step 3: Install PHP 7.4 on Ubuntu 20.04
Once Server is fully updated you can install PHP 7.4 using apt install php7.4 -y
command as shown below. Current PHP 7.4 is the latest release available for use but if you want to install lower PHP version then you can install that as well. For installing PHP 7.3 you can use apt install php7.3 -y
and for installing PHP 7.2 you can use apt install php7.2 -y
command. You can install any of the release based on your requirement.
Step 4: Check PHP Version
You can check the installed PHP version using php -v
command as shown below. As you can see from below output, current PHP version is 7.4.3
. More can be checked on PHP official documentation.
Step 5: Write a Simple PHP Script
Once PHP is installed it is now time to test the installation by writing a simple PHP Script. You can use phpinfo()
function to display information about PHP as shown below.
root@localhost:~# vi test.php <?php phpinfo(); ?>
Run Your Script
root@localhost:~# php test.php phpinfo() PHP Version => 7.4.3 System => Linux localhost 5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x86_64 Build Date => May 26 2020 12:24:22 Server API => Command Line Interface Virtual Directory Support => disabled Configuration File (php.ini) Path => /etc/php/7.4/cli Loaded Configuration File => /etc/php/7.4/cli/php.ini Scan this dir for additional .ini files => /etc/php/7.4/cli/conf.d Additional .ini files parsed => /etc/php/7.4/cli/conf.d/10-opcache.ini, /etc/php/7.4/cli/conf.d/10-pdo.ini, /etc/php/7.4/cli/conf.d/20-calendar.ini, /etc/php/7.4/cli/conf.d/20-ctype.ini, /etc/php/7.4/cli/conf.d/20-exif.ini, /etc/php/7.4/cli/conf.d/20-ffi.ini, /etc/php/7.4/cli/conf.d/20-fileinfo.ini, /etc/php/7.4/cli/conf.d/20-ftp.ini, /etc/php/7.4/cli/conf.d/20-gettext.ini, /etc/php/7.4/cli/conf.d/20-iconv.ini, /etc/php/7.4/cli/conf.d/20-json.ini, /etc/php/7.4/cli/conf.d/20-phar.ini, /etc/php/7.4/cli/conf.d/20-posix.ini, /etc/php/7.4/cli/conf.d/20-readline.ini, /etc/php/7.4/cli/conf.d/20-shmop.ini, /etc/php/7.4/cli/conf.d/20-sockets.ini, /etc/php/7.4/cli/conf.d/20-sysvmsg.ini, /etc/php/7.4/cli/conf.d/20-sysvsem.ini, /etc/php/7.4/cli/conf.d/20-sysvshm.ini, /etc/php/7.4/cli/conf.d/20-tokenizer.ini PHP API => 20190902 PHP Extension => 20190902 Zend Extension => 320190902 Zend Extension Build => API320190902,NTS PHP Extension Build => API20190902,NTS
Step 6: Uninstall PHP 7.4 from Ubuntu 20.04
If you want to remove PHP packages then you can use apt remove php7.4 -y
command to remove PHP 7.4 from your Server. Similarly, if you have PHP 7.3 then you can use apt remove php7.3 -y
command and if you have PHP 7.2 then you can use apt remove php7.2 -y
command to remove PHP packages.
root@localhost:~# apt remove php7.4 -y Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: apache2 apache2-bin apache2-data apache2-utils apturl-common ca-certificates-java default-jdk-headless default-jre default-jre-headless fonts-dejavu-extra gir1.2-dbusmenu-glib-0.4 gir1.2-dee-1.0 gir1.2-goa-1.0 gir1.2-javascriptcoregtk-4.0 gir1.2-snapd-1 gir1.2-unity-5.0 gir1.2-webkit2-4.0 java-common libapache2-mod-php7.4 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libatk-wrapper-java libatk-wrapper-java-jni libice-dev liblua5.2-0 libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev linux-headers-5.4.0-45 linux-headers-5.4.0-45-generic linux-image-5.4.0-45-generic linux-modules-5.4.0-45-generic linux-modules-extra-5.4.0-45-generic openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless patch perl-openssl-defaults php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline python3-dateutil python3-debconf python3-debian python3-software-properties sgml-base software-properties-common unattended-upgrades update-notifier-common x11proto-core-dev x11proto-dev xml-core xorg-sgml-doctools xtrans-dev Use 'apt autoremove' to remove them. The following packages will be REMOVED: php7.4 0 upgraded, 0 newly installed, 1 to remove and 114 not upgraded. After this operation, 72.7 kB disk space will be freed. (Reading database ... 207117 files and directories currently installed.) Removing php7.4 (7.4.3-4ubuntu2.2) ...
Popular Recommendations:-
How to Install Ruby on Ubuntu 18.04 with Easy Steps
Best Explanation of Wrapper Classes in Java: Autoboxing and Unboxing with Examples
Best Steps to Install Java on RHEL 8/CentOS 8
15 ansible-vault command examples to encrypt and decrypt sensitive data/files on Linux
Learn HTML Image Maps(v5) with Best Examples
C# data types with Best Examples (.NET v4.7)