Install NGINX & PHP 5.3 with FPM on Centos 6 (1)


Install NGINX Dependency
NGINX depend on PCRE :

yum install pcre-devel


Install NGINX from the source :


cd /usr/local/src/
wget http://nginx.org/download/nginx-1.0.8.tar.gz
cd ..
tar -zxvf src/nginx-1.0.8.tar.gz
cd nginx-1.0.8/
./configure \
--with-ipv6 \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_stub_status_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-http_gzip_static_module
make
make install
ln -s /usr/local/nginx/conf /etc/nginx

Let’s change the NGINX configuration now :

nano /etc/nginx/nginx.conf

Delete all lines and insert this configuration :


user nobody;
worker_processes  10;

events {
        worker_connections  1024;
}

http {
      	add_header Cache-Control no-cache;
        add_header Cache-Control max-age=0;
        server_names_hash_bucket_size  64;
        include mime.types;
        default_type  application/octet-stream;
        sendfile on;
        keepalive_timeout 10 10;

        # GZIP Compression #
        gzip  on;
        gzip_comp_level 1;
        gzip_proxied any;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
        gzip_buffers     4 8k;

        # Logging Configuration #
        log_format main '$remote_addr - $remote_user [$time_local] '
        '"$request" $status  $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';
        #accesslog /var/log/nginx_access.log main;
        access_log /dev/null;
        #error_log /var/log/nginx_error.log debug;
        error_log /dev/null;

        # Options #
        include /etc/nginx/options.conf;
        include /etc/nginx/sites-enabled/*.conf;
}

See that we added “Options”, so we have to make those file later. Now let’s configure FastCGI option :


nano /etc/nginx/fastcgi_params

Add configuration bellow on after the last line :


fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

Options file included on the main NGINX configuration :

nano /etc/nginx/options.conf

Copy paste this :


## Size Limits
client_body_buffer_size         128K;
client_header_buffer_size	1M;
client_max_body_size	        1M;
large_client_header_buffers     8 8k;

VHOST configuration :


mkdir /etc/nginx/sites-enabled/
mkdir /etc/nginx/sites-available/
nano /etc/nginx/sites-available/00-default.conf

Default VHOST configuration :

nano /etc/nginx/sites-available/00-default.conf

Put this default VHOST config :


server {
        listen *:80;
        location / {
                root /var/www/html;
                index index.php index.html;
        }
}

Make link for the site-enabled configuration :

ln -s /etc/nginx/sites-available/00-default.conf /etc/nginx/sites-enabled/.

Make sample for VHOST configuration for sites that you will create later. If you want to create VHOST later, just copy this sample config, adjust, then add the link to the /etc/nginx/sites-enabled/ :

nano /etc/nginx/sites-available/01-sample.conf

Sample VHOST configuration with PHP-FPM (we will install PHP later) :


server {
        listen *:80;
        server_name sampletest.com www.sampletest.com;
        #access_log /dev/null;
        #error_log /dev/null;
        location / {
                root /home/username/public_html;
                index index.php;
                real_ip_header X-Forwarded-For;
                # if file exist, return it right away
                if (-f $request_filename) {
                        break;
                }

        # this is not actually a htaccess config, but customized rewrite config
        include /home/username/htaccess;

        # make NGINX call PHP
        location ~ \.php$
                {
                fastcgi_pass 127.0.0.1:9001;
                fastcgi_param SCRIPT_FILENAME /home/username/public_html$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_index index.php;
                include /usr/local/nginx/conf/fastcgi_params;
                }
        }
}

NGINX init.d startup script :

nano /etc/init.d/nginx

Copy paste bellow :


#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi

set -e

case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0

This script will not work without Debian dpkg :


cd /usr/local/src/
wget http://ftp.de.debian.org/debian/pool/main/d/dpkg/dpkg_1.14.31.tar.gz
tar -zxvf dpkg_1.14.31.tar.gz
cd dpkg
cd dpkg-1.14.31/
ls
./configure
make
cd utils/
make install

Now make it executable :


chmod +x /etc/init.d/nginx

Dont forget to make default page for NGINX :


mkdir /var/www
ln -s /usr/local/nginx/html /var/www/

Now your NGINX server is ready. PHP 5.3 with FPM will be explained on the second part of this article.

Advertisements

About piyecarane

Bla bla bla about me
This entry was posted in Linux, nginx, Technical Standard and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s