Skip to content

Run Django under Yaws

q9c9p edited this page Sep 13, 2010 · 4 revisions

With some minor changes, main concepts of Run Rails under Yaws apply here too.

Details for running django with Fastcgi can be found at How to use Django with FastCGI, SCGI, or AJP

Let’s say we have a running django instance on localhost, port 3000. First setup a virtual host in yaws config with:

yaws.conf

...
<server localhost>
        port = 80
        listen = 0.0.0.0
        appmods  = </, mod_django>
        listen_backlog = 100
        docroot = /path/to/docroot/
        fcgi_app_server = 127.0.0.1:3000
        <opaque>
            static_paths = /media/   # we want to serv static files under /path/to/docroot/media/ from localhost:80/media/
        </opaque>
</server>
...

<opaque> directive allows adding any {key, value} couples to default configuration, which we will use in our mod_django.erl code.

mod_django.erl

-module(mod_django).

-compile(export_all).

-include_lib("/nclude_path/include/yaws_api.hrl").

out(Arg) ->

    %% checking against the static path settings in opaque directive
    case proplists:get_value("static_paths", Arg#arg.opaque) of 

        P when is_list(P) ->
            Sp = lists:filter(fun(Y) -> Y =/= [] end, 
                lists:map(fun(X)-> string:strip(X) end, string:tokens(P, ","))),

            case lists:filter(fun(Y) -> string:rstr(Arg#arg.server_path, Y) == 1 end, Sp) of
                [_|_] ->

                    %% request path matches one of the static paths
                    error_logger:info_msg("static serving ~p~n",[Arg#arg.server_path]),
                    {page, Arg#arg.server_path};

                _ ->

                case yaws_cgi:call_fcgi_responder(Arg) of
                    R when is_list(R) ->
                        case proplists:get_value(status, R) of
                        404 -> 
                            {page, Arg#arg.server_path}; % Let Yaws try to serve static files
                        _  -> R
                        end;

                    X -> X
                end
            end;
        _ ->

            {page, Arg#arg.server_path} 

    end.

compile mod_django.erl with:


erlc mod_django.erl

and place mod_django.beam in /yawsroot/var/lib/yaws/ebin/

Remember to add FORCE_SCRIPT_NAME = ’’ to your django project settings.py

Enjoy your django application on Yaws.

Clone this wiki locally