Does ubuntu come with php?

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:


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.

Finally, the third part of our LAMP tutorial series: how to install PHP on Ubuntu. In this tutorial, we’ll show you how to install various versions of PHP, including PHP 7.4, PHP 8.0 and the latest PHP 8.1.

This tutorial should work for any Ubuntu release and other Ubuntu-based releases. Ubuntu 18.04, Ubuntu 20.04, Ubuntu 22.04, Ubuntu 21.10, and should even work for Xubuntu, Kubuntu and similar distros.

Tutorials here:

  • Before we begin
  • How to Install PHP 7.4 on Ubuntu 20.04 (not recommended)
  • How to install PHP 8.1 on Ubuntu 22.04 or 20.04
  • How to install PHP 8.0 on Ubuntu 22.04 or 20.04
  • How to change the PHP version you’re using
  • How to upgrade to PHP 8.1 (or 8.0) on Ubuntu
  • Speed up PHP by using an opcode cache

For the first part of our LAMP series, go to our How to Install and Optimize Apache on Ubuntu tutorial.

And for the second part, go to How to Install MySQL/MariaDB on Ubuntu.

Before we begin installing PHP on Ubuntu

  • PHP has different versions and releases you can use. Starting from the oldest that is currently supported – PHP 7.4, and onto PHP 8.0 and the latest – PHP 8.1. We’ll include instructions for PHP 8.0, PHP 8.1. We recommend that you install PHP 8.1 as it’s stable and has lots of improvements and new features. If you still use PHP 7.4, you definitely need to upgrade ASAP because its security support ends at the end of 2022.
  • You’ll obviously need an Ubuntu server. You can get one from Vultr. Their servers start at $2.5 per month. Or you can go with any other cloud server provider where you have root access to the server.
  • You’ll also need root access to your server. Either use the root user or a user with sudo access. We’ll use the root user in our tutorial so there’s no need to execute each command with ‘sudo’, but if you’re not using the root user, you’ll need to do that.
  • You’ll need SSH enabled if you use Ubuntu or an SSH client like MobaXterm if you use Windows.
  • Check if PHP is already installed on your server. You can use the ‘which php’ command. If it gives you a result, it’s installed, if it doesn’t, PHP is not installed. You can also use the “php -v” command. If one version is installed, you can still upgrade to another.
  • Some shared hosts have already implemented PHP 8.1 in their shared servers, like Hawk Host and iWebFusion.

Now, onto our tutorial.

PHP 7.4 is included by default in Ubuntu 20.04’s repositories. So the instructions are pretty similar to other variations.

The instructions are also similar for Ubuntu 22.04, but Ubuntu 22.04 will use whatever the default PHP version it’s using.

We don’t recommend using PHP 7.4, so you should skip to some of the other installation methods below. Use 8.0 or 8.1 instead.

Update Ubuntu

Again, before doing anything, you should update your server:

apt-get update && apt-get upgrade

Install PHP 7.4

Next, to install PHP 7.4 on Ubuntu 20.04, just run the following command:

apt-get install php

This command will install PHP 7.4, as well as some other dependencies.

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )

And that’s it. PHP 7.4 is installed on your Ubuntu 20.04 server.

Install PHP 7.4 modules (extensions)

These are the most common PHP 7.4 modules often used by php applications. You may need more or less, so check the requirements of the software you’re planning to use:

apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php

To check all the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

How to install PHP 8.1 on Ubuntu 22.04 or 20.04

PHP 8.1 is the newest PHP version released on 25 Nov 2021. You can start using it now. These are the instructions on how to install it on Ubuntu 22.04 or Ubuntu 20.04 (or any other Ubuntu)

Update Ubuntu

First, update your Ubuntu server:

apt-get update && apt-get upgrade

Add the PHP repository

To install PHP 8.0 you’ll need to use a third-party repository. We’ll use the repository by Ondřej Surý that we previously used.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 8.1

After you’ve added the repository, you can install PHP 8.1 with the following command:

apt-get install php8.1

This command will install additional packages:

  • libapache2-mod-php8.1
  • libpcre2-8-0
  • php8.1-cli
  • php8.1-common
  • php8.1-opcache
  • php8.1-readline
  • …and others.

And that’s it. To check if PHP 8.1 is installed on your server, run the following command:

php -v

Which should return something like this:

PHP 8.1.1 (cli) (built: Dec 31 2021 07:26:20) (NTS)

Install PHP 8.1 modules (extensions)

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

apt-get install libapache2-mod-php8.1 php8.1-fpm libapache2-mod-fcgid php8.1-curl php8.1-dev php8.1-gd php8.1-mbstring php8.1-zip php8.1-mysql php8.1-xml

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to install PHP 8.0 on Ubuntu 22.04 or 20.04

Instead of using the older PHP version that’s included by default in Ubuntu 20.04, you should use PHP 8.0 or 8.1. Here are the instructions on how to install PHP 8.0 on Ubuntu 20.04/22.04

Update Ubuntu

Of course, as always, first update Ubuntu:

apt-get update && apt-get upgrade

Add the PHP repository

You can use a third-party repository to install the latest version of PHP. We’ll use the repository by Ondřej Surý.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 8.0

After you’ve added the repository, you can install PHP 8.0 with the following command:

apt-get install php8.0

This command will install additional packages:

  • libapache2-mod-php8.0
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php8.0-cli
  • php8.0-common
  • php8.0-json
  • php8.0-opcache
  • php8.0-readline

And that’s it. To check if PHP 8.0 is installed on your server, run the following command:

php -v

Install PHP 8.0 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

apt-get install libapache2-mod-php8.0 php8.0-fpm libapache2-mod-fcgid php8.0-curl php8.0-dev php8.0-gd php8.0-mbstring php8.0-zip php8.0-mysql php8.0-xml

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to change the PHP version you’re using

If you have multiple PHP versions installed on your Ubuntu server, you can change what version is the default one.

To set PHP 8.1 as the default, run:

update-alternatives --set php /usr/bin/php8.1

To set PHP 8.0 as the default, run:

update-alternatives --set php /usr/bin/php8.0

You can verify what version of PHP you’re using with the following command:

php -v

If you’re following our LAMP tutorials and you’re using Apache, you can configure Apache to use PHP 8.0 with the following command:

a2enmod php8.1

And then restart Apache for the changes to take effect:

systemctl restart apache2

How to upgrade to PHP 7.4, 8.0 or 8.1 on Ubuntu

If you’re already using an older version of PHP with some of your applications, you can upgrade by:

  1. Backup everything.
  2. Install the newest PHP and required modules.
  3. Change the default version you’re using.
  4. (Optionally) Remove the older PHP
  5. (Required) Configure your software to use the new PHP version. You’ll most likely need to configure Nginx/Apache, and many other services/applications. If you’re not sure what you need to do, contact professionals and let them do it for you.

Speed up PHP by using an opcode cache

You can improve the performance of your PHP by using a caching method. We’ll use APCu, but there are other alternatives available.

If you have the ‘php-pear’ module installed (we included it in our instructions above), you can install APCu with the following command:

pecl install apcu

There are also other ways you can install APCu, including using a package.

To start using APCu, you should run the following command for PHP 8.1:

echo "extension=apcu.so" | tee -a /etc/php/8.1/mods-available/cache.ini

Or this command for PHP 8.0:

echo "extension=apcu.so" | tee -a /etc/php/8.0/mods-available/cache.ini

If you’re following our LAMP tutorials and you’re using Apache, create a symlink for the file you’ve just created.

For PHP 8.1:

ln -s /etc/php/8.1/mods-available/cache.ini /etc/php/8.1/apache2/conf.d/30-cache.ini

For PHP 8.0:

ln -s /etc/php/8.0/mods-available/cache.ini /etc/php/8.0/apache2/conf.d/30-cache.ini

And finally, reload Apache for the changes to take effect:

systemctl restart apache2

To further configure APCu and how it works, you can add some additional lines to the cache.ini file you previously created. The best configuration depends on what kind of server you’re using, what applications you are using etc. Either google it and find a configuration that works for you, or contact professionals and let them do it for you.

That’s it for our basic setup. Of course, there are much more options and configurations you can do, but we’ll leave them for another tutorial.

Does Ubuntu include PHP?

PHP is a programming language used for developing web applications. You must install PHP packages on a Ubuntu system to run the application written on it. Generally, it is used to create e-commerce websites, blogs, and API applications. If you're looking for an easy way to install PHP on Ubuntu 22.04, look no further.

Does Ubuntu 20.04 have PHP?

Verify the installation of PHP on Ubuntu You can now see that version 7.4. 3 of PHP is installed on Ubuntu 20.04 LTS.

How do I know if PHP is installed Ubuntu?

Open a bash shell terminal and use the command “php –version” or “php -v” to get the version of PHP installed on the system.

Where is PHP in Ubuntu?

The default location for the php. ini file is: Ubuntu 16.04: /etc/php/7.0/apache2.