Report this

What is the reason for this report?

LEMP Stack and SSL with prestashop => TTFB > 8Secs

Posted on March 26, 2015
Freelance

By Freelance

WordPress Montréal Développeur

Hello community :)

I hosted on VPS with digitalocean, in Singapour (we are located in Australia). I use Prestashop software for my store. I installed nginx, php-fpm and mysql (LEMP Stack) by myself.

But I have like 8 sec for any page in prestashop to load this page (8sec only for the Time To First Byte)

nginx.conf :

user  www-data;
worker_processes  auto;
worker_priority        15;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 20;
    client_max_body_size 15m;
    client_body_timeout 60;
    client_header_timeout 60;
    client_body_buffer_size  10K;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    send_timeout 60;
    reset_timedout_connection on;
    types_hash_max_size 2048;
    server_tokens off;


	gzip on;
    gzip_static on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 2;
    gzip_min_length 512;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/css text/javascript text/xml text/plain text/x-component 
    application/javascript application/x-javascript application/json 
    application/xml  application/rss+xml font/truetype application/x-font-ttf 
    font/opentype application/vnd.ms-fontobject image/svg+xml;

# Disable for IE < 6 because there are some known problems

 #fastcache

fastcgi_cache_path /etc/nginx-cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";


	include /etc/nginx/blacklist.conf;
    include /etc/nginx/sites-enabled/*;
}

My vhost config :

server {

server_name sub.domain.com;
# Normal
listen 80;
return 301 https://$server_name$request_uri;
}


# SSL
server {

listen 443 spdy;

# Error / Access log
error_log /var/log/nginx/domain.com_error.log;
access_log /var/log/nginx/domain.com_access.log;

#ssl :
include '/etc/nginx/conf.d/ssl.conf';

# Server settings :
server_name sub.domain.com;
root /home/usersys/sites/domain.com;
index index.html index.htm index.php;
proxy_buffers 16 16k;
proxy_buffer_size 32k;

# Location php :
include '/etc/nginx/conf.d/location-php.conf';

# Cache static :
include '/etc/nginx/conf.d/cache_static.conf';

# Rewrite rule presta :
include /etc/nginx/conf.d/ps_rewrite_rules.conf;
}

ssl.conf file :

ssl on;
ssl_certificate /etc/ssl/www.domain.com.crt;
ssl_certificate_key /etc/ssl/www.domain.com.key;


ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:32m;
ssl_buffer_size 4k;
ssl_session_timeout 10m;

client_max_body_size 15m;
client_body_buffer_size 128k;

ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/ssl/trusted.crt;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;

ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;

add_header Strict-Transport-Security "max-age=31536000";
add_header X-Content-Type-Options nosniff;

location-php.conf file :

location ~ \.php$ {

try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param HTTPS on;
fastcgi_read_timeout 300;
fastcgi_buffers 256 16k;
fastcgi_buffer_size 32k;
fastcgi_cache phpcache; # The name of the cache key-zone to use
fastcgi_cache_valid 200 30m; # What to cache: 'code 200' responses, for half an hour
fastcgi_cache_methods GET HEAD; # What to cache: only GET and HEAD requests (ot POST)
add_header X-Fastcgi-Cache $upstream_cache_status; # Allow us to see if the cache was HIT, MISS, or BYPASSED inside a browser's Inspector panel
fastcgi_cache_bypass $no_cache; # Dont pull from the cache if true
fastcgi_no_cache $no_cache; # Dont save to the cache if true


include fastcgi_params;

}

cache_static.conf :

# Expire rules for static content

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
# access_log logs/static.log; # I don't usually include a static log
}

# Feed
location ~* \.(?:rss|atom)$ {
expires 1h;
add_header Cache-Control "public";
}

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|woff|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}

ps_rewrite_rules.conf :

rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/order$ /index.php?controller=order last;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php last;
}

Thanks a lot :)



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Even for one image I got a TTFB due 2 secs

Screenshot

To improve the TTFB and overall website performance, consider the following steps:

  1. Optimize Database Queries:

    • Use an optimization tool like phpMyAdmin or Adminer to review and optimize your database tables.
    • Consider using a caching mechanism like Memcached or Redis to reduce database queries.
    • Make sure your database server has enough resources (RAM, CPU) to handle the workload.
  2. PHP-FPM Configuration:

    • Review and optimize your PHP-FPM configuration (typically found in the /etc/php/{version}/fpm/pool.d/ directory) to ensure it’s tuned for your server’s resources.
    • Adjust the pm (process manager) settings, such as pm.max_children, pm.start_servers, and pm.min_spare_servers, to match your server’s capacity.
    • Monitor PHP-FPM logs for any errors or performance bottlenecks.
  3. Server Resources:

    • Ensure your server has sufficient CPU, RAM, and disk space to handle the traffic and resource requirements of your PrestaShop website.
    • Consider upgrading your server or adding more resources if necessary.
  4. Nginx Configuration:

    • Optimize your Nginx configuration to better serve static files and cache content if applicable.
    • Use the expires directive to set cache expiration headers for static assets.
    • Implement browser caching and leverage content delivery networks (CDNs) to serve static content.
  5. Content Compression:

    • Enable gzip compression for text-based content (HTML, CSS, JavaScript) to reduce the size of data sent to clients.
  6. Minimize HTTP Requests:

    • Reduce the number of HTTP requests by combining CSS and JavaScript files and using image sprites.
    • Use asynchronous loading for non-essential scripts to prevent blocking.
  7. Content Delivery Network (CDN):

    • Consider using a CDN to distribute static content to geographically distributed servers, reducing server load and improving load times.
  8. Optimize Images:

    • Compress and optimize images to reduce their file size. You can use tools like ImageMagick or online services like TinyPNG.
  9. Browser Caching:

    • Implement browser caching headers to instruct clients to store static assets locally, reducing the need for repeated downloads.
  10. Reduce Third-Party Requests:

-   Minimize the use of external scripts and resources from third-party domains as they can slow down your website.
  1. Content Preloading:
-   Implement preloading for essential assets using the `rel="preload"` attribute in HTML. This can improve the perceived loading speed of your website.
  1. Content Delivery Optimization:
-   Use Content Delivery Optimization (CDO) techniques like HTTP/2 and HTTP/3 to enhance the speed of loading resources.
  1. Caching:
-   Implement full-page caching or object caching if applicable and appropriate for your website. Tools like Varnish or Redis can be used for this purpose.
  1. Monitor and Diagnose:
-   Use tools like New Relic, Blackfire, or your server's performance monitoring utilities to diagnose bottlenecks and identify performance issues.
  1. SSL/TLS Optimization:
-   Ensure your SSL/TLS configuration is optimized. Use modern TLS protocols and ciphers to minimize the SSL handshake time.
-   Consider using a Let's Encrypt certificate, which is free and easy to configure.
  1. Content Management System (CMS) Optimization:
-   Review and optimize your PrestaShop theme and extensions. Remove any unnecessary or resource-intensive modules.
  1. Server and Software Updates:
-   Keep your server's operating system, Nginx, MySQL, PHP, and PrestaShop up to date with the latest security patches and updates.

By following these optimization steps and regularly monitoring your website’s performance, you should be able to significantly reduce the Time To First Byte (TTFB) and improve the overall loading speed of your PrestaShop website on a LEMP stack with SSL/TLS enabled.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.