Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run django with nginx on a windows machine?

Tags:

nginx

django

I have a django project. I have installed nginx server. I want to run nginx along with django on windows machine. I have tried a few blogs Nginx Django Uwsgi. But all of them required uwsgi. But the uwsgi is not installing on windows and gives "uname" error. Is there any way to install and run nginx and django on windows ? Any pointers will be very helpfull thanks.

like image 570
Akash Deshpande Avatar asked Jan 12 '14 05:01

Akash Deshpande


People also ask

Can I run Django with Nginx?

It takes you through the steps required to set up Django so that it works nicely with uWSGI and nginx. It covers all three components, providing a complete stack of web application and server software. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Can you run Nginx on Windows?

Nginx is a web server that is very popular with Linux and BSD systems. It can also be installed on Windows 10.

How do I run Django on Windows?

Django can be installed easily using pip . In the command prompt, execute the following command: pip install django . This will download and install Django. After the installation has completed, you can verify your Django installation by executing django-admin --version in the command prompt.


4 Answers

After the Django1.9+, the FastCGI support via the runfcgi management command is removed. So you have to use old version django, or use the mod_wsgi + apache.

If you only want to develop the django code, then you can just edit nginx.conf add proxy_pass http://127.0.0.1:8000 into the location option. For example:

    location /api/ {
        proxy_pass http://127.0.0.1:8000; 
    }

    location /static/ {
        proxy_pass http://127.0.0.1:8000; 
    }
like image 119
Kris Zhang Avatar answered Oct 11 '22 06:10

Kris Zhang


Best way (IMHO) is using apache+mod_wsgi. Both uWSGI and Gunicorn are not windows-friendly (albeit uWSGI has cygwin support, so you can eventually try it [if you are brave enough]).

Another approach would be installing a linux vm (virtualbox, vmware...) on the windows machine to host your application and proxy requests from local IIS to the virtual system.

like image 35
roberto Avatar answered Oct 11 '22 06:10

roberto


This question is from a year ago but I'll answer this for the others looking for a solution to their Windows Django/nginx problems.

As uWSGI is clearly not an option for Windows (forget about cygwin/virtual environments). I didn't want to go for mod_wsgi + Apache because I had to merge an existing AngularJS/nginx project together with my newly created Django project.

I ended up using FastCGI, even though Django support for it will soon be deprecated, it still works. This (somewhat badly written but it does help) tutorial helped me with this.

The key actions were:

  • Make sure flup is installed ( pip install flup)
  • Edit your nginx.conf as shown in the tutorial:

    location / {    
    # host and port to fastcgi server
    fastcgi_pass 127.0.0.1:<port nr>;
    fastcgi_pass_header Authorization; 
    fastcgi_hide_header X-Accel-Redirect;
    fastcgi_hide_header X-Sendfile;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors    off;
    fastcgi_param       CONTENT_LENGTH  $content_length;
    fastcgi_param       CONTENT_TYPE    $content_type;
    fastcgi_param       PATH_INFO       $fastcgi_script_name;
    fastcgi_param       QUERY_STRING    $query_string;
    fastcgi_param       REMOTE_ADDR     $remote_addr;
    fastcgi_param       REQUEST_METHOD  $request_method;
    fastcgi_param       REQUEST_URI     $request_uri;
    fastcgi_param       SERVER_NAME     $server_name;
    fastcgi_param       SERVER_PORT     $server_port;
    fastcgi_param       SERVER_PROTOCOL $server_protocol;
    }
    
  • Then run your Django project with fcgi, with the port being the same one as seen above in the .conf python manage.py runfcgi method=threaded host=127.0.0.1 port=<port nr>

Worked for me.
If you don't have any reason like I had for which you really want to use nginx, I'd suggest going for the mod_wsgi + Apache webserver approach, it will probably make your life a lot easier.

like image 35
Fluppe Avatar answered Oct 11 '22 06:10

Fluppe


You can try to use Helicon Zoo to run Django applications on Windows machine with IIS 7+. Helicon Zoo is stable and production-ready.

Tutorial here: Running Django on Windows

like image 28
rukeba Avatar answered Oct 11 '22 05:10

rukeba