Access Django App on AWS ec2 host - django

This issue might seem very trivial but please try to suggest a solution for this if possible.
I have deployed a django App on AWS ec2 host and I am able to run the following command successfully.
(venv)[ec2-user#ip-xxx-xx-xx-xx abc]$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
January 03, 2016 - 13:15:31
Django version 1.7.1, using settings 'abc.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
But I am not able to access http://127.0.0.1:8000/ from the browser.
On googling it's suggesting to use nginx or gunicorn. I am not sure if nginx, gunicorn etc. are to be used for this.
Can someone please let me know how can this be accessed from browser ?
Thanks,

You need to open HTTP port in AWS instance menu.(all ports except ssh closed in AWS)
Go to your console.aws.amazon.com, then pick your instance and go to last menu item "security groups". It lauch wizard, click on "Inbound" in bottom menu, then "edit", and add HTTP or any port what you want :)
And be sure you using your public AWS IP, to open in browser
Add some screen for you, hope it help:

In order to archive this all you need to do 3 simple steps.
Go to the EC2 Console and open up the security group of your instance. go to inbound tab.
click Edit and then press add rule...add entry with type of Custom TCP Rule and port 8000..
Go to your Django project,your app and open up the settings.py script. List your IPv4 address in the allowed_hosts section.. Ex : ALLOWED_HOSTS=['public IPv4']'
Now in the console run command python manage.py runserver 0.0.0.0:8000...
Open any web browser(from any device) and open the link 'public IPv4':8000
No need any app servers like Apache, NginX..

#Baterson is right. You should open port 8000 for your server first.
Official document is here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html . It is highly detailed.

Related

Deploy Django Application on Cloud, but even cannot get access to ip_address:8000

I am a beginner on Django. And try to deploy a testproject on cloud server to check if it works or not.
Server: Ubuntu 16.04
And after I create virtualenv on the server with nginx installed.
I execute below:
python manage.py runserver 0.0.0.0:8000
And then I go to the browser to access my server's http://ip-address:8000.
But it failed to show anything of my application.
I already added the ip-address to ALLOWED_HOST. But still not working.
Are there any thoughts for this situation?
maybe this method work for you because it worked for mine on the ec2 instance on Amazon web server.
step1: go to your dashboard find launch-wizard menu and open it.
step2: Now click on inbound after that click on edit. Here click on Add rule and select Custom TCP rule & enter 8000 in port range. Now again click on Add rule and select HTTP and similarly do for HTTPS then click on save button.
step3: save and restart your AWS machine. Hopefully, your Django app runs on 8000 port.
For a Linux instance in the security group, follow these steps to verify the security group rule:
Connect to a Linux instance by using a password.
Run the following command to check whether TCP 80 is being listened. netstat -an | grep 80
If the following result returns, web service for TCP port 80 is enabled.
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
If not then there is a problem with your security group setup, please go through the official documentation and see how you can fix it:
https://www.alibabacloud.com/help/doc-detail/25471.htm

SSH tunnelling to a remote server with django

I'm trying to set up an SSH tunnel to access my server (currently an ubuntu 16.04 VM on Azure) to set up safe access to my django applications running on it.
I was able to imitate the production environment with Apache WSGI and it works pretty good but since I'm trying to develop the application I don't want to make it available to broader public right now - but to make it visible only for a bunch of people.
To the point: when I set up the ssh tunnel using putty on Windows 10 (8000 to localhost:8000) and I run http://localhost:8000/ I get the folowing error:
"Not Found HTTP Error 404. The requested resource is not found.".
How can I make it work? I run the server using manage.py runserver 0:8000.
I found somewhere that the error may be due to the fact that the application does not have access to ssh files, but I don't know whether that's the point here (or how to change it).
Regards,
Dominik
After hours of trying I was able to solve the problem.
First of all, I made sure putty connects to the server and creates the desired tunnel. To do that I right-clicked on the putty window (title bar) and clicked event log. I checked the log and found the following error:
Local port 8000 forwarding to localhost:8000 failed: Network error:
Permission denied
I was able to solve it by choosing other local port (9000 instead of 8000 in my instance).
Second of all, I edited the sshd_config file: sudo vi etc/ssh/sshd_config
and added these three lines:
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts yes
I saved the file and restarted the ssh service:
sudo service ssh stop
sudo service ssh start
Now when I visit localhost:9000 everything works just fine.

How to view the webpage in Django tutorial on Linode server

I know I have similar question with this post: Django tutorial on remote server: how to view in my browser? But I just can't solve it with the answer it has.
I am playing with Django framework now. I am trying to do the Django tutorial on my Linode server. Everything works fine, but when we have to examine the webpage we have, the tutorial says that we have to check http://127.0.0.1:8000/ on the server. I only have command line access on the Linode server, so I don't know how to see that webpage on the linode from my desktop. I tried to use the command like python manage.py runserver 123.123.123.123:8000 and set the ip to my Linode server, but I still can't access that webpage from my desktop (I do remove the firewall for 8000 port). Does anyone know how can I check the change for the remote webpage I created within Django framework on the Linode server? Thanks.
Just to make sure, are you trying to access the following URL in your browser (where the server ip address is 123.123.123.123)?
http://123.123.123.123:8000
Secondly, are you able to telnet to port 8000, to make sure you have correctly opened the port on the firewall.
I had this problem too and I wanted to clarify what I did for others who might get stuck:
First, remove the firewall for port 8000
sudo nano /etc/iptables.firewall.rules
-A INPUT -p tcp --dport 8000 -j ACCEPT
Second, reboot the linode
Third, restart the development server
cd /home/mycode/mysite
python manage.py runserver 123.123.123.123:8000
(where you use your ip)

Connecting to EC2 Django development Server

I am new to EC2 and web development. Currently I have a Linux EC2 instance running, and have installed Django. I am creating a test project before I start on my real project and tried running a Django test server.
This is my output in the shell:
python manage.py runserver ec2-###-##-##-##.compute-1.amazonaws.com:8000
Validating models...
0 errors found
Django version 1.3, using settings 'testsite.settings'
Development server is running at http://ec2-###-##-##-##.compute-1.amazonaws.com:8000/
Quit the server with CONTROL-C.
To test that it is wroking I have tried visiting: ec2-###-##-##-##.compute-1.amazonaws.com:8000 but I always get a "Cannot connect" message from my browser.
Whenever I do this lcoally on my computer however I do successfully get to the DJango development home page at 127.0.0.1:8000. Could someone help me figure out what I am doing wrong / might be missing when I am doing this on my EC2 instance as opposed to my own laptop?
Using an ec-2 instance with Ubuntu, I found that specifying 0.0.0.0:8000 worked:
$python manage.py runserver 0.0.0.0:8000
Of course 8000 does need to be opened for TCP in your security group settings.
You probably don't have port 8000 open on the firewall. Check which security group your instance is running (probably "default") and check the rules it is running. You will probably find that port 8000 is not listed.
1) You need to make sure port 8000 is added as a Custom TCP Rule into your Security Group list of inbound ports
2) Odds are that the IP that you see listed on your AWS Console, which is associated to your instance is a PUBLIC IP OR a PUBLIC Domain Name(i.e. ec2-###-##-##-##.compute-1.amazonaws.com or 174.101.122.132) that Amazon assigns.
2.1) If it is a public IP, then your instance has no way of knowing what the Public IP assigned to it is, rather it will only know the its assigned Local IP.
2.2) To get your Local IP on a Linux System, type:
$ ifconfig
Then look at the eth0 Data and you'll see an IP next to "inet addr" of the format xxx.xxx.xxx.xxx (e.g. 10.10.12.135) This is your Local IP
3) To successfully runserver you can do one of the following two:
$ python manage.py runserver <LOCAL IP>:8000
or
$ python manage.py runserver 0.0.0.0:8000
** Option Two also works great as Ernest Ezis mentioned in his answer.
EDIT : From The Django Book : "The IP address 0.0.0.0 tells the server to listen on any network interface"
** My theory of Public IP could be wrong, since I'm not sure how Amazon assigns IPs. I'd appreciate being corrected.
I was having the same problem. But I was running RHEL on EC2. Besides from adding a rule to security group, I had to manually add a port to firewalld.
firewall-cmd --permanent --add-port=8000/tcp
firewall-cmd --reload
That worked for me! (Although no idea why I had to do that)
Yes, if you use quick launch EC2 option, you should add new HTTP rule (just as it appears on the list) to run a development server.
Adding a security group with the inbound rules as follows usually does the trick unless you have something else misconfigured. The port range specifies which port you want to allow incoming traffic on.
HTTP access would need 80
HTTP access over port 8000 would need 8000
SSH to server would need 22
HTTPS would need 443

Django on SSH public IP

I have Django installed on my SSH-server. But the SSH-server I'm working on, is not at my home. So the local ip-adres Django creates (127.0.0.0:8000) isn't available for me, I can't test my Django-apps graphicly.
What could be a Solution to get it work?
Djano has a command runserver that could set the IP-adres of the server like this:
django-admin.py runserver [IP]:[port]
But which IP I have to fill in there to make it work? The only IP I have is the IP of the SSH-server. Isn't it possible to fill in the IP-address of my webhost(but where I need to give in password etc)?
Thanks a lot!
Choose a publicly available port (say 8000), or redirect an existing web server to use the port you choose, and then start with
django-admin.py runserver 0.0.0.0:8000
Edit
If you happen not to control your available ports, you probably need to hook apache in front of your app. See https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/ for info on how to deliver your app with apache and mod_wsgi.
You can try Putty with its tunnels. This program for windows, but I think you can find it for Linux if you use it. I used it to get access to my Postgres databse with pgAdmin with this article. So you can use tunnel for 8000 port and program to get access to it on your local machine.