System-Level Optimization
sudo apt update && sudo apt upgrade to apply the latest performance patches and security fixes, ensuring your Debian system is up-to-date./etc/sysctl.conf to optimize network and memory performance. Key parameters include:
net.core.somaxconn = 65535 (max TCP connections backlog)net.ipv4.tcp_tw_reuse = 1 (reuse TIME-WAIT sockets)vm.swappiness = 10 (reduce swap usage)sudo sysctl -p.Web Server Optimization (Nginx)
/etc/nginx/nginx.conf, set worker_processes auto (matches CPU cores) and worker_connections 1024 (per-worker max connections). Use epoll event model (events { use epoll; }) for efficient I/O handling.gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript;
gzip_comp_level 6; # Balance between speed and compression
```.
keepalive_timeout 65; (keep connections alive for 65 seconds) and keepalive_requests 100000 (max requests per connection) to reduce TCP handshake overhead.location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
For dynamic content, use a reverse proxy (e.g., Nginx upstream) or a caching layer like Varnish.listen 443 ssl http2;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_prefer_server_ciphers on;
```.
Web Server Optimization (Apache)
prefork (process-based) with event (thread-based) in /etc/apache2/mods-enabled/mpm_event.conf:<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
Disable unused modules (e.g., mod_status, mod_info) to reduce memory usage./etc/apache2/mods-enabled/cache_disk.conf:<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
```.
.htaccess files (in virtual host configs) to improve performance:<Directory /var/www/html>
AllowOverride None
</Directory>
```.
Database Optimization (MySQL/MariaDB)
/etc/mysql/mysql.conf.d/mysqld.cnf, set innodb_buffer_pool_size to 70-80% of available RAM (e.g., innodb_buffer_pool_size = 1G for 1.5GB RAM) to cache table data.mysqlslowquery.log (enable with slow_query_log = 1 and long_query_time = 2) to identify and optimize slow queries. Add indexes to frequently queried columns.max_connections based on your workload (e.g., max_connections = 50) and use pm.max_children in PHP-FPM to match (see PHP optimization below).PHP Optimization
/etc/php/7.x/fpm/php.ini (adjust version as needed), enable OPcache to cache compiled PHP scripts:opcache.enable = 1
opcache.memory_consumption = 256
opcache.max_accelerated_files = 10000
opcache.interned_strings_buffer = 64
opcache.fast_shutdown = 1
Restart PHP-FPM (sudo systemctl restart php7.x-fpm) after changes./etc/php/7.x/fpm/pool.d/www.conf, adjust:
pm = dynamic (better resource management)pm.max_children = 50 (max child processes)pm.start_servers = 5 (startup servers)pm.min_spare_servers = 5 (min idle servers)pm.max_spare_servers = 35 (max idle servers).Monitoring and Maintenance
htop (process monitoring), netdata (real-time metrics), or Prometheus + Grafana (advanced dashboards) to track CPU, memory, disk I/O, and network usage.tail -f /var/log/nginx/access.log) and MySQL slow query logs to identify bottlenecks (e.g., high 404 errors, slow queries).sudo apt autoremove), clear package caches (sudo apt clean), and delete old log files (logrotate or manual deletion) to free up disk space.