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

Thursday, November 6, 2014

Run fcgiwrap with nginx under Slackware

I need to do some test on perl CGI scripts, but with NGINX server, it is not supported directly to run those CGI scripts.  Search around, there is this fcgiwrap tool.  It looks exactly like what I need for the simple test.

Fcgiwrap depends on fcgi library, which has a SlackBuild script.  I install the fcgi library with sbopkg, and proceed to write a SlackBuild script for fcgiwrap.  The fcgiwrap is a very simple program, it has only one C file for the main program.  But it needs a spawning parent to control it, the author provide a simple perl script (on the main page of the fcgirwap project.) to spawn the fcgiwrap program.  I also borrow the startup script from Nginx's FastCGI example and modified a little and name it rc.spawn-fcgi.  Both the scripts are included in the SlackBuild package that I have submitted.

After installing it, I add the following section into the server section of nginx.conf file:
  location ~ \.pl$ {
     root /var/www/cgi;
     try_files $uri =404;
            include        fastcgi_params;
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     fastcgi_pass unix:/tmp/cgi.sock;
   }
After started the spawn-fcgi and reload nginx's config, the perl CGI works without problem.

Tuesday, November 20, 2012

Slackware 14 + nginx + FastCGI + PHP (PHP-FPM)

I like the light-weight and fast web server nginx (Engine X).  But it is not included in the default Slackware packages.

First we need to build nginx for Slackware.  There is SlackBuilds script for nginx.  Download the script from SlackBuilds and nginx source code.

As this writing, the stable ngix version is 1.2.5 and SlackBuild script is for version 1.2.2.  You need to modified the script to match the version.  I also like to build with IPv6 support (--with-ipv6) and don't need the select and poll module (--without-poll-module, --without-select-module), we are running Slackware 14, epoll is the best method.  I also remove lines that include perl module support.  Your preferences may not be the same as mime.  I also change the build script to include nginx's default index.html and 50x.html into /var/html.  Th build script diff is here:

--- a/nginx.SlackBuild 2012-09-28 21:34:38.000000000 -0700
+++ b/nginx.SlackBuild 2012-11-19 17:02:38.712164716 -0800
@@ -25,8 +25,8 @@
 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 PRGNAM=nginx
-VERSION=${VERSION:-1.2.2}
-BUILD=${BUILD:-2}
+VERSION=${VERSION:-1.2.5}
+BUILD=${BUILD:-1}
 TAG=${TAG:-_SBo}
 
 if [ -z "$ARCH" ]; then
@@ -85,9 +85,8 @@
   --group=${NGINXGROUP:=nogroup} \
   --error-log-path=/var/log/nginx/error.log \
   --http-log-path=/var/log/nginx/access.log \
-  --with-rtsig_module \
-  --with-select_module \
-  --with-poll_module \
+  --without-select_module \
+  --without-poll_module \
   --with-http_ssl_module \
   --with-http_realip_module \
   --with-http_addition_module \
@@ -99,14 +98,13 @@
   --with-http_random_index_module \
   --with-http_secure_link_module \
   --with-http_stub_status_module \
-  --with-http_perl_module \
-  --with-perl_modules_path=$installvendorlib \
   --http-client-body-temp-path=/var/tmp/nginx_client_body_temp \
   --http-proxy-temp-path=/var/tmp/nginx_proxy_temp \
   --http-fastcgi-temp-path=/dev/shm \
   --without-mail_pop3_module \
   --without-mail_imap_module \
-  --without-mail_smtp_module
+  --without-mail_smtp_module \
+  --with-ipv6
 
 make
 make install DESTDIR=$PKG
@@ -118,9 +116,11 @@
 find $PKG -perm 444 -exec chmod 0644 {} \;
 find $PKG -perm 555 -exec chmod 0755 {} \;
 
+sed --in-place "s/root[ \t]*html/root \/var\/html/" $PKG/etc/nginx/nginx.conf
 # Remove some other empty and/or unnecessary directories from the package
 eval $(perl '-V:archlib')
-rm -rf $PKG/$archlib $PKG/usr/html $PKG/var
+mv $PKG/usr/html  $PKG/var
+#rm -rf $PKG/$archlib $PKG/usr/html $PKG/var
 
 # Add an init script
 mkdir -p $PKG/etc/rc.d
@@ -150,4 +150,4 @@
 cat $CWD/doinst.sh > $PKG/install/doinst.sh
 
 cd $PKG
-/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
+/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

Now build the nginx server by running the script:
./ngix.SlackBuild

You will have a package file after build /tmp/nginx-1.2.5-x86_64-1_SBo.txz (I am compiling it under Slackware64), install it with the installpkg command.  And change mode of the startup script:

chmod +x /etc/rc.d/rc.nginx

One good thing about Slackware 14 is that we have already PHP-FPM built in the stock php package and does not need to go through the hassle to build PHP-FPM ourselves.  You need to make the startup script executable by

chmod +x /etc/rc.d/rc.php-fpm

Edit the file /etc/php-fmp.conf, change catch_workers_output to yes.  This will allow you to get meaningful error message in the log.

Edit the file /etc/nginx/nginx.conf, I have changed the document root to /var/html and move it to the server part:

 server {
        listen       80;
        server_name  localhost;

 root /var/html;
       ...
 }
Uncomment the line on related to fastcgi:
location ~ \.php$ {
 fastcgi_pass  127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include       fastcgi_params;
}
Note original lines
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
has been changed to the above one.

Now, fire up the php-fpm and nginx server:

/etc/rc.d/rc.php-fpm start
/etc/rc.d/rc.nginx start
I have created a small test file /var/html/php.php:
<html>
<body>
<h1>NGINX + FASTCGI + PHP</h1>
<?php
print "<h2>Hello from PHP!</h2>"
?>
</body></html>
When you access http://localhost, you should be able to see:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

When accessing http://localhost/php.php, you should be able to see:

NGINX + FASTCGI + PHP

Hello from PHP!

You may like to add the start up scripts to your rc.local script, so that the services are automatically started when you boot up your machine next time.

Reference:

* Linux Slackware 13.0 + Nginx + PHP FastCGI using PHP-FPM

Tuesday, July 24, 2012

nginx module reply with header only

When testing with a module written for nginx, I have only headers need to send back and no response body.  I set the content length to be zero and finished the module with the following line:
return ngx_http_send_header(r);
The browser was not receiving anything from the server and waiting till time out.  After digged through the nginx source, it seems nginx need to be informed data has been finished with a call to
ngx_http_send_special (r, NGX_HTTP_LAST);
After adding the necessary code and ended the module with a call to ngx_http_send_special(), it works as expected.