# Fixing the '500 Internal Server Error' message while running PHP

> Fix a 500 Internal Server Error in PHP by correcting file permissions, ownership, .htaccess directives and memory limits.

Source: https://helpcenter.ruachost.com/developers/php/fixing-the-500-internal-server-error-message-while-running-php/

A **500 Internal Server Error** indicates that something went wrong on the server while executing a PHP script. On **ruachost.com**, this error is often caused by incorrect file permissions or invalid directives in `.htaccess`.

## Common Causes

### 1. File and Directory Permissions

- PHP scripts may fail if permissions are set incorrectly.
- Typical errors include:

  - `SoftException in Application.cpp: UID of script is smaller than min_uid`
  - `Mismatch between target UID and UID of file`
  - `File is writeable by others`

### Fix

- Directories should be set to `755`.
- Files should be set to `644`.
- Example commands:

  ```
  cd public_html
  find . -type d -exec chmod -c 755 {} \;
  find . -type f -exec chmod -c 644 {} \;
  ```
- Do not change permissions on the `public_html` directory itself.

### 2. Invalid .htaccess Directives

- Servers running PHP as a CGI binary cannot use `php_flag` or `php_value` directives in `.htaccess`.
- Example error:

  ```
  Invalid command 'php_flag', perhaps misspelled or defined by a module not included in the server configuration
  ```

### Fix

- Move PHP directives into a custom `php.ini` file.
- Remove or comment out invalid directives in `.htaccess`.

### 3. Ownership Issues

- Files owned by `root` or `nobody` instead of your user account can trigger errors.
- **Fix:** Contact Ruachost Support to correct file ownership if needed.

### 4. Memory Limits

- Scripts may exceed PHP memory limits.
- **Fix:** Increase `memory_limit` in `php.ini` or `.user.ini`.

  ```
  memory_limit = 256M
  ```

## Troubleshooting Steps

1. **Check error logs**:

   - Shared hosting: view logs in **cPanel → Metrics → Errors**.
   - VPS/Dedicated: check `/usr/local/apache/logs/error_log` or `/usr/local/apache/logs/suphp_log`.
2. **Correct permissions** using `755` for directories and `644` for files.
3. **Review** `.htaccess` for invalid PHP directives.
4. **Adjust memory limits** if scripts are resource‑intensive.
5. **Contact support** if ownership issues persist.
