Django Application running on Ubuntu VPS: This site can’t be reached - django

I am running an ubuntu 16.04 cloud VPS server. I've set up a venv and activated it, and installed django.
I run the server with
python3 manage.py runserver 0.0.0.0:8000
I am trying to access this application from a remote computer (not inside the same LAN); I'm trying to make the application visible to the world outside the VPS and VPLAN. When I try to access the site in my home computer broswer like: xx.xx.xxx.xxx:8000 I get the error:
This site can’t be reached. http://xx.xx.xxx.xxx:8000/ is unreachable.
Now I've tried a traceroute and it seems to reach the server ok. I also did
sudo ufw enable
sudo ufw 8000 allow
sudo iptables -S | grep 8000 (and see the proper entries)
In the settings file I have:
ALLOWED_HOSTS = ["*", "0.0.0.0", "localhost", "xx.xx.xxx.xxx","xxx.temporary.link"]
If I wget localhost:8000 I get a response fine. I have tried doing all of the above as root and as another dedicated user but it makes no difference.
I ran through this guide
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
and I still have the same issue.
Does anyone have any other ideas? Thanks in advance

Try:
sudo ufw allow 8000
Not:
sudo ufw 8000 allow

Related

Why would varnish.service suddenly change its port? (From 80 to 6081)

I have a WordPress site with gunicorn and varnish running on an AWS instance.
This morning, the website gave a "502 Bad Gateway nginx" error.
Upon investigation, it looks like the varnish.service port was:
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
According to some notes, the port needs to be 80 and not 6081. Changing the port to 80 fixed the nginx error.
This issue seems to happen about once a year where the varnish.service port suddenly changes by itself and someone has to manually change the port back to 80.
So my question is - why would varnish.service suddenly change its port? As far as I know, there were no updates or changes anywhere.
It depends on what file you're editing.
Make sure you're editing /etc/systemd/system/varnish.service. If that file isn't there, just run the following command:
sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/
When you're done editing the port, just run the following 2 commands:
sudo systemctl daemon-reload
sudo systemctl restart
See https://www.varnish-software.com/developers/tutorials/installing-varnish-ubuntu/#systemd-configuration for a detailed tutorial.

port number not accessible when running webpack-dev-server

I have a centOS 7.2 box as my web server hosted in aws. I found something interesting : when I run my web site using a nginx docker container, I'm able to access it from my local machine. i.e. run docker command
docker run -d -p 8000:80 my-nginx-image
and access the web site through the below url (my local machine is connected to that aws host machine via a vpn connection)
http://10.77.20.253/index.html
This works perfectly well. However, when I try to host the site using webpack-dev-server, i.e.
webpack-dev-server --host 0.0.0.0 --port 8000
I can access it from that web server with no problem, but I can't access it from my local machine. I always get a timeout error.
I then did a
netstat -anp
on that linux box, I noticed that when running from docker, it is listening on
:::8000
while when I run from wds, it was listening on
0.0.0.0:8000
I'm not sure what i'm missing here, so far I have tried
webpack-dev-server --host localhost --port 8000
webpack-dev-server --host 127.0.0.1--port 8000
webpack-dev-server --host 10.77.20.253 --port 8000 (the internal ip address)
but none of them works.
Any thoughts on it??

How do I access my runserver after compile my django script from SSH?

I ran my script from SSH. On my browser, I tried to go to that specific IP:Port and it doesn't display?
python manage.py runserver
or
python manage.py runserver 182.110.213.173:8005
Don't use the dev server to make your site publicly available, it's insecure. If you publicly available, then deploy it properly.
If you are still testing, another option is to use an SSH tunnel.
ssh -L 9000:localhost:8000 user#yourserver.com
Now, run the dev server on the remote machine:
python manage.py runserver 8000
Now port 8005 on the remote machine is forwarded to port 9000 on your local machine, and you can access your site at http://localhost:9000 as long as the ssh tunnel is open.

Local Django website won't load in browser

I'm guessing there's a very simple solution to this, but I searched every forum and setup guide and can't figure it out:
I built a Django/CentOS-6.3 environment on my local server (using VirtualBox and Vagrant). When I startup my server in the vagrant terminal with 'python manage.py runserver [::]:8000' it starts up with no errors.
Validating models...
0 errors found
May 31, 2013 - 13:56:15
Django version 1.5.1, using settings 'mysitename.settings'
Development server is running at http://[::]:8000/
Quit the server with CONTROL-C.
However, when I try to navigate to 'http://127.0.0.1:8001' in my browser (I set up port forwarding from port 8000 to port 8001 in my Vagrantfile), the browser just hangs for 5 minutes until it times out, then it returns the message:
> The connection was reset
> The connection to the server was reset while the page was loading.
> ...
This is the exact same message I get from the browser even after I shut down my local server. My computer obviously recognizes this as a forwarded port, because any other port I try (such as 8000) instantly returns an error saying that it can't establish a connection to the server at 127.0.0.1:8000.
With regard to the server files, I have done many similar setups with Django/Ubuntu in the past and have never had any issues, but there must be something different about Django/CentOS that is causing this to happen (or maybe I made a mistake someone in one of my server files). I have followed guides for setting up Django & PostgreSQL on CentOS, too, but to no avail. I'll comment some of the files I have created/edited below.
If anyone has a solution, or even has advice on where to start looking for errors, I would very much appreciate it.
If your network is configured correctly and your django application with
python manage.py runserver 0.0.0.0:8000
and you still can't access your django app from the VM host there is almost certainly a firewall issue. The solution above is good if you are running iptables.
I deployed CentOS 7 on a virtualbox VM from a Windows 7 host. I didn't know that this distribution uses firewalld, not iptables to control access.
if
ps -ae | grep firewall
returns something like
602 ? 00:00:00 firewalld
your system is running firewalld, not iptables. They do not run together.
To correct you VM so you can access your django site from the host use the commands:
firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --reload
Many thanks to pablo v for pointing this out in the post "Access django server on virtual Machine".
the host's "127.0.0.1" is not the same as the guest's "127.0.0.1". Per default the command
python manage.py runserver
listens only to the guest's localhost. You should be able to test it from within the vm (use "vagrant ssh" to login) and run
curl -I http://127.0.0.1:8000/
The host as a different IP. To access the development server from the host you have to start it without ip restriction:
python manage.py runserver 0.0.0.0:8000
Yes:
python manage.py runserver [::]:8000
should be the same. But that's IPv6 syntax AFAIK. Are you sure that the "manage.py runserver" command supports IPv6 by default? I've never used ipv6 addresses w/ django, but looking at the source (https://github.com/django/django/blob/master/django/core/management/commands/runserver.py) there seams to be a flag that the default to False ("--ipv6"). Perhaps that's the "real" problem?
Regards,
For a similar problem,
This command worked like a charm for me
python manage.py runserver [::]:8001
Check your iptables, and stop it. Ubuntu commonly does not open the iptables when it starts.

Pycharm: run server with root permission

Can I run django-server in Pycharm with root permission?
I need to start the server (in debug mode) at the port 80.
I set host to 0.0.0.0 and port to 80 but if when I try to start it from Pycharm I get this error:
Error: You don't have permission to access that port.
I also tried to run this custom command:
sudo runserver 0.0.0.0:80
but it fails. I use macOsX Lion, any suggestions?
You can either run PyCharm as root itself and its child processes will inherit the root permission or you can use port >= 1024.
If you want to run it to be accessible on port 80 while running on the unprivileged port, consider setting up some reverse proxy on port 80 that will redirect traffic to Django port >= 1024 so that it can run without root.
I'd recommend using nginx as a front-end.
Also check this question.
Run PyCharm from Terminal like so:
sudo /Applications/PyCharm.app/Contents/MacOS/pycharm
You need to launch the binary as sudo.
This will allow PyCharm to run the dev server as root on port 80.
replace 0.0.0.0 with localhost ;-)