Create subdomain upon user registration - php

I have a website where I want users that sign up to get their own subdomain. This subdomain is virtual, and every subdomain uses the same web server files.
I use PHP and Apache, and I know about Virtual Hosts, but I'm wondering where I need to put the vhosts code. First, I don't have access to httpd.conf. Second, I want this to be done automatically upon registration.
I've read about virtual hosts, but didn't find anything that answers my questions. Is there anyone who can explain me how all this works together, or know where I can find my answers?

Can you tell apache to read an extra .conf file? (traditionally you store your vhosts in httpd-vhosts.conf)
if so, add something like the following and restart your webserver
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /abs/path/to/webroot
ServerName domainname.com
ServerAlias *.domainname.com
<Directory /abs/path/to/webroot>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
then in php, you can see which subdomain the user is requesting by inspecting:
$_SERVER['HTTP_HOST']
ie. if the user requests http://user1.domainname.com/index.php
$_SERVER['HTTP_HOST'] will have user1.domainname.com
you can then explode('.', $_SERVER['HTTP_HOST']) to inspect each segment.. etc.

You will need 3 thing for this:
1.
Set your DNS for *.yourDomain.com
2.
Add the ServerAlias directive to the apache configuration for this domain:
ServerName www.yourDomain.com
ServerAlias *.yourDomain.com yourDomain.com
Also make sure that you apache server has UseCanonicalName set to on (this is the default)
3.
Grep the subdomain name with PHP:
$nameChunks = explode('.', $_SERVER['HTTP_HOST']);
$subDomainName = $nameChunks[count($nameChunks) - 3];

(inspired by Janek's comment)
IF your Apache instance is configured for * aliasing, then there is no need to create a virtual named host - You can fake it with PHP by evaluating $_SERVER['HTTP_HOST'].
To determine if your Apache instance will handle it, edit your local /etc/hosts file (or windows equivalent - %SystemRoot%\system32\drivers\etc\hosts) so that the desired virtual name is pointing to your server.
For instance
# An example HOSTS file.
192.168.1.4 testserver testserver.com subdomain.testserver.com secondname.com
This assume that 192.168.1.4 is the IP of your server. Everything after that are alias's that the server can be called.
Then, as Janek suggested create a page that will echo $_SERVER['HTTP_HOST'] to see if it capturing the name correctly. If so, then all that is required is a DNS change and Apache can remain unchanged.
Otherwise without access to Apache.conf (this kind of implies that you don't have access to a lot of things) this will be difficult to pull off. The programming won't be - but the implementation will be.
Here's why:
Apache by default will serve up virtual hosts. But you do need access to the server's conf directory (often located in /etc/httpd/conf.d/) so you can create the virtual host "include" file (IF the Apache configuration is setup to include it - most recent installs should be).
You will need to be able to cycle Apache (restart). Without this the Virtual Host changes won't take affect.
You will need to be able to change the DNS. You can always change your local /etc/hosts file - but in order for visitors to get to your site, you'll need to be able to push through a DNS change. (Which may instantaneous - or it may take 24 hours to propagate).
The scripting certainly can be done (see Cpanel and WHM)

You will first of all need to setup the DNS server to resolve any subdomains to your main IP address, i.e. *.domain.com will have to resolve to your server's IP address. Then the web server needs to handle all incoming requests regardless of subdomain as well, invoking some PHP script. The PHP script can then simply figure out what domain was requested from the $_SERVER['HTTP_HOST'] header and act accordingly.
You're not actually "creating subdomains upon user registration".

Related

PHP Wamp server - 2 SSL certificates on one server

I have a PHP (Wamp) server that should host two different domains.
Each domain has a different certificate files (.crt .key)
I am trying to edit the httpd-ssl.conf file to configure each domain certificate.
However, I cannot define the correct filter in the virtual host header. Only this filter works:
VirtualHost default:443
Which basiclly means that all domain are directed to one default certificate (And I need each one to direct to a different certificate)
I want to configure it so each domain will use a different filter. Example:
VirtualHost domain1.com:443
VirtualHost domain2.com:443
But this does not work. When I configure it like this, neither of the domains get the certificate.
I am only trying to edit the httpd-ssl file, should I also edit other files?
Thanks
Solution:
You need to create 2 virtual hosts with their two respective ServerName directives (ServerName domain1.com and ServerName domain2.com). Do not use , usually <VirtualHost *:443> is what you need. The VirtualHost directive determines on which IP address to listen, not the name. If you put a domain name inside it's going to resolve it to an IP address on server startup, something you do not want for various reasons. See this answer on Serverfault for an example configuration.
https://serverfault.com/questions/744960/configuring-ssl-with-virtual-hosts-under-apache-and-centos/745285#745285

Redirect Traffic to Different pages bases on the address used

I Have a webserver running on my raspberry pi and I am using it for multiple projects. I can easily enough access the different pages with something of the from ip-addrss\project-name.php. I was looking to eliminate the ip address and found I could set up the domain names on my router. so http:/projector or projector.local redirects to the raspberry pi. The problem is it always goes to the default page. I can setup http:/ProjectA and http:/ProjectB but they both go to index.php. is there a way in php to redirect based on the url used to get there. so index.php would redirect to projectA.php or projectB.php depending on which url was used? I looked through $_SERVER and $_POST but they didn't seem to have the right information. Some research lead me to believe apache could do this but I have experience configuring apache.
You COULD do it in PHP, by checking $_SERVER['HTTP_HOST'], but that value can be manipulated by who is making the request. So I can access http://ProjectA while specifying the headers host: ProjectB or similar, and you will think that it's ProjectB.
In fact, if you look at the HTTP request, HTTP_HOST is the only way one would determine the domain name. So it doesn't matter if you do it in PHP or Apache.
In Apache, you could do it by enabling vhosts mod for apache. If you're running linux, the command line might be something like this a2enmod vhosts_alias. This will allow you to configure different hosts, determined by the host HTTP header, and IP. Each virtual host points to an individual directory. You can have 2 hosts pointing at the same directory, but you'd have to modify the directory properties, something like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName ProjectA
ServerAlias www.ProjectA #you can skip this line if it doesn't apply
DocumentRoot /var/www/foo
<Directory "/var/www/foo">
DirectoryIndex ProjectA.php
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I didn't try to configure the same directory differently for 2 different hosts. My instincts say that it should work, but it may not.
Here's a guide on how to configure virtual hosts on Ubuntu. https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
I have no idea how different it is on Raspberry Pi. But the apache config files should have exactly the same syntax and rules. Only paths and commands might differ.

Configuration: DNS or Apache

I have the following problem.
I have two domains: www.domain1.de and www.domain2.de
I also have path is on Jelastic server where to find my PHP page
myphpsite.jelastic.dogado.eu.
Now I wanted to do the following.
1) If I go to www.domain1.de, then should address bar of the Web Browser
www.domain1.de be displayed, but the page is fetched from myphpsite.jelastic.dogado.eu.
2) When I go to www.domain2.de, then should address bar of the Web Browser
www.domain2.de be displayed, but the page is fetched from myphpsite.jelastic.dogado.eu / admin /.
so
1) www.domain1.de -> myphpsite.jelastic.dogado.eu
2) www.domain2.de -> myphpsite.jelastic.dogado.eu / admin /
The first one I can do by CNAM Record
But we can I solve the second problem without frames?
thank you
This is just a matter of configuring Apache VirtualHosts (assuming that you're using Apache), or Nginx Server Blocks (if you're using Nginx). Leo's link can help you with either of them, within a Jelastic context (where to find those config files etc.): http://docs.jelastic.com/multiple-domains-php
Here's a quick example for Apache:
<VirtualHost *:80>
DocumentRoot /var/www/webroot/ROOT
ServerName domain1.de
ServerAlias www.domain1.de
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/webroot/ROOT/admin
ServerName domain2.de
ServerAlias www.domain2.de
</VirtualHost>
You might also wish to define different log files for each domain etc.
There are many possibilities, and since Jelastic gives you direct access to the server config. files you can configure almost anything that you could want here. It's just a matter of reviewing the Apache (or Nginx) docs and trying it out.
Notes:
Beware that the Jelastic default configuration defines a wildcard to catch all requests. You need to place your custom configuration before that (either literally, or via an include), or else overwrite the default VirtualHost configuration block - otherwise your config. will not have any effect.
The above example handles HTTP only. If you need to handle HTTPS, you need to configure this separately. (e.g. :443 instead of :80)
Remember that you need to restart Apache (or Nginx) for config. changes to take effect.
See also:
Apache VirtualHost Documentation
Nginx Server Blocks Documentation

Cannot create local copy of Magento

I'm trying to create a copy of an existing Magento website on my localhost for developing purposes.
I followed all the steps listed here: Copy ec2 files to local
I also created a fake domain for my localhost so that there’s a “.” in it as I read somewhere this is required by magento. So now my localhost fake URL is something like: www.mysite.local
I have XAMPP installed on OS X 10.9.1 and I placed the magento filesystem as follows:
/Applications/XAMPP/xamppfiles/htdocs/magento/
In the database I added the local URL as follows:
update core_config_data
set value = ‘http://www.mysite.local/’
where config_id = 6;
and on local.xml I entered the following parameters:
<host><![CDATA[localhost]]></host>
<username><![CDATA[myusername]]></username>
<password><![CDATA[mypassword]]></password>
<dbname><![CDATA[mydatabase]]></dbname>
where localhost is just localhost, myusername is the username for the db I restored on my local XAMPP server, mypassword is the password for that same db, mydatabase is the name of the same db.
Still, when I browse to www.mysite.local/magento/ or www.mysite.local I don’t see anything appear.
What am I doing wrong?
Thank you so much!
If you're using a local server, here's some basic trouble shooting to help you debug:
Firstly, is the local server active? Can you visit http://localhost without it displaying server not found? If you can't, your local server is most not running. Try firing up xampp and launching Apache.
Secondly, it seems you're trying to create a virtual host for your local server. That's great! Virtual hosts allow you to create individual URLs for projects on the same server. E.g. http://myproject.dev.
However, you first need to make sure that the server understands what you're doing.
You will need to create a virtual host in your server if you haven't already.
I see you're using xampp. What you need to do is navigate to your xampp install, and edit your apache/conf/extra/httpd-vhosts.conf file, which is the file xampp recommends you use solely for virtual hosts.
Reading: Setting Up Virtual Hosts for XAMPP
For example, in Apache, a hosts config file may look like this:
# Base
<VirtualHost *:80>
DocumentRoot "X:/"
ServerName localhost
</VirtualHost>
# Project - Some Project of Mine
<VirtualHost *:80>
DocumentRoot "X:/projects/myproject/public"
ServerName myproject.dev
ErrorLog X:/projects/myproject/logs/apache.error.log
CustomLog X:/projects/myproject/logs/apache.access.log common
php_value error_log X:/projects/myproject/logs/php.error.log
</VirtualHost>
(navigating to myproject.dev displays the files in my X:/projects/myproject/public directory)
This answer is not to explain virtual hosts to you however. There are plenty of amazing resources online to help you get started with setting up your own.
Don't forget to restart your server when you add a virtual host!
If this is already set up, is your computer's hosts file set up to point to your server?
Your hosts file on your computer is used to tell it to do certain actions when you enter a matching url in your browser.
Reading: The Hosts File and what it can do for you
Reading: How to Edit Your Hosts File
For example, using the apache conf file settings above, my hosts file must also include:
# My project - Localhost
127.0.0.1 myproject.dev
It tells my computer to send the request to my local server (at localhost) when I use the URL myproject.dev. The local server then picks up the request, sees that you're accessing myproject.dev and checks if it has any virtual hosts matching that name. Well, whaddya know, it does! It then looks at the DocumentRoot setting for the location of the server files, and continues the process. Think of your hosts file as a local DNS of sorts.
If you've just added the site to your hosts file, it may take a few minutes to start resolving correctly. Wait a little, clear your browser cache and try again.
Finally, if these steps are done, and you're receiving nothing, it may be a server configuration problem, or a .htaccess issue.
If you're running on windows, you can check your event log for apache server errors. If you have set up logging on the virtual host, you can check those files to see if it's picking up your requests, and what it's doing with them if it is.
Most issues after that point will at least yield a visible error in your browser (or a blank page).
I hope this helps!
Also checking the magento error logs could tell you what the issue is, assuming it's actually hitting magento at all at this point.
As Sean mentioned above, one of the most common problems I've seen when copying a magento site is accidentally omitting the .htaccess file - make sure it's present in your site root.
If you tested your site before making the change to core_config_data, then you can also try deleting everything in var/cache and var/session. Also make sure that the web user has write permissions on var.
Regards
Hans

PHP Domain Mapping Script

I don't know what is the exact term for this, so my title could be incorrect.
Basically what I what to do is to write a PHP script that has an input field:
Domain Name: <input type='text' name='dname' id='dname' value='http://example.com' />
<input type='submit' name='addname' value='Add A Domain' />
When user type their own domain into the text field and press submit, the script will automatically make a directory, copy some PHP scripts there and map the domain name to there. (The domain name is pointing to our server, of course.)
I have already figured out the mkdir() and copy() part, but I couldn't figure out the mapping part. How to add an entry to map http://example.com to /home/user/public_html/copy1/ automatically, using PHP?
While you could do that directly from your PHP page, I suggest not to do that, for many reasons, from high failure risks (in case page execution gets interrupted suddenly, for example) to security risks (your httpd user will have write access to its own configuration + some stuff on the filesystem where it shouldn't).
Some times ago I wrote a similar control "website creation control panel" that works pretty much this way:
The php script receives the website creation request and stores it somewhere (e.g. in a database). dot.
Another script, running as root via cron each, let's say, five minutes checks the website creation requests queue. If there is any:
Mark the site creation task as "locked"
Create the directory at appropriate location, populate with scripts etc.
Change all the permissions as needed
Create new virtualhost configuration, and enable it
Make the webserver reload its own configuration
Mark the site creation task as "done"
The second script can be written in whatever language you like, PHP, Python, Bash, ...
About apache configuration
To create a directory "mapped" to your domain, you could use something like this:
First of all, "slugify" your domain name. I usually take the (sanitized!) domain name, and convert all dots with double-dash (that is not a valid domain name part). I don't like dots in file/directory names a part from file extension separation.
Then, you can create something like this (assuming domain is www.example.com):
<VirtualHost *:80>
ServerName www.exampple.com
DocumentRoot `/my-sites/wwwroot/www--example--com`
<Directory "/my-sites/wwwroot/www--example--com">
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
AllowOverride All
</Directory>
ErrorLog /var/log/apache2/www--example--com_error.log
CustomLog /var/log/apache2/www--example--com_access.log vhost_combined
</VirtualHost>
<VirtualHost *:80>
## redirect non-www to www
ServerName www.example.com
ServerAlias example.com
RedirectMatch permanent ^(.*) http://www.example.com$1
</VirtualHost>
Assuming you are placing your site files in directories like /my-sites/wwwroot/www--example--com.
Some security-related improvements
In my case, I also preferred not running the second script by root either; to do so you have to add some changes in order to let a less privileged user do some things on your system:
Create a directory with write access for your user, let's say /my-sites/conf/vhosts
Create an apache virtualhost containing the following line: Include "/my-sites/conf/vhosts/*.vhost", and enable it
Then, let your user reload apache configuration, by installing sudo and adding this to your /etc/sudoers:
Cmnd_Alias A2RELOAD = /usr/sbin/apache2ctl graceful
youruser ALL=NOPASSWD: A2RELOAD
%yourgroup ALL = NOPASSWD: A2RELOAD
And, of course, also give write permissions to your websites base directory to the user that will be used to run the script.
I think you need to add VirtualHosts to apache config (such as httpd.conf)
like this:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /home/user/public_html/copy1/
</VirtualHost>
Apache documents for virtual host config:
http://httpd.apache.org/docs/2.2/vhosts/name-based.html
http://httpd.apache.org/docs/2.2/vhosts/mass.html
It is over 10 months now, so I'll just suggest what I have found.
While writing directly to httpd.conf seems the only way, but recently our site change the server. It has cause us so much trouble in those file / folder permission, and the hosting company refuse to help us due to security concern.
So I have a second look and discovered I am using CPanel for hosting, I can certainly use Addon Domain feature to create and add in new domains.
But it has its limits. Since it is an addon domain, we can no longer limit the bandwidth and disk usage per domain. Everything is shared. But that doesn't matter to us anyway.
So far it works. You can do it by either using the CPanel API library available on the official cpanel website, or you can use the direct URL request way to create the domain. Since we are making a new wordpress install, we create a new database as well.
http://{username}:{password}#{mysite.com}:2082/frontend/x3/sql/addb.html?db={dbname}
http://{username}:{password}#{mysite.com}:2082/frontend/x3/sql/adduser.html?user={dbuser}&pass={dbpass}
http://{username}:{password}#{mysite.com}:2082/frontend/x3/sql/addusertodb.html?db={dbname}&user={dbuser}&ALL=ALL
http://{username}:{password}#{mysite.com}:2082/frontend/x3/addon/doadddomain.html?domain={newsite.com}&user={ftp_user}&dir={ftp_dir}&pass={ftp_pass}&pass2={ftp_pass}
It doesn't have to be wordpress. You can use this to install joomla, drupal, phpbb or even you custom script.
Hope it helps anyone who is reading this. Thanks.

Categories