Support Online
Skip to main content

Performance Optimization After Laravel LEMP Installation

💡 What You Will Learn in This Guide

In this guide, after installing your Laravel application on Ubuntu 22.04, we will examine step by step the performance, security and cache settings you can apply to make it suitable for the production environment.


⚙️ 1. Put Laravel Application in Production Mode

Change the environment settings in the .env file:

nano /var/www/gezilistesi/.env

Update the following lines:

APP_ENV=production
APP_DEBUG=false
APP_URL=https://ornek.com

This setting is important to avoid revealing error details to the user and to increase security.


⚡ 2. Run Laravel Cache and Optimize Commands

Laravel can improve performance by caching configuration, route and view files in memory.

Run all optimization commands:

php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize

These commands significantly reduce application loading time.


🔐 3. Secure Files and Permissions

Remove unnecessary permissions on the Laravel directory and grant write permissions only to required folders:

sudo chown -R www-data:www-data /var/www/gezilistesi/storage
sudo chown -R www-data:www-data /var/www/gezilistesi/bootstrap/cache
sudo chmod -R 755 /var/www/gezilistesi
sudo chmod -R 775 /var/www/gezilistesi/storage /var/www/gezilistesi/bootstrap/cache

This setting allows Laravel to perform log and cache operations and protects other files.


🌐 4. Enable GZIP and Cache Settings for Nginx

Open your Nginx configuration file:

sudo nano /etc/nginx/sites-available/gezilistesi

Add the following lines inside the server block:

# GZIP sıkıştırma
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss;
gzip_min_length 1000;

# Statik dosyalar için tarayıcı önbellekleme
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
access_log off;
}

Test and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

In this way, CSS, JS and images are cached in the browser, speeding up page loading time.


🧩 5. Optimize PHP-FPM Settings

Edit the PHP-FPM configuration file:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Optimize the following values:

pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 10

Restart the PHP-FPM service after changes:

sudo systemctl restart php8.1-fpm

These settings allow for more efficient handling of PHP requests under heavy traffic.


🧱 6. Improve Database Performance

Edit the MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or update the following values:

query_cache_type = 1
query_cache_limit = 1M
query_cache_size = 64M
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M

Restart the MySQL service:

sudo systemctl restart mysql

These settings enable query caching and increase efficiency when working with large tables.


🛡️ 7. Add SSL (HTTPS) Support

Set up HTTPS with Let's Encrypt certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d ornek.com

Certificates are automatically renewed every 90 days, ensuring a secure connection.


📊 8. Optimize Laravel Log Management

Enable log rotation to prevent log files from growing too large:

sudo nano /etc/logrotate.d/laravel

Add content:

/var/www/gezilistesi/storage/logs/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 www-data www-data
}

This structure returns log files every day and prevents unnecessary disk space usage on the server.


🧠 9. Change Cache Driver (Redis or Memcached)

Redis is recommended for a faster cache system:

sudo apt install redis-server -y

Make the following change to your .env file:

CACHE_DRIVER=redis
SESSION_DRIVER=redis

Redis is 5-10 times faster than file-based cache.


🏁 Result

Congratulations! Now your Laravel application is not only installed, but also production-level optimized 🎯 Thanks to Nginx caching, PHP-FPM settings, SSL and Redis integration, you will get stable performance even under high traffic.

💡 You can immediately implement these optimizations in your own project or try them instantly using fully optimized Laravel servers on GenixNode. ☁️