What permissions for PHP scripts/directories?

What permissions for PHP scripts/directories?

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

Stefan Bogdanescu

Founder & Senior Architect

What permissions for PHP scripts/directories?

When migrating a legacy web application, determining the correct file structure and permissions is often where the real work begins. You are essentially asking how to configure the filesystem so that the web server (Apache) can execute your code while denying direct access to sensitive logic or credentials. The goal is to separate public-facing scripts from internal logic without relying on arbitrary directory names like /php or /inc.

File Extensions and Server Parsing

The file extension .php does not grant a script "execute" privileges in the operating system sense; it simply signals to the web server that the content should be processed by the PHP interpreter. Apache relies on configuration files (usually httpd.conf or included vhost configs) to map file extensions to handlers.

By default, Apache treats .php files as text unless a handler is explicitly defined. When you request a script via <http://host/register.php>, the server checks its MIME type definitions. If PHP processing is enabled globally (which it usually is), the server passes the file content to the PHP daemon or module for execution. The extension itself is irrelevant to the OS permissions; it is purely a signaling mechanism for the web server software.

Placement: httpdocs vs. Custom Directories

For a standard public script like register.php, placing it in /httpdocs (or the traditional public_html) is the correct approach. This directory typically serves as the document root, meaning any URL path maps directly to files inside it.

You do not need to recreate the old /php or /inc directories under your public web root unless you have specific legacy dependencies that hardcode those paths in URLs. Instead, create a clean structure within your document root:

mkdir -p /path/to/site/public/inc
chmod 755 /path/to/site/public/inc

You can then reference internal logic via relative paths. If register.php sits in /public/register.php, you can include a file from /public/inc/db_login.php using:

require_once __DIR__ . '/../inc/db_login.php';

This approach avoids exposing sensitive files if you ever accidentally expose the wrong URL path later. It also aligns with modern practices where public_html is the entry point, and everything else is accessed via relative directory structures.

File Permissions: Read, Execute, or Both?

There is a common misconception regarding PHP script permissions. From an operating system perspective, Apache reads these files as text to send them to the PHP interpreter. It does not treat .php files as binaries that need an "execute" (x) permission bit for the web server process to function correctly.

Therefore, setting permissions to --X (execute only) is incorrect and will cause a 403 Forbidden error if the user lacks read access. Setting them to R-X is also unnecessary overhead and can be confusing. The standard, secure default is 644 (R--r--r--).

chmod 644 register.php db_login.php

This grants the owner full control (read/write), while allowing the group and others to read the file. This ensures Apache can parse the code without exposing it as a downloadable binary. If you ever stop the PHP service, the files are still readable via HTTP because they are text; changing permissions to --X would break the application entirely.

Isolating Sensitive Logic with inc

The practice of keeping database credentials and login logic in a separate file like db_login.php is sound, provided that file is never served directly through the browser. Placing this file in an /inc directory (or similar) outside the public root or within a protected subdirectory is essential.

If you place db_login.php inside /httpdocs/inc, it remains accessible via a URL unless configured otherwise. To prevent direct access to credential files:

  1. Restrict Directory Listing: Ensure .htaccess or virtual host settings disable directory listing in the /inc folder.
  2. Use File Permissions: While 644 allows reading, if you want to be extra cautious against automated scanners trying to guess file paths, you can restrict access further, but note that Apache must still be able to read it. A stricter approach often involves placing the file outside the document root entirely (e.g., /var/www/site/conf/db_login.php) and including it via an absolute path starting with /.

Summary of Best Practices

To recap, your migration should follow these steps:

  1. Structure: Place public scripts in /httpdocs and internal logic in a subdirectory like /inc within the same root or outside it.
  2. Permissions: Set all PHP files to 644. Do not use execute permissions for text-based scripts.
  3. Security: Ensure sensitive include files are not directly accessible via a URL. Disable directory listing and verify that your .htaccess rules do not inadvertently expose hidden directories.

By adhering to these standards, you maintain the security posture of separating public data from internal logic without relying on outdated directory naming conventions.

Our network