NGINX
How to achieve optimal NGiNX configuration:
- gzip static assets (jpg, css, javascript)
- worker_processes = number of CPU’s
- try_files not if
- expires
PHP-FPM
- Process management
- Dynamic process spawning
- Auto recovery if crash
- Easy to integrate with NGiNX
- Use Unix sockets (not TCP/IP) achieve optimal and secure PHP-FPM setup
NGINX Configuratrion
This NGINX configuration guide and setup is optimised for Ubuntu 10.04 or later releases
Stable – nginx-1.0.5 (http://nginx.org/download/nginx-1.0.5.tar.gz)
Linux Kernel 3.0 builds, use dev version 1.1.1 (http://nginx.org/download/nginx-1.1.1.tar.gz)
| ./configure \ –prefix=/opt/nginx \ –conf-path=/etc/nginx/nginx.conf \ –pid-path=/var/run/nginx.pid \ –lock-path=/var/lock/nginx.lock \ –http-log-path=/var/log/nginx/access.log \ –error-log-path=/var/log/nginx/error.log \ –http-client-body-temp-path=/var/lib/nginx/body \ –http-proxy-temp-path=/var/lib/nginx/proxy \ –http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ –http-uwsgi-temp-path=/var/lib/nginx/uwsgi \ –http-scgi-temp-path=/var/lib/nginx/scgi \ –with-http_ssl_module \ –with-http_gzip_static_module \ –user=www-data \ –group=www-data \ –without-mail_pop3_module \ –without-mail_imap_module \ –without-mail_smtp_module |
NGiNX startup script (e.g. /etc/init.d/nginx)
Download http://nginx-init-ubuntu.googlecode.com/files/nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
Change:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/nginx/sbin
DAEMON=/opt/nginx/sbin/nginx
lockfile=/var/lock/nginx.lock
NGINX_CONF_FILE=”/etc/nginx/nginx.conf”
Basic NGiNX WordPress MS config (e.g. /etc/nginx/sites-enabled/sitename.com)
| server { listen 80; server_name sitename.com *.sitename.com; error_log /var/log/nginx/sitename.com/error.log; access_log /var/log/nginx/sitename.com/access.log; root /var/www/sitename.com/; index index.php;location / { try_files $uri $uri/ /index.php?$args; }# Expires for static assets location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires 24h; log_not_found off; } # Rewrite multisite upload files location ~ \.php$ { |
Main NGiNX Configuration (e.g. /etc/nginx/nginx.conf) - Optimised for Linux O/S
| # Generic startup file. user {user} {group}; #worker_processes should be equal to the amount of available processing threads/less worker_processes 4; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log pid /var/run/nginx.pid;events { #max allowable worker_connections per worker_process multi_accept on; use epoll; http { sendfile on; tcp_nodelay on; tcp_nopush on; keepalive_timeout 65; client_header_timeout = 300; client_body_timeout = 300; send_timeout = 300; connection_pool_size 256; client_header_buffer_size 128k; large_client_header_buffers 4 2k; output_buffers 1 32k; postpone_output 1460; ignore_invalid_headers on; marker #Reset timed out connections reset_timedout_connection on; ##Compression gzip on; gzip_http_version 1.1; gzip_buffers 16 8k; gzip_vary on; gzip_proxied any; gzip_comp_level 2; gzip_disable “msie6″; gzip_types text/plain text/css application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript; #Serve already compressed files directly gzip_static on; # Upstream to abstract backend connection(s) for PHP. include sites-enabled/*; |
PHP-FPM Configuration (Part of PHP-5.3+)
This PHP-FPM configuration guide and setup is optimised for Ubuntu 10.04 or later releases
Stable – PHP-5.3.8 (http://us2.php.net/distributions/php-5.3.8.tar.gz)
| ./buildconf –force./configure \ –prefix=/opt/php5 \ –with-config-file-path=/opt/php5/etc \ –with-config-file-scan-dir=/opt/php5/etc/conf.d \ –with-fpm-user=www-data \ –with-fpm-group=www-data \ –enable-fpm \ –with-mcrypt \ –enable-mbstring \ –with-openssl \ –with-mysql \ –with-mysql-sock \ –with-gd \ –with-png-dir \ –with-jpeg-dir \ –with-xpm-dir= \ –enable-gd-native-ttf \ –with-pdo-mysql \ –with-libxml-dir \ –with-mysqli \ –with-curl \ –enable-zip \ –enable-sockets \ –with-zlib \ –enable-exif \ –enable-ftp \ –with-iconv \ –with-gettext \ –enable-gd-native-ttf \ –with-t1lib \ –with-freetype-dir make |
PHP Configuration (e.g. /opt/php5/etc/php.ini)
[Optimised variables to set for WPMS + NGiNX]
| ; Maximum execution time of each script, in seconds max_execution_time = 300 ; Maximum amount of time each script may spend parsing request data. max_input_time = 600 ; Maximum amount of memory a script may consume (Needs to be set fairly high for hosting a WordPress MS install) memory_limit = 512M ;Allow file uploads. file_uploads = On ; Maximum size of post data that PHP will accept. Note: this value needs to be the same as set in nginx.conf under the client_max_body_size paramater. upload_max_filesize = 100M ; Maximum number of files that can be uploaded via a single request max_file_uploads = 20 ; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. Note: needs to be set to use fastcgi_split_path_info in nginx. cgi.fix_pathinfo=0 ; Define default timezone date.timezone = Africa/Johannesburg ; Session ; Handler used to store/retrieve data. Note this needs to be set to tie into your entire memcached pool session.save_handler = memcache ; Use a comma separated list of server urls to use for storage: session.save_path = “tcp://localhost:11211?persistent=1&weight=1&timeout=1&retry_interval=15″ ;Use cookies – needs to be enabled for WP session.use_cookies = 1 |
PHP-FPM Configuration (e.g. /opt/php5/etc/php-fpm.conf)
Optimised variables to set:
| ;Global Options ;Pid file pid = run/php-fpm.pid ; Send FPM to background daemonize = yes;Pool Definitions ;The address on which to accept FastCGI requests, needs to be the same as configured in nginx.conf, we use the unix socket which offers the highest optimisation and security listen = /var/run/php5-fpm.sock ;Set permissions for unix sockets, needs to be the same as configured in nginx.conf listen.owner = www-data listen.group = www-data ;Unix user/group of processes user = www-data group – www-data;Choose how the process manager will control the number of child processes. pm = dynamic ;This value sets the limit on the number of simultaneous requests that will be served, these values have been optimised for a 2Gb box, start with a low enough value and monitor pm.max_children = 25 ; The number of child processes created on startup. ; Default Value: min_spare_servers + (max_spare_servers – min_spare_servers) / 2 pm.start_servers = 4 ; The desired minimum number of idle server processes. pm.min_spare_servers = 2 ; The desired maximum number of idle server processes. pm.max_spare_servers = 10 ; The number of requests each child process should execute before respawning. pm.max_requests = 500;Additional variables that could be set: php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f email@yoursysadmin.com php_admin_value[memory_limit] = 512M php_admin_value[upload_max_filesize] = 100M php_admin_value[max_file_uploads] = 20 php_admin_value[post_max_size] = 100M php_admin_value[date.timezone] = Africa/Johannesburg |
