Why Nginx?
Its hard to be a server/linux/software geek and not know about Nginx. I’ve been meaning to play with Nginx for a while. Thought about installing it on SW VPS but I just never found the time to do it. I did attempt to install nginx with RT 3.8 and that was a total failure on my part and mainly because I couldn’t get RT’s quirky perl to work. I will give that another try since I understand Nginx much better this time around.
Before I start I like to thank Boris from WHT, Nathan of SM Script, and #nginx on freenode IRC. and yawn.it
Although I mention using SM script, you can use this tutorial for pretty any scripts out there that uses php and .htaccess
A large part of this post is compromised of this tutorial I found on the web on nginx. Really well written and modified a little by me for CentOS5.
First, lets start with some packages we needed to compile nginx with no errors. Make sure all the development packages are installed. And of course a compiler (gcc)!
yum install ffmpeg-devel libxml2-devel libXpm-devel libjpeg-devel libpng-devel mysql-devel libtool make patch gcc
Next, we need to compile a custom version of php 5.2.6 patched and integrated with php-fpm and ffmpeg-php (for SM script). CentOS default php binary is located in “/usr/bin”, the compile of the source code defaults in “/usr/local/bin”. The nice thing about this whole process is the fact that you can have both php binaries installed and depending on what you want to turn on (apache or nginx), you can just activate it accordingly.
Download php 5.2.6 and php-fpm. Patch php.
wget http://us.php.net/get/php-5.2.6.tar.gz/from/this/mirror tar -xvvzf php-5.2.6.tar.gz wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.9.diff.gz gzip -cd php-5.2.6-fpm-0.5.9.diff.gz | patch -d php-5.2.6 -p1
Download ffmpeg-php and build it right into the binary (I prefer it this way).
wget http://downloads.sourceforge.net/ffmpeg-php/ffmpeg-php-0.6.0.tbz2?modtime=1224044751&big_mirror=0 bunzip2 -c ffmpeg-php-0.6.0.tbz2 | tar xvf - mv ffmpeg-php-0.6.0 php-5.2.6/ext/ffmpeg cd php-5.2.6 autoconf ./configure --enable-fastcgi --enable-fpm --with-zlib --enable-mbstring --with-mysql --with-mysql-sock --with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf --without-sqlite --disable-pdo --disable-reflection --with-ffmpeg=yes make all install strip /usr/local/bin/php-cgi
Start configuring php-fpm.conf
On a side note I copied the sample php.ini from the source folder. This was a mistake as the sample didn’t have short_open_tag enabled. This was to give me a big headache for a while. Best thing is to copy your default CentOS php.ini which also already have ioncube loaded. The default location for your new php binary will be loaded from /usr/local/lib
cp /etc/php.ini /usr/local/lib vi /usr/local/etc/php-fpm.conf
Find these two lines. Remove the Comments (arrows) and replace the “nobody” with the user and group you would like php-fpm to run as.
<!-- <value name="user">nobody</value> --> <!-- <value name="group">nobody</value> -->
If you don’t do this, starting php-fpm will show this error.
fpm_unix_conf_wp(), line 124: please specify user and group other than root, pool ‘default’
Next we will compile Nginx (There is an easy way to install using RPM but I’m not sure what options and where it gets put. Link here)
Although the default modules that are activated with nginx is plenty enough, I wanted to enabled two extra modules. One is http_stub_status_module and flv streaming. As of version 2.1 SM Script does not support FLV seeking but the new update for the flash player should give that ability so might as well get ready by activating the module.
You will need pcre library to compile nginx.
wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz tar -xvvzf nginx-0.6.32.tar.gz wget http://downloads.sourceforge.net/pcre/pcre-7.8.tar.gz?modtime=1220617433&big_mirror=0 tar -xvvzf pcre-7.8.tar.gz cd nginx-0.6.32 ./configure --with-http_stub_status_module --with-http_flv_module --with-pcre=/path/to/pcre-7.8/ make make install
Edit Nginx config file
vi /usr/local/nginx/conf/nginx.conf
Use the following nginx config. This is a basic config that will run nginx with SM Script. (NOTE: Config isn’t optimized for best performance with script). Replace “root /var/www/html” with your path for SM script. Click here to get all the converted rewrites for nginx.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# the 300m represent 300megs. This is the max upload. Change it if you need.
client_max_body_size 300m;
server {
listen 80;
# replace domainname.com with your domain name. I discover you need to do this for encoded files.
server_name domainname.com;
# Replace /var/www/html with your path to SM script.
root /var/www/html;
location / {
index index.php index.html;
}
# This is your php-fpm section. Replace it accordingly (For example if you decide to run it on a different port).
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# This enable flv streaming allowing fast forwarding. Next version of SM should support it.
location ~ \.php {
flv;
}
# This is for nginx to deny access to .htaccess file on your SM script root.
# Leave it there so you can easily turn on apache if the need arise.
location ~ /\.ht {
deny all;
}
# Copy all the converted SM .htaccess rules here. Yes all of it needs to go in there.
rewrite ^/signup/$ /index.php?view=signup last;
....
....
....
}
}On a side note: Here is how the apache rewrite is converted to nginx
Apache: RewriteRule ^signup/$ index.php?view=signup [L]
Nginx: rewrite ^/signup/$ /index.php?view=signup last;
Replace it with all lines following this rule and it will work.
start nginx and php-fpm
php-fpm start /usr/local/nginx/sbin/nginx
Since I can’t find any init script I do the lazy route of starting up the services by putting the commands in rc.local These commands will automatically run next time the VPS is restarted.
edit the file and put the 2 start commands in the file.
vi /etc/rc.local
php-fpm start /usr/local/nginx/sbin/nginx
Now if you decide to stop nginx and start apache for whatever reason all you have to do is.
php-fpm stop kill `cat /usr/local/nginx/logs/nginx.pid` /etc/init.d/httpd start
Some Notes:
You may see this error when you browse the site after successfully starting nginx.
“No input file specified”
This error pops up if your fastcgi_param isn’t set correctly.
Part 2 is done!
Continue onto Part 3 which will involve setting up a Remote Backup and MySQL replication.

Tuesday, 27. January 2009
Good work! Thank you!
I always wanted to write in my blog something like that. Can I take part of your post to my blog?
Of course, I will add backlink?
Sincerely, Timur I.
Tuesday, 3. February 2009
Sure.. make sure you have a backlink
Wednesday, 4. February 2009
Hi there,
I have already seen it somethere
Thanks
Tania
Wednesday, 4. February 2009
Hi there
If anyone knows or provide..
I need UK VPN account.. (to bypass unblock etc..)
I already have USA vpn account..
I dont want to provide vpn service..
I want to buy and enjoy one..
Thursday, 5. February 2009
Hello. Your site displays incorrectly in Firefox, but content excellent! Thanks for your wise words.
Friday, 6. February 2009
Thank you very much – these look great!
Thursday, 1. October 2009
Thank you so much! This was exactly what I was looking for. Awesome job
Friday, 2. October 2009
Just FYI, the new php-fpm is compiled differently and I ran into some snags (although its not complicated at all). I also recommend the latest nginx. I think even installing the development edition is ok since there seems to be some vulnerabilities in nginx which is fixed on the beta.