keepcalculator

06 Numbers and Powers

Lamp Application Ownership & Permissions

Card No. 06·21266 · Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered
You enter
Finish time, Distance
You get
Average pace, Average speed
Rounding
2 dp

This page explains how ownership and permissions work for files and applications in a typical LAMP stack (Linux, Apache, MySQL, PHP), and how to set them up correctly. You’ll learn what each permission means, how to avoid common pitfalls like white screens or 403 errors, and how to keep your server secure.

Formula

The underlying formula for file permissions is simple: each file or directory has an owner, a group, and three sets of permissions (read, write, execute) for the owner, the group, and everyone else. In numeric (octal) notation, read = 4, write = 2, execute = 1, and you add them together. For example, 750 means the owner has read+write+execute (7), the group has read+execute (5), and others have no permissions (0).

Worked Example

Suppose you have a directory /var/www/html and you want the owner to have full access, the group to read and execute, and others to have none. The numeric value is 750. To apply it, run sudo chmod 750 /var/www/html. To change the owner and group, use sudo chown www-data:www-data /var/www/html. This is a common setup for a PHP application served by Apache, where the web server runs as www-data.

Why Ownership and Permissions Matter

In a LAMP stack, the web server (Apache) runs as a specific user (often www-data on Debian/Ubuntu). When a PHP script needs to read a file or write to a directory, the server must have the appropriate permissions. If the owner is your personal user and the permissions are too restrictive, the server can’t access the files, leading to errors. Conversely, if permissions are too open (like 777), any user on the system can modify your files, creating a serious security risk.

How to Set Ownership and Permissions

Here’s a step-by-step approach to configuring a typical LAMP application:

  1. Identify the web server user: Run ps aux | grep apache or ps aux | grep nginx to see which user the server runs as. On Ubuntu, it’s often www-data.
  2. Set the owner and group: Use sudo chown -R www-data:www-data /var/www/html to give the server ownership of the files. For a single file, omit -R.
  3. Set directory permissions: Directories need execute permission to be traversed. A common safe choice is 755 (owner rwx, group r-x, others r-x). Use sudo find /var/www/html -type d -exec chmod 755 {} \;.
  4. Set file permissions: Files typically need only read and write for the owner, and read for others. Use 644 (sudo find /var/www/html -type f -exec chmod 644 {} \;).
  5. Adjust for writable directories: If your application needs to upload files or write cache, give the web server write access to those specific directories, e.g., sudo chmod 775 /var/www/html/storage and ensure the group is www-data.

Common Problems and Solutions

  • 403 Forbidden error: This usually means the Apache user cannot read the file or traverse the directory. Check that the directory has execute permission for the group or others, and that files are readable.
  • White screen of death (PHP error): Often caused by a file that isn’t readable by the server. Check the file’s permissions and owner.
  • Cannot write to upload directory: The web server user needs write permission on that directory. Use sudo chown www-data:www-data uploads && sudo chmod 775 uploads.
  • Permission denied when using SSH: If you can’t edit a file via SFTP, your user isn’t the owner. Either change the owner to your user or use sudo.

Security Best Practices

Never use 777 permissions on any file or directory in a production environment. This allows any user on the system to modify your code, which could lead to a full compromise. Instead, use the principle of least privilege: give the web server only the permissions it needs. For example, if your app only needs to read files, use 644 for files and 755 for directories. If you need to write, restrict write access to the specific directories that require it, and never to the entire web root.

Alternatives and Advanced Topics

For more complex setups, you might use Access Control Lists (ACLs) to grant finer-grained permissions, or you might run PHP-FPM with a different user per site (using php-fpm pool configurations). Some developers prefer to use setfacl to give the web server write access to a directory without changing the owner. For example, sudo setfacl -m u:www-data:rwx /var/www/html/cache. This is useful when you want to keep your own user as the owner but still allow the server to write.

Frequently Asked Questions

What is the difference between chown and chmod?

chown changes the owner and group of a file or directory, while chmod changes the permissions (read, write, execute) for the owner, group, and others. Both are needed to fully control access.

Why does my PHP script get a “Permission denied� error when trying to write to a file?

This means the user running PHP (usually www-data) does not have write permission on the file or directory. Check the owner and permissions, and either change the owner to www-data or add write permission for the group if the group is www-data.

Is it safe to use 777 permissions?

No. 777 gives write and execute permissions to every user on the system. This is a common cause of security breaches. Use the most restrictive permissions that still allow your application to function.

How do I find out which user Apache is running as?

Run ps aux | grep apache or ps aux | grep httpd. The first column shows the username. On Ubuntu, it’s usually www-data. You can also run apachectl -S to see the user and group directives in the config.

What does 755 mean in simple terms?

It means the owner can read, write, and execute; the group can read and execute; and everyone else can read and execute. For directories, execute means you can enter the directory. For files, execute means you can run the file as a program.

Can I set permissions for a single file without affecting others?

Yes, use chmod on the specific file path, e.g., sudo chmod 640 /var/www/html/config.php. This is useful for sensitive files like configuration files that should not be readable by others.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0 | 100000
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →