A classic issue!
The cause of the error:
By uninstalling php8.3-fpm, you've removed the FastCGI Process Manager (FPM) implementation of PHP, which was likely being used by Apache to execute PHP scripts. Now, Apache doesn't know how to handle PHP files anymore.
When you request a PHP file (in this case, index.php, the entry point of your Laravel project), Apache reads the file as plain text instead of executing it as PHP code. Hence, you're seeing the PHP source code displayed in your browser.
Why did PHP-FPM work with Apache?
Before uninstalling PHP-FPM, Apache was configured to use mod_proxy_fcgi (or mod_fastcgi) to forward PHP requests to the PHP-FPM daemon. PHP-FPM listened on a socket (e.g., /run/php/php8.3-fpm.sock or a TCP port) and executed PHP scripts. Apache would send the request to PHP-FPM, which would execute the PHP code and return the result to Apache, which then sent it back to the client (your browser).
The solution:
You have two options:
Option 1: Reinstall PHP-FPM (recommended)
Reinstall php8.3-fpm to get back the FastCGI setup:
sudo apt install php8.3-fpm
After installation, restart Apache and PHP-FPM services:
sudo systemctl restart apache2
sudo systemctl restart php8.3-fpm
This should restore the previous setup, and your Laravel project should work as expected.
Option 2: Use mod_php (Apache module for PHP)
Alternatively, you can configure Apache to use the libapache2-mod-php8.3 module, which embeds a PHP interpreter directly into Apache. This approach doesn't require PHP-FPM.
First, make sure the libapache2-mod-php8.3 package is installed:
sudo apt install libapache2-mod-php8.3
Then, enable the php8.3 module in Apache:
sudo a2enmod php8.3
Disable any existing proxy_fcgi module configuration (if present):
sudo a2dismod proxy_fcgi
Finally, restart Apache:
sudo systemctl restart apache2
With mod_php enabled, Apache will execute PHP scripts directly, without relying on PHP-FPM.
Verification
After applying either solution, verify that:
- Your Laravel project's homepage is displayed correctly (no PHP code visible).
- Check the Apache logs (
/var/log/apache2/error.log) for any PHP-related errors.
- Run
phpinfo() (create a new PHP file with <?php phpinfo(); ?> content) to confirm PHP is executed correctly.
Additional hint for Laravel
Ensure your Laravel project's public/.htaccess file is correctly configured. It should contain the standard Laravel directives, including:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This configuration helps Laravel's URL rewriting and routing work correctly with Apache.
Now, your Laravel project should be back up and running smoothly!