Skip to content

Run Django under Yaws

q9c9p edited this page · 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:


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

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





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

                    X -> X
        _ ->

            {page, Arg#arg.server_path} 


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

Enjoy your django application on Yaws.

Something went wrong with that request. Please try again.