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:

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

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

RubyOnRail:
cat /var/services/backend-rubyfcgi/run
#!/bin/sh
umask 22
export RAILS_ENV=production
exec setuidgid rubysite spawn-fcgi -n -a 127.0.0.1 -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/nginx.pid;

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 ip.add.re.ss:80 default accept_filter=httpready;

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

cat /usr/local/etc/nginx/vhosts/conf-site.com
server {
listen ip.add.re.ss;
server_name site.com www.site.com

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

location ~* ^.+\.(php)$ {
fastcgi_pass 127.0.0.1:9000;
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.

P.S.
Interesting link shows nginx as third popular web server.

31 comments:

Peter 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.

aurora said...

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

Anonymous said...

Urteter nuytre: http://plodanlyu.chez.com

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
http://s1.shard.jp/camptai/camp-letterman.html 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
http://s1.shard.jp/camppe/camp-musical-de-lanaudiere.html Click Here

Anonymous said...

[url=http://levitranowdirect.com/#jvede]levitra online[/url] - cheap levitra online , http://levitranowdirect.com/#xacni levitra online

Anonymous said...

[url=http://paydayloansheredirectly.com/#xtnoc]payday loans[/url] - payday loans , http://paydayloansheredirectly.com/#ifhkh payday loans

Anonymous said...

[url=http://buycialispremiumpharmacy.com/#raumc]buy cialis online[/url] - generic cialis , http://buycialispremiumpharmacy.com/#xuwcy generic cialis

Anonymous said...

[url=http://viagraboutiqueone.com/#aiowu]viagra 120 mg[/url] - buy viagra online , http://viagraboutiqueone.com/#tbtds viagra 25 mg

Anonymous said...

[url=http://buyonlineaccutanenow.com/#nphkf]accutane without prescription[/url] - accutane 10 mg , http://buyonlineaccutanenow.com/#zzzyo 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 []

Guo Guo said...

caihuanglin20150304
timberland boots
michael kors canada
tory burch shoes
louis vuitton outlet
abercrombie and fitch
ugg boots
canada goose outlet
ugg boots
canada goose outlet
cheap nfl jerseys
canada goose
coach outlet
coach factory
canada goose jackets
hollister outlet
toms promo code
coach outlet online
cheap jerseys wholesale
gucci outlet
michael kors outlet
fitflops outlet
tory burch handbags
michael kors handbags
coach factory outlet
burberry scarf
christian louboutin outlet
burberry sale
retro jordans
michael kors outlet online
ralph lauren outlet

chenlina said...

chenlina20150611
oakley sunglasses
hollister clothing
jordan 5
abercrombie and fitch
coach outlet store online
gucci bags
www.louisvuitton.com
michael kors handbags
mont blanc pens
louis vuitton handbags
louis vuitton handbags
hollister kids
michael kors
discount oakley sunglasses
ray ban sungalsses
louis vuitton
louis vuitton outlet
chanel handbags
timberlands
jordan 6 rings
ray ban sunglasses
ray ban outlet
michael kors
coach outlet online
hermes birkin
kevin durant basketball shoes
michael kors outlet online
oakley sunglasses sale
coach factory outlet
kate spade
ray ban outlet
louis vuitton outlet
replica rolex watches
michael kors
gucci handbags
insanity workout
chanel handbags
christian louboutin outlet
hogan outlet
abercrombie store

风骚达哥 said...

20150922 junda
abercrombie
Hollister uk
Air Jordan 3 III Retro Free Shipping
louis vuitton
coach outlet
Cheap Jordans For Sale Wholesale
canada goose jackets
Christian Louis Vuitton Red Bottoms
New Louis Vuitton Handbags Outlet
Official Coach Factory Outlet Online Sale
Jordan 13 Shoes For Sale
canada goose outlet
Real Louis Vuitton Bags
nike trainers
Jordan 8 Bugs Bunny
Authentic Louis Vuitton Belts Outlet Store
burberry outlet
Louis Vuitton Bags On Sale
true religion jeans
canada goose outlet online
Lebron James Basketball Shoes Sale
Louis Vuitton Handbags Discount Off
nfl jerseys
Abercrombie T-Shirts
Discount Ray Ban Polarized Sunglasses
coach factory outlet online
Louis Vuitton Outlet USA
celine
Louis Vuitton Luxury Official Website
michael kors outlet

Minko Chen 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
1026MINKO

Chenzhen 20160303chenzhen said...

louis vuitton handbags
air jordan homme
michael kors outlet
true religion sale
lebron james shoes 13
louis vuitton handbags
cheap ray ban sunglasses
coach outlet
louis vuitton handbags
coach factory outlet
cheap oakleys
michael kors outlet
louis vuitton outlet
ralph lauren
louis vuitton outlet stores
tory burch flats
adidas running shoes
nfl jerseys wholesale
michael kors outlet
ugg outlet
copy watches
toms outlet
timberlands
hollister shirts
coach outlet online
oakley sunglasses
kevin durant shoes
oakley vault
nike roshe run
oakley canada
coach outlet
nfl jerseys
michael kors bags
ugg boots
louis vuitton outlet stores
ray ban sunglasses
michael kors handbags
celine bags
insanity workout
ray ban sunglasses outlet
20160321chenzhen

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
jordans
retro jordans
louis vuitton handbags
coach outlet
toms
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

Gege Dai said...

michael kors outlet online
nfl jersey wholesale
ralph lauren outlet
nba jerseys
jordan 11
toms shoes
salomon shoes sale
ugg outlet store
nike free running
prada handbags
ferragamo shoes
nba jerseys
cheap ray ban sunglasses
cazal sunglasses
ugg boots
abercrombie and fitch
ferragamo shoes
mulberry handbags sale
louis vuitton pas cher
nike outlet
oakley sunglasses
polo ralph lauren
nike outlet store
prada sneakers
christian louboutin shoes
coach outlet
coach outlet
air max 90
ray ban sunglasses
rolex watches
longchamp pliage
burberry sunglasses on sale
michael kors outlet
ralph lauren uk
puma shoes
czq20160726

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
20172.6chenjinyan

shengda xu said...

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

kkk said...

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

shuaige cai 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
20171209caihuali

dong dong23 said...

uggs
mac cosmetics
prada
adidas superstar
reebok outlet
cartier bracelet
longchamp handbags
fitflops
oakley sunglasses
longchamp uk
2017.12.27chenlixiang

dong dong23 said...

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

5689 said...

zzzzz2018.7.30
football soldes
pandora jewelry
ugg boots clearance
christian louboutin shoes
football pas cher
golden goose
ugg boots
hermes belt
mbt
nike factory store

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
20188.10chenjinyan

chenmeinv0 said...

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

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
2018.11.2xukaimin

Coqicoqi said...

Jordan 11
Jordan Retro 11
Pandora
Kyrie Shoes
Pandora Jewelry Outlet
Yeezy boost 350 v2
Jordan 4
Jordan 11
Pandora Jewelry Official Site
Jordan 11
Ryan20181201

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
Ryan20190317

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
Rodney20190418