web.py nginx-egaz erabiltzeko fastcgi moduen spawn-fcgi-gaz debian gnu/linux-en

Instalaziñue:

su
aptitude install nginx-full spawn-fcgi python-pip
pip install web.py
exit

Proiektuentzako karpeta bat sortu, adibidez:

mkdir /home/projects/zpy
mkdir /home/projects/zpy/private
mkdir /home/projects/zpy/public
mkdir /home/projects/zpy/public/static
touch /home/projects/zpy/private/zpy.py

Haulako estrukturie okingo lekidau:

.
├── private
│   └── zpy.py
└── public
    └── static

Oin python-en script-e fcgi moduen erabiltzeko:

nano /home/projects/zpy/zpy.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import web

urls = ("/.*", "zpy")
app = web.application(urls, globals())

class zpy:
    def GET(self):
        return "kaixo mundua!"
        
if __name__ == "__main__":
    web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
    app.run()

Baimenak emon:

chmod +x /home/projects/zpy/zpy.py

FCGI demoniñue sortu:

su
nano /etc/init.d/spawn-fcgi
#! /bin/sh
### BEGIN INIT INFO
# Provides:          spawn-cgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: fastcgi for python
# Description:       fastcgi for python using nginx
#                    placed in /etc/init.d.
### END INIT INFO
#
# original https://gist.github.com/ozeias/47394
# modified to use with python
#
# to install in debian gnu/linux:
# update-rc.d spawn-fcgi defaults
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/spawn-fcgi
NAME=spawn-fcgi
DESC=spawn-fcgi

HOST='127.0.0.1'
PORT=9000

FOLDER='/home/projects/zpy/private'
SCRIPT='zpy.py'
USER='zital'
GROUP='zital'

DAEMON_OPTS="-d ${FOLDER} -f ${FOLDER}/${SCRIPT} -a ${HOST} -p ${PORT} -P /var/run/${NAME}.pid -u ${USER} -g ${GROUP}"
 
test -x $DAEMON || exit 0
 
set -e
 
case "$1" in
	start)
		echo -n "Starting $DESC: "
		start-stop-daemon --start --exec $DAEMON -- $DAEMON_OPTS
	;;
	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --pidfile /var/run/$NAME.pid
	;;
#	restart)
#		echo -n "Restarting $DESC: "
#		start-stop-daemon --stop --quiet --pidfile \
#		/var/run/$NAME.pid --exec $DAEMON
#		sleep 1
#		start-stop-daemon --start --quiet --pidfile \
#		/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
#		echo "$NAME."
#	;;
	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop}" >&2
		exit 1
		;;
	esac
 
exit 0

demoniñue martxan imiñintzeko:

update-rc.d spawn-fcgi enable
update-rc.d spawn-fcgi defaults
/etc/init.d/spawn-fcgi start
exit

Haulako zeuzer agertuko dasku:

Starting spawn-fcgi: spawn-fcgi: child spawned successfully: PID: 4619

Oin nginx konfigureku:

su
nano /etc/nginx/sites-available/python
server
{
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;

	# Make site accessible from http://localhost/
	server_name localhost;

	location /static
	{
		root /home/projects/zpy/public;
		if (-f $request_filename)
		{
			rewrite ^/(.*)$  /$1 break;
		}
	}

	location /
	{
	        fastcgi_param REQUEST_METHOD $request_method;
	        fastcgi_param QUERY_STRING $query_string;
	        fastcgi_param CONTENT_TYPE $content_type;
	        fastcgi_param CONTENT_LENGTH $content_length;
	        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
	        fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
	        fastcgi_param REMOTE_ADDR $remote_addr;
	        fastcgi_param REMOTE_PORT $remote_port;
	        fastcgi_param SERVER_ADDR $server_addr;
	        fastcgi_param SERVER_PORT $server_port;
	        fastcgi_param SERVER_NAME $server_name;
	        fastcgi_param SERVER_PROTOCOL $server_protocol;
	        fastcgi_param PATH_INFO $fastcgi_script_name;

	        fastcgi_pass 127.0.0.1:9000;
	}
}

Defektuzko konfiguraziñue kendu eta python-ena imiñi:

rm -rf /etc/nginx/sites-enabled/*
ln -s /etc/nginx/sites-available/python /etc/nginx/sites-enabled/python
/etc/init.d/nginx restart

Oin kontutan okin estatikoren bat gurebazuien sartun, js, css etc… “static” karpeta barruen sartun biherko dala, “public” karpetan eztau balijoko, adibidez:

“/public/a.txt” eztau balijoko
“/public/static/a.txt” bai balijoko dau :)

Eskerrak:

https://gist.github.com/ozeias/47394 spawn-fcgi-n demonidxuegaitzik :)

One thought on “web.py nginx-egaz erabiltzeko fastcgi moduen spawn-fcgi-gaz debian gnu/linux-en

Leave a Reply

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