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.
To install PHP on Ubuntu Server, follow these steps:
Connect to the server via SSH.
Update the package list:
sudo apt update
sudo apt install build-essential libssl-dev
<version>
with the desired version:curl -L -O https://www.php.net/distributions/php-<version>.tar.gz
<version>
with the downloaded version:tar xzf php-<version>.tar.gz
cd php-<version>
./configure
make
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
There are several ways to find out which version of PHP a website is running:
phpinfo()
in the website's root directory.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
Create a file named phpinfo.php
with the following content:
<?php
phpinfo( );
?>
Save the file in the root directory of your website (where the index.html
or index.php
file is located).
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.
To switch between installed PHP versions on Ubuntu, follow these steps.
dpkg --list | grep php
php-switch
package, which allows change PHP versions easily:sudo apt-get install -y php-switch
php-switch 8.2
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.
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.
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
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.
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.
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 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.
If the script works on another server, the issue may be related to the configuration of the current server.