Why Laravel Performance Optimization Matters
We know how important it is to keep your website fast and reliable. A slow Laravel application can affect user experience, impact your search engine rankings, and even hurt your business. That’s why optimizing Laravel application performance is a key part of delivering high-quality Laravel Web App Development Services, ensuring a smooth user experience and handling more visitors as your site grows.
But how do you ensure your Laravel application runs optimally? Laravel performance best practices go beyond adding new features or writing better code. It's about tuning various settings of your app, from server configurations to caching strategies and database queries.
In this blog, we'll walk you through some of the best practices we follow at our organization for Laravel application optimization. We’ll explore how we can configure PHP settings to leverage Laravel’s caching capabilities without compromising functionality, helping you optimize Laravel for production.
Tuning PHP Configuration for Laravel Applications
Optimising PHP configuration is one of the most effective steps in enhancing Laravel application performance. We've seen 30-40% speed improvements just by adjusting a few settings in their php.ini file.
Memory Limit
This parameter in php.ini controls how much memory PHP scripts can use. Laravel needs more memory than normal websites, specially when working with large datasets or complex operations. A good higher memory limit allows scripts to run without getting memory limits.
Our recommended settings:
#For most Laravel applications
memory_limit = 256M
#For apps handling large data or file processing
memory_limit = 512M
#For enterprise applications with heavy processing
memory limit = 1024M
Max Execution Time:
Operations like database migrations and report generation can exceed PHP's default execution limits. The max_execution_time directive prevents script termination during these essential processes. Without proper configuration, critical operations may fail midway through execution which may cause data inconsistencies.
Our recommended settings:
#For most applications
max_execution_time = 300
#For apps with heavy data processing
max_execution_time = 600
Upload Limits
If users upload files to your Laravel app, these settings control what they can actually upload.
These settings sets maximum file sizes and total POST data your server will accept. Depending on your application’s requirements and features, you can increase these values.
Our recommended settings:
upload_max_filesize = 50M
post_max_size = 50M
Max Input Vars
Laravel applications often have forms with lots of fields, especially admin panels and data entry screens. So, the max_input_vars parameter limits how many forms fields PHP will process at once.
Our recommended settings:
Increasing this limit makes sure that large forms or API requests with many parameters are processed correctly without hitting PHP’s input variable limits.
Session Configuration: Manage Session Data
Laravel applications depend on PHP's session handling for user authentication and state management. Proper session configuration directly impacts application security and user experience across your platform. The session.gc_maxlifetime directive controls session data retention on the server. This parameter determines how long user sessions remain active during periods of inactivity,
Our recommended setting:
session.gc_maxlifetime = 1440
This setting creates a 24-minute session timeout period. When users remain inactive beyond this threshold, the system automatically invalidates their session data. Users must then re-authenticate to regain access to protected areas of the application.
What is OPCache and Why It’s Important
If you're running a Laravel application and haven't properly configured OPCache yet, you're leaving significant performance improvements. In our experience working with various Laravel deployments, OPCache consistently delivers great performance improvements you can achieve with minimal effort.
Understanding OPCache and its impact
Each time a user opens your Laravel app, PHP starts from scratch, it reads through your code, translates it into executable instructions (opcodes), and then runs them. Without OPCache, this cycle repeats on every request, even even when the code hasn’t changed in days. That’s a lot of wasted processing for something that could be skipped entirely. By enabling OPCache, you let PHP keep those compiled instructions in memory, so it can jump straight to running the code which enabls for less overhead for the server and faster pages load for your users
OPCache eliminates this redundant work by storing the compiled opcodes in memory. Once your PHP files are compiled the first time, subsequent requests skip the parsing and compilation steps entirely, by taking the ready-to-execute opcodes directly from memory.
The performance difference is substantial. We’ve seen Laravel application optimization yield 2-3x performance improvements with proper OPCache tuning.
How to Enable OPCache in PHP
Locating the Active php.ini File:
PHP loads different configuration files based on how it executes on your server. The same system may use separate php.ini files for command line operations, Apache modules, and PHP-FPM processes. Finding the correct configuration file prevents configuration errors and ensures your OPCache settings take effect properly.
Quick tip: Run php --ini from your command line to see exactly which configuration file PHP is using.
Enable OPCache:
Find the opcache.enable directive in your php.ini file and set it to 1 to enable OPCache:
After making the change, restart the PHP service for the change to take effect:
For apache:
sudo systemctl restart apache2
For Php-fpm :
sudo systemctl restart php7.x-fpm
Recommended OPCache Settings for Laravel
Now comes the important part - getting the settings right for your Laravel application.
opcache.memory_consumption
Recommended Setting:
#For small Laravel apps (under 100 files)
opcache.memory_consumption = 128
#For most Laravel applications
opcache.memory_consumption = 512
#For large enterprise applications
opcache.memory_consumption = 1024
opcache.max_accelerated_files
This sets how many PHP files OPCache will cache at once.
Recommended Setting:
opcache.max_accelerated_files = 10000
A normal Laravel application typically contains 3,000 to 5,000 files when including vendor packages and framework dependencies. Setting this limit to 10,000 provides enough capacity for application growth
opcache.interned_strings_buffer
This setting caches common text strings your app uses repeatedly.
Recommended Setting:
#For most applications
opcache.interned_strings_buffer = 16
#For larger applications with lots of text processing
opcache.interned_strings_buffer = 32
If your application works with a lot of strings, increasing this limit can also give you a nice performance boost.
Production vs Development OPCache Configuration
The validate_timestamps directive controls whether OPCache verifies file modification times before serving cached content. The revalidate_freq setting defines the checking interval in seconds. These parameters directly impact how OPCache handles file changes during deployment processes.
Recommended Setting for Production (Maximum Speed):
opcache.validate_timestamps = 0
opcache.revalidate_freq = 0
What this does: OPCache never checks if your files have changed, giving maximum performance. When you deploy new code, you'll need to restart PHP to clear the cache.
Recommended setting for development:
opcache.validate_timestamps = 1
opcache.revalidate_freq = 2
OPCache checks for file changes every 2 seconds, so you see your code changes without restarting anything.
opcache.enable_cli
This setting controls whether OPCache is enabled for PHP CLI (Command Line Interface) scripts. By default, OPCache is disabled for CLI scripts to prevent unnecessary caching of scripts that are only executed once. However, enabling it can improve performance when running repetitive CLI tasks
Recommended Setting:
#For enabling OPCache in CLI scripts:
opcache.enable_cli = 1
#For disabling OPCache in CLI scripts (default):
opcache.enable_cli = 0
Improving Performance by Caching Routes, Views, and Configurations
Laravel comes with some useful caching features that most developers don’t use. These features are built directly into the framework which can speed up your application with just a few commands.
These features include Laravel’s built-in caching functionalities for routes, views, and configurations, so that it reduces the need to recompile and reprocess these elements on every request.
Route caching
Laravel applications often involve multiple routes. Usually, Laravel loads these routes on every request, which can introduce performance lags. So, Laravel provides a route caching feature that compiles all your routes into a single cached file.
To enable route caching:
Route caching feature should only be used in production environments since in such environments, routes do not change frequently. But during development, it's better to disable it so we can add, modify, or delete routes without clearing the cache.
View caching
Laravel views are usually rendered every time they’re requested especially for applications with complex Blade templates, this can result in additional processing time. Laravel provides a mechanism for view caching to store precompiled views in memory, reducing the overhead of rendering again the views on every request.
To enable route caching:
php artisan view:clear
Configuration Caching
Laravel also allows you to cache your application’s configuration files, which helps eliminate the overhead of reading configuration values from files on each request
To enable configuration caching:
Note:
Cache invalidation becomes necessary when modifying routes, views, or configuration files. The cached versions must be cleared using Laravel's artisan commands to reflect the updated changes in your application.
php artisan route:clear
php artisan view:clear
php artisan config:clear
Optimizing Apache Timeout Settings for Laravel
Apache's Timeout directive defines how long the server maintains connections before terminating unresponsive requests. This setting affects various scenarios: client request delays, backend server communication, and transaction completion timeouts.
Proper configuration ensures smoother operations for long-running tasks like file uploads or database-intensive queries, which is essential when optimizing Laravel for production. However, excessive timeout settings can lead to resource exhaustion when multiple slow requests accumulate simultaneously.
Recommended Setting:
Conclusion:
Laravel optimization involves multiple components working together, PHP settings, OPCache configuration, and built-in caching features all which contribute to better application performance.
Performance tuning requires continuous attention as applications grow. Different application sizes have unique performance requirements, and configurations that work well initially often need modification as your application expands.
Running these optimizations through your staging setup first prevents production problems. Every Laravel application behaves differently depending on its architecture and usage patterns, making post-implementation monitoring essential to verify that your changes improve performance as intended.
At Surekha Technologies, our team specializes in Laravel application optimization. We help businesses implement these performance strategies effectively, ensuring your application runs smoothly while maintaining security and stability standards.