Search This Blog

Friday, August 3, 2007

Move from apache 2.2 to Nginx as a PHP or Ruby web server

Recently we started to move moderately loaded news web site from apache 2.2 to Nginx due to it's incredible performance and security.

So, here the short instruction set to start Nginx with PHP and RubyOnRails under daemontools monitoring on FreeBSD:

cat /var/services/backend-phpfcgi/run
exec 2>&1
exec setuidgid backend spawn-fcgi -n -a -p 9000 -f /usr/local/bin/php-cgi

I'm using spawn-fcgi from lighttpd package, however the php-cgi can be ran directly.

cat /var/services/backend-rubyfcgi/run
umask 22
export RAILS_ENV=production
exec setuidgid rubysite spawn-fcgi -n -a -p 9001 -f /home/rubysite/www/public/dispatch.fcgi

Nginx configuration files:

cat /usr/local/etc/nginx/nginx.conf
user www;
worker_processes 30;
timer_resolution 1000ms;

error_log /var/log/nginx-error.log error;
pid /var/run/;

events {
worker_connections 2048;
use kqueue;

http {
include mime.types;
default_type application/octet-stream;

access_log off;
gzip on;
gzip_comp_level 6;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

include vhosts/conf-*;
server {
listen default accept_filter=httpready;

error_page 404 /404.html;
location / {
root /home/default/www;
index index.html;

cat /usr/local/etc/nginx/vhosts/
server {

location / {
root /home/site/www;
index index.php;

location ~* ^.+\.(php)$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/site/www/$fastcgi_script_name;

include fastcgi_params;

cat /usr/local/etc/nginx/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 GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

Nginx runs under www privileges and the back-end as a backend user so security is much improved.

Interesting link shows nginx as third popular web server.


Anonymous said...

Great thing!
Firstly to note, I pleased to surprise this blog appeared functioning among my bookmarks' urls.
Secondly, I'm wondering why you prefer tcp (AF_INET) sockets over local (AF_UNIX)? I've heard of the lack of local sockets in favour of tcp on Linux, but this is not your case ;-)
Thirdly, if anyone prefers the Perl execution with major mod_perl features ( persistent forks and CoW, but no APache::/ModPerl:: namespaces ), I have the FCGI::Spawn for them on CPAN.

harald said...

i can't find your nginx configuration for ruby/rails ... how do you use the spawned rails process from nginx?

Anonymous said...

Urteter nuytre:

Anonymous said...

Congratulations on possessing certainly one among one of the most refined blogs Ive arrive throughout in a while! Its simply wonderful how much you’ll be able to think about away from a thing principally merely because of how visually beautiful it is. Youve place collectively an amazing blog web site area –great graphics, movies, layout. That is certainly a must-see web site! ivana helsinki handbags| футболка татарин| gucci meier bag| leisurely Click Here

Anonymous said...

Really like your web sites particulars! Undoubtedly a beautiful supply of knowledge that is extremely helpful. Keep it up to carry publishing and i’m gonna proceed studying by means of! Cheers. где купить футболку цска| как появилась фамилия загородский| возникновение фамилии абалока| doubtless improved the quality of the soil for many years afterward Click Here

Anonymous said...

[url=]levitra online[/url] - cheap levitra online , levitra online

Anonymous said...

[url=]payday loans[/url] - payday loans , payday loans

Anonymous said...

[url=]buy cialis online[/url] - generic cialis , generic cialis

Anonymous said...

[url=]viagra 120 mg[/url] - buy viagra online , viagra 25 mg

Anonymous said...

[url=]accutane without prescription[/url] - accutane 10 mg , accutane online

Anonymous said...

It's fantastic that you are getting thoughts from this
paragraph as well as from our discussion made at this time.

Feel free to surf to my site ... poker gambling games []

Unknown said...

michael kors uk
basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
ferragamo outlet
dallas cowboys
michael kors uk
louis vuitton outlet store
coach outlet online
nike free
hogan,hogan outlet,scarpe hogan,hogan sito ufficiale,hogan interactive
toms outlet
oakley sunglasses wholesale
celine outlet
tory burch outlet online
ugg boots clearance,ugg australia,uggs on sale,ugg slippers,uggs boots,uggs outlet,ugg boots,ugg,uggs
links of london jewellery
chicago bulls
hollister uk
nike outlet store
new york jets
oklahoma city thunder
the north face outlet

chenlili said...

2016-4-21 xiaobao
toms shoes
louis vuitton
pandora jewelry
lebron james shoes
kevin durant shoes
ralph lauren outlet
louis vuitton handbags
louboutin pas cher
giuseppe zanotti sneakers
cheap oakley sunglasses
coach outlet online
retro jordans
louis vuitton handbags
coach outlet
jordan 4 toro
marc jacobs
michael kors handbags
insanity workout
michael kors
mont blanc pens
coach outlet online
kate spade
ray ban outlet
michael kors outlet
kobe 11
nike free run
kobe 9
louis vuitton outlet
nike uk
fake watches
toms shoes
oakley sunglasses
michael kors online
ray ban uk
nike air max
louis vuitton outlet online
ed hardy clothing
jordan 11s

chenmeinv0 said...

coach outlet online
true religion outlet
tory burch shoes
ugg outlet
hollister jeans
moncler soldes
instyler curling iron
toms outlet
cheap rolex watches
ugg boots

Unknown said...

kd shoes
tory burch outlet online
valentino outlet
yeezy shoes
coach factory outlet online
cheap jordan shoes
louis vuitton outlet online
louis vuitton handbags
ralph lauren sale

kkk said...

ray ban sunglasses outlet
coach handbags outlet
adidas uk
mulberry bags
yeezy boost
montblanc pens
kate spade bags
ray ban sunglasses outlet
nike sneakers

Unknown said...

football shirts
michael kors outlet
coach outlet
michael kors handbags
montblanc pens
canada goose outlet
michael kors outlet
oakley sunglasses wholesale
polo ralph lauren
oakley sunglasses

Unknown said...

mac cosmetics
adidas superstar
reebok outlet
cartier bracelet
longchamp handbags
oakley sunglasses
longchamp uk

Unknown said...

adidas shoes
reebok outlet
nike uk
barbour jackets
pandora uk
supra shoes
dolce and gabbana
air max plus

chenlili said...

nfl jerseys
adidas nmd
columbia outlet
tory burch outlet
alexander mcqueen
x-large clothing
breguet watches
nike factory
tom brady jersey
maillot de foot pas cher

chenmeinv0 said...

armani handbags
nike air max
basket store
kevin durant shoes
adidas outlet store
argentina world cup jersey
dsquared2 outlet
timberland boots
softball bats

chenmeinv0 said...

chaussure nike
armani sunglasses
jordan 5
basket store
nike canada
nike free run
sac longchamp
ugg jimmy choo
kevin durant shoes
gucci handbags

Coqicoqi said...

Jordan Retro
Yeezy boost
Nike Air Max 270
Jordan 4
Nike 270
Pandora Outlet
Jordan Retro
Red Bottom Shoes For Women
Pandora Jewelry Official Site

Bilibili said...

Pandora Jewelry
Nike Air Max 270
Jordan 9
Air Jordan 11
Kyrie Irving Shoes
Yeezy boost 350 v2
Pandora Outlet
Pandora Jewelry
Jordan 4

12122 said...

Jiyonkathi 29th March 2021 Full Episode 466 Watch Online gillitv