If you’re hosting Laravel apps on Debian 12, you may face the need to support multiple PHP versions — for example, running a legacy Laravel project that requires PHP 7.4 alongside a new project using PHP 8.2. Since Debian 12 ships only with PHP 8.2 by default, this guide will walk you through installing PHP 7.4, configuring Nginx to run both Laravel projects, and securing them with Cloudflare SSL.

Step 1, Add Sury PHP Repository

Install Sury’s repository to access PHP 7.4 packages:

sudo apt update
sudo apt install -y lsb-release ca-certificates apt-transport-https software-properties-common gnupg2
wget -qO - https://packages.sury.org/php/apt.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/php.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update

Step 2, Install PHP Versions and Extensions

Install both versions of PHP along with common Laravel dependencies:

# PHP 7.4
sudo apt install -y php7.4 php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-xml php7.4-curl php7.4-zip

# PHP 8.2
sudo apt install -y php8.2 php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip

Step 3: Prepare Laravel Project Directories

Create directories for each Laravel version:

sudo mkdir -p /var/www/laravel74 /var/www/laravel82

Each project must point its Nginx root to the public/ directory:

cd /var/www/laravel74 && composer create-project laravel/laravel:^8 .
cd /var/www/laravel82 && composer create-project laravel/laravel:^10 .

Set correct permissions:

sudo chown -R www-data:www-data /var/www/laravel74 /var/www/laravel82

Step 4, Configure Nginx Server Blocks with PHP and SSL

Create two Nginx config files:

/etc/nginx/sites-available/laravel74.conf

server {
    listen 80;
    server_name laravel74.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name laravel74.example.com;

    ssl_certificate     /etc/ssl/certs/cloudflare.pem;
    ssl_certificate_key /etc/ssl/private/cloudflare.key;
    include snippets/ssl-params.conf;

    root /var/www/laravel74/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

/etc/nginx/sites-available/laravel82.conf

server {
    listen 80;
    server_name laravel82.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name laravel82.example.com;

    ssl_certificate     /etc/ssl/certs/cloudflare.pem;
    ssl_certificate_key /etc/ssl/private/cloudflare.key;
    include snippets/ssl-params.conf;

    root /var/www/laravel82/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable both sites:

sudo ln -s /etc/nginx/sites-available/laravel74.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/laravel82.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Make sure to place Cloudflare’s origin SSL certs in the specified paths or use Let’s Encrypt as an alternative.

Step 5: Final Laravel Setup

Run the artisan setup using the proper PHP version:

cd /var/www/laravel74 && php7.4 artisan config:cache
cd /var/www/laravel82 && php8.2 artisan config:cache

Ensure writable directories:

sudo chown -R www-data:www-data storage bootstrap/cache

Step 6: Local Testing with /etc/hosts

Add the following to /etc/hosts for testing on a local machine:

127.0.0.1 laravel74.example.com
127.0.0.1 laravel82.example.com

Step 7: Switch Default PHP Version (CLI)

To change which version of PHP runs when you use php -v, use:

sudo update-alternatives --config php

You will see a prompt like:

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.2   82        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.2   82        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

Choose the number for PHP 7.4 (e.g., 1), and press Enter.

To make sure it’s switched:

php -v

Should now show PHP 7.4.x.

Step 8: Update Other PHP Tools

Some tools like phpize or php-config also use alternatives:

sudo update-alternatives --config phpize
sudo update-alternatives --config php-config

Conclusion

You’ve now set up a robust multi-version Laravel environment on Debian 12 with Nginx, secured with Cloudflare SSL. This setup allows you to run legacy and modern Laravel apps in parallel with clean separation and flexibility.

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.