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.

25 comments:

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

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 []

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

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

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

Unknown 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

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

Unknown said...

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

Unknown said...

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

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

12122 said...

https://www.gillitv.net/v/jiyonkathi-29th-march-2021-full-episode-466-watch-online/

Jiyonkathi 29th March 2021 Full Episode 466 Watch Online gillitv