Connect local domain to Django - django

I have several applications that I'm running on Docker. Each one of them has own domain, however when I start containers I can access them only on 127.0.0.1:8000 or 127.0.0.1:8001. But I need to reach them on domain like test1.mydomain.local and test2.mydomain.local.
I tried to change my host file like this:
127.0.0.1 *.mydomain.local
127.0.0.1 localhost
However when I start docker again - it doesn't work. I have .env file where all domains are written down but I don't get how to get this worked.
Please help me to figure it out.

It's not possible to specify wildcards in the host file. Set them explicitly or use a local DNS.

Related

Plausible analytics on a server with a webapp

I have Django hosted with Nginx on DigitalOcean. Now I want to install Plausible Analytics. How do I do this? How do I change the Nginx config to get to the Plausible dashboard with mydomain/plausible for example?
Setup plausible by either running the software directly or in a docker container - let's say it runs on port 8080
Then in your nginx.conf - you should have a server block for your domain
Within that add a location block with the path you want plausible on and add a proxy pass directive to forward the requests to localhost:8080
Monitor access.log and error.log to debug any issues that may happen

Hosts file working for custom subdomains but not custom domains

I am developing a multi tenant app and need to modify my etc/hosts file to test the different URLs tenants might use. Some will use a sub domain like tenant1.mysite.com and others will use entirely their own URL like tenant2.com (to mask the fact it's a whitelabel site and pretend its their own).
In my hosts file I have:
127.0.0.1 mytenantsdomain.com
127.0.0.1 localhost
127.0.0.1 thor.localhost
127.0.0.1 potter.localhost
127.0.0.1 testtenant.com
localhost, thor.localhost, potter.localhost all work as expected when adding :8000 to them. i.e. they connect to the local development server and show expected tenant-specific content. But, mytenantsdomain.com and testtenant.com both give ERR_CONNECTION_REFUSED - I'm guessing its for the lack of the port :8000 tbh.
I have tried a few fixes like flushing the cache with the below but nothing has worked.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Anybody know what else I can try to get them all working?
For anyone coming to this later - after a bit more digging (on serverfault.com), the following lessons:
etc/hosts isn't the place for this. It just resolves a text domain
name to an IP. It's nothing to do with ports.
The solution was just to add :8000 after the .com - http://mytenantsdomain.com:8000/ resolves to the local dev server.
To make this permanent (i.e. not have to add :8000 to it), port forwarding would be the way to go. So you need a solution something like this: https://serverfault.com/questions/791181/redirecting-traffic-to-a-specific-address-and-port-using-pf-on-macos

AWS EC2 Instance Connection Refused in browser

I am somewhat new to this, so it's possible it's an obvious or dumb fix I have not thought of.
I have a EC2 instance I created with this AMI: ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20201026. I am using a 2020 Macbook Pro with Big Sur.
When I SSH into the server from my macbook's terminal, either via:
ssh -i FirstKeyPair.pem ubuntu#ec2-X-XX-XXX-XX.us-east-2.compute.amazonaws.com
or
ssh -i FirstKeyPair.pem ubuntu#X.XX.XXX.XX
I have no issues. To see if everything was working, I tried creating an index.html file in the root directory with nothing but hello world in it. I then ran PHP -s localhost:3000
However, when I try to navigate to my public IP X.XX.XXX.XX:3000, or `ec2-X-XX-XXX-XX.us-east-2.compute.amazonaws.com:3000 in my Chrome browser, I get "This site can’t be reached, X.XX.XXX.XX refused to connect, ERR_CONNECTION_REFUSED".
I have checked my Security Groups and opened everything, so they look like this.
And the same is happening on my outbound rules. I checked that I don't have a firewall on my mac as well. How can I get my PHP server, or just that index.html file, to show up when I navigate to my IP in the browser?
You're running your server on localhost which means that it is not accessible on the normal network interfaces. Try php -S 0.0.0.0:3000 instead. This says to listen on all interfaces. But be careful - this is not a recommended configuration and you should look into putting a real web server (i.e. Apache, nginx, etc.) in front of it.

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

How to locally test Django project accessing it using example.com domain?

I'm willing to test Django project, but I want to access it using “example.com“. Before I used “127.0.0.1:8000“ (manage.py runserver), but now I have need to test how my project acts on different TLDs. All I want to do is somehow tell my computer that “example.com“ points to “127.0.0.1:8000“. I don't want others to have access to it, so it's not question about deployment.
I am Linux user. Any help would be much appreciated!
sudo vi /etc/hosts and add the following line at the end.
127.0.0.1 example.com
Also, if you run the web server as root, you can use port 80 and not have to type example.com:8000 into the address bar.
change the hosts file at /etc/hosts
Just edit /etc/hosts so that example.com maps to that 127.0.0.1.
I don't think you can map it to a specific port number though, so you'll need to either access example.com:8000 or run the server on port 80 (probably via sudo).
Simple, edit your hosts file at /etc/hosts so that your site "example.com" maps to the localhost "127.0.0.1"