Access web server on VirtualBox/Vagrant machine from host browser? - django

I have a Django web server on a VirtualBox/Vagrant machine running Ubuntu.
I have followed this guide to create a Django project: https://docs.djangoproject.com/en/dev/intro/tutorial01/
I have a web server running at http://127.0.0.1:8000/ inside my guest machine. This is the first time I am running a Django web server. It is supposed to be a hello world app.
How can I access this web application from my host browser?
I have tried running ifconfig in the guest to get the IP that I should visit I found a promising IP address in inet addr.
But I have tried entering the following into my host browser and it didn't work.
http://inetaddrnumbers:8000/
How can I access the web server from my browser?

Try this.
Open the vagrant file (should be in the directory where you specified to create a new vagrant machine).
Search for config.vm.network. If you didn't setup the file earlier, it should be commented.
Change it to look something like this config.vm.network "private_network", ip: "55.55.55.5". Here ip address (55.55.55.5) can be any ip address you want.
Now logout from the vagrant machine and reload your vagrant machine by this command vagrant reload.
Again ssh to your vagrant machine and restart your django server by this command python manage.py runserver 0.0.0.0:80. Again the port address (80) can be 8000 if you want so.
After that, in your browser, enter the following address 55.55.55.5, and hopefully you should see your webapp.
Now if you would like to go further, you can edit your host file, and add this line
55.55.55.5 mynewdomain.com
Then in your browser, enter the follow address,
mynewdomain.com
And you should see your web app. Note that, www is not added in the domain name inside the host file, so only mynewdomain.com can be accessed. You can however add it.
Hope this helps. Cheers.

Complementing #Kakar answer, this configuration can also be done using this:
config.vm.network "private_network", type: "dhcp"
This will assign an IP automatically.
For further reading: https://www.vagrantup.com/docs/networking/private_network.html

Related

Accessing django website hosted on vm with mobile device

I run my django website with python manage.py runserver 0.0.0.0:8080 on Vagrant which is set up to forward 8080 port to 8081 on host machine. I'm able to access this website on host by going to it's local ip (192.168.X.X) but can't on mobile device (of course also by going into it's local ip).
Any idea? All I could find about this is to run server with 0.0.0.0 what is already happening in my case.
Never mind, I've forgot to enable public_network in my Vagrantfile.

How to host a django website in my pc and view it in another pc?

I have a django project in my pc.
In terminal I've run python3 manage.py runserver <my ipaddress>:8001
When I try to open the link in another pc, it is showing error page which says:
Invalid HTTP_HOST header: '<my ipaddress>:8001'. You may need to add '<my ipaddress>' to ALLOWED_HOSTS.
What should I do?
And moreover is it possible to put some text in place of ipaddress in the url?
For example, I want to host it as myproject/ instead of that complex url.
On one condition this will work
if both computers are on the same network like local Hotspot or same
LAN network
steps:
add '*' in your django projects's setting file in Allowed Host it will look like
ALLOWED_HOSTS = ['*']
run your server on this ip 0.0.0.0 and port any like 8000 using this command
manage.py runserver 0.0.0.0:8000
run ifconfig if you are using linux ipconfig if windows then you will get your ip address of your server
Open browser in another computer and enter the ip of server shown in 3rd step with port as 8000
http://ip-of-server:8000
Instead of passing <my-ip-address> to the runserver command, pass 0.0.0.0.
If both the machines are in the same network you can run the application on 0.0.0.0 IP address (refers to all IPv4 addresses on the local machine). Refer this link wiki 0.0.0.0 for more details. So, on application server run this:
manage.py runserver 0.0.0.0:8001
Now, from the other machines, access it using http://youripaddresss:8001 , where < youripaddress > is the actual ip address of your machine.
If both computers are not on the same network (local hotspot or LAN network)
You can use ngrok to view whatever is running on your localhost from any device
Using ngrok to view your django project from any device
follow the steps below:
Add '*' in your django projects's setting file in Allowed Hosts:
ALLOWED_HOSTS = ['*']
Download ngrok from the official website
Unzip the downloaded file and then go to the directory where the ngrok file is located via your terminal
Then type the command:
ngrok http 8000
or
./ngrok http 8000
Now you can open the url generated by ngrok on any device to view what is running on your pc:
https://randomly_generated_subdomain.ngrok.io
PS: this can also be used for any webserver running locally, not just django site
ngrok is a great tool that can be used to:
Run personal cloud services from your home
Demo websites without deploying
Build webhook consumers on your dev machine
Test mobile apps connected to your locally running backend
Stable addresses for your connected devices that are deployed in the field
Run your server with this command python manage.py runserver 0.0.0.0:8000
Then you can access your website from any system connected to your wifi with an URL like 192.168.0.1:8000
You may get your IP Address using this command in CMD ipconfig in windows and ifconfig in mac
Also add your IP to ALLOWED_HOSTS in setting.py
ALLOWED_HOST = ['*'] in your settings.py
python manage.py runserver 0.0.0.0:8000
then make sure your machine firewall allows incoming and outgoing traffic.
I use linux machine so from control center go to firewall and allow both incoming and outgoing.
then on your local network machine.
:8000
This is it !
worked for me

How can I change django runserver url?

I'm trying to change django project url, so that users, who wants to connect to website in local area network will see url instead of localhost:8000 or 127.0.0.1. I need to change localhost:8000/users/board to be http://example.eu. I've tried to python manage.py runserver http://example.euand then thought about changing url's but it doesn't work. Also tried to change hosts file(Windows OS), but as far as I understand I need real ip address not url. Is it possible to do this thing and how?
You can use python manage.py runserver 0.0.0.0:8000. 0.0.0.0 means all IPv4 addresses on the local machine. So the server can be reachable by 127.0.0.1 and your private ip address like 10.10.5.8. So now others can access the server using http://10.10.5.8:8000. You the runserver on port 80, so that port can be removed from the url (by default is 80).
But to use any domain instead of ip, you have to change the hosts file of all the clients using the server to add domain to ip address mapping. Alternatively you can configure local network server to map the particular url to your system ip.
Run a local domain with the same port
Opening the /etc/hosts file on your mac with
sudo nano /etc/hosts
And for windows I believe you need to open:
C:\Windows\System32\Drivers\etc\hosts
In here add the domains you want, for example I added vazkir.com
....
127.0.0.1 localhost
127.0.0.1 vazkirtje.com
127.0.0.1 www.vazkirtje.com
.....
Lastly you can add it the domain to you ALLOW_HOSTS in your settings.py:
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "vazkirtje.com", "www.vazkirtje.com"]
And now you can visit your Django application (with port 8000) at:
http://vazkirtje.com:8000
The solution is not perfect since you do have to specify the port you are using, but this does let you use a domain for your local Django application in a relatively easy manner;)
Run a local non-existent domain without specifying the port
If you do want to run it without specifying the port; so just vazkirtje.com then you can use port 80, because this is the default port for HTTP.
Make sure the domain you are testing does not exist, since the domain lookup will first be done on existing domains. So check if you get a similar message to the one I got on chrome, when visiting the url:
Now you can specify this port by adding port "80" to the "runserver" command. You only do need to use "sudo" to run the command at this port, since you need admin rights for this. So run:
sudo python manage.py runserver 80
And now you should be able to access your Django application by visiting:
http://vazkirtje.com

Can't get server up and running digitalocean / django

I am very confused about how to set up my server because nothing seems to be working right. (I am a novice to all this)
I have the domain name dreamof.science with the registrar alp names. I have cloud hosting through digitalocean.
On digital ocean I have a droplet with nginx and django installed on my server through ssh on a secondary user (not root). There is an app I started from a book I am reading that teaches you django added onto it through my github in the directory sites/stratosphere.dreamof.science/source/django
I have been reading about this for days and the more I read the more I get confused. A records, AAA records, CNAMEs, PTR records, subdomains...I just want to know how the heck to get this server up and my app to run.
On my registrar I have my name servers pointed to the ones that digital ocean gave me for dreamof.science xx1.digitalocean.com xx2 etc.... On my registrar it also says I have 2 A records which all point to the same IP address which is the IP of my droplet. dreamof.science www.dreamof.science
I also have a CNAME to stratosphere.dreamof.science I am under the impression that this is my subdomain because you're not supposed to run apps on the regular domain....or something like that. Hence why I have my files in stratosphere.dreamof.science and nginx server config pointing towards stratosphere.dreamof.science
Now when I go to dreamof.science it just says "this webpage is unavailable." Same thing with stratosphere.dreamof.science and even when I just go directly to the server IP nothing shows up. I have the server running through the console on digital ocean and the droplet is active.
What is wrong here?
First, try creating a basic (empty) Django project in somewhere like /var/www/myproject. Start a debug server on port 8000 that accepts all connections using the runserver command like so:
python manage.py runserver 0.0.0.0:8000
Then navigate to http://dreamof.science:8000/ and see if you return a successful debug screen. That will tell you if the domain resolves correctly.
Now try setting a basic Nginx config, similar to the following:
server {
server_name dreamof.science;
listen 80;
location / {
proxy_pass http://localhost:8000;
}
}
Make sure that you don't have a firewall set to reject connections on that port.
Now try visiting http://dreamof.science/ to see if Nginx is running and set up to proxy the root domain to port 8000.

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)