📜 ⬆️ ⬇️

nginx + FastCGI (over spawn-fcgi) + lua

And so we have a task. Making the lightweight web server nginx and lua work together (in this case lua via wsapi (lua web server API) -fastcgi).
What do we need? Well, for starters head on his shoulders. Well, that is not something that would be particularly needed, but useful.
And then ... And then the server in our case with GNU / Linux on board (but I think it will come down and, say, FreeBSD), we will look at the example of Ubuntu GNU / Linux, this has happened historically.

We put nginx.
 # apt-get install nginx 

and nginx itself is with us, this is understandable, everything is simple here.
Further miracles begin.
As our programmers working with lua in Ubuntu say, luarocks is so subdued that my mother does not recognize , plus luarocks released version 2.0 a few days before the release of Ubuntu 9.10 and the version was not included in the release, but programmers want it. So luarocks will temporarily be made from raw, and later we will make our repository and we will collect the bag.

but for now ...
    1. $ sudo apt-get install unzip lua5.1 liblua5.1-dev libreadline-dev
    2. $ mkdir -p ~ / build && cd ~ / build
    3. $ wget http://luarocks.org/releases/luarocks-2.0.tar.gz (get the latest version from http://luarocks.org)
    4. $ tar -zxf luarocks-2.0.tar.gz && cd luarocks-2.0
    5. $ ./configure --with-lua-include = / usr / include / lua5.1
    6. $ make
    7. $ sudo make install

Yes, so of course we will sort out some dustbin in the system, but for the time being we will leave it this way, for the time being.

luarocks we set, well done.
we put wsapi-fcgi
 # luarocks install wsapi-fcgi

Yeah, well done, set.
')
now perhaps spawn-cgi is useful to us, so that our wsapi-fcgi would be waiting for us somewhere :)
 # apt-get install spawn-cgi

The next problem is the lack of a startup script for spawn-fcgi in Ubuntu. is solved using approximately such a script:

#! /bin/sh <br/>
<br/>
### BEGIN INIT INFO <br/>
# Provides: spawn-fcgi <br/>
# Required-Start: $all <br/>
# Required-Stop: $all <br/>
# Default-Start: 2 3 4 5 <br/>
# Default-Stop: 0 1 6 <br/>
# Short-Description: starts FastCGI <br/>
# Description: starts FastCGI with spawn-fcgi <br/>
### END INIT INFO <br/>
<br/>
PATH = / usr / local / sbin: / usr / local / bin: / sbin: / bin: / usr / sbin: / usr / bin<br/>
NAME =spawn-fcgi<br/>
PID = / var / run / spawn-fcgi.pid<br/>
DAEMON = / usr / bin / spawn-fcgi<br/>
DAEMON_OPTS = "-f /usr/local/lib/luarocks/bin/wsapi.fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -P $PID " <br/>
. / lib / lsb / init-functions<br/>
<br/>
test -x $DAEMON || exit 0 <br/>
<br/>
set -e <br/>
<br/>
case "$1" in <br/>
start ) <br/>
log_daemon_msg "spawn-fcgi starting" <br/>
start-stop-daemon --start --pidfile $PID --exec $DAEMON -- $DAEMON_OPTS <br/>
echo "done." <br/>
;; <br/>
stop ) <br/>
log_daemon_msg "spawn-fcgi stopping" <br/>
start-stop-daemon --stop --pidfile $PID --retry 5 <br/>
rm -f $PID <br/>
echo "done." <br/>
;; <br/>
restart ) <br/>
echo "Stopping $NAME : " <br/>
start-stop-daemon --stop --pidfile $PID --retry 5 <br/>
rm -f $PID <br/>
echo "done..." <br/>
sleep 1 <br/>
echo "Starting $NAME : " <br/>
start-stop-daemon --start --pidfile $PID --exec $DAEMON -- $DAEMON_OPTS <br/>
echo "done." <br/>
;; <br/>
* ) <br/>
echo "Usage: /etc/init.d/ $NAME {start|stop|restart}" >& 2 <br/>
exit 1 <br/>
;; <br/>
esac <br/>
<br/>
exit 0


we put the script in place in /etc/init.d/spawn-fcgi, add it to the load level, voila! With the launch as fcgi wsapi the issue is resolved.
 # update-rc.d spawn-fcgi defaults 21 


we configure nginx that would give everything that comes to our host which is necessary further for processing of wsapi-fcgi
My example is not particularly correct, just for the needs for which it was written was enough.

in / etc / nginx / sites-available, create the fcgi file with the following content:
  server {
         listen to our-server-wsapi.cxm: 80;
         server_name our-server-wsapi.cxm;
         access_log /var/log/nginx/ourserver-wsapi.cxm.access.log;

         location / {
                           fastcgi_pass localhost: 9000;
                           fastcgi_index index.lua;

                           fastcgi_param SCRIPT_FILENAME / srv / www / nginx / fcgi / lua $ fastcgi_script_name;
                           fastcgi_param QUERY_STRING $ query_string;
                           fastcgi_param REQUEST_METHOD $ request_method;
                           fastcgi_param CONTENT_TYPE $ content_type;
                           fastcgi_param CONTENT_LENGTH $ content_length;
                          }
 }


create a symlink from it in / etc / nginx / sites-enabled, restart nginx

at / srv / www / nginx / fcgi / lua we place our test index.lua
go to the browser and check if it works.

This text was written for internal documentation, but decided to publish, suddenly someone will come in handy.
I did not meet the documentation on such a bundle in Russian (however, in English too)

Source: https://habr.com/ru/post/76487/


All Articles