Some months ago I wrote how to combine Apache2 and php5-fpm, now I want to show you how you could migrate to nginx.
The netcraft webserver survey from May 2011 shows that nginx is still increasing slightly and is currently providing access to 7.35% of all monitored domains:
Selv compiling a bit old-school, that’s why I use packages from Dotdeb:
1 | echo "deb https://packages.dotdeb.org squeeze all" >> /etc/apt/sources.list |
Fetch and import GnuPG key:
1 | wget http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add - |
Nginx installation is pretty easy:
1 | aptitude update && aptitude install nginx |
Now we are ready to install it:
1 | aptitude install php5-cli php5-suhosin php5-fpm php5-cgi |
Additional support for MySQL and capcha generation:
1 | aptitude install php5-mysql php5-curl php5-gd |
Nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | user www-data; worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; server_names_hash_bucket_size 128; access_log /var/log/nginx/access.log; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } |
Example Host configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | server { server_name zeldor.biz www.zeldor.biz; listen 80; server_tokens off; location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm/zeldor.biz.soc; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/vhosts/zeldor.biz/httpdocs$fastcgi_script_name; access_log /var/log/nginx/zeldor.biz/access.log; error_log /var/log/nginx/zeldor.biz/error.log; } location / { root /var/www/vhosts/zeldor.biz/httpdocs; access_log /var/log/nginx/zeldor.biz/access.log; error_log /var/log/nginx/zeldor.biz/error.log; index index.php index.html index.htm test.php; try_files $uri $uri/ /index.php?q=$uri; } # Deny access to .htaccess files location ~ /\.ht { deny all; } } |
PHP-FPM Configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | cat /etc/php5/fpm/pool.d/zeldor.biz.conf [zeldor.biz] listen = /var/run/php5-fpm/zeldor.biz.soc listen.owner = www-data listen.group = www-data listen.mode = 0666 user = www-data group = www-data pm = dynamic pm.max_children = 20 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 10 php_admin_value[open_basedir]=/var/www/vhosts/zeldor.biz/httpdocs php_admin_value[session.save_path]=/var/www/vhosts/zeldor.biz/tmp php_admin_value[upload_tmp_dir]=/var/www/vhosts/zeldor.biz/tmp php_admin_value[memory_limit] = 128M php_admin_value[upload_max_filesize] = 100M php_admin_value[post_max_size] = 100M |
Restart to go:
1 | /etc/init.d/php5-fpm && /etc/init.d/nginx restart |
By the way, this Nginx setup is working fine with WordPress, vBulletin, DataLife Engine and PHPKIT.
UPDATE – 05.06.2011:
If Is Evil and I will show you what you can do, to avoid if statements. (Thx to Finkreghs Twitter feedback )
1 2 3 4 | # Very bad idea if (!-e $request_filename) { rewrite . /index.php last; } |
1 2 | # Better one try_files $uri $uri/ /index.php?q=$uri; |
UPDATE – 14.06.2011:
Useless use of cat:(Thx to Finkregh)
wget http://www.dotdeb.org/dotdeb.gpg && cat dotdeb.gpg | apt-key add –
1 | wget http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add - |
Finkregh says
useless use of cat: “wget http://www.dotdeb.org/dotdeb.gpg && cat dotdeb.gpg | apt-key add -”
–> “wget http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add -“