Skip to main content

Configuration

PHP.ini Configuration

Production Settings

ini

; Basic Settings
memory_limit = 512M
max_execution_time = 300
max_input_time = 300
post_max_size = 50M
upload_max_filesize = 50M
; Error Handling
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/php/error.log
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
; Security
expose_php = Off
allow_url_fopen = Off
allow_url_include = Off
session.cookie_httponly = On
session.cookie_secure = On
session.use_strict_mode = On
; Performance
opcache.enable = 1
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.validate_timestamps = 0
opcache.save_comments = 0
opcache.fast_shutdown = 1

Development Settings

ini

; Debug Settings
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
; Development Tools
xdebug.mode = debug,develop
xdebug.start_with_request = yes
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
; Performance (Relaxed)
opcache.validate_timestamps = 1
opcache.revalidate_freq = 2

Environment Configuration

.env File Structure

env

# Application
APP_NAME="My PHP Application"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://myapp.com
# Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp
DB_USERNAME=dbuser
DB_PASSWORD=secure_password
# Cache
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
# Mail
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
# Logging
LOG_CHANNEL=stack
LOG_LEVEL=error

Configuration Management

php

<?php
class Config
{
private static array $config = [];
public static function load(string $path): void
{
if (file_exists($path)) {
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value, " \t\n\r\0\x0B\"'");
$_ENV[$name] = $value;
putenv(sprintf('%s=%s', $name, $value));
}
}
}
public static function get(string $key, $default = null)
{
return $_ENV[$key] ?? $default;
}
}