Post4VPS Forum | Free VPS Provider
Compile Nginx from Source - Centos - Printable Version

+- Post4VPS Forum | Free VPS Provider (https://post4vps.com)
+-- Forum: VPS Discussion (https://post4vps.com/Forum-VPS-Discussion)
+--- Forum: Tutorials (https://post4vps.com/Forum-Tutorials)
+--- Thread: Compile Nginx from Source - Centos (/Thread-Compile-Nginx-from-Source-Centos)



Compile Nginx from Source - Centos - Neco - 12-02-2016

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. 

Installing from source is not as easy as installing it from the repository. This way you have to configure it and compile it before you can start using it. In the repository they add the version they compiled. Compiling from source is good option when you need newest version of nginx, fixing security vulnerabilities. fixing bugs and such.


Compiling from source is useful because you can add modules that are not available in regular installation.

1. Install dependencies
Code:
yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

2. Download the source code
Grab the latest version from http://nginx.org/en/download.html

Code:
wget http://nginx.org/download/nginx-1.11.6.tar.gz
tar -xzf nginx-1.11.6.tar.gz

3. Preparing the nginx source
For a full list of options you can look at  ./configure --help

Options for basic file path names
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-http_gzip_static_module option enables nginx to use gzip (Before serving a file from disk to a gzip-enabled client, this module will look for a precompressed file in the same location that ends in ".gz". The purpose is to avoid compressing the same file each time it is requested.).[recommended for reducing size of information sent]
  • --with-http_stub_status_module option enables other plugins over nginx to allow us to get the status (This module provides the ability to get some status from nginx.). [recommended for getting stats]
  • --with-http_ssl_module - required if you want to run a HTTPS server. See How To Create a SSL Certificate on nginx for CentOS 6
  • --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)
  • --with-http_realip_module is used for getting the IP of the client when behind a load balancer. This is useful when serving content behind CloudFlare like services.
  • --without-http_scgi_module - Disable SCGI module (normally used when running CGI scripts)
  • --without-http_uwsgi_module - Disable UWSGI module (normally used when running CGI scripts)
  • --without-http_fastcgi_module - Disable FastCGI module (normally used when running CGI scripts)
Example configuration would look like:

Code:
./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

4. Compiling
Now when we have configured our nginx, it is time to build it.

Code:
make
make install


5. Run the nginx
We will create a user nginx which will run the process.
Code:
useradd -r nginx

Create a startup script

Code:
vi /etc/init.d/nginx


Paste following

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

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

Make the script executable:

Code:
chmod +x /etc/init.d/nginx

Set the service to start whenever the system boots:

Code:
chkconfig --add nginx
chkconfig --level 345 nginx on


Start nginx server

Code:
service nginx start

The setup is complete and nginx is running on port 80.