Instalasi Nginx CentOS 6
Instalasi Nginx CentOS 6
4 x64 VPS
inShare
Write an Article
Introduction
nginx pronounced as "engine x" is an HTTP and reverse proxy server, as well as a
mail proxy server.
nginx is an opensource web server which uses epoll mechanism to serve clients as
opposed to Apache which uses a thread based model which delegates the requests to
an instance in the thread pool. nginx is being used more over Apache because of its
speed. nginx has over 13% market share and increasing continuously.
Why compile from source
nginx has many modules which add features on top of existing VPS. Some of the
famous 3rd party modules are:
Create a new VPS with your domain name. We will be using 'example.com' as the
domain or you can use the server IP instead of the hostname.
Setting Up Pre-requisites to Compile from Source
Securely Login into your VPS
You can login into the VPS with your password for your host.
nginxVersion="1.5.5"
wget http://nginx.org/download/nginx-$nginxVersion.tar.gz
tar -xzf nginx-$nginxVersion.tar.gz
ln -sf nginx-$nginxVersion nginx
These options are the basic variables which we override to use default system paths
at /etc/ to ensure it works simliar when installed via rpm. The user and group
option are used to run the nginx worker processes in non-privileged.
--user
--group
--prefix
--sbin-path
--conf-path
--pid-path
--lock-path
--error-log-path
--http-log-path
Other options
--with-pcre option enables to match routes via Regular Expression Matching when
defining routes. [recommended, you will find more use of this once you start adding
and matching routes]
--with-file-aio - enables asynchronous I/O, better than the default send file
option (recommended if you are allowing users to download static files)
cd nginx
./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module
Once we are able to configure the source which even checks for additional
requirements like the compiler(gcc, g++) which we installed in the pre-requisites
step:
make
make install
Add the user nginx to the system. This is a one time command:
useradd -r nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# pidfile: /var/run/nginx.pid
# user: nginx
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/run/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|
reload|force-reload|configtest}"
exit 2
esac
wget -O /etc/init.d/nginx
https://gist.github.com/sairam/5892520/raw/b8195a71e944d46271c8a49f2717f70bcd04bf1a
/etc-init.d-nginx
This file should be made executable so that we can use it via 'service nginx ':
chmod +x /etc/init.d/nginx
http {
include mime.types;
default_type application/octet-stream;
# add the below 2 lines under http around line 20
types_hash_bucket_size 64;
server_names_hash_bucket_size 128;
Start the server. This will start the VPS on port 80.
Setup Complete
Welcome to nginx!
ssh [email protected]
cd ~/src/
nginxVersion="1.5.5" # set the value here from nginx website
wget http://nginx.org/download/nginx-$nginxVersion.tar.gz
tar -xzf nginx-$nginxVersion.tar.gz
rm nginx # removes the soft link
ln -sf nginx-$nginxVersion nginx
cd nginx
./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module
make
make install