Sign In
Sign In

Installing and Switching PHP Versions on Ubuntu: A Step-by-Step Guide

Installing and Switching PHP Versions on Ubuntu: A Step-by-Step Guide
Hostman Team
Technical writer
PHP Ubuntu
20.11.2024
Reading time: 5 min

PHP is a scripting programming language commonly used for developing web applications. It allows developers to create dynamic websites that adapt their pages for specific users. These websites are not stored on the server in a ready-made form but are created on the server after a user request. This means that PHP is a server-side language, meaning scripts written in PHP run on the server, not the user's computer.

There are many different versions of PHP. The language becomes more powerful and flexible with each new version, offering developers more opportunities to create modern web applications. However, not all websites upgrade or are ready to upgrade to the latest PHP version and remain on older versions.

Therefore, switching between versions is an essential task for many web developers. Some developers want to take advantage of new features introduced in newer versions, while others need to fix bugs and improve the security of existing applications. In this article, we will go over how to install PHP on Ubuntu and how to manage different PHP versions.

How to Install PHP on the Server

To install PHP on Ubuntu Server, follow these steps:

  1. Connect to the server via SSH.

  2. Update the package list:

sudo apt update
  1. Install the required dependencies:
sudo apt install build-essential libssl-dev
  1. Download the installation script from the official website, replacing <version> with the desired version:
curl -L -O https://www.php.net/distributions/php-<version>.tar.gz
  1. Extract the downloaded file, replacing <version> with the downloaded version:
tar xzf php-<version>.tar.gz
  1. Navigate to the directory with the installed PHP:
cd php-<version>
  1. Configure the installation script:
./configure
  1. Build PHP:
make
  1. Install PHP:
sudo make install

After completing these steps, PHP will be installed on your server. The next step is to install a web server to work with PHP. The configuration may involve specifying the PHP module in the web server configuration file and setting up how .php files are handled.

Finally, restart the web server. For example, to restart Apache, you can run the following command:

sudo service apache2 restart

How to Check PHP Version

There are several ways to find out which version of PHP a website is running:

  • Use the terminal.
  • Create a script with phpinfo() in the website's root directory.

Check PHP Version via Terminal

Run the command in the terminal:

php -v

You will get output similar to:

PHP 8.3.13 (cli) (built: Oct 30 2024 11:27:41) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.13, Copyright (c) Zend Technologies
   with Zend OPcache v8.3.13, Copyright (c), by Zend Technologies

Check PHP Version with phpinfo()

  1. Create a file named phpinfo.php with the following content:

<?php
phpinfo( );
?>
  1. Save the file in the root directory of your website (where the index.html or index.php file is located).

  2. Open this file in your browser by using the following URL:

http://your_website_address/phpinfo.php

You will see a page with detailed information about the PHP configuration.

After finding out the PHP version, be sure to delete the phpinfo.php file as it contains important server configuration information that attackers could exploit.

How to Manage PHP Versions

To switch between installed PHP versions on Ubuntu, follow these steps.

  1. Check if multiple PHP versions are installed. To see the list of installed PHP packages, run the command:
dpkg --list | grep php
  1. Install the php-switch package, which allows change PHP versions easily:
sudo apt-get install -y php-switch
  1. Switch to the desired PHP version using the php-switch command. For example, to switch to PHP 7.4, run:
php-switch 8.2
  1. Verify which PHP version is currently active by running:
php -v

Some scripts and extensions may only work with certain PHP versions. Before switching, make sure that all the scripts and extensions you are using support the new version. Otherwise, the website may become inaccessible or malfunction.

Troubleshooting

If PHP scripts are not being processed on your server, the first thing to check is the web server's functionality. Open a browser and go to the website where PHP scripts are not working. If the page opens but the PHP script output is not displayed, the problem may lie with PHP.

Here are some steps you can take to troubleshoot the issue.

Check PHP Service Status

Run the following command, using your PHP version (e.g., PHP 8.3):

sudo service php8.3-fpm status

If the service is running, the output should indicate active (running). If the service is not running, start it with this command:

sudo service php8.3-fpm start

Check PHP Log Files

To view PHP log files, use the following command:

tail /var/log/php7\8.3-fpm.log

This command will display the last few lines of the PHP log file, which may help identify the issue.

Check PHP Configuration

Open the php.ini file in a text editor and ensure the display_errors option is set to On. This will allow PHP errors to be displayed on your website pages.

Check for Script Errors

Open the PHP scripts in a text editor and look for syntax errors or other issues that could prevent the scripts from working properly.

Check for Web Server Restrictions

Check the web server configuration for any restrictions that might affect the execution of PHP scripts. For example, there may be restrictions in the .htaccess file that prevent certain directories from running scripts.

Test the Script on Another Server

If the script works on another server, the issue may be related to the configuration of the current server.

PHP Ubuntu
20.11.2024
Reading time: 5 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us
Hostman's Support