Why Your Browser Might Be Rendering PHP Code Instead of HTML
Seeing raw PHP code, such as <?php echo "Hello World"; ?>, render directly in a web browser is almost always a symptom of a misconfigured server rather than a bug within the script itself. This behavior indicates that your web serverâwhether Nginx or Apacheâis failing to invoke the PHP interpreter before sending the response back to the client. When this happens, the file extensions might be wrong, the mod_php module could be missing, or the virtual host configuration simply hasn't been updated to map .php files to the correct handler.
In many shared hosting environments, this issue is rare because the provider handles the mapping automatically. However, on a Linux system like Ubuntu 14.04 with PHP 7 installed (as suggested by your environment), you are likely managing the stack yourself or using a lightweight container where manual intervention is required. If you followed DigitalOcean documentation but still see code in the browser, the most probable culprit is that the specific application directory was not added to the virtual host's index.php, or the .php file extension handling was never enabled for that specific path.
Diagnosing the Handler Mismatch
The first step is to verify how your server treats the request. Open a terminal and check the output of the script directly using the command line:
php /path/to/your/script.php
If this command outputs valid data or an error message (and not raw HTML), the PHP interpreter is functioning correctly in isolation. The issue lies strictly in the web server configuration. If you are using Apache, you need to ensure that LoadModule php7_module lib/apache2/modules/php7.so is uncommented in your apache2.conf or a specific module include file, and that AddType application/x-httpd-php .php is present in your virtual host configuration.
For Nginx, the syntax differs entirely. You must locate your server block configuration (usually in /etc/nginx/sites-available/default) and ensure the fastcgi_pass directive points to a valid FastCGI socket or port, and that include fastcgi_params is included with fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;. A common mistake on Ubuntu 14.04 is relying on default configurations that assume PHP 5 modules exist, leaving PHP 7 unlinked to the server process.
The Virtual Host Trap
A frequent oversight during manual installation involves the DocumentRoot. If your .htaccess file (if enabled) or Nginx config attempts to serve files from a directory where permissions are restricted, the server might return a 403 error or fallback to serving the raw source if the configuration falls back incorrectly. However, seeing actual code execution suggests the server is running PHP, just not for the right URL path.
Ensure that your index.php is actually being requested. If you have an index.html, Apache might serve that by default unless Options +MultiViews is disabled or DirectoryIndex index.php index.html is explicitly set in the virtual host config. If you are trying to access a specific script via URL, ensure there isn't a rule rewriting it to a static file.
Modern Alternatives and Security Contexts
While troubleshooting this configuration is valid for legacy systems like Ubuntu 14.04, it is worth noting that running PHP versions older than the current LTS releases introduces significant security risks. If you are using this stack specifically to manage databases via phpMyAdmin, consider that modern deployments often separate the database management interface into a dedicated container or use tools designed for cloud-native stacks.
If you are looking for a more robust and secure way to manage your database without wrestling with legacy Ubuntu configurations, platforms like https://laravelcompany.com offer managed solutions that abstract away these underlying server complexities. They handle the PHP versions, database connections, and security patches automatically, allowing you to focus on application logic rather than fastcgi_params directives.
Ultimately, fixing the raw code display requires aligning your virtual host configuration with the installed PHP module. Once the server recognizes .php as executable script rather than static text, the browser will render the output correctly, and phpMyAdmin (or any other PHP tool) will function as expected.