How to enable user custom domains in PHP - php

I'm having a system where users can input their purchased domain into their profile, so when accessing their domain, it should replace their custom domain, e.g.
http://domain.com/custom-name to http://purchaseddomain.com.
So when they access their purchase domain, it should take them to their profile including their navigation links, such as links on their page will be replaced with their purchased domain, for example viewing their records would be:
http://domain.com/custom-name/records to http://purchaseddomain.com/records.
Tumblr enables this feature, however I have no idea how this all works:
This is exactly how I like to have a feature like this, I've searched on SO, but it didn't seem to help.
Now this is a problem, I'm not sure how I can validate, confirm and merge their purchased domain into my server without a problem using PHP - I'm using Codeigniter for this.
Is there a solid, stable plugin/library or detailed tutorial that can have the ability to enable custom domains masking a internal domain?
My server is running Ubuntu 11.10 on nginx 1.0.6.
The templating will be just fine for me, which I can do - all I need help on is how to safely accept and merge their domain to my server.
EDIT: Just looked into nginx VirtualHostExample, this looks good overall but how will I be able to dynamically add/remove those domain entries while the domain has an A record pointing to my server?

You won't merge their domain to your server.
In fact, when they will register their domains, they will make it point to your server.
On your server configuration, you'll have to dynamically create rules that implicitly redirect the page to the one they created on your server.
So, users will see http://purchaseddomain.com/on-uri but you serve the page http://domain.com/custom-name/one-uri
I.E:
it's like if you added on an .htaccess - even if you don't use apache, it's just to explain what the "system" must be:
RewriteCond %{HTTP_HOST} purchaseddomain\.com$ [NC]
RewriteRule (.*) /custom-name/$1

The accepted answer mentions customers pointing their DNS to your web server. But, that's not enough to make it work in this day and age.
If your customers just CNAME to your domain or create the A record to your IP and you don't handle TLS termination for these custom domains, your app will not support HTTPS, and without it, your app won't work in modern browsers on these custom domains.
You need to set up a TLS termination reverse proxy in front of your webserver. This proxy can be run on a separate machine but you can run it on the same machine as the webserver.
CNAME vs A record
If your customers want to have your app on their subdomain, e.g. app.customer.com they can create a CNAME app.customer.com pointing to your proxy.
If they want to have your app on their root domain, e.g. customer.com then they'll have to create an A record on customer.com pointing to your proxy's IP. Make sure this IP doesn't change, ever!
How to handle TLS termination?
To make TLS termination work, you'll have to issue TLS certificates for these custom domains. You can use Let's Encrypt for that. Your proxy will see the Host header of the incoming request, e.g. app.customer1.com or customer2.com etc., and then it will decide which TLS certificate to use by checking the SNI.
The proxy can be set up to automatically issue and renew certificates for these custom domains. On the first request from a new custom domain, the proxy will see it doesn't have the appropriate certificate. It will ask Let's Encrypt for a new certificate. Let's Encrypt will first issue a challenge to see if you manage the domain, and since the customer already created a CNAME or A record pointing to your proxy, that tells Let's Encrypt you indeed manage the domain, and it will let you issue a certificate for it.
To issue and renew certificates automatically, I'd recommend using Caddyserver, greenlock.js, OpenResty (Nginx).
tl;dr on what happens here;
Caddyserver listens on 443 and 80, it receives requests, issues, and renews certificates automatically, proxies traffic to your backend.
How to handle it on my backend
Your proxy is terminating TLS and proxying requests to your backend. However, your backend doesn't know who is the original customer behind the request. This is why you need to tell your proxy to include additional headers in proxied requests to identify the customer. Just add X-Serve-For: app.customer.com or X-Serve-For: customer2.com or whatever the Host header is of the original request.
Now when you receive the proxied request on the backend, you can read this custom header and you know who is the customer behind the request. You can implement your logic based on that, show data belonging to this customer, etc.
More
Put a load balancer in front of your fleet of proxies for higher availability. You'll also have to use distributed storage for certificates and Let's Encrypt challenges. Use AWS ECS or EBS for automated recovery if something fails, otherwise, you may be waking up in the middle of the night restarting machines, or your proxy manually.
If you need more detail you can DM me on Twitter #dragocrnjac

This is what is working for me:
server {
server_name *.mydomain.com
root /var/www/$host;
...
}
Then you need to make directories like: /var/www/user1.mydomain.com/, /var/www/user2.mydomain.com/, ...
I couldn't figure out how to leave the '.mydomain.com' out of the directory name. If anyone has any idea, pls let me know :)

Related

Build a simple PHP proxy server for a shared host

I've searched a lot but I couldn't find a PHP proxy server which can be run on a shared host, so I decided to build a very simple one from scratch but I'm still on the first step. I've created a subdomain httpp.alvandsoft.com and also redirected all its subdirectories (REQUEST_URI) to the main index.php to be logged and to know what whould a proxy server exactly receive and send
(The log is accessible through https://httpp.alvandsoft.com/?log=1&log_filename=log.txt)
But whenever I set it as a proxy for Telegram or other apps, it doesn't receive ANY requests at all, even when I use 443 or 80 ports, neither in different proxies such as HTTP, SOCKS or MTPROTO.
Is proxy something that depends on the server's settings and works in a way other than regular HTTP requests and responses or I'm missing something?
I found it out myself. HTTP(s) proxies send their requested URL as Host request header and many hosts and websites, check this request header and if it's not a member of their valid IPs, redirect it immediately.

Create a sub domain to enable a CNAME Record with PHP

I am building a Saas Application.
In it, there are multiple user accounts, who will be accessing the application from their own domains.
They will link their domains to my root domain where the application is installed, using CNAME records.
When a user registers, they are asked to enter their domain name. We need to create a new Alias for enabling the CNAME Record programmatically using PHP.
Is there any way to do it?
Edit
Each client of the application needs to access the application via their domains (a custom domain that client owns) which are pointed to our domain (the base domain of the application)using CNAME record in their DNS records.
How can I programmatically bind the custom domain supplied by the client with my hosting account from PHP? Say adding of Parked domain or Domain Alias from PHP code.
Yes, but how you do it depends on your DNS server - and you told us nothing about that. With Bind (for example) it's just a matter of updating the zone files and reloading the config. With Active Directory, an LDIF call. However what you propose is completely unworkable in practice and also unnecessary.
You cannot start serving DNS for a zone you don't already control.
Nobody in their right mind is going to open up their DNS to give you programmatic access.
I also said its unnecessary - all the DNS does is point traffic to your service. So it doesn't matter if you configure the DNS or someone else does. Each HTTP[S] request coming in bears the domain name - so your service can easily apply the relevant branding / billing / data multiplexing required.
Sorting out how you provision SSL certificates is a much more complicated problem than DNS. Again the business of setting up the data and configuring it is trivial, but you need to prove to a third party (the certificate authority) that you are the legal entity described by the certificate (which you are not).
Hence your options are:
1) Get your client to configure the DNS CNAME pointing to your service name and provide you with the certificate
2) you provision a DNS CNAME and SSL certificate as ${client}.${yourdomain}
3) ask your clients to proxy the request

Domaining with PHP

I have a site say abc.com and a user account is accessed at abc.com/user. Now I want to add functionality that user can link his site say user.com to abc.com/user. Can u please give me an overview that how this process can be done dynamically via php?
Set up DNS to point the host name at the server
Set up the server configuration so the site is the default virtual host (so if an unrecognised domain name is used to request it, the right site will be loaded)
Use $_SERVER['HTTP_HOST'] instead of the path.
If I understood correctly - you want that a person who is entering user.com will see the content from abc.com/user.
I would recommend checking out launchrock.com they do exactly this, and it involves adding a CNAME dns record as I remember, another way to do this is to create a Proxy flag rule in httpd (mod_proxy must be enabled for this), so that you won't have to restart the server, you can always the .htaccess in your www dir.
doing so dynamically will be easy if you go the proxy route, as you just need to append a new proxypass every time a new site is deployed, or create a parameterized rule, like a regular htaccess, as far as automating DNS records creation, I'm sure that's possible and maybe DNS provider such as godaddy or AWS have api for those kinds of things, but more research is required.

How to mask URL's like cloudapp, so users can showoff our data on "their" server

I'm planning to build a webapp where users can show off their data. I want our users to be able to show it off on their own site, just like cloudapp. You can set your own domain name, but still use the application's hosting service.
So my service is http://example.com/showoff but I want users to have the option to use http://showoff.example.net for their data.
I'm planning on doing it in PHP, but i'm open for suggestions if it comes to building this kind of service.
Let's assume your domain is "showoff.com" and their domain is "acme.com"
You have two basic options: "showoff.acme.com" or "acme.showoff.com". I have done this both ways.
For "showoff.acme.com":
The customer will need to set up a CNAME entry pointing to showoff.com in their DNS. Explaining to the customer how to do this is not trivial, and doesn't make much business sense unless you have a small, high-paying customer base.
You will need to configure an Apache virtualhost to answer all requests to all domains (not just showoff.com)
You cannot use SSL unless you configure a virtualhost and IP and certificate for each individual customer. (A pain!)
For "acme.showoff.com":
You will need a wildcard DNS record to point *.showoff.com to your server's IP (whether this is easy depends on your DNS provider); if you are unable to do this, you will need to create a DNS record for each customer.
You will need to configure an Apache virtualhost to answer all requests to *.showoff.com
You can use a wildcard SSL certificate for *.showoff.com
In both scenarios, in your PHP you can find out what domain was requested with $_SERVER['HTTP_HOST']. You will want to check this against your database of recognized domains, to determine which customer's data to show.
As you can see, most of the work is in DNS and Apache; it doesn't really matter whether you use PHP or some other language.
They have to point a DNS entry for showoff.usersite.com to your server. You can read that from the $_SERVER variables and off you go! Point another domain to your site and try: print_R($_SERVER) and you will see the domainname pointing to you.

looking to create a hosted solution

I am in the early stages of thinking through a product I am looking to develop, the product is a hosted e-commerce solution. What kind of things do I need to think about when allowing users to register there own domain names? Hows does that even work? I know I can do subdomains, but no idea how a users own domain would work on the hosted solution?
Any ideas?
You can run multiple domains on a single host using virtual hosts, as supported by HTTP 1.1. See the Apache docs for setting up virtual hosts for Apache httpd for example (you may use some other web server).
You probably don't want to get involved with the actual domain registration: users should register the domain with an established domain registrar and configure the A record to point to your server.
All a user would then do is specify their domain name and you'd update Apache's httpd.conf (or equivalent config file) appropriately.
You might want to look into the easy way out - A resellers account. I had one before, it was easy to use and setup. The only problem I had was there are extra fees if you want people to register domains though your website. It's worth the money if your going to make it back.
I used JustHost.com, there pricing is fair but there e-mail system and cPanel are out-dated. E-mail is fine if you use POP3 or IMAP.
Good Luck,
CwTechies

Categories