CentOS 5 – Instalar Nginx desde código fuente

Vamos a ver como compilar e instalar Nginx (“engine x”) desde fuente en CentOS 5.

Compilación e Instalación

# yum install pcre pcre-devel openssl openssl-devel
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.0.0.tar.gz
# tar xf nginx-1.0.0.tar.gz
# cd nginx-1.0.0
# ./configure --prefix=/usr/local/nginx --without-http_autoindex_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --conf-path=/usr/local/nginx/etc/nginx.conf --with-http_stub_status_module
# make && make install

en este punto, tenemos instalado Nginx en el path /usr/local/nginx/ y su archivo de configuración está localizado en /usr/local/nginx/etc/nginx.conf.

Probaremos primero si Nginx funciona correctamente


# /usr/local/nginx/sbin/nginx
# ps axuf |grep nginx

root 22291 0.0 0.1 7204 812 pts/0 S+ 12:27 0:00 \_ grep nginx
root 21778 0.0 0.0 18600 692 ? Ss 12:26 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 21779 0.0 0.1 18996 1424 ? S 12:26 0:00 \_ nginx: worker process

Sólo queda que naveguemos por la ip para ver si vemos el mensaje de bienvenida

Welcome to nginx!

Seguidamente, sería útil tener en nuestro PATH la ruta de Nginx.

# echo 'export PATH=$PATH:/usr/local/nginx/sbin/' >> /etc/profile.d/nginx.sh
# chmod a+x /etc/profile.d/nginx.sh
# export PATH=$PATH:/usr/local/nginx/sbin/

Un comando útil de nginx es el que nos comprueba si el archivo de configuración es correcto

# nginx -t
nginx: the configuration file /usr/local/nginx/etc/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/etc/nginx.conf test is successful

Vamos a configurar brevemente Nginx con algunas opciones, este último paso, será diferente según sea el hardware donde correrá Nginx. Respecto al usuario, cambiaremos el usuario de nobody a nginx

# useradd nginx
# mkdir -p /var/log/nginx
# touch /var/log/nginx/error.log
# mkdir -p /usr/local/nginx/etc/vhosts
# cp /usr/local/nginx/etc/nginx.conf /usr/local/nginx/etc/nginx.conf.bak
# vim /usr/local/nginx/etc/nginx.conf


user nginx;
worker_processes 8;
worker_rlimit_nofile 32768;
error_log /var/log/nginx/error.log;
events {
worker_connections 4096;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
server_tokens off;
keepalive_timeout 5;
gzip on;
gzip_min_length 1100;
gzip_buffers 64 8k;
gzip_comp_level 3;
gzip_http_version 1.1;
gzip_proxied any;
gzip_types text/plain application/xml application/x-javascript text/css;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
include /usr/local/nginx/etc/vhosts/*.conf;
}

Comprobamos configuración y ordenamos a Nginx que recargue la misma (sin reiniciar)

# nginx -t
# nginx -s reload

Creamos script de daemon de 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: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/etc/nginx.conf"

lockfile=/var/lock/subsys/nginx

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

Probamos el script


# /etc/init.d/nginx start
# /etc/init.d/nginx stop
# /etc/init.d/nginx restart
# /etc/init.d/nginx reload

Dejamos Nginx en el arranque del sistema

# /sbin/chkconfig nginx on

Enjoy! 🙂

Links
http://www.superstaradmin.co.uk/2010/12/compiling-nginx-on-centos/
http://articles.slicehost.com/2009/2/2/centos-adding-an-nginx-init-script
http://articles.slicehost.com/2009/2/2/centos-installing-nginx-from-source
http://systemadmin.es/2011/02/instalar-nginx-con-php-5-3-mediante-php-fpm-y-eaccelerator

1 thought on “CentOS 5 – Instalar Nginx desde código fuente

Leave a Reply

Your email address will not be published. Required fields are marked *