How do i run django application without port number - django

How do i run django application without port number: i had tried Django: Run django app on server without port? but didn't work.

Web services must bind a port on a interface of the system. So, you should specify a port number to run your Django application. The default port number for HTTP is 80, for HTTPS 443. But you can use a custom port between [1-65535]:
For example;
python manage.py runserver 7000

You may try the following:
python manage.py runserver 80
or if you don't have permissions (assuming you are using Linux):
sudo python manage.py runserver 80
Then, you can access your application: http://localhost/

In general, web services need a port to run. If the port used is default http (80) or https (443) port, modern web browsers hide it from seeing in the address bar.
In a development server, you can hide the port(because you don't want to see it anymore) by assigning it to port 80 if it is not used by any other web service in the system(otherwise django will complain):
python manage.py runserver 80
In a production server, you need to use servers like Gunicorn to run your django app in the backend and a web server like Nginx or Apache to serve your backend to external world. In that case, since web servers use http/https ports, no ports will be visible in the browser.

Related

How to replace port 8000

I'm working on a project now and I'm currently using Django+uWSGI+Nginx to deploy the backend on the server. There is also a frontend using Vue.js on the same server.
So the frontend is www.mysite.com
The backend uses port 8000 as www.mysite.com:8000
But I encountered a problem, that is, many users' work network blocked port 8000, so that users could only use the front end, but could not connect to the back end.
Is there any way to avoid using www.mysite.com:8000 and replace it with another url?
when you run the server you can specify the port:
python3 manage.py runserver 8000
you can also modify manage.py:
from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8000"
(In your case replace 8000 with the port you want)

configuring Django Channels for windows in production

Please, I need to configure Django Channels on redis-channel-layer on windows IIS in production. It is running very well in development.
I have installed redis, daphne. I have set the IIS as proxy server with URL Rewrite pointing the Inbound to localhost 6379 to redis-layer channels. I used python manage.py runworker. and also started the daphne server with the daphne command.
They all ran very well, but there is no websocket handshake for my url.

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

Django runserver on all ports?

Why can't I run python manage.py runserver on all ports?
I understand this is probably a newbie question, but what prevents me from achieving this?
I would like to access my app without having to include the port, like a regular website
WARNING - Do not run the test server in production!
The reason you have to type in the port when connecting to the test server is because it doesn't run on a standard web port being "http: 80 and https: 443". If you use the command below it will not require the port number be provided when connecting to the test server. Keep in mind that you will need root or sudo access and if something is already running on port 80 it will fail.
Runserver with port:
python manage.py runserver -p 80
Just run it on port 80 and you won't have to specify the port.
You can't blast it on all other ports because many, many other services already use those other ports. Network services need to have ports specified.