How to setup custom domain hosting using Django and Apache? - django

Something I'm trying to figure out now is to setup custom domain hosting so users can host a profile page on any domain of their choosing. I was able to get it to work now but it requires a lot of custom work.
My current setup is to have the user change their DNS settings to redirect to my domain. Then on my domain I add another virtual host to deal with this new domain and then update my Django middleware to look at the PATH_INFO within the request in order to choose the page to serve.
Is there a way this can be changed to automate this process so this can be done entirely automatically without modifying Apache or the middleware? I was thinking of using mod_rewrite to pass this info along to a specific page in Django and then doing a lookup in a table to see the page this page to display for this domain.

You should be able to set up your webserver such that it will accept any domain pointed at it. Once you've done that, the user needs only to create an A record pointing their domain or a subdomain to the IP address of your site, and then their domain will bring up your site.
Then, you would just need to setup some domain checks in your views (request.get_host()). For instance, your index view should branch on the host, loading your homepage for your domain and the user's profile for any other domain. You can have the user add their domain to their profile and then use that to lookup the right profile. You might need to add some middleware to protect other views from being accessed by anything but your domain, but that's basically the process.

Related

Allow users to add custom domain pointing to a subdomain in my app on Google Cloud Platform

I am trying this since last 2 days.
I build an example app using a very detailed tutorial here - https://john2x.com/blog/wildcard-certs-from-lets-encrypt-cert-manager-ingress-nginx-gke.html
What I have working right now:
I have an app running at demo-app.example.com wherein users can have their own subdomain like john.demo-app.example.com
I got the wildcard certificates working as well
I have two questions now:
1 - How do I allow users to add their custom domain pointing to the subdomain in my app. When I just create a CNAME pointing to a subdomain in my app, it does not work, I think because of how the whole thing is configured.
2 - I want to move from demo-app.example.com to just example.com to the top level, so how should the DNS zone be configured for this?
Finally, given the requirements, is Kubernetes the best I could use or I would be better off using something else. It is a NodeJS app btw.

How do I point my domain name from hostgator to heroku

I'm trying to point my hostgator domain name to my heroku app, but I keep getting the error when I try to manage my domain names.
An error occurred. Please verify the validity of your nameservers and try again.
I've already added the domain name to heroku in the sites dashboard > settings. My app is free, I'm ready to move up and pay for the full version. I've been figuring some things out and I am ready to deploy. And to do this I have to point my domain name to heroku. I know I'm missing something. I've read the docs already, a lot, But it's not all the way clear. I know it's something simple I'm missing. Thanks in advance
EDIT. I don't want my domain to be a subdoamain. I want it to be the main domain. because you can't monetize a site that is a subdomain.
EDIT to be clear I want the domain name mysite.com from hostgator to be used with my heroku app. So when I type mysite.com my heroku app appears. I want my heroku app to go from this
mysite.herokuapp.com
to this
mysite.com
and I do not want it to be a subdomain
I'm going to outline two approaches here. One works if you still have cpanel because you're paying for hosting with HostGator. (That makes no sense if you're using Heroku for hosting, so it's not a long term solution.) The other works if you just have a domain registered with HostGator and theoretically in either case.
Either way, do this:
heroku domains:add www.mysite.com
heroku domains:wait 'www.mysite.com'
1) If you have hosting with HostGator
Log into your HostGator cpanel interface at mysite.com/cpanel. Scroll down to Domains and open up the Advanced Zone Editor (click the link). You're not going to add a record. Instead, you're going to change one that's already there. Find the record with the name "www.mysite.com." (it has a period at the end) with the type CNAME. Click Edit. Change the CNAME field to the DNS target heroku gives you when you type heroku domains or scroll down to Domains And Certificates under Settings in your Heroku dashboard. Mine looks like www.mysite.com.herokudns.com. Finalize the change.
Visit www.mysite.com. Please put www in the url. www must be in the url. This should now point to your Heroku site. Perhaps you will see a 400 (bad request) error. This might be because your backend isn't allowing Heroku as a host. If you have a Django app, for example, that's not in debug mode (you should set DEBUG to False for security reasons), you must also set the ALLOWED_HOSTS list in settings.py to include 'www.mysite.com'. Otherwise you'll get 400 errors when you try to access the site from the www.mysite.com domain.
The end result is that www.mysite.com points where you want it to, and so will www.mysite.com/some_page.
But what if a user types in only "mysite.com"? They'll be directed to your HostGator site. A simple solution is to go to the CPanel's Redirects section, also under Domains, and make a permanent redirect from your domain (leaving the textbox that starts with "/" blank) to http://(((www.mysite.com/))) [StackOverflow won't let me put it in without something like parentheses there.] All this does is force "www" to appear in the URL even when the user doesn't type it.
Now, whatever the user types in, they'll end up at your Heroku site. I don't mind having a "www" hanging out in the URL box, so this works for me.
2) If you don't have hosting with HostGator, just ditched it, or simply don't like the company (quite understandable)
If you don't have HostGator hosting or ditched it, you probably can't access the cpanel stuff. First, add the domain as shown at the top. Now install the PointDNS add-on for Heroku. It automatically syncs with your custom domains (even after you install it). So when you open up PointDNS in the Heroku dashboard, you'll see it set up with basically everything you need. In HostGator, at https://portal.hostgator.com/domain/manage), click on your domain name and find the link to change the nameservers. Select Manually set my nameservers, and replace the default nameservers with the nameservers you see labelled with NS on the main page of the PointDNS add-on page. (You're also free to add more or or change them in PointDNS.)
HostGator says it will take a couple days to propagate. I saw the change take effect within seconds.
If it doesn't work without www. now (check pages besides the index page), I've outlined a potential solution here.
With this PointDNS solution, you may want to test your site at different places in the world to make sure it's available there. I made sure mine is available from places where I expect people to view it, but it's also available in Beijing, so I don't think you'll have geographic limitations with PointDNS. I like to use www.webpagetest.org to test the site from different locations.
According to https://devcenter.heroku.com/articles/custom-domains you must add a CNAME record like "CNAME www example.herokuapp.com" first at Hostgator. After that, you must add www.example.com (with or without www) as a custom domain to your project. It's really simple.

how can i check that a admin user is logged into django website or not into NGINX

I want to see that admin user is logged in or not into nginx.conf file .
I need this for my specific requirement.
My Try :
I tried to fetch COOKIES into nginx.conf file.
but when admin is logged out so cookies are changed so i am not able figure out that if a admin is logged in or not.
As far as I know this is not possible.
Django uses encrypted cookies by default and stores all data in the session table in the DB (also encrypted). You could check if the user has a cookie set in nginx but you won't be able to verify if that cookie value actually means "Admin" or "random other cookie value".
Also unless you're using custom nginx modules to check the contents of the cookie you run the risk of someone managing to trick nginx into thinking the user is an admin when that is not the case.
I'm not sure what the use case is here but you could try doing something with the django middleware or looking for third party plugins instead of using nginx.
If you want to limit file access to a specific file, e.g. admin-only images/javascript/data files or such you could try the HttpAccessKeyModule for Nginx and just generate a custom 'url' for your admin to access them.
You can also try looking into: http://nginx.org/en/docs/http/ngx_http_auth_request_module.html and see about delegating the check to some part of Django which just returns Yes or No to nginx.
There might be another nginx plugin somewhere which you can add/enable for an admin user from within django. But this requires you to think the other way around. With django telling nginx the user is an admin. Instead of nginx finding it out itself.
Hope this helps you in some way.

django-registration - how do i change example.com in the email?

I have the django-registration setup and it sends emails - yay!
However, it's decided that it would be ace to call my site "example.com", which is not the name I decided to use. Cool name, but not for me.
How do I change example.com to something else? I read somewhere that I go to the admin page but - spoiler alert - I've never used the admin page in django and am not actively planning on this currently (but maybe someday?)
So I go to the admin page - /admin/sites/site/ - and sure enough I can see "example.com" and "domain name". But now what? I added my sites domain name and "display name", but how do I select to use it? I even deleted example.com!
However, the email insists on still sending out example.com. All the sites I read just say "use admin", but I don't know if maybe my admin is broken (which would be odd, as I really haven't touched it) or if there is a link i'm missing?
Any ideas?
The entry you saw originally on the admin/sites/site/ page, with example.com and domain name, is the one you should simply edit for the quickest results (instead of adding a new Site object).
What's really happening?
In your settings file, Django automatically defines a SITE_ID property when you create your project, which is by default set to 1. This points to the example.com Site you see in the admin. If you want to use the Site object you created, or change Site objects later when you need the functionality, you can change that SITE_ID property to the id of the Site object you created. Hope this helps!
The site object in the template comes from the Django Site model. When you do a syncdb, it defaults automatically to example.com
If you login to Django's admin interface, you will find "Sites". Inside it, you will be able to change example.com to whatever you like. Just set the id of the site u want to use as current site in your settings.py as SITE_ID = ID.
e.g if you editted the example.com domain name to mysite.com and display name as MYSITE. then SITE_ID = 1 because that is the only content we have in our "Sites" table and its ID is 1. if like to add another "Sites" details leaving the example.com untouched, remember to set the SITE_ID to ID of the one u added

(Django) Sharing authentication across two sites that are on different domains

I have two sites say foo.com and bar.com and are both Django based. Primary registration occurs on foo.com (I'd like the main user db to be here) and I'd like for three things to happen:
1) User that logs in to foo.com is automatically able to access bar.com without logging in again
2) User that logs in to bar.com directly is authenticated against foo.com user db.
3) There is no need for a user to register at bar.com directly.
How can I achieve this? If it greatly simplifies things I can make bar.com a subdomain of foo.com (eg. bar.foo.com) but they must be separate sites.
It depends on your requirements. If you're able to, the simple solution is to simply host both sites on one Django instance. In other words, your Django project hosts both sites but you have a url rewrite rule that maps foo.com to http://localhost/foo/ and bar.com to http://localhost/bar/. Django's auth system will "just work" under this scenario. Rewrite rules can of course also apply to subdomains; I've built a system that hosts hundreds of subdomains using this technique.
If this isn't an option, sharing databases between your Django instances and setting SESSION_COOKIE_DOMAIN, as mentioned by others, should work.
Your 3rd requirement could easily be solved by sharing the same database between the two sites (therefore having the same Users table.
The 1st requirement is tricky because of cross domain issues (the session cookie will not be shared).
What you are really looking for is a Single Sign On (SSO). You might consider django-openid.
I had a very similar problem but OpenID was not a viable solution for me. With the advent of multiple databases in django >1.2, it is now pretty easy to share session and login data across sites. This blog post does a great job of explaining how to get it set up. Hopefully others find this as useful as I did.
I think what you are looking for is the SESSION_COOKIE_DOMAIN setting. You would set it like this:
SESSION_COOKIE_DOMAIN = 'foo.com'
See http://docs.djangoproject.com/en/dev/topics/http/sessions/#session-cookie-domain for more information on that. This does assume that both applications are using the same session storage backend.