Client Login

Contact Us

  • Sales
  • Support
  • Abuse

Close Support


Optimizing PHP / Mysql / Nginx on VPS

Optimizing PHP / Mysql / Nginx on VPS
  • 19

Optimizing PHP / Mysql / Nginx on VPS

How to ensure a higher performance of VPS server that runs on Nginx + PHP + Mysql?

This article provides a checklist of the main settings that will greatly optimize the server. The configuration will take no more than 10 minutes and does not require anything other than editing the configuration files.
Examples of settings are given for the operating system Debian 7 and VPS server with 1 processor and 512 MB of RAM.


Settings are performed in the /etc/nginx/nginx.conf file, as well as in the virtual host settings (usually in the / etc / nginx / sites-enabled folder)

Number of workers

The number of nginx workers must match the number of CPU cores:

worker_processes  1;
Cache-Control headers

Setting Cache-Control headers will significantly relieve your server from repeated calls to files that do not change (or change rarely, for example, css / js / jpg / png / gif):

location ~* \.(css|js|png|gif|jpg)$ {
    expires max;
Access log

Extra disk operations because of the recording of logs we do not need, turn off:

access_log off;
Unix sockets

We include unix sockets for working with PHP:

location ~ \.php$ {
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	fastcgi_pass unix:/var/run/php5-fpm.sock; # also need to configure php-fpm, see below
	fastcgi_index index.php;
	include fastcgi_params;


The settings are made in the configuration file fpm php-fpm.conf, which in our case is located here /etc/php5/fpm/pool.d/www.conf.

Unix sockets

Convinced that php-fpm works with unix sockets, and not with tcp:

listen = /var/run/php5-fpm.sock

Install the APC extension – the internal PHP cache, which will significantly save resources to the PHP parser:

apt-get install php-apc

Configuring MySQL

All MySQL settings are executed in the file my.cnf, which is usually located here /etc/my.cnf.


If you use only MyISAM tables, set this value to 30% … 40% of all available RAM on the server:

key_buffer = 128M

If you use only InnoDB tables, set this value to the maximum possible (80% of available memory). In our case, we set:

innodb_buffer_pool_size = 350M

Attention, you can set this value only by significantly reducing the ” ” key_buffer ” ‘. Between these two settings you need to make a choice, which depends on the type of tables used (MyISAM or InnoDB).


A significant acceleration of writing for innoDB tables can be achieved by setting this parameter to 0, when the write buffer will be flushed to disk not after each operation, but once per second:

innodb_flush_log_at_trx_commit = 0

Setting this option to O_DIRECT avoids double caching (it turns off the operating cache for MySQL data files):

innodb_flush_method = O_DIRECT

This option determines the size of the cache for the created threads. It is selected experimentally, but it is better to increase the starting value to 16:

thread_cache_size = 16

Enable the internal MySQL cache:

query_cache_size = 16М

The value should be increased as necessary. Do not forget that the cache stops working efficiently on tables that are frequently updated.

As a summary – a short list with the most important settings highlighted:

  • Nginx
    • worker_processes
    • expires max for statics
    • access_log off
    • unix-сокеты
  • PHP
    • APC module
  • MySQL
    • key_buffer
    • innodb_buffer_pool_size
    • innodb_flush_log_at_trx_commit = 0
    • query_cache_size
    • innodb_flush_method = O_DIRECT
    • thread_cache_size

Comments are closed.