Could someone tell me how I can run Django on two ports simultaneously? The default Django configuration only listens on port 8000. I'd like to run another instance on port xxxx as well. I'd like to redirect all requests to this second port to a particular app in my Django application.
I need to accomplish this with the default Django installation and not by using a webserver like nginx, Apache, etc.
Thank you
Let's say I two applications in my Django application. Now i don't mean two separate Django applications but the separate folders inside the 'app' directory. Let's call this app1 and app2
I want all requests on port 8000 to go to app1 and all requests on port XXXX to go to app2
HTH.
Just run two instances of ./manage.py runserver. You can set a port by simply specifying it directly: ./manage.py runserver 8002 to listen on port 8002.
Edit I don't really understand why you want to do this. If you want two servers serving different parts of your site, then you have in effect two sites, which will need two separate settings.py and urls.py files. You'd then run one instance of runserver with each, passing the settings flag appropriately: ./manage.py runserver 8002 --settings=app1.settings
One other thing to consider - django's session stuff will use the same session cookie for each site, and since cookies are not port specific, you'll have issues with getting logged out every time you switch between windows unless you use multiple browser sessions/private browsing during development.
Although this is what you need to do when logging in as 2 different users on the same site, logging into 2 different sites both running django on different localhost ports doesn't have to work like this.
One easy solution is to write a simple middleware to fix this by appending the port number to the variable name used to store your session id. Here's the one I use.
The built-in web-server is intended for development only, so you should really be using apache or similar in an situation where you need to run on multiple ports.
On the other hand you should be able to start up multiple servers just by starting multiple instances of runserver. As long as you are using a separate database server I don't think that will have any extra problems.
If you need more information about the configuration of server/servers you can check out Django documentation related to this topic.
Related
I am using Django and channels (for WebSockets).
Earlier, when I was developing, I created some objects in memory when a user does a request, and then websockets can use these objects.
After, I run the production server with ssl, and for testing, I had to run apps separately: python manage.py startsslserver and daphne ... project.asgi:application.
And now sockets do not have access to objects, which are initialized in django server.
Anybody knows, how I can solve this problem?
More information is needed for a clear answer. django websocket problems are usually associated with other causes such as nginx.conf or daphne.service etc..
This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 5 years ago.
I am getting into Flask/Python model and it seems to be getting along fine in the initial stages. Is there a way to run python application as a web application on a simple desktop computer and use that app over LAN? If yes, what will be the process for that?
I mean, I understand that frameworks like flask/django/bottle run their own server instance which results in the execution of such web apps. In this way they are practically acting as IIS/Apache. Correct?
The reason for this question is that this app will be accessed by only 4-5 individuals & we all are a part of the same team.
If the number of users are going to be limited to 4-5, then django server might just be enough for you. You'll just need a router and devices with WiFi access or just an access to the router.
You can simply run your server as,
python3 manage.py runserver 0.0.0.0:8000
python or python3, depending on what you're using.
After this your django project would be visible on all the devices connected to your router, at the address,
Local-IP-Address-Of-Device-Runing-Django-Project:8000.
Note: Django is excellent during the development phase but it's not recommended for production use, or when users increase. see docs here
So I recommend that if number of users increase(increase in load), ideally you should switch to gunicorn with ngnix or apache server (gunicorn is easy and widely used for python apps. It usually works well ngnix as a reverse proxy; entrance point to your server). There are many tutorials for hosting your website using gunicorn with ngnix as a reverse proxy.
Hope this helps. Thanks.
I have multiple Django projects being used as individual services, they are then all used by each other in return.
This then means all the services are set running on individual ports which can be a bit unreliable as I need to remember when starting the project with
manage.py runserver 0.0.0.0:8080
Ideally for each project I would just use the runserver command and it would know which port to run on automatically.
Is this possible without the need of bash aliases?
This is well beyond the scope of what the development server should be doing. If you need to run your apps in a way that they can actually talk to each other, even in development, you should probably be using a more configurable server. Gunicorn would be ideal. Then you could use something like Foreman (or the Python port, Honcho) with a Procfile that lists all the apps and their ports, then start the whole thing with a single command.
Consider this: that information has to be stored somewhere. Hence you could put it in a configuration file and make a script that loads the appropriate host and port to run. The manage.py file doesn't support a particular configuration file so you'll need to do this outside of manage.py. The script would then callmanage.py runserver host:port` with the correct details.
I have ever tried to do this, I thought maybe I cound the set default port in the setting.py.
When I dug a bit to the code, I found that the default port number is defined in django/core/management/commands/runserver.py. It's used by all django projects, so you can't set individual default ports for different django projects.
I created a Django Python application in localhost and setup virtualenv for running python application. I run this application from terminal and can access with this url- http://127.0.0.1:8000/ . On hosting time (server) how can i access this application using url?
I suggest you to check out this page of the django documentation if you haven't already: https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
Deployment of a web application is not as easy as running the python development server, that is extremely simpler and is discouraged to be used on a server by the django documentation itself.
On a theoretic point of view, the reason why you can't access remotely the development server is because it binds the server on local only access: to make it accessible from an external computer you need to bind it to listen on 0.0.0.0 instead of 127.0.0.1, then you will be able to reach it on example.com:8000 depending on what the server's domain is and what port you choose. To use 0.0.0.0 you call manage.py runserver like this:
./manage.py runserver 0.0.0.0:8000
but avoid to do it, and opt for a more robust deployment :)
If your trying to deploy your application for the first time, and you want to get a feel of accessing your app from url, try heroku, https://www.heroku.com/
Easy and you don't have to worry about setting up everything.
I would love to be able to use Python and Django for web applications at the company I work for. We currently use PHP because everyone is familiar with it and easy to deploy for a large number of clients. We can host anywhere between 10 to 100 websites on a single virtual server.
Is it possible to serve a number of websites from a single Apache and Python installation? Each website must have their own domain among other things, such as email accounts.
I wouldn't use Apache, the current best practice is an Nginx frontend proxying requests to uWSGI servers. Read about the uWSGI Emperor mode. It's very versatile. http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html. Each individual app can be modified, removed added to dynamically. We use it at PythonAnywhere to serve thousands of web applications
There are other WSGI servers that you can use as well. uWSGI just seems the most scalable in my experience.
Yes, It is definitely possible. In our setup, typically we have django behind mod_wsgi, Apache and nginx
You can configure apache's Virtualhost, to point to a specific mod_wsgi which in turn points to specific code.
Quoting from here - Refer to the SO post for further information.
There are at least two methods you can try to serve from a single
instance:
Use apache + mod_wsgi and use the WSGIApplicationGroup and/or
WSGIProcessGroup directives. I've never needed these before so can't
be completely sure these will work the way you want, but regardless
you can definitely use mod_wsgi in daemon mode to greatly improve
your memory footprint.
You can play with Django middleware to deny/allow URLs based on the
request hostname (see HttpRequest.get_host() in the Django docs).
For that matter, even though it would be a slight performance hit,
you can put a decorator on all your views that checks the incoming
host.
Yes, you can easily serve up many sites using a single Apache / mod_wsgi installation. Typically you would do that with a separate virtualhost section for each website. See the virtualhost docs. You want to use a different servername directive in each virtual host config to specify what hostnames get routed to which config. See more detailed documentation in name based virtual hosts