How to run a PHP file from ubuntu?

How to run a PHP file from ubuntu?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Critical Distinction Between www-data and Your User Account

You are encountering a 404 error because your web server cannot find the file in its configured document root, or it is being blocked by permissions. In a standard LAMP stack on Ubuntu, the most common mistake involves placing files in /usr/var/html/. First, note that the directory path you provided contains a typo: it should be /var/www/html/, not /usr/var/html/. However, even correcting the path won't solve the problem if the web server user cannot access the file or if the server configuration doesn't point to that location.

When you install LAMP on Ubuntu, Apache serves content for the root domain (localhost) from a specific directory. By default, this is /var/www/html. If you place your a.php in /var/www/html/, Apache will attempt to execute it. However, if you placed it in /usr/var/html/ (assuming you meant /var/www/html/a.php but typed the wrong path), or if you created a custom directory structure outside the default root, Apache ignores files there unless explicitly configured to do so.

To run a PHP file from Ubuntu successfully, you must ensure three things: correct permissions, correct location, and correct execution context.

Fixing Permissions and Ownership

The second critical factor is how the file is owned. Apache on Ubuntu runs as the www-data user. If your PHP file is owned by your personal user account (e.g., yourname) with restricted permissions like 600, Apache will be unable to read or execute it, even if the path is correct. This often manifests as a 500 Internal Server Error rather than a 404, but sometimes misconfigured servers hide these errors behind generic responses.

To fix this, you need to change the ownership of your directory and files to www-data. You can do this from your user shell:

sudo chown -R www-data:www-data /var/www/html/a.php
# Or if you are creating a custom project directory:
sudo mkdir -p /var/www/myproject
cd /var/www/myproject
touch index.php
sudo chown -R www-data:www-data .

After changing ownership, ensure the file permissions allow reading and executing. Standard practice is 644 for files (read/write for owner, read-only for group/others) and 755 or 755 for directories. You can set this globally with:

sudo chmod -R 755 /var/www/myproject

Verifying Apache Configuration

If permissions are correct but you still see a 404, the issue likely lies in Apache's virtual host configuration. Apache does not automatically serve files from every directory under /var/www. It only serves files from the directories defined in Include directives within site configurations.

Check your current document root by looking at the Apache config file, usually located at /etc/apache2/sites-available/000-default.conf. Look for a line resembling:

<Directory /var/www/html>
    AllowOverride All
    Require all granted
</Directory>

If your a.php is located in a different path, such as /var/www/mysite/, you must either move the file to /var/www/html/ or create an alias. To add an alias without moving files, edit the config:

Alias /mysite "/var/www/mysite"
<Directory /var/www/mysite>
    AllowOverride All
    Require all granted
</Directory>

After making changes to the configuration, you must restart Apache for them to take effect:

sudo systemctl reload apache2

Why Not Run PHP Directly from Command Line?

It is important to distinguish between running a script via the command line and serving it via a browser. When you run php /var/www/html/a.php in your terminal, you are invoking the PHP CLI (Command Line Interface). This bypasses Apache entirely. While useful for testing logic or debugging without starting a server, this approach is not what happens when you visit a URL in a browser. The browser sends an HTTP request, and Apache handles the routing and response formatting.

If your goal is to deploy a Laravel application, which requires a specific directory structure (public folder) and often uses php artisan serve, that workflow differs significantly from running raw PHP files on a LAMP stack. For complex frameworks like Laravel, managing permissions and virtual hosts becomes even more critical due to the requirement for specific entry points in the public directory.

If you need further assistance with deploying Laravel applications or configuring your Ubuntu server for production environments, resources such as https://laravelcompany.com can provide detailed guidance on setting up these stacks correctly.

Our network