How to Install NGINX, PHP with PHP-FPM, and MySQL on Centos 5.5 (1)


This article is for my personal note. I wont write about dependency softwares installation. I assume you already have them. If you dont, during installation you will know what softwares must be installed first as dependency.


cd /usr/local/
wget http://nginx.org/download/nginx-0.8.54.tar.gz
tar -zxvf nginx-0.8.54.tar.gz
cd nginx-0.8.54
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_gzip_static_module
make
make install
ln -s /usr/local/nginx/conf /etc/nginx


Edit the config file :


nano /etc/nginx/nginx.conf

Paste this config :


user nobody;
worker_processes  8;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  10 10;

gzip  on;
gzip_comp_level 1;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_buffers     4 8k;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status  $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

#        access_log  /var/log/nginx_access.log  main;
access_log /dev/null;
#        error_log  /var/log/nginx_error.log debug;
error_log /dev/null;
###
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout           3m;
client_header_buffer_size    1k;
large_client_header_buffers  4 4k;
output_buffers   1 32k;
postpone_output  1460;
tcp_nopush       on;
tcp_nodelay      on;

include /usr/local/nginx/sites-enabled/*;
}

Add a tweak a bit for fastcgi parameters so that PHP will be served better :


nano /etc/nginx/fastcgi_params

Paste this config on the bottom of the config file :


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

Finally let’s make a startup file on /etc/init.d/ :


nano /etc/init.d/nginx

Paste this :


#! /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/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

Dont forget to chmod it :


chmod 750 /etc/init.d/nginx

Now let’s make a virtual host sample for nginx :


mkdir /usr/local/nginx/sites-enabled
ln -s /usr/local/nginx/sites-enabled /etc/sites
nano /etc/sites/default.conf

Paste this :


server {
listen *:80;

location / {
root   /var/www/html;
index index.php index.html;

# if file exists return it right away
if (-f $request_filename) {
break;
}

# otherwise rewrite the fucker
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php$1 last;
break;
}

}

# if the request starts with our frontcontroller, pass it on to fastcgi
#        location ~ \.php$
#        {
#                fastcgi_pass 127.0.0.1:9000;
#                fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
#                fastcgi_param PATH_INFO $fastcgi_script_name;
#                fastcgi_index index.php;
#                include /usr/local/nginx/conf/fastcgi_params;
#        }
}

We’ll uncomment php fastcgi configuration once we’ve installed php with fpm
Now make /var/www/html directory and make default index page :

mkdir -p /var/www/html
nano /var/www/html/index.html

Write any html codes you like, or just create a hello world text without any html tag.

Now let’s start our NGINX and test it in the browser

/etc/init.d/nginx start

See .. you’ll get some error πŸ˜€ Of course .. it’s a Centos. You need a dpkg package from Debian.
Let’s get it installed :


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

Now try to start nginx. You’ll end up with a smiley shining happy face πŸ˜›

Advertisements

About piyecarane

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

3 Responses to How to Install NGINX, PHP with PHP-FPM, and MySQL on Centos 5.5 (1)

  1. Pingback: » Install Nginx, PHP-FPM & MySQL in Centos - Sandalian.com

  2. Pingback: NGINX with PHP-FPM and MongoDB | VPS Cloud Management Manual

  3. i found a working script from this blog, found great, worked for me, i share it to everybody
    http://www.discusswire.com/install-nginx-php-fpm-mysql-centos/

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