FastCGI Dynamic Server Config. for Django/Apache - django

I'm trying to run Django 1.4 with Pyton 2.7, Flup and mod_fastcgi on Apache. So what I did was:
Add mod_fastcgi to httpd.conf
Create two files : .htaccess and index.fcgi in my public web root inside a directory called portal - c:\xampp\htdocs\portal - the actual Django project is in d:\projects\portal so inside index.fcgi I have the following:
#!C:/Python27/python.exe
import sys, os
from django.core.servers.fastcgi import runfastcgi
sys.path.append("D:/projects/portal")
os.environ['DJANGO_SETTINGS_MODULE'] = "portal.settings"
runfastcgi(method="threaded", daemonize="false")
But when running localhost/portal/ I get the following error in my Apache error.log
File "C:\Python27\lib\site-packages\flup\server\fcgi_base.py", line 1020, in _setupSocket
'If you want FCGI, please create an external FCGI server '
It's telling me to create an external FCGI server which I could but I need it to be on a dynamic server. Any ideas?
Update:
My setup is on a Windows server. I can also start a Linux server so the question is more of Apache, FastCGI and adding sites witout changing any config on the server - Apache/FastCGI Server

You did not start your fcgi server.

Related

How to run wsgi along the side of the daphne ASGI for django channels

i am using django channels in my project using using official django channels v2, my simple channels app is completed and working fine if run python manage.py runserver
but
i want to run django channels in different port so i am now using daphne
using daphne my_project.asgi:application --port 8001 it working fine in 8001 port
INFO Starting server at tcp:port=8001:interface=127.0.0.1
INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
and i also run python manage.py runserver in another terminal parallely working fine. now my both channels in 8001 and django in 8000 port working correctly but my runserver command running ASGI/Channels instead of wsgi development server,
Starting ASGI/Channels version 2.2.0 development server at http://127.0.0.1:8000/
instead of
Starting development server at http://127.0.0.1:8000/
settings.py
ASGI_APPLICATION = 'my_project.routing.application'
WSGI_APPLICATION = 'my_project.wsgi.application'
if i debug any function in views.py request, it is ASGI request instead of django wsgi request
asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
django.setup()
application = get_default_application()
my question is:
how to get django request instead of ASGI request in our normal function view request(like def index(request)) or if we install django channels every request become ASGI request?
what is the use of the python mange.py runworker command
Like you can read here: https://asgi.readthedocs.io/en/latest/
ASGI (Asynchronous Server Gateway Interface) is a spiritual successor
to WSGI, intended to provide a standard interface between
async-capable Python web servers, frameworks, and applications.
Where WSGI provided a standard for synchronous Python apps, ASGI
provides one for both asynchronous and synchronous apps, with a WSGI
backwards-compatibility implementation and multiple servers and
application frameworks.
so answer for your question nr 1 is: Yes, all requests will be ASGI.
Question nr 2 - it's a command to run multiple workers to process your channel requests in asynchronous way https://channels.readthedocs.io/en/1.x/deploying.html#run-worker-servers

How to serve Flask app on different port than main site running on port 80?

Read through the other threads, but none seemed to address my need.
I have a website running fine on default port 80/443 using Apache on CentOS 7.
I have now installed gunicorn and copied my files to a /api subdirectory on the server. I am able to start gunicorn successfully using "gunicorn -b 0.0.0.0:8000 api:app"; however, I don't seem to be able to browse to https://example.com:8000 (site can't be reached).
Do I need to use a VirtualHost's file specifically for the Flask /api directory? If so, what would that look like without disrupting the main site?
EDIT: Note this is a Flask-RESTful API, no static files or anything on the Flask side. Just endpoints/routes to serve.

Eventlet is_monkey_patched issues False

Hello I have a django app. My whole system configuration is the following: python 3, django 1.11, eventlet 0.21.0.
1) Nginx as an upstream server:
upstream proj_server {
server unix:///tmp/proj1.sock fail_timeout=0;
server unix:///tmp/proj2.sock fail_timeout=0;
}
2) Supervisor that controls workers. There is a gunicorn worker:
[program:proj]
command=/home/vagrant/.virtualenvs/proj/bin/gunicorn -c /vagrant/proj/proj/proj/deploy/gunicorn.small.conf.py proj.wsgi:application
directory=/vagrant/proj/proj/proj/deploy
user=www-data
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/proj.log
3) This is a gunicorn.small.conf content:
bind = ["unix:///tmp/proj1.sock", "unix:///tmp/proj2.sock"]
pythonpath = "/vagrant/proj/proj/proj/deploy"
workers = 2
worker_class = "eventlet"
worker_connections = 10
timeout = 60
graceful_timeout = 60
4) And this is proj.wsgi content:
"""
WSGI config for proj project.
This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.
Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.
"""
import eventlet
eventlet.monkey_patch()
from eventlet import wsgi
import django.core.handlers.wsgi
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
So, as you can see there is a chain: nginx as an upstream server calls one of the gunicorn eventlet workers using two sockets proj1.sock or proj2.sock.
Note that according with the eventlet documentation I try to use eventlet.monkey_patch() as early as possible. The most appropriate place for that is proj.wsgi that is called by gunicorn in the first place.
However it seems that library isn't monkey patched.
To check this I added to the proj/proj/proj/__init__.py (the first module that called by the django application) the following code:
import eventlet
import os
print("monkey patched os is: " + str(eventlet.patcher.is_monkey_patched('os')))
print("monkey patched select is: " + str(eventlet.patcher.is_monkey_patched('select')))
print("monkey patched socket is: " + str(eventlet.patcher.is_monkey_patched('socket')))
print("monkey patched time is: " + str(eventlet.patcher.is_monkey_patched('time')))
print("monkey patched subprocess is: " + str(eventlet.patcher.is_monkey_patched('subprocess')))
then i issued **./manage.py check** and got that answer:
monkey patched os is: false
monkey patched select is: false
monkey patched socket is: false
monkey patched time is: false
monkey patched subprocess is: false
What am I doing wrong?
What if you change proj.wsgi file content to one line raise Exception? That should eliminate eventlet from suspects.
I'm not good with Django, here's a pure speculation:
based on name, proj.wsgi is executed when WSGI server is about to start
manage.py check doesn't seem to be related to remote network service (WSGI), seems to be a general management command, so it shouldn't execute WSGI related code
One possible solution, taken from your question text:
proj/proj/proj/init.py (the first module that called by the django application
Try to put monkey_patch call in there.
P.S.: you don't need supervisor for gunicorn, its master process (arbiter) is designed to run forever in spite of problems with workers.

django-websocket-redis development server not working

I'm working on a Django app that's using the django-websocket-redis app to publish events from the server side.
I've successfully configured a test server using nginx and uWSGI to route both HTTP and WS requests correctly to my Django app, but on my local development environment I cannot get this working.
According to the django-websocket-redis documentation, it's enough to start the Django development server and everything should work fine, but it seems like this is far away from reality.
Here is what I've checked:
redis is running on localhost:6379 and is responding to PING requests
tried to run the server on different ports (80, 8080, 8000) to check if the django-websocket-redis makes any assumption about the development server's port, but nothing changed
searched for solution online, but there is nothing about this topic
On my local environment on the client side I see a 404 error when my app tries to connect to the local WebSocket. My settings.py sets the WEBSOCKET_URL to the correct URL (on test server it's working, but locally isn't).
Anyone has an idea what am I doing wrong? Thanks!
I've found a workaround to this.
I've modified my wsgi.py to this and it works now:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
from django.conf import settings
from ws4redis.django_runserver import WebsocketRunServer
if settings.DEBUG:
_django_app = get_wsgi_application()
_websocket_app = WebsocketRunServer()
def application(environ, start_response):
if environ.get('PATH_INFO').startswith(settings.WEBSOCKET_URL):
return _websocket_app(environ, start_response)
return _django_app(environ, start_response)
else:
application = get_wsgi_application()
UPDATE: it's not needed to create a custom management command as I mentioned previously, so I've deleted that part of the answer.

Django & Apache: How to debug on Testing server

I am trying to debug an issue that happens on our testing server. So how do I make it so that I can access our testing server when I start Django by typing:
python manage.py runserver
?
Does it have to pass through Apache? If so, I need to configure Apache somehow but I am not using mod_wsgi and so, don't know how to do this.
Thanks! :)
the test server runs its own web server. the defaul options starts a server on
http://127.0.0.1:8000/, which you can then open in your browser
you can specify an optional ip address/server using
manage.py runserver ip:port
using ip 0.0.0.0 for all network interfaces