Installing and Switching PHP Versions on Ubuntu: A Step-by-Step Guide
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.
And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with US based VPS and Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.
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 Copy link
To install PHP on Ubuntu Server, follow these steps:
-
Connect to the server via SSH.
-
Update the package list:
sudo apt update- Install the required dependencies:
sudo apt install build-essential libssl-dev- 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- Extract the downloaded file, replacing
<version>with the downloaded version:
tar xzf php-<version>.tar.gz- Navigate to the directory with the installed PHP:
cd php-<version>- Configure the installation script:
./configure- Build PHP:
make- Install PHP:
sudo make installAfter 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 restartHow to Check PHP Version Copy link
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 Copy link
Run the command in the terminal:
php -vYou 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 TechnologiesCheck PHP Version with phpinfo() Copy link
-
Create a file named
phpinfo.phpwith the following content:
<?php
phpinfo( );
?>-
Save the file in the root directory of your website (where the
index.htmlorindex.phpfile is located). -
Open this file in your browser by using the following URL:
http://your_website_address/phpinfo.phpYou 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 Copy link
To switch between installed PHP versions on Ubuntu, follow these steps.
- Check if multiple PHP versions are installed. To see the list of installed PHP packages, run the command:
dpkg --list | grep php- Install the
php-switchpackage, which allows change PHP versions easily:
sudo apt-get install -y php-switch- Switch to the desired PHP version using the php-switch command. For example, to switch to PHP 7.4, run:
php-switch 8.2- Verify which PHP version is currently active by running:
php -vTroubleshooting Copy link
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 Copy link
Run the following command, using your PHP version (e.g., PHP 8.3):
sudo service php8.3-fpm statusIf 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 startCheck PHP Log Files Copy link
To view PHP log files, use the following command:
tail /var/log/php7\8.3-fpm.logThis command will display the last few lines of the PHP log file, which may help identify the issue.
Check PHP Configuration Copy link
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 Copy link
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 Copy link
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 Copy link
If the script works on another server, the issue may be related to the configuration of the current server.