Comprehensive Guide for Ubuntu 20.04/24.04 with Nginx
Ubuntu 20.04/24.04 was released with PHP 8.3 as the default PHP version available in the Ubuntu repositories. This guide covers the essential steps to harden and optimize PHP 8.3 for production environments, particularly for WordPress hosting with Nginx.
Standard PHP 8.3 support ends (php.net)
Ubuntu/Ondřej repository support ends (5-year support cycle)
Extended support with Ubuntu Pro (free tier available)
↑ 50%
Performance improvement over PHP 7.4
↑ 15%
Performance improvement over PHP 8.1 and 8.2
/etc/php/8.3/fpm/conf.d/ directory. This approach makes it easier to manage
updates and quickly identify custom configurations.
| Method | Scope | Use Case | Can Be Overridden |
|---|---|---|---|
| conf.d/*.ini | Server-wide | Default settings for all sites | Yes |
| PHP-FPM Pool | Per-site | Site-specific configurations | Depends on directive |
| php_admin_value | Per-site (pool) | Locked site-specific settings | No |
| .user.ini | Per-site (document root) | User-level overrides | Yes |
The php_admin_value directive is used within PHP-FPM pool configurations and provides the
highest level of security for configuration settings. When used, the specified setting:
Hardening involves removing dangerous default settings applied during installation. The following directives must be configured to enhance security:
Purpose: Prevents PHP from including files from external sources (URLs). This setting blocks potential security vulnerabilities where attackers could include malicious code from remote servers.
allow_url_fopen to be enabled.
Always check plugin documentation and test thoroughly. If needed, enable this on a per-site basis using
PHP-FPM pools.
Purpose: Prevents PHP from attempting to execute parts of the URL path if the requested file is not found. This blocks attackers from exploiting path manipulation to execute arbitrary code.
Purpose: Prevents PHP from sending version information in HTTP response headers. This reduces information leakage that attackers could use for reconnaissance.
↓ Apply Hardening ↓
PHP functions are reusable code blocks that perform specific tasks. However, certain functions pose security risks if misused or if they enable harmful behavior. These "dangerous" functions often involve:
| Function | Risk | Attack Vector |
|---|---|---|
| exec() | Command Injection | Executes shell commands directly |
| system() | Command Injection | Executes shell commands and outputs result |
| passthru() | Command Injection | Executes commands and passes raw output |
| shell_exec() | Command Injection | Executes commands via shell |
| eval() | Code Injection | Executes arbitrary PHP code |
| assert() | Code Injection | Can execute arbitrary code in older PHP versions |
exec() or system() without proper sanitization, attackers can inject malicious
commands that execute on the server.
Blocking certain PHP functions can impact WordPress plugins that rely on those functions for their features. When troubleshooting plugin issues, the Nginx error log is your primary diagnostic tool.
.ini extension. Use
descriptive names like server_override.ini, 99-custom.ini, or
security-hardening.ini.
In nano editor:
Ctrl + O to saveEnter to confirm filenameCtrl + X to exitAlways test configuration changes on a development server before applying them to production. This helps identify compatibility issues without affecting live sites.
Document all custom configurations and the reasons for each change. This makes troubleshooting easier and helps team members understand the setup.
Before making configuration changes, create backups of existing configuration files. This allows quick rollback if issues arise.
Apply hardening measures gradually, testing after each change. This approach makes it easier to identify which setting causes issues if they occur.
Regularly check error logs after implementing changes. This helps catch issues early and understand how applications interact with new settings.
Use PHP-FPM pools to isolate different sites. This provides security boundaries and allows site-specific configurations without affecting others.
Now that we have hardened the PHP configuration, the next phase involves optimizing PHP performance. The optimization section will cover:
| Topic | Key Takeaway |
|---|---|
| PHP 8.3 Support | Extended support until 2029 (Ubuntu/Ondřej) or 2034 (Ubuntu Pro) |
| Performance | Up to 50% faster than PHP 7.4, 15% faster than PHP 8.1/8.2 |
| Configuration Method | Use override files in conf.d directory instead of modifying php.ini |
| Critical Directives | allow_url_fopen = Off, cgi.fix_pathinfo = 0, expose_php = Off |
| Dangerous Functions | Will be disabled at site level using PHP-FPM pools |
| Service Management | Always reload PHP-FPM after configuration changes |