Sunday, June 23, 2013

Web Server NginX+PHP (Fast CGI)+PostgreSQL On Centos 5 - Komputekid

Nginx (Read: Enjin X or Engine X)  is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Igor Sysoev started development of Nginx in 2002, with the first public release in 2004. Nginx now hosts nearly 6.55% (13.5M) of all domains worldwide. 
Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. 
Nginx is one of a handful of servers written to address the C10K problem. Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. 
Even if you don't expect to handle thousands of simultaneous requests, you can still benefit from Nginx's high-performance and small memory footprint. Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers. 
Nginx powers several high-visibility sites, such as WordPressHuluGithubOhlohSourceForge andTorrentReactor

Ok Lets Begin ^_^

Update and Upgrade
# yum list update
# yum –y upgrade

Install PostgreSQL and Run it
# yum install –y postgresql postgresql-server postgresql-devel
# service postgresql start
# chkconfig postgresql on

Install a need packet of installation
# yum install -y wget patch gcc libtool libmcrypt-devel libxml2-devel flex bison make pcre-devel zlib-devel openssl-devel gd-devel

Download the packet you need
# cd /usr/local/src
# wget
# wget
# wget
# wget
# wget
# wget

Compile and Patch PHP-FPM
# tar -zxf php-5.3.0.tar.gz
# gzip -cd php-5.3.0-fpm-0.5.12.diff.gz | patch -d php-5.3.0 –p1
# cd php-5.3.0
./configure \
--enable-fastcgi \
--enable-fpm \
--enable-cli \
--with-mcrypt \
--with-zlib \
--enable-mbstring \
--with-openssl \
--with-gd \
--with-jpeg-dir=/usr/lib64 \
--enable-gd-native-ttf \
--without-sqlite \
--disable-pdo \
--disable-reflection \
--with-libdir=lib64 \
--with-xpm-dir=/usr/lib64 \
--enable-gd-jis-conv \
--with-freetype-dir=/usr/include/freetype2 \
--with-ttf=/usr/include/freetype2 \
##note: if you confuse about that option, open this link: ##

# make all install
# strip /usr/local/bin/php-cgi

Modification a configuration of PHP-FPM at line 63 and 65:
# vi /usr/local/etc/php-fpm.conf

Unix user of processes
<value name="user">nobody</value>
Unix group of processes
<value name="group">nobody</value>

Install X-Cache
#cd /usr/local/src
# tar -zxf xcache-1.2.2.tar.gz
# cd xcache-1.2.2
# phpize
# ./configure --with-php-config=/usr/local/bin/php-config --enable-xcache
# make install

Add the Module of PostgreSQL
# cd /usr/local/src/php-5.3.0/ext/pgsql/
# phpize
# ./configure
# make install

Create a Configuration for PHP
vi /usr/local/lib/php.ini
engine = On
y2k_compliance = On
zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20090626/
xcache.shm_scheme = "mmap"
xcache.size = 64M
extension =
variables_order = "EGPCS"
register_argc_argv = On
auto_globals_jit = On

Make PHP-FPM Run at start up
# cd /etc/init.d/
# ln -s /usr/local/sbin/php-fpm php-fpm
# vim /etc/rc.local
## add this line to the end of rc.local##
/etc/init.d/php-fpm start

Install NginX
# cd /usr/local/src
# tar -zxf nginx-0.7.61.tar.gz
# cd nginx-0.7.61
./configure --pid-path=/usr/local/nginx/logs/ --sbin-path=/usr/local/sbin/nginx --with-md5=/usr/lib --with-sha1=/usr/lib --with-http_ssl_module  --with-http_dav_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module
# make
# make install
and then install daemon for nginx
# cd /usr/local/src/
# chmod 777 nginx-daemon
# mv nginx-daemon /etc/init.d/nginx
# tar -zxf apps-sys-utils-start-stop-daemon-IR1_9_18-1.tar.gz
# cd apps/sys-utils/start-stop-daemon-IR1_9_18-1/
# gcc start-stop-daemon.c -o start-stop-daemon
# cp start-stop-daemon /usr/sbin
# chkconfig --add nginx
# chkconfig --level 345 nginx on

Configuration NginX for PHP and PHP-FPM
# vi /usr/local/nginx/conf/fastcgi_params
and add this line at end of file:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
vi /usr/local/nginx/conf/nginx.conf
user  nobody;
worker_processes  5;
error_log  /var/log/nginx/error.log;
events {
worker_connections  768;
# mkdir /var/log/nginx
# chmod 777 /var/log/ginx

it is example for my configuration server at Politeknik Negeri Jakarta:

Start NginX and PHP-FPM
# service nginx start
# /etc/init.d/php-fpm start

Test Your Server
# vim /usr/local/nginx/html/phpinfo.php

and open this file from web browser:

OK Finish ^_^
+++ NCA

- Wiki NginX
- Magnet-ID
- And Many other