Configuring port for django development environment, so the site is available from outside - django

I am experimenting with django and I throw the code on my server like explained in the first chapters of the django book 2.0.
I have apache running on this server, too (port 80). If I stop apache I can start my django site by calling
sudo python manage.py runserver 0.0.0.0:80
If I access it from another machine by
http://myservername:80
it works fine. Now, apache is running an important page, and I don't want to let apache stoped. How do I make mysite available on another port?
Edit: I'll try to explain more:
When apache runs, typing into the adressfield of my browser, shows me the "important wepage".
Starting my django test project with
sudo python manage.py runserver 0.0.0.0:anotherport
and accessing trying to acces it by
http://ipadressofserver:anotherport
does not work.
If apache is tuned off, and I start my django project by
sudo python manage.py runserver 0.0.0.0:80
I can access it by
http://myservername
http://myservername:80 (the browser changes this to http://myservername/
http://myserverIP and http://myservrIP:80 (The latter resolves in the former).
I am not experienced in Serveradministration so please ask me, if there is something specific I can tell you, to help me solve the problem, please ask me, and I'll provide the information - if possible.

Specify a different port when starting the dev server:
$ python manage.py runserver 0.0.0.0:8000
and connect to the site via:
http://myserverip:8000

You should be able to configure your router appropriately to point to any port. This question should more directly relate to how do you expose a specific port to be browsed. Any information you could provide about your router would be more helpful to address this.

Related

How to set virtualenv to stay active on a host server

I created a website that uses vuejs as the frontend and django as the backend with another service running behind everything that im making api calls to.
So django is setup in a way to look at the dist folder of Vuejs and serve it if you run manage.py runserver. but the problem is that my service that I created is
is also in python and it needs to run in a virtualenv in order to work (It uses tensorflow 1.15.2 and this can only run in a contained environment)
I'm sitting here and wondering how I can deploy the django application and keep the virtualenv active and Im coming up with nothing, I've tried doing some research on this but everything I found was not relevant to my problem. I've deployed it and when I close the ssh connection the virtualenv stops.
If there is anyone that can enlighten my ways I would appreciate it.
i think you need to nginx: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
if you are search for keep states just in terminal i suggest tmux https://github.com/tmux/tmux/wiki
You can use uWSGI and nginx to deploy Django apps on server. Here's helpful articles:
https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-centos-7
https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-centos-7
Django official docs also has a page about it: https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/uwsgi/
There are articles from developers so you can refer them in case you get stuck anywhere:
https://www.freecodecamp.org/news/django-uwsgi-nginx-postgresql-setup-on-aws-ec2-ubuntu16-04-with-python-3-6-6c58698ae9d3/
https://medium.com/#biswashirok/deploying-django-python-3-6-to-digital-ocean-with-uwsgi-nginx-ubuntu-18-04-3f8c2731ade1

How can I setup my Django server on LAN

I have made a Django employee portal which will be accessed by LAN only.
It works when another employee opens it by typing the IP address of the server on their web browser.
However I don't have much experience with Django and I think that this is not the proper way to do so. I run my server using python manage.py runserver and use sqlite3 as database.
Is this the correct way to do so? How should I deploy my portal.
I am very new to Django and would appreciate some help.
I am using a windows machine and I used pycharm to make my project.
And also I need to know how can I have the server running even when I close pycharm, as ctrl-C or closing pycharm breaks the server
The simplest way to allow everyone on your network to access your Django webserver is to do python manage.py runserver 0.0.0.0:8000
This allows anyone on the network to access it by using your IP address (or computer name) and the port 8000. (so something like 192.168.1.2:8000)
This of course isn't really nice specially if you intent to use this as a production environment. panchicore's answer should help you setup a good production environment.
Setting up Django and your web server with uWSGI and nginx
There is not an official way to do it, what I do effectively, intranet solutions as well, is with nginx and uWSGI (on ubuntu).
Serving with Windows? perhaps: https://www.toptal.com/django/installing-django-on-iis-a-step-by-step-tutorial is a proper way to do so.
I think for ip address issue you can use host names
https://wesbos.com/localhost-mobile-device-testing/
and for running server in background you can use gunicorn with supervisor check this out https://www.agiliq.com/blog/2014/05/supervisor-with-django-and-gunicorn/

How to configure Elastic IP with django app in aws?

I am building an app using django in EC2-ubuntu and i have associated Elastic ip with my instance.
i have done following steps :
1. first created instance of ubuntu in ec2 free tier.
2. installed python.
3. installed pip.
4. installed django.
5. create a django project using django-admin startproject.
6. run server using these commads python manage.py runserver 0.0.0.0:80
7. created an elastic ip and associated to the instance.
8. configure security inbound settings with http 0.0.0.0:80 address.
9. able to ping my project using any browser.
But the problem is when i am closing my putty session where i supplied runserver command, django project is also stopped. i did not stop it manually.
Please, help me to keep on running after closing putty session as well.
Thanks,
Kripa Sharma
Take a look at this Answer
I highly recommend that you start using Elastic Beanstalk (Python instance) to take care of all these steps for you. Very simple to setup, and no need to worry about any of the steps you listed.
You can use this instruction to see how you can deploy a Django app in less than 5 minutes.
The problem
You are trying to persist the debug server for a remotely deployed application.
You probably need to review the runserver command documentation. Here are the relevant parts:
django-admin runserver [addrport]
Starts a lightweight development Web server on the local machine. By default, the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an IP address and port number explicitly.
...
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
A webserver
Having skimmed the above docs, you may want to look at "How to deploy with WSGI" section, which gives a few recommendations for commonly used Web servers. My favorite, Gunicorn, includes a usage example:
$ pip install gunicorn
$ gunicorn myproject.wsgi
Having decided, and installed a webserver, you'd need to "daemonize" it and expose it to the world.
The former is usually done by creating a service on your OS, for ubuntu it would be either upstart or systemd depending on the version. Gunicorn docs have examples for both.
The latter is usually achieved with an http-server/proxy such as nginx or apache httpd. And again, Gunicorn has an example for us.
You can see why I like it so much ☺️
Epilogue
While technically possible to run the debug server as a service or even in a terminal multiplexer such as GNU screen or tmux, it's not a recommended or stable long term solution.
That said, these are very useful to know about, so read on the above tools and learn to use them, since they would be invaluable to have in your toolset in the future, for example to avoid accidentally terminating a long running command (such as migration), etc.

The page load, but didn't show up the content

I'm trying to connect to the server with IP_address_server:8000, but the page load without ever wanting to connect.
In fact, I start a Django project, and I did python3 manage.py runserver 0.0.0.0:8000. In the project settings.py, I've included IP_address_server in ALLOWED_HOSTS (on the server), but I got the same issue.
Could anyone be able to tell me what could be the problem?
First, if you are (really meant to)hosting your django application on cloud, you should not use django's inbuild server, that is manage.py runserver. Check official docs, quoting here:
Now’s a good time to note: don’t use this server in anything resembling a production environment. It’s intended only for use while developing. (We’re in the business of making Web frameworks, not Web servers.)
Now, if I am wrong and your application is indeed hosted on Nginx/apache, check server logs, for Nginx, /var/log/nginx/, for Apache, /var/log/apache2/.
If not, you can follow some of Django deployment guides, eg here or here

django - run project from PyCharm server not accessible

When I run my django server from PyCharm it is not accessible from the outside world.
I read that you can run in the cmd like that, and it works.
python manage.py runserver 0.0.0.0:8000
How do I run it to be accessible to the outside world from PyCharm?
I need to run it from there is order to debug.
thanks!
At the upper-right corner (by default) of PyCharm you can edit the configuration for your server:
In there specify as host 0.0.0.0.
Don't forget to take care of your firewall/NAT by opening and forwarding the appropriate ports.