Showing posts with label nginx. Show all posts
Showing posts with label nginx. Show all posts

Tuesday, December 11, 2012

Running webtoolkit application as nginx FastCGI on CentOS 6.x

Wt (pronounced as witty) is a powerful C++ library for developing web applications. Witty-based apps can be integrated as FastCGI with nginx and other web servers. This guide is about the integration of witty w/ nginx on CentOS 6.x OS.

On this how to, we'll be using the simplest witty example: hello

I assumed you already had installed a CentOS 6.x minimal x86_64.

The Steps

  1. Login as a sudoer user.
  2. Append EPEL repository by creating the file /etc/yum.repos.d/epel.repo with the following content:
  3. [epel]
    name=Extra Packages for Enterprise Linux 6 - $basearch
    #baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
    mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
    failovermethod=priority
    enabled=1
    gpgcheck=0
    
  4. Install required packages: nginx, witty and CentOS development kit:
  5. $ sudo yum install nginx 
    $ sudo yum install wt
    $ sudo yum install fcgi
    $ sudo yum install spawn-fcgi
    $ sudo yum install wt-devel wt-examples      # Only for development env
    $ sudo yum groupinstall "Development Tools"  # Only for development env
    $ sudo yum install nano                      # Only for development env
    
  6. Go to Wt's examples directory and edit the CMakeLists.txt archive:
  7. $ cd /usr/lib64/Wt/examples/hello
    $ sudo nano CMakeLists.txt
    
    and replace
    WT_ADD_EXAMPLE(hello.wt hello.C)
    by:
    ADD_EXECUTABLE(hello.wt hello.C)
    TARGET_LINK_LIBRARIES(hello.wt ${EXAMPLES_CONNECTOR})
    
  8. Run CMake specifying FastCGI support & copy resulting binary to nginx document root:
  9. $ sudo rm -rf target
    $ sudo mkdir -p target
    $ cd target
    $ sudo cmake ../ -DEXAMPLES_CONNECTOR=wtfcgi -DCONNECTOR_FCGI=yes -DCONNECTOR_HTTP=no 
    $ sudo make
    $ sudo cp -a hello.wt /usr/share/nginx/html/
    
  10. Create a new archive /etc/sysconfig/spawn-fcgi-hello.wt with the following content:
  11. FCGI_SOCKET=/var/run/hello.wt.socket
    FCGI_PROGRAM=/usr/share/nginx/html/hello.wt
    FCGI_USER=nginx
    FCGI_GROUP=nginx
    FCGI_EXTRA_OPTIONS="-M 0700"
    OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/hello.wt.socket.pid -- $FCGI_PROGRAM"
  12. Allow nginx to write at /var/spool/wt/run/:
  13. $ sudo chgrp nginx /var/spool/wt/run/
    $ sudo chmod g+w /var/spool/wt/run/
  14. Launch hello app via spawn-fcgi:
  15. $ source /etc/sysconfig/spawn-fcgi-hello.wt
    $ spawn-fcgi $OPTIONS
    
    now the FastCGI is running and waiting.
  16. Create a new file /etc/nginx/conf.d/wt.conf w/ the following content:
  17. server {
        listen  9091;
        server_name  _;
    
        # by default relative to /usr/share/nginx/html
        location / {
          access_log /var/log/nginx/nginx-fastcgi-access.log;
          gzip off;
    
          # the full path /usr/share/nginx/html/hello.wt
          if ($uri !~ "^/hello.wt/$") {
            fastcgi_pass unix:/var/run/hello.wt.socket;
          }
          include /etc/nginx/fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
    
  18. Restart nginx:
  19. $ service ngnix restart
    
  20. Visit http://HOST:9091 and enjoy it!

Wednesday, July 4, 2012

The NGINX's RAM-only Jail. Adding nginx to RAM-only diskless Linux box

Without any doubt, nginx is one of the best ever created HTTP servers, it solves the C10K problem with an elegant event-drive architecture. Many big sites around the WWW use it: Wikipedia, Hyves, etc.

This is a how to prepare your rambox to run nginx smoothly. It makes sense only after the preparation of the RAM-only PXE boot in my previous post. This post isn't about any kind of nginx optimization stuff beside of its compilation in a single binary rock. At the end of this guide, you will be running the nginx very much like in a jail.

Run these steps BEFORE the step "Change ownership to everything" at RAM-only PXE boot in my previous post.
  1. Download latest stable version nginx and upack it.
  2. $ pushd /tmp/wrk
    $ ngx=nginx-1.2.1.tar.gz
    $ cache $ngx http://nginx.org/download/$ngx
    $ tar -xvzf /tmp/cache/$ngx -C .
    $ mv $ngx nginx
    
  3. Add options depending on the features that you want to support. Beside of the default options I only add static compilation options:
  4. $ pushd nginx
    $ ./configure --with-ld-opt="-static -static-libgcc" \
      --with-cc-opt="-static -static-libgcc"
    
    make it (jN means N threads devoted to compilation and linking):
    $ make -j2
    
    ensure that it's not a dynamic executable:
    $ ldd objs/nginx
     not a dynamic executable
    $ popd
    
  5. Copy the nginx executable to rambox's /sbin :
    $ popd 
    $ pushd sbin
    $ chmod +w .
    $ cp ../../nginx/objs/nginx .
    $ chmod -w .
    $ popd
    
  6. Create /usr/local/nginx directories:
  7. $ mkdir -p -m 0755 usr/local/nginx/{conf,logs}
    
  8. Copy some needed libs:
  9. $ chmod +w lib
    $ cp /lib64/{ld-2.12.so,ld-linux-x86-64.so.2} lib/
    $ cp /lib64/{libc-2.12.so,libc.so.6} lib/
    $ cp /lib64/{libnsl-2.12.so,libnsl.so.1} lib/
    $ cp /lib64/{libnss_compat-2.12.so,libnss_compat.so.2} lib/
    $ chmod -w lib
    
  10. Copy mime conf file:
  11. $ cp ../nginx/conf/mime.types usr/local/nginx/conf/
    
  12. Create the nginx.conf file with some basic settings:
  13. $ dd of=usr/local/nginx/conf/nginx.conf << EOT
    # user and group to run nginx
    user www www;
    
    # numbers of dedicated CPUs
    worker_processes 1;
    
    # pid archive
    pid /var/run/nginx.pid;
    
    events {
        # max connections in WAIT 
        worker_connections  128;
      
        # accept & enqueue NEW connections, put them in WAIT
        multi_accept        on;
    }
    
    http {
            include       mime.types;
            default_type  application/octet-stream;
    
            server {
                    listen 80;
    
                    autoindex on;
     
                    location / {
                            root /var/www;
                            index  index.php index.html index.htm;
                    }
            }
    }
    EOT
    
  14. Resume the rambox creation process and once it gets started run:
  15. $ nginx
Enjoy it by browsing at http://HOSTADDRESS

Post install steps

Daemonize nginx by running it at /init script!