Nginx + php-fpm is extremely slow

nginxphp-fpm

I have a very low tier VPS – 1GB ram and 1 CPU w/ 30gb SSD running CentOS (v7.2.1511). The server only servers php-fpm (v5.4.16) and nginx (1.6.3). Mysql and Mongo databases are on another server.

I know that my servers have very low specs but the performance that I get for less than 1 request per second is insanely slow – it takes about 20 seconds to run a PHP script. Static content runs very fast. It is the dynamic content that takes forever.

Here are my config files:

# php-fpm.d/www.conf:

listen = /var/run/php-fpm/php-fpm.sock
pm = dynamic
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_children = 5
php_value[session.session_handler] = files

# nginx.conf

worker_processes = 1
events {
    worker_connections = 512;
}

http {
    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   65;
    types_hash_max_size 2048;

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

    include /etc/nginx/sites/*;
}

# a config for a website

server {
    listen 80;
    server_name example.com;
    index index.php;
    root /var/www/example.com;
    include conf.d/wordpress.conf;
}


# conf.d/wordpress.conf

location / {
    try_files $uri $uri/ /index.php?$args;
}

include conf.d/php.conf;

# conf.d/php.conf

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

# fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

fastcgi_param  REDIRECT_STATUS    200;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

I believe there is a problem with php-fpm, so I am still tweaking it. Can you help me figure out the problem?

Best Answer

Most of slow php pages could be traced to database issues. You might want to create a script that runs only 1 query, and print the time needed to execute the query. Please see https://stackoverflow.com/questions/17035859/how-to-find-php-execution-time for ways to record execution times. Do record execution times in parts in your script, from there we would determine whether the queries or other stuff in PHP is the culprit or whether the configuration is the problem. If the execution time recorded in PHP (for example, from start to bottom of the script) is significantly larger than the time experienced by the browser (check request times using, for example, chrome developer tools) , then you might have configuration problem. If it is the same, then we might have a performance problem, where we could dissect by timing start and stop of various parts of the script.