お問い合わせ
image

How to Upgrade PHP 8.0 to PHP 8.2 FPM on an AWS Lightsail Ubuntu Instance for Laravel

制作・開発
profile

Iftekhar Eather

As PHP continues to evolve, staying updated with the latest stable versions is crucial for performance, security, and compatibility with modern frameworks like Laravel. If you’re running a Laravel application on an AWS Lightsail instance with Ubuntu and are looking to upgrade from PHP 8.0 FPM to PHP 8.2 FPM, this guide is for you.

Why Upgrade to PHP 8.2?

PHP 8.2 brings numerous improvements, including new features, optimizations, and deprecations of older functionalities. Upgrading ensures that your application benefits from these advancements while maintaining security compliance.

Step-by-Step Guide to Upgrading PHP on AWS Lightsail

Step 1: Install PHP 8.2 FPM

Before you start the upgrade, SSH into your AWS Lightsail instance. Once connected, begin by updating your package lists and installing PHP 8.2 FPM along with the necessary PHP extensions.

sudo apt update
sudo apt install php8.2-fpm php8.2-mbstring php8.2-xml php8.2-curl php8.2-mysql php8.2-zip php8.2-gd php8.2-bcmath

These extensions are commonly required by Laravel applications, but you can customize the list based on your specific needs.

Step 2: Update Nginx Configuration

Your Laravel application runs on Nginx, which needs to be configured to use the new PHP 8.2 FPM socket.

  1. Edit the Nginx site configuration file by opening it in a text editor sudo nano /etc/nginx/sites-available/default
  2. Update the fastcgi_pass directive to point to PHP 8.2:Replace the existing line fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; With the new line fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
  3. Save and close the file by pressing Ctrl + X, then Y, and finally Enter.

Step 3: Disable PHP 8.0 FPM

With PHP 8.2 FPM installed, you should disable the older PHP 8.0 FPM service to avoid conflicts:

sudo systemctl disable php8.0-fpm
sudo systemctl stop php8.0-fpm

Step 4: Enable and Start PHP 8.2 FPM

Now, enable and start the PHP 8.2 FPM service:

sudo systemctl enable php8.2-fpm
sudo systemctl start php8.2-fpm

Step 5: Restart Nginx

To apply the changes, restart Nginx:

sudo systemctl restart nginx

Step 6: Verify the PHP Version

To ensure that the upgrade was successful, create a phpinfo file in your web root directory:

  1. Create the file sudo nano /var/www/html/info.php
  2. Add the following content <?php phpinfo(); ?>
  3. Access this file via your browser: Navigate to http://ipordomain/info.php. You should see the PHP version displayed as 8.2, confirming the upgrade succeeded.

Step 7: Ensure the PHP CLI Uses PHP 8.2

Even after upgrading PHP for Nginx, you may find that the command-line interface (CLI) still uses the older version of PHP (e.g., PHP 8.0). This can cause issues when running artisan commands like php artisan schedule:list. Here’s how to ensure the CLI uses PHP 8.2:

  1. Check the current PHP CLI version: php -v If it shows PHP 8.0 instead of PHP 8.2, you’ll need to update the default PHP version for the CLI.
  2. Update the PHP CLI using update-alternatives: Use the update-alternatives tool to set PHP 8.2 as the default version for the CLI:
    • sudo update-alternatives --set php /usr/bin/php8.2
    • sudo update-alternatives --set phpize /usr/bin/phpize8.2
    • sudo update-alternatives --set php-config /usr/bin/php-config8.2
    • Alternatively, you can directly update the symlink:
  3. sudo ln -sf /usr/bin/php8.2 /usr/bin/php
  4. Verify the change by running the following: php -v It should now display PHP 8.2 as the active version.

Step 8: Clear Laravel Cache (Optional)

After upgrading PHP, it’s a good idea to clear Laravel’s cached data to ensure everything runs smoothly with the new PHP version:

php artisan optimize:clear

Conclusion

Upgrading from PHP 8.0 to PHP 8.2 FPM on your AWS Lightsail instance is a straightforward process that ensures your Laravel application remains secure, performant, and compatible with the latest features. By following the steps outlined in this guide, you’ll be up and running with PHP 8.2 in no time.