DigitalOcean Droplet not serving django app with python runserver command - digital-ocean

i know i have seen similar questions on the issue but most of them seemed to be solved by allowing the port 8000 by using the command sudo ufw allow 8000. i have tried that but it is still not displaying when i visited my droplet_ipaddress:8000 link. i followed the official tutorials https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04.
SYSTEM INFORMATION
OS = Ubuntu 22.04 LTS
Python = 3.10
Django = 4.0.4
the only difference with my work and the project process is that i did not build an dummy django project on my digitalocean vps rather i use git to clone my project repo and did the neccesary dependency installing using pip. Has anyone encountered the same problem and is there any solution.
when ever i ran python manage.py runserver 0.0.0.0:8000
the output is shown below as expected … but my page doesn't not display as expected
Performing system checks...
System check identified no issues (0 silenced).
You have 33 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): account, accounts, admin, ads, auth, chat, contenttypes, notification, payments, report, sales, sessions, sites, socialaccount.
Run 'python manage.py migrate' to apply them.
May 10, 2022 - 11:41:04
Django version 4.0.4, using settings 'ma_mall.settings.production'
Starting ASGI/Channels version 3.0.4 development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
when i visit the page its says
searching for the allowed port on the system gave this
which clearly shows the the port is now allowed to serve the app

In Digital Ocean, it is possible that you have a second firewall enabled that can be configured directly on the platform. You can find it under Droplet>Networking. Check if a firewall exists and adjust it for your port. Since two firewalls are a bit cumbersome and pointless, I would recommend using only the Digital Ocean firewalls and disabling ufw permanently via ufw disable.

Related

Django webserver automatically shuts off on local server when I try to access the admin section

I'm a noob trying to learn Django for the first time, I created a project in a virtualenv on Windows 10. It worked well in the beginning where I was able to login to the admin section after running '''python manage.py runserver'''
But now when I run the same command I'm able to see the Django landing page but as soon as I try to hit http://localhost:8000/admin/ or http://127.0.0.1:8000/admin the server automatically disconnects and I get the "This site can’t be reached" error on Chrome.
I tried changing the port number by running python manage.py runserver 0.0.0.0:8001 but it didn't work. I tried to check if the port (8000) is currently in use by running the cmd (as an admin) netstat -a -b but couldn't find any issues.
The server just quits without any error message
Edit: Currently using Python 3.7.0 and django-3.0.1
There's a ticket about this issue: https://code.djangoproject.com/ticket/31067.
This seems to be a bug in Python 3.7.0, and appears to be fixed in Python 3.7.1. It's still unknown what the exact trigger is for this bug.
Since Django officially only supports the latest patch release of a Python series, this won't be fixed in Django. You can either upgrade your Python version to the latest patch release of 3.7, or downgrade Django to 2.2.
It's a Django 3.0 issue as I've seen. There are so many issues on GitHub, regarding this error.
You may try downgrading to Django 2.* versions for now. Version 2.1/2.2 works fine.

Setting up Django on a clean VM: issue linking runserver to VM's ip

I'm setting up my Django site on a clean Ubuntu VM. Gone through the process of installing the necessary tools, I follow this tutorial and apart from the additional python packages I've added for my site it's the same process.
According to this instructional guide to check whether everything is running smoothly you run python manage.py runserver 0.0.0.0:8000 and it should then run on the IP:8000 of the site.
above is the instance of this running
and this above shows the external ip of the google cloud instance running this.
However when i try to visit http://35.227.49.155:8000/ I get This site can't be reached and it's double confirmed with no activity on the shell on a request picked up.
UPDATE
Since as suggested that likely google cloud's firewall might be blocking this I've tried made this port open
but still can't get it to work.

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.

How to setup SSL on a local django server to test a facebook app?

I've configured my local machine's HOSTS configuration to access the local server ( # 127.0.0.1 ) whenever I hit http://www.mydomain.com on the browser.
And I was using this to interact with facebook's graph api to build my app. But now facebook requires us to have an HTTPS url or rather an SSL secured url to interact with their api.
So the question is -> How do I setup SSL on a local django server ?
Not to necro a thread, but I found this tool to be extremely easy to use.
It's a premade django application with very simple install instructions.
You can add a certified key once it is installed simply by running:
python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key
I hope this helps any passer-by who might see this.
With django-extensions you can run the following command:
python manage.py runserver_plus --cert certname
It will generate a (self-signed) certificate automatically if it doesn't exist. Almost too simple.
You just need to install the following dependencies:
pip install django-extensions
pip install Werkzeug
pip install pyOpenSSL
Now, as Ryan Pergent pointed out in the comments, you lastly only need to add 'django_extensions', to your INSTALLED_APPS and should be good to go.
I used a tunnel before, which worked, but this is much easier and comes with many other commands.
Short answer is you'll need to setup a proper webserver on your development machine. Use whichever one (Apache, nginx, cherokee etc) you're most familiar with.
Longer answer is that the django development server (manage.py runserver) isn't designed to do SSL etc and the effort to make it do so is likely greater than you'd want to spend.
See discussions of this passim on the django-users list: http://groups.google.com/group/django-users/browse_thread/thread/9164126f70cebcbc/f4050f6c82fe1423?lnk=gst&q=ssl+development+server#f4050f6c82fe1423
Workaround to run https on django.
This can be done with stunnel that lets the Facebook server and stunnel on your machine communicate in SSL and stunnel turns around to communicate with Python in HTTP. First install stunnel. For instance in Mac OS X:
brew install stunnel
Then you need to create a settings file for stunnel to execute. You can create a text file anywhere. For instance, you can create dev_https and input:
pid=
cert=/usr/local/etc/stunnel/stunnel.pem
foreground=yes
debug=7
[https]
accept=8001
connect=8002
TIMEOUTclose=1
stunnel creates a fake certificate. By default on Mac, it’s at /usr/local/etc/stunnel/stunnel.pem. It’ll bring up a warning on your browser saying that your webpage can be fake but Facebook operations still work right. Since stunnel has to listen on one port and Python’s development server cannot run on the same server, you must use different ports for accept (incoming) and connect (internal). Once you have your dev_https file or whatever you called it, run
sudo stunnel dev_https
to start the tunnelling. Then start your Python server.
HTTPS=1 python manage.py runserver 0.0.0.0:8002
Environment variable HTTPS must be set to 1 for it to return secure responses and since we previously set the internal port to 8002, we listen on 8002 from all incoming IPs. Then, your IP:8001 can accept HTTPS connections without changing your webserver and you can continue running another instance of HTTP Python server on a different port.
ref:
https://medium.com/xster-tech/django-development-server-with-https-103b2ceef893
I understand this has already been answered, but for a clearer solution:
Step 1: Install library
pip install django-sslserver
Step 2: Add to installed apps in settings.py
INSTALLED_APPS = [
'sslserver',
'...'
]
Step 3: Run the code using runsslserver instead of runserver. Certificate & key are optional.
python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key
This doesn't solve the automatic testing issue via
./manage.py test
but to run a server with HTTPS you can use RunServerPlus: http://pythonhosted.org/django-extensions/runserver_plus.html
Just install django-extensions and pyOpenSSL:
pip install django-extensions pyOpenSSL
and then run:
python manage.py runserver_plus --cert cert
I've been able to setup ssl on django's test server by using stunnel. Here is some info on how to set it up
Just a note, I wasn't able to get it working using the package provided by debian in apt-get and I had to install from source. In case you have to do the same, please check out the excellent instructions debian forums on how to build debian packages.
There are plenty of instructions online and also on stunnel FAQ on how to create your pem certificate, but ultimately dpkg-buildpackage on Debian built it for me.
I would imagine that things could actually be more straight forward on Windows.
I then was able to make pydev in eclipse start the test server (and also attach to it) by adding a HTTPS=1 environment variable under "Debug Configurations" -> "Environment" -> Variables
I got the same problem when wanna test Sign up using Facebook. After use django SSL Server from https://github.com/teddziuba/django-sslserver. This problem is solved. You may need it too.
This discussion page is really old, earlier Django does not supported SSL, it needs to be done through stunnel or Werkzeug.
Django now supports SSL configuration with django-sslserver:
https://djangopackages.org/packages/p/django-sslserver/
Add in install app and pass certs in command line.

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

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.