Upgrade php on apache server
Upgrading PHP on the web server
Firstly, this article is primarily about upgrading the php version of the webserver rather then the php-cli version.
to find the php-cli version run php -v
on the server.
To find the active version of PHP being used by your web server, you can create a simple PHP file to check the version that Apache or another web server is using. Here’s how you can do it:
Step 1: Create a PHP Info File
Go to the root directory of your web server. For example, it might be /var/www/html/ on a typical Apache setup. Create a file named info.php
1
sudo vim /var/www/html/info.php
Add the following code to the file:
1
<?php phpinfo(); ?>
Save and exit the file.
Step 2: Access the PHP Info Page in Your Browser
Open your browser and navigate to: http://your-server-ip/info.php
This page will display detailed information about the PHP configuration, including the version that the web server is using. Look for the “PHP Version” at the top of the page.
Step 2: Update the Package List
1
2
3
sudo apt update
Step 3: Add the Repository for the New PHP Version
If you are upgrading to a PHP version that is not available by default, you may need to add a repository. For Ubuntu, you can add the “ondrej/php” repository, which maintains PHP versions:
1
2
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Step 4: Install the New PHP Version
Install the desired version of PHP, for example, PHP 8.3:
1
sudo apt install php8.3
You will also need to install the necessary PHP modules, depending on what your applications require:
1
2
sudo apt install php8.3 libapache2-mod-php8.3
Step 5: Disable the Current PHP Version
Disable the current version of PHP from Apache:
1
2
3
sudo a2dismod php8.1 # Replace with your current version if different
sudo a2enmod php8.3
Step 6: Restart Apache
To apply the changes, restart Apache:
1
sudo systemctl restart apache2
Step 7: Verify the Upgrade
1
sudo vim /var/www/html/info.php
1
<?php phpinfo(); ?>
Step 9: Remove the Info File
For security reasons, delete the info.php
file:
1
sudo rm /var/www/html/info.php
For security reasons, delete the info.php file after you have verified the PHP version: