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.
Related
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
I've had this problem for a while, and have unsuccessfully searched far and wide for an answer.
<img src="/images/test.jpg" />
Gets an image from (root path - in my case in production in LAMP)
htdocs/images/test.jpg
Whether it's called from htdocs/index.php or htdocs/foo/bar/index.php
I use XAMPP in development, and inside htdocs have project folders, so the method described above although will work when live requires me to alter it to:
<img src="/projectName/images/test.jpg" />
when working locally.
To make this simpler i define a constant BASE, which in development I use:
define('BASE','/projectname/)
And then when it's live I change to:
define('BASE','/')
<img src="<?php echo BASE;?>images/test.jpg" />
This is obviously really annoying and ends up causing several issues. Please can someone shed some light on this situation, what I'm specifically looking to do is use root path in my image/script sources but for:
<img src="/images/test.jpg" />
when called from htdocs/projectName/foo/test.php
to look for the image in:
htdocs/projectName/images/
Is this possible?
An alternate way to handle this is with the use of Virtual Hosts.
A virtual host acts like a second version of localhost that works specifically for a subfolder. For example, you could set up Apache so that when you visit http://example (no .com or anything), it shows you the content from http://localhost/example/. All CSS and JavaScript and links would act as if they were operating from the root folder of a website, since the leading example folder has been trimmed out of the URL.
I can't find a walkthrough that I used to use for XAMPP, but here a similar one that covers all of the main points. It was written for Windows, but I imagine that there are similar mechanisms that you can use for LAMP:
To summarize, here's what the article tells you to do:
Enable Virtual Hosts within Apache
Set it up so that when you visit example, you are sent to 127.0.0.1
Configure Apache so that when someone visits 127.0.0.1 (but the name of the website is example), then it shows content from the example folder.
This is how your production site (which is a single server with multiple websites) has a different "root" for each website.
Have you ever thought about the base-tag in the header of your html content? http://www.w3schools.com/tags/tag_base.asp
<head>
...
<base href="<$path />">
...
</head>
get base path:
$path = $_SERVER['SERVER_NAME'] == 'production.host' ? '/' : 'projectName';
You can either try the solution provided by others in this thread (which are programming solutions), or as an alternative, I do something different (a setup solution).
For me, I like to create an independent environment, in which my projects and development files are separate from XAMPP as much as possible (I am using XAMPP, but the principle applies to other hosting environments). This allows me to easily install new updates for XAMPP whenever they become available without worrying about my projects, and also I like to have all my projects in one folder dedicated to development. This development folder will contain projects for web, mobile, and other environments.
The way I set it up, is I have a c:\dev\ folder, that will contain a list of my projects, each project is on its own. So, for example, c:\dev\project1\, c:\dev\project2\ and so on.
Now, after I create those folders for development, I make sure that the httpd-vhosts.conf file (located at c:\xampp\apache\conf\extra\) and the hosts file (located at c:\Windows\System32\drivers\etc\) have the correct references.
Lets assume that one of my projects is called project1. and it is typically located at c:\xampp\htdocs\project1 then I would normally access it via the browser as http://localhost/project1
However, in order to have an independent environment, and as explained earlier, I would create a development folder in the c: drive called c:\dev, then I would move project1 into it, and ended up with c:\dev\project1
Then, to access this project via typing project1.dev in the web browser, I appended the httpd-vhosts.conf file located at c:\xampp\apache\conf\extra\ as follows:
<VirtualHost project1.dev:80>
ServerAdmin admin#project1.dev
DocumentRoot "C:/dev/project1"
ServerName project1.dev
ServerAlias www.project1.dev
ErrorLog "logs/project1.dev.error.log"
CustomLog "logs/project1.dev.access.log" combined
<Directory "C:/dev/project1">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
Also, I have to update the hosts file (located at c:\Windows\System32\drivers\etc\, and add the following entry:
127.0.0.1 project1.dev
127.0.0.1 www.project1.dev
(where 127.0.0.1 is the same as your localhost. Also note that you have to add the second entry in the hosts file for the www alias).
This allows me to access my project as: http://project1.dev
Once this is set, I can now write my code consistently for both my development and production environments, and my references to the root will work just fine.
Also, with this setup, I do not care if I need to update my XAMPP or switch to LAMP or anything else, all I care about is making sure I take care of one file only, which is my setup file httpd-vhosts.conf. And as I mentioned, I always prefer having a separate folder for development, and I can have different types of projects in the development environemt, for example, mobile projects, web projects, ... etc.
Hope this helps.
Note about multisite (subdomain setup)
If you would like to setup a multisite (subdomain setup), then after you enable WordPress for multisite according to the WordPress instructions, you have to do the following:
Assuming you want the following structure for multisite:
project1.dev
www.project1.dev (this is an alias to project1.dev)
sub.project1.dev (this is another setup, subdomain)
Then you have to add the following entry to httpd-vhosts.conf
<VirtualHost sub.project1.dev:80>
ServerAdmin admin#project1.dev
DocumentRoot "C:/dev/project1"
ServerName sub.project1.dev
ErrorLog "logs/sub.project1.dev.error.log"
CustomLog "logs/sub.project1.dev.access.log" combined
<Directory "C:/dev/project1">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
And update the hosts file to include
127.0.0.1 www.project1.dev
Note that DocumentRoot points to the same directory for both the main site and the subdomain site.
Repeat the process for each new subdomain you add in your WordPress network.
I think the best way to do this is with a baseurl, set your baseurl in your config and use it for external files.
$config = new Config();
public $baseurl = "http://dev050.nl";
You can use it then as
$config->baseurl;
And maybe this is something interesting for you:
http://twig.sensiolabs.org/
hope it helped.
I do a similar thing to switch databases between my dev server and the hosting server if it's any use to you. No reason why it shouldn't work for a base URL.
// db connect
if($_SERVER['SERVER_NAME'] != "dev.mydomain.org"){
try{
$pdo = new PDO('mysql:host=mysql.***.net;dbname=***;charset=utf8', '***', '***');
}catch(PDOException $ex){
header('Location: error_db.php');
}
}else{
try{
$pdo = new PDO('mysql:host=localhost;dbname=***;charset=utf8', '***', '***');
}catch(PDOException $ex){
header('Location: error_db.php');
}
}
A possible solution could be to use mod_rewrite to automatically change the path on you development server
RewriteCond %{HTTP_REFERER} ^https?://localhost/testsite/
RewriteCond %{REQUEST_URI} \.(jpg|gif|png|bmp)$
RewriteRule ^ /testsite/$1
I believe the above mod_rewrite rule is correct but it would be best to verify this yourself.
I got this task form school, to make a PHP web application. But I don't really understand what this requirement might mean
It should be possible to run this application outside the domain root
e.g. sample URL: http://localhost/task/.
I searched a little bit on the internet but was not able to find anything that I could understand ?
I have wamp, and the folder where is my sites is wamp/www/task
When they say "outside of domain root" it means that you should not be forced to go to
http://localhost/yourfile.php
but you could put it in a subdir, like
http://localhost/task/yourfile.php
What they want you to do is harder to guess, but it could mean that you need to be able to run it in any subdir, so take care of you imports to be able to handle that (e.g.: not hardcode the dir you're working in).
Domain root seems to be at localhost, this just means it should be easy to rename your web application folder and make it still work at anywhere.
# http://localhost/task
$ cd wamp/www/
# http://localhost/task2 - should be accessible without you needing to change anything
$ mv task task2
From technical point of view, you should use relative path for all your links and images as well as external resources such as javascript / css files
you can set vitual host for your web server & access your PHP Application likw www.oorja.local
in the wamp server, just add below code at end of your httpd.conf file, which allow you access your PHP application without localhost, Document root and Directory have your physical pathe of your application directory.
ServerName oorja.local
DocumentRoot E:/LAMPSYSTEM/wamp/www/oorja/public
<Directory E:/LAMPSYSTEM/wamp/www/oorja/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
This question has been asked amply all over the internet and on SO -- The available answers are obscured by specific details about everyone's particular installation. Hopefully this question will help everyone, including me, understand this better.
My question is "How should we configure the Apache .conf file to handle multiple domains?"
(I know the answer "depends". Hopefully, 80% of the people out there are going to have a basic setup at first and then make it more complex later.) I hope that's a good assumption.
I'm comparing Magento's install to Wordpress. Under Wordpress, the "best way" to install it seems to be to put the whole core into a folder called /wordpress. Then, using Apache rewrite rules and permalinks, obscure the /wordpress folder. In a multi-domain situation, the Wordpress core smartly handles the whole process for you without much modification of the .htaccess files. Further, whatever modifications do need to be made, Wordpress can do it for you - or a plugin can. Gosh, I wish Magento was as easy to install as Wordpress. But it's not. (yet!)
Under Magento, the term "store" is very misleading when we think about URLs. So let's ignore it for now and just worry about the lowest level -- the "Store View". In my case, I have the following:
Main Web Site -> Main Web Site Store -> Default Store View
www.site1.com -> Main Store -> site1_english
www.site2.com -> Main Store -> site2_english
The first column is the domain.
The second column is the store --
misleading title for this discussion.
The last column is the "Store View".
As for where to physically place the files. Most people, including me, want to leave the Magento core alone. No changes at all. So we put that into the htdocs root as follows:
/var/www/html/magento
This is where everyone gets confused I think. The Linux distros are not consistent about where this location is. And worse, lots of people are trying to do this on an economical hosting account. I'm using EC2 myself. Ignoring all that... Stick /magento in your document root. The next thing you might add is a /wordpress folder. Yay. Do it later.
In my apache config, I have it setup like this:
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site1.com
DocumentRoot /var/www/html/magento
<Directory /var/www/html/magento/>
AllowOverride All
</Directory>
# Other directives here
SetEnv MAGE_RUN_CODE "site1"
SetEnv MAGE_RUN_TYPE "website"
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
DocumentRoot /var/www/html/magento
<Directory /var/www/html/magento/>
AllowOverride All
</Directory>
# Other directives here
SetEnv MAGE_RUN_CODE "base"
SetEnv MAGE_RUN_TYPE "site2_en"
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/nowhere
</VirtualHost>
The /nowhere folder has an index.html that says "no where".
The situation that I'm seeing is as such:
Navigate to www.site1.com -> Magento redirects to the Default Store View (on the IP address)
Navigate to www.site2.com -> Magento redirects to the Default Store View (on the IP address)
Navigate to the IP address of my server -> Magento displays the Default Store View
The Default Store View's BASE URL is http://my.ip.add.ress
Here are my questions:
What are we supposed to put in MAGE_RUN_CODE and MAGE_RUN_TYPE? Some sites say "website", some say "store". What is supposed to go in there?
Why don't I ever get to my nowhere site?
Why does the www.site1.com keep redirecting to the Default Store View?
Some of the documentation out there talks about creating dedicated folder or copying around index.php or .htaccess. Or making our own .htaccess. In this article, I selected the Apache config file.
Why won't that work right?
Why does the www.site1.com keep redirecting to the Default Store View?
Go to
System->Configuration->General->Web
There you should find Base secure and Base unsecure URL for you store. Moreover to the upper left corner you should see the scope of your settings. So for each store view you should change(if possible) URL of your store view.
What are we supposed to put in MAGE_RUN_CODE and MAGE_RUN_TYPE? Some sites say "website", some say "store". What is supposed to go in there?
If I don't mistake in code you specify the code of the "object" you want to launch, and in type you specify the type of this object - store or website. But this is not 100% info.
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".