Domaining with PHP - 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.

Related

Is a script to allow a user to make a subdomain under the domain point to an IP address possible?

How would this have to be created if possible? Can it only be created in cPanel, or is it possible to make a script for users to use without logging into cPanel that allows them to make their domain?
That should be possible depending on your hosting / server setup:
Add a wildcard domain CNAME record *.yourdomain.com;
Use a rewrite rule to direct all traffic to anything.yourdomain.com to a php script to handle the requests;
Do whatever you want in your php script: Use a header redirect, present another page in an iframe, etc.
A subdomain is just an entry in the DNS server configuration. Likely you'll be able to update the configuration by adding a bunch of lines in a file, or a new file altogether. This really depends on the DNS server software, maybe the one you use also has an API to be updated via a TCP socket.
Also if the target IP is shared (and thus you use name-based virtual host), or it's a different one, maybe you want an HTTP server listening on the port 80 on that IP.

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.

How to enable user custom domains in 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 :)

Setting up custom domain of users' inputted domain

This may be a server question, but I'm asking how to do this system in PHP before you vote to close as off-topic.
I'm setting up a custom cloning site system, where paid users can have their own software but on a custom subdomain build, e.g. user1.domain.com. I want to allow them to have their own paid domain pointing to my server replacing their built subdomain. However, I don't know how to do this kind of process in raw PHP.
For example, on an administration page, they can input their paid domain and I want the domain to be added to BIND DNS/nginx on my server, but I need to know what I need take from there (except URL validation) such as checking if the domain is pointing my server when they enter the nameservers of my server from their domain manager (GoDaddy, 123-reg, etc) and so on and making sure the domain is properly active and can be taken well when accessing the new domain that replaces the built domain (nginx can do this?)
What steps should I take into making this kind of system? Is there any server support I need to take into such as cron jobs or such.
My server is running Ubuntu 11.10 with nginx 1.0.6.
EDIT: I already have a wildcard A record in BIND.
Just point *.domain.com to your server. Assuming you are running Apache, you just define your VirtualDocumentRoot in httpd.conf and you're off and running. Nothing to do in PHP or anything else.
Check out the documentation here: http://httpd.apache.org/docs/2.0/vhosts/mass.html
UPDATED
Didn't catch you were using nginx, sorry about that - looks very similar to Apache:
root /PATH/TO/WEBROOT/$host;
in your server { config. http://wiki.nginx.org/VirtualHostExample
Edit your configuration file (nginx.conf) and change the server section beneath http as shown in the VirtualHostExample at the link above, replacing the server name and paths with your server's info.

Seamlessly direct multiple domains to a single set of PHP files?

I need to direct multiple domains to a single set of (PHP) files. So I point a domain at my server, which then goes to a single index.php file. This index.php file then detects the domain accessing it and returns the appropriate content. I do not want to add domains or set-up sites manually though as this is for a content management service. So it should be a case of a user signing up and it immediately works without me having to manually do anything. The file set must also exist only once, so updates can easily be applied to everyone.
I am currently on shared hosting, but I believe I may need to move to a VPS (running Apache) to achieve this.
How do I go about doing this?
Thanks
Unless I'm missing something about what you're trying to do, a simple CNAME DNS record may work. Just CNAME the new domain to your existing 'main' domain.
However, if you need to know what domain was requested using shared hosting this very well may not work. The catch is you would also have to setup the hosting account to accept requests from the new domain. This seems to be what you're trying to avoid.
If this is the case, you'll need a static IP, and Apache setup to accept wildcard domains and pass them to your application (index.php in this case). You may not need a VPS for this, just a relatively configurable shared hosting account.
Update: To get this working on shared hosting, check out the Apache VirtualHost directive. That's what Apache uses to setup a (wait for it) 'virtual' web server (document root, logs, etc) allowing a single server to host multiple sites. Also checkout Named Based Virtual Hosting, you'll need an account that doesn't do virtual hosting that way.
"So it should be a case of a user signing up and it immediately works without me having to manually do anything"... So you want magic. Anyways domains point to a directory so that is easy just point all domains to the same directory. PHP can examine the full URL so you can use that to select the content.
If you are using something like Joomla you might be able to customize the starting extension. For example all joomla content is stored in tables named jos_XXX.
It should theoretically be modifyable to the base table is domain_com_XXX
Maybe, but you really need to find a good programmer for this, I don't know of any systems that will do this out of the box. Maybe someone else might know of one.

Categories