Step by Step Guide Installing PHP on Ubuntu 22.10

thelinuxterminal

Author - Sanjay

Introduction

If you are a developer or a website owner, you must have heard of PHP, the popular programming language that powers most of the websites on the internet. If you are running Ubuntu 22.10 and want to install PHP, this guide is for you. In this article, we will cover everything you need to know to install PHP on Ubuntu 22.10.

PHP is a server-side scripting language used for developing dynamic web pages and applications. Ubuntu 22.10, the latest version of the Ubuntu operating system, comes with PHP 8.0 pre-installed. However, you might need to install additional PHP modules or a different version of PHP for your project.

Table of Contents

Pre-Requisistes

A Ubuntu 22.10 server or a Desktop with root privilages.

Checking for PHP Installation

Before installing PHP on Ubuntu 22.10, you should check if it's already installed. You can do this by running the following command in the terminal:

php -v

If PHP is installed, you will see the version of PHP installed on your system. If not, you will get an error message.

Update Ubuntu and Prepare System for Installation

Before we begin with the installation process, it's essential to prepare your system for PHP installation. The following steps will ensure that your system is ready for PHP installation.

It's essential to update Ubuntu before installing PHP. This ensures that you have the latest security patches and updates installed. You can update Ubuntu using the following command:

$thelinux $ sudo apt-get update && sudo apt-get upgrade

Update repo

Installing Essential Packages

Next, we need to install some essential packages required for PHP installation. You can install these packages using the following command:

$thelinux $ sudo apt-get install software-properties-common

Now moving to next step.

Adding PPA Repository

We need to add a Personal Package Archive (PPA) repository for PHP installation. You can add the repository using the following command:

$thelinux $ sudo add-apt-repository ppa:ondrej/php

Now that we have prepared our system for PHP installation, we can move on to the actual installation process. There are three methods you can use to install PHP on Ubuntu 22.10:

1. Installing PHP with Apache2

If you're using Apache2 as your webserver, you can install PHP by running the following command

$thelinux $ sudo apt-get install php8.0 libapache2-mod-php8.0

This command above install PHP 8.0 and the Apache2 module required for PHP execution.

2. Installing PHP with Nginx

If you're using Nginx as your webserver, you can install PHP by running the following command:

sudo apt-get install php8.0-fpm

This will install PHP 8.0 and the FastCGI Process Manager required for Nginx to execute PHP scripts.

3. Installing PHP via Command Line

You can also install PHP via the command line using the following command:

$ sudo apt-get install php8.0-cli

This will install the PHP from command line .

Testing PHP Installation on Ubuntu 22.10

To test if PHP is installed correctly on your Ubuntu 22.10 system, you can create a PHP file with the following command:

sudo nano /var/www/html/info.php

This will create a new file called "info.php" in the "html" folder of the Apache2 webserver. Add the following code to the file:

<?php
phpinfo();
?>

Save and exit the file . You can now access the PHP info page by opening a web browser and navigating to "http://localhost/info.php" or "http://<your-server-ip>/info.php". If you see the PHP version and other information on the page, PHP has been installed correctly on your Ubuntu 22.10 system.

Troubleshooting Common PHP Installation Issues

Here are some common issues you might encounter during PHP installation on Ubuntu 22.10 and how to troubleshoot them:

PHP not Running on Apache2

If PHP is not running on Apache2 after installation, you can enable the PHP module using the following command:

sudo a2enmod php8.0

Then, restart the Apache2 webserver using the following command:

sudo systemctl restart apache2

PHP File Downloading Instead of Running

If your PHP file is downloading instead of running, you need to add the PHP MIME type to the Apache2 configuration file. Open the configuration file using the following command:

sudo nano /etc/apache2/mods-enabled/dir.conf

add index.php and the problem should be solved .

Conclusion

In this tutorial we havge seen how to install php on Ubuntyu 22.10 server . Php is super easy to install either on nginx or on apache .