PHP File Execution Not Working on Freshly Installed Ubuntu Unity (23.10), Prints File Contents Instead

Abstract: Resolve PHP files printing as text in Ubuntu by explicitly running them with the php command or starting a local server.

Created: 2026-10-13 by
The issue occurs because the terminal interprets the filename as a text file to be displayed rather than a script to be executed by the PHP interpreter. To resolve this, you must explicitly invoke the php command followed by the script path, such as php index.php. This ensures the binary processes the code instead of printing raw source text to standard output.

Understanding Terminal Behavior with Script Files

When you type a filename into a Linux terminal without any preceding command, the shell attempts to execute it as a program or binary. If the file does not have executable permissions set via chmod +x, or if the system cannot identify an executable handler for that specific file extension, many modern shells and environments will fall back to displaying the file's contents. This is similar to running the cat command on the file. In the context of Ubuntu Unity 23.10, this behavior is often observed when users expect the environment to automatically detect and run PHP scripts simply by naming them.

This default behavior is not necessarily a bug but a feature of how interactive shells handle non-executable files. It prevents accidental execution of potentially dangerous scripts while allowing users to quickly view source code. However, for web development workflows where frequent testing of PHP files is required, this can become a significant friction point. Understanding this distinction between executing a binary and reading a text stream is crucial for troubleshooting why your PHP code appears as plain text in the terminal window.

The Correct Execution Method

The most direct solution is to use the PHP interpreter explicitly. Instead of typing just filename.php, you should type php filename.php. This command tells the shell to look for the php executable in your system's PATH and pass the subsequent argument as input to that executable. The PHP engine then parses the file, executes the logic, and outputs the result, which might be HTML, JSON, or plain text depending on what your script produces.

If you are developing a web application, you might also need to start a built-in server to test routing and HTTP requests properly. You can do this by running php -S localhost:8000 from the directory containing your index.php file. This starts a local development server that listens on port 8000, allowing you to access your application via a browser at http://localhost:8000. This method is far more robust for testing dynamic content than trying to run individual scripts directly from the command line interface.

Configuring File Associations and Shebangs

For those who prefer running scripts directly by name, you can configure the file to be self-executing using a shebang line. Add <?php at the very top of your PHP file is not sufficient for shell execution; you need the shell to understand the file. However, PHP files are typically interpreted by the PHP engine, not the shell itself. A common workaround is to create a wrapper script or rely on the explicit php command. Unlike Python or Bash scripts, PHP does not natively support being called directly from the shell via shebang in all configurations without additional setup.

If you frequently work with PHP on Ubuntu, consider checking if the php-cli package is fully installed. Sometimes, minimal installations might miss certain CLI utilities. You can verify the installation by running php --version. If this returns a version number, the interpreter is available. If it says command not found, you need to install it using sudo apt install php-cli. This step is essential for ensuring that the php command is recognized globally across your terminal sessions.

Common Pitfalls in Ubuntu Unity Environments

Ubuntu Unity 23.10 uses GNOME-based components under the hood, even though the desktop environment has a different look. Users coming from Windows backgrounds might expect double-clicking a file in the file manager to execute it. In Linux, double-clicking a PHP file usually opens it in a text editor like Gedit or VS Code because there is no default "open with PHP" action defined for GUI file managers. This reinforces the need to use the terminal for execution.

Another common pitfall involves permission errors. If you attempt to make a PHP file executable using chmod +x script.php and try to run it, you may encounter errors unless the file has a proper shebang and is treated as an executable script by the kernel. Since PHP is an interpreter, not a compiler, the kernel doesn't know how to run it directly without invoking the interpreter first. Therefore, relying on php script.php remains the most reliable and standard approach for CLI execution.

For more detailed guidance on managing file permissions and execution contexts in Ubuntu, you might find Ubuntu 20.04: Permission Error when Writing to /var/www/html helpful, as the principles of ownership and execute bits apply similarly to user directories. Additionally, understanding how to navigate and run commands recursively can be beneficial if you are processing multiple files, as discussed in Running CMD Recursively on Directories in Ubuntu.

Verifying Your PHP Installation

To ensure your environment is correctly set up, open a new terminal window and check the PHP version. Run php -v and confirm that it matches the expected version for Ubuntu 23.10. If the command is not found, reinstall the package using sudo apt update && sudo apt install php. After installation, clear your shell's hash table by running hash -r to ensure the terminal recognizes the newly added binaries immediately without needing to restart the session.

Frequently Asked Questions

Why does my PHP file print HTML instead of executing?

This happens if you are viewing the file through a web server that is misconfigured or if you are opening the file directly in a browser without going through the correct URL. Ensure you are accessing http://localhost via the built-in server or Apache/Nginx, rather than opening the local file path directly.

Can I make PHP files executable like .exe files in Windows?

No, PHP is an interpreted language, not a compiled binary. You cannot make a .php file directly executable by the kernel. You must always invoke the php interpreter to process the script, either via the command line or through a web server module.

What is the difference between php-cli and php-fpm?

php-cli is the Command Line Interface used for running scripts directly from the terminal. php-fpm (FastCGI Process Manager) is designed to handle web server requests efficiently. For terminal execution, you only need php-cli installed.

How do I stop PHP from showing syntax errors in the terminal?

PHP displays errors to standard error by default. To suppress them during execution, you can redirect stderr to null using php script.php 2>/dev/null. However, it is recommended to keep errors visible during development to debug issues effectively.

Is it safe to use chmod +x on PHP files?

It is generally unnecessary and can be confusing. While it won't break security if done carefully, it does not make the file self-running. It is better to stick to the standard practice of calling php filename.php to maintain clarity and compatibility across different systems.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More