Do i need to install php on ubuntu?

PHP - Scripting Language

PHP is a general-purpose scripting language suited for Web development. PHP scripts can be embedded into HTML. This section explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL.

This section assumes you have installed and configured Apache2 Web Server and MySQL Database Server. You can refer to the Apache2 and MySQL sections in this document to install and configure Apache2 and MySQL respectively.

Installation

PHP is available in Ubuntu Linux. Unlike Python, which is installed in the base system, PHP must be added.

To install PHP and the Apache PHP module you can enter the following command at a terminal prompt:

sudo apt install php libapache2-mod-php

You can run PHP scripts at a terminal prompt. To run PHP scripts at a terminal prompt you should install the php-cli package. To install php-cli you can enter the following command:

sudo apt install php-cli

You can also execute PHP scripts without installing the Apache PHP module. To accomplish this, you should install the php-cgi package via this command:

sudo apt install php-cgi

To use MySQL with PHP you should install the php-mysql package, like so:

sudo apt install php-mysql

Similarly, to use PostgreSQL with PHP you should install the php-pgsql package:

sudo apt install php-pgsql

Configuration

If you have installed the libapache2-mod-php or php-cgi packages, you can run PHP scripts from your web browser. If you have installed the php-cli package, you can run PHP scripts at a terminal prompt.

By default, when libapache2-mod-php is installed, the Apache 2 Web server is configured to run PHP scripts using this module. Please verify if the files /etc/apache2/mods-enabled/php8.*.conf and /etc/apache2/mods-enabled/php8.*.load exist. If they do not exist, you can enable the module using the a2enmod command.

Once you have installed the PHP related packages and enabled the Apache PHP module, you should restart the Apache2 Web server to run PHP scripts, by running the following command:

sudo systemctl restart apache2.service 

Testing

To verify your installation, you can run the following PHP phpinfo script:

<?php
  phpinfo();
?>

You can save the content in a file phpinfo.php and place it under the DocumentRoot directory of the Apache2 Web server. Pointing your browser to http://hostname/phpinfo.php will display the values of various PHP configuration parameters.

References

  • For more in depth information see the php.net documentation.

  • There are a plethora of books on PHP 7 and 8. A good book from O’Reilly is Learning PHP, which includes an exploration of PHP 7’s enhancements to the language.

  • Also, see the Apache MySQL PHP Ubuntu Wiki page for more information.

PHP is the language that is mostly used for server-side programming. It is executed, run, and must be installed on the webserver. Since it is an interpreted language, it doesn’t need any compiler. PHP handles the interaction between the front-end of the web pages and the back-end servers or databases.

Installation of PHP on Ubuntu 20.04 LTS

Before getting started with the installation of PHP, first, always update Ubuntu’s package repository.

Do i need to install php on ubuntu?

After updating Ubuntu’s package repository, install the PHP by typing the command given below.

Do i need to install php on ubuntu?

It will take a while to download and install the latest stable version of PHP.

Verify the installation of PHP on Ubuntu

Once it is installed successfully, you can verify and check the version of PHP by typing the “php -v” command.

Do i need to install php on ubuntu?

Alright! You can now see that version 7.4.3 of PHP is installed on Ubuntu 20.04 LTS.

Install Apache PHP module

If you want to install other requisite software like Apache PHP Module with it, then type the command below.

$ sudo apt install libapache2-mod-php

Do i need to install php on ubuntu?

Now restart the Apache server for the changes to take effect, and if you want to load the PHP module, type the following command.

$ sudo systemctl restart apache2

Do i need to install php on ubuntu?

If it is restarted without throwing any error, you can verify it by checking the status using the command given below.

$ sudo systemctl status apache2

Do i need to install php on ubuntu?

If it is active and running, then you are ready to go.

Find and Install Extensions of PHP on Ubuntu 20.04

In case you want to install some further extensions of PHP, you can type the following command in the terminal.

Then, hit the tab button twice on your keyboard to see the available extensions of PHP.

Do i need to install php on ubuntu?

For example, if we want to install the PHP MySQL extension, the command would be like this:

$ sudo apt install php-mysql

It will ask you to take additional disk space for the extension to install, then press “y” to continue the process.

Do i need to install php on ubuntu?

Downloading and installation of the PHP MySQL extension will start.

Do i need to install php on ubuntu?

This is how you can find and install any extension of PHP of your desire.

Conclusion

This is the simplest way to install and get started with the PHP on Ubuntu 20.04 LTS. In this post, we have explained the installation of PHP and its extensions.

About the author

Do i need to install php on ubuntu?

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.

Does Ubuntu come with PHP?

Note: Ubuntu 22.04 ships with PHP 8.1 in its repositories. This means that if you attempt to install PHP without a specified version, it will use 8.1. If you would like to use a different version of PHP on your Ubuntu 22.04 server, you can use the phpenv project to install and manage different versions.

Does Ubuntu 20.04 have PHP?

NOTE: Ubuntu 20.04 ships with PHP 7.4 in its upstream repositories. Just install it and the extensions you with the apt package manager.

Does PHP need to be installed?

No, if you installed a web server (e.g. Apache) on your computer it will not include PHP. You need to install it if you need it. There are apps such as WAMP and XAMPP that will install Apache, MySQL and PHP on your computer without any hassle.

How do I start PHP in Ubuntu?

How to run PHP on Ubuntu.
sudo apt-get install php. ... .
In order to restart Apache web server just type the following command sudo gedit /var/www/html/index. ... .
In this tutorial im using gedit to create my webpages but you can also install an IDE like netbeans and manage you projects from there in a way more efficient way..