How To Install PHP On Ubuntu 20.04 or 22.04. In this post, we introduce PHP , its advantages. After that we show you how to install different PHP versions on Ubuntu 20.04 or 22.04.
Today, many organizations prefer building their websites and server side applications using PHP. Well, PHP is one of the popular open source scripting languages used specifically for web development and modifying several elements within a database. Also, an HTML embedded server side language that runs on the server to produce dynamic online content. Developers even use the open-source scripting language for creating web applications, managing dynamic websites, and customer relationship management systems.
Being an open source language, it is available for free use and is modified to meet the needs of the user. Earlier, PHP stood for Personal Homepage, but now it is referred to as Hypertext Pre-processor. Over time, it got transformed into a scripting language that made it easier for developers to create dynamic web pages and applications.
It’s been around 26 years since PHP’s initial release. Its latest version 8 was published in November 2020, but version 7 is still the most popular. The most widely used version of PHP is the Zend engine. There are a few additional versions as well, including Hip Hop, HPVM, and Parrot.
The best part about PHP is before a document or website is sent to the user’s browser, this serverside language is run on the server. As a result, dynamic web pages that change depending on user input or database content are made using PHP. The PHP framework makes it easier to reuse existing code and eliminates the need to create lengthy, intricate code for web apps.
ServerSide Language: Before sending the webpage to the user’s browser, PHP is run on the server, i.e., the user can view only the HTML output.
Scalability: PHP has the ability to handle multiple requests at once. If you planning to design a website like an e-commerce store that may receive a lot of traffic, you must choose PHP as an ideal choice.
Object Oriented Programming: Another key feature of PHP is it supports object oriented programming (OOP). Using this language, developers generate reusable codes and develop complex applications without any hassle.
Large Library: Developers make proper use of supported libraries of functions to perform multiple tasks without any hassle. Additionally, developers even take help from these libraries to write code quickly and efficiently.
Dynamically Typed Programming Language: PHP are dynamically typed programming language, i.e., the data type or variable is determined only during the runtime. As a result, the variables are modified, and code is written without having to declare data types. Also, if any error exists in the data type, it may not be caught until runtime. Using this feature, developers build applications faster and more efficiently.
Free and Open Source Scripting Language: Since PHP is a free and open source scripting language anyone use it. With PHP, web developers no longer need to invest in or purchase expensive software as it is easily helps to create dynamic websites. It is readily accessible for use with events or web applications and is downloaded from any location.
Fast and Efficient: Applications built with PHP and connected to a database are loaded quickly with this powerful language. All in all, PHP is fast and loads more quickly than other computer languages do on slow internet connections as well as manages multiple requests at once.
Compatibility with Operating systems: Another benefit of PHP is it works well with Windows, Linux, macOS, and other popular operating systems. This feature makes it a versatile language that is used on any device.
Large Community Support: PHP has a large community for developers to locate solutions to issues and obtain assistance when required. It also has a wide range of libraries available that care used for various function modules for data representation and creating web applications. These built-in libraries help save time as well as the efforts of developers.
Easy to Learn: PHP is easy to learn and has a simple syntax that makes it easy for programmers to create code. Basically, anyone who is acquainted with C programming works on PHP with ease.
We have arrived to the main part of this article blog How To Install PHP On Ubuntu 20.04 or 22.04.
Before starting, update all the system packages to the updated version by running the following command.
apt update -y
apt upgrade -y
Once all the system packages are updated, you need to install some required packages on your server. Well, install all of them by running the following command.
By default, the latest version of PHP is included in the Ubuntu default repository. However, if you want to install multiple PHP versions then you will need to add the PHP Ondrej repository to your server. You install the PHP repository with the following command.
add-apt-repository ppa:ondrej/php
Next, update the repository cache using the following command.
apt update -y
Now, use the following syntax to install your required PHP versions on your server.
apt install php(versions-number)
For example, if you want to install the PHP 8.2 version run the following command.
apt install php8.2 -y
Next, verify the installed PHP version with the following command.
php --version
You see the PHP version in the following screen.
If you want to install PHP 8.1, run the following command.
apt install php8.1 -y
To install PHP 7.4 version, run the following command.
apt install php7.4 -y
If you want to install an older PHP version 5.6, run the following command.
After installing PHP, you also need to install various PHP extensions to extend their features. The basic syntax to install the PHP extension is shown below.
apt install php8.2-(extension)
For example, if you want to install MySQL, Curl, GD, and XML extensions, run the following command.
By default, all PHP configuration files are stored in the /etc/php directory. Here is the list of all main configuration files for the PHP 8.2 version.
PHP Apache: /etc/php/8.2/apache2/php.ini
PHP-FPM: /etc/php/8.2/fpm/php.ini
PHP CLI: /etc/php/8.2/cli/php.ini
All PHP module’s configuration directory locations are shown below:
If you are using multiple PHP versions on the same server. Then, you easily switch between your desired PHP version using the update alternatives command.
Run the following command to set the default PHP versions:
update-alternatives --config php
You should see a list of all installed PHP versions on the following screen.
Type your desired PHP version number from the above list and press the Enter key. Next, verify your new PHP version using the following command.
php --version
See your default PHP version on the following screen.
How To Install PHP On Ubuntu 20.04 or 22.04 Conclusion
In this guide, we learned how to install PHP on Ubuntu 20.04 or 22.04. We also learned how to switch between multiple PHP versions and use it with Apache and Nginx web servers. We hope you now easily host your PHP application on the server.
Finally, many web applications demand a strong programming language that comes with a reliable database management system; PHP is one of them. Lastly, PHP is the fastest Programming language that is easy to use and makes the connection securely with databases.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.