title: Optimizing NGINX + PHP-FPM + MariaDB for WordPress (10k hits/day)?
body: <p>I'm trying to optimize a VPN server that's running Nginx + PHP-FPM + MariaDB for a website that has a lot of traffic. </p>
  
  <p>Machine specs are:</p>
  
  <ul>
  <li>8GB RAM</li>
  <li>8 CPU cores</li>
  <li>1x30GB SSD</li>
  </ul>
  
  <p>Machine uses CENTOS 7.1</p>
  
  <p>Here's my <code>nginx.conf</code>:</p>
  
  <pre><code># Server globals
  user                    nginx;
  worker_processes        8;
  error_log               /var/log/nginx/error.log;
  pid                     /var/run/nginx.pid;
  
  
  # Worker config
  events {
          worker_connections  1024;
          use                 epoll;
  }
  
  
  http {
      # Main settings
      sendfile                        on;
      tcp_nopush                      on;
      tcp_nodelay                     on;
      client_header_timeout           1m;
      client_body_timeout             1m;
      client_header_buffer_size       2k;
      client_body_buffer_size         256k;
      client_max_body_size            256m;
      large_client_header_buffers     4   8k;
      send_timeout                    30;
      keepalive_timeout               60 60;
      reset_timedout_connection       on;
      server_tokens                   off;
      server_name_in_redirect         off;
      server_names_hash_max_size      512;
      server_names_hash_bucket_size   512;
  
  
      # Log format
      log_format  main    '$remote_addr - $remote_user [$time_local] $request '
                          '"$status" $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
      log_format  bytes   '$body_bytes_sent';
      #access_log          /var/log/nginx/access.log  main;
      access_log off;
  
  
      # Mime settings
      include             /etc/nginx/mime.types;
      default_type        application/octet-stream;
  
  
      # Compression
      gzip                on;
      gzip_comp_level     9;
      gzip_min_length     512;
      gzip_buffers        8 64k;
      gzip_types          text/plain text/css text/javascript
                          application/x-javascript application/javascript;
      gzip_proxied        any;
  
  
      # Proxy settings
      proxy_redirect      off;
      proxy_set_header    Host            $host;
      proxy_set_header    X-Real-IP       $remote_addr;
      proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass_header   Set-Cookie;
      proxy_connect_timeout   90;
      proxy_send_timeout  90;
      proxy_read_timeout  90;
      proxy_buffers       32 4k;
  
  
      # SSL PCI Compliance
      ssl_session_cache   shared:SSL:10m;
      ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      ssl_ciphers        "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
  
  
      # Error pages
      error_page          403          /error/403.html;
      error_page          404          /error/404.html;
      error_page          502 503 504  /error/50x.html;
  
  
      # Cache
      proxy_cache_path /var/cache/nginx levels=2 keys_zone=cache:10m inactive=60m max_size=512m;
      proxy_temp_path  /var/cache/nginx/temp;
      proxy_cache_key "$host$request_uri $cookie_user";
      proxy_ignore_headers Expires Cache-Control;
      proxy_cache_use_stale error timeout invalid_header http_502;
      proxy_cache_valid any 3d;
  
      map $http_cookie $no_cache {
          default 0;
          ~SESS 1;
          ~wordpress_logged_in 1;
      }
  
  
      # Wildcard include
      include             /etc/nginx/conf.d/*.conf;
  }
  </code></pre>
  
  <p>Also here's my <code>domainname.conf</code>:</p>
  
  <pre><code>[domainname.tld]
  listen = 127.0.0.1:9003
  listen.allowed_clients = 127.0.0.1
  
  user = admin
  group = admin
  
  pm = ondemand
  pm.max_children = 70
  pm.start_servers = 3
  pm.min_spare_servers = 3
  pm.max_spare_servers = 10
  
  env[HOSTNAME] = $HOSTNAME
  env[PATH] = /usr/local/bin:/usr/bin:/bin
  env[TMP] = /tmp
  env[TMPDIR] = /tmp
  env[TEMP] = /tmp
  </code></pre>
  
  <p>And here's the main <code>www.conf</code>:</p>
  
  <pre><code>[www]
  listen = 127.0.0.1:9000
  listen.allowed_clients = 127.0.0.1
  user = apache
  group = apache
  pm = dynamic
  pm.max_children = 50
  pm.start_servers = 5
  pm.min_spare_servers = 3
  pm.max_spare_servers = 35
  </code></pre>
  
  <p>And finally here's the <code>my.cnf</code>:</p>
  
  <pre><code>[mysqld]
  datadir=/var/lib/mysql
  socket=/var/lib/mysql/mysql.sock
  symbolic-links=0
  
  skip-external-locking
  key_buffer_size = 512M
  max_allowed_packet = 128M
  table_open_cache = 1024
  sort_buffer_size = 8M
  read_buffer_size = 8M
  read_rnd_buffer_size = 256M
  myisam_sort_buffer_size = 128M
  thread_cache_size = 8
  query_cache_size= 512M
  thread_concurrency = 32
  
  
  #innodb_use_native_aio = 0
  innodb_file_per_table
  
  max_connections=500
  max_user_connections=250
  wait_timeout=5
  interactive_timeout=50
  long_query_time=10
  
  #slow_query_log=1
  #slow_query_log_file=/var/log/mysql-slow-queries.log
  
  
  [mysqld_safe]
  log-error=/var/log/mariadb/mariadb.log
  pid-file=/var/run/mariadb/mariadb.pid
  
  #
  # include all files from the config directory
  #
  !includedir /etc/my.cnf.d
  </code></pre>
  
  <p>The website is working, well okay because caching is enabled (page cache), I turned off the caching for a while on heavy load and there was error 500.</p>
  
  <p>Also here's the memory usage:</p>
  
  <pre><code>#free -g
                total        used        free      shared  buff/cache   available
  Mem:              7           2           2           0           3           4
  Swap:             0           0           0
  </code></pre>
  
  <p>Any help is much appreciated, thank you.</p>