I have a default Virtual Host. It is configured to watch Document Root inside /var/www/html and works great. But I have a problem. I have multiple sites inside subdirectories and I don't know how to set Document Root for each of them. For example, /var/www/html/test, /var/www/html/test2, etc. When I include file from /var/www/html/test, it searches inside var/www/html. For example, I include file like include_once '/core.php', but I have an error Failed opening required '/var/www/html/core.php', because there is no current file here. Is it possible to set Document Root for each of directories inside main Virtual Host? Thank you!
My main Virtual Host configuration:
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Also, you can say, that I need to use relative paths, but it isn't possible in some ways, because, sometimes, I work with ready sites, created by another people.
Is it possible to set Document Root for each of directories inside
main Virtual Host?
Short answer is no, you cannot. 1 Document Root per Virtual Host. In order to understand how the things are working you need to understand what is the directives context. Lets take as an example VirtualHost directive. As we can see from the docs it has only 1 Context which is Server Config. And server config context tells next:
directive may be used in the server
configuration files (e.g., httpd.conf), but not within any
VirtualHost or Directory containers. It is not allowed in
.htaccess files at all.
But DoocumentRoot has two contexts: server config and virtual host. Rest you should figure out for yourself ;)
There are plenty ways how you can achieve desired result
Running several name-based web sites on a single IP address
Running different sites on different ports
Mixed port-based and ip-based virtual hosts
Hope that this answer will help you!
Related
Lets say, i have 3 domain names
domain1.com
domain2.com
domain3.com
All # records currently go to my server, resolving to root/index.php.
Currently, i redirect each domain to different sub-folders with header redirects within index.php. So each url looks like
http://domain1.com/new_root1/index.php
http://domain2.com/new_root2/index.php
http://domain3.com/new_root3/index.php
Not very pretty.. but it works.
I am trying to configure my server to redirect with httpd.conf. What i would like is have domain1 and domain2 resolve:
http://domain[*].com/index.php
on local ip 192.168.0.101:80
and domain3 resolve:http://domain3.com/index.phpon local ip 192.168.0.100:1979
There is plenty of documentation, tutorials,etc, but nothing seems to fit this..
I have looked at .htacess. Preferred comments are to use httpd.conf. Searched Apache docs. All attempts fail.
Where do i look? Tx in advance
With basic name based vhost configuration all of the domains will use port 80 and work as expected. You can set httpd.conf to load all individual .conf files in a particular folder and then add individual .conf files to that folder, each containing 1 vhost definition.
Near the bottom of my apache2.conf I added:
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
# Include all my projects vhost conf files:
Include /vagrant/vhosts/*.conf
And in the folder I specified for loading my project vhost conf files I add a bunch of .conf files that look like this:
<VirtualHost *:80>
ServerName www.demo.vm
ServerAlias demo.vm
DocumentRoot /vagrant/htdocs/demo.vm/public
<Directory /vagrant/htdocs/demo.vm/public>
Require all granted
Options Indexes FollowSymLinks
</Directory>
SetEnv APPLICATION_ENV "development"
</VirtualHost>
If this is a local server you would have to edit your operating system hosts file to point your custom domain to the localhost.
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.
Cakephp 3 and Subdomain
I have an application cakephp 3 inside the root of my server and need to create a subdomain that this also goes in the same root with the cake.
example:
bin
config
src
...
shop (Sub domain)
How have the cake in my root it does not allow access subdomio.
What can I do to get around this problem?
Because the file convention of cakephp is sometimes hard to get the result you want to.
Correct me if I'm wrong. Your situation looks like this?
www.maindomain.com, which run by 'src'.
But you want the sub.maindomain.com use other files outside the cakephp file structure
I think it's too much effort to work outside the file convention. I think this is more '.htaccess' and 'virtual hosts' issue.
I've found a link that maybe can you help you "CakePHP subdomains with htaccess"
UPDATE:
Also keep in mind the folder-permission issue when you try to fix this.
I think the best approach is to have a VirtualHost configured, I am assuming you use Apache, so that the shop folder isn't part of the root of the server. Here you can see how to do it:
Move shop folder to outside Apache root so it's not part of the main website. If it's in /var/www/html/shop move it to /var/www/
Add a VirtualHost to Apache httpd.conf file or add it as shop.conf into /etc/apache2/sites-available/ assuming your server is an Ubuntu 14.04, you must know where to save the file according to your distro.
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName shop.local
ServerAlias shop.local
ServerAdmin alejandro#ulfix.com
DocumentRoot /var/www/shop/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Optional: Edit /etc/hosts file so that you can reach the new subdomain if it is in a VM o you can't resolve it from your computer:
192.168.100.14 shop.local # Change IP to your Server's IP
Enable site in Apache (In Ubuntu you'll need to run: sudo a2ensite shop.local) and restart Apache Server.
See CakePHP 2.x virtual host file for Apache2 for reference.
I have a Django application hosted on my main domain (example.com), and I now need to host a PHP application on a subdomain (forum.example.com).
In the directory of the main domain, I have the following .htaccess entries:
SetHandler mod_python
PythonPath "/home/.../apps/example'] + sys.path"
PythonOption mod_python.importer.path "['/home/vlive/python']+ sys.path"
PythonHandler django.core.handlers.modpython
#PythonDebug On
SetEnv DJANGO_SETTINGS_MODULE example.settings
SetEnv PYTHON_EGG_CACHE /tmp/egg-cache
At present, when I load the subdomain (forum.example.com) I still see the main site (example.com).
How can I fix this?
You need to set up your subdomains via the VirtualHost directive and only add the mod_python / mod_wsgi handler in one of the VirtualHosts.
You said you loaded the subdomain and it still shows the main site. Would you mind showing us your Apache's site configuration?
The most common reason for wrong VirtualHost being selected is a missing NameVirtualHost directive that matches host/port qualifier specified in VirtualHost directive.
What NameVirtualHost directive do you have set? What is the argument to the two VirtualHost configurations?
The general layout would be:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName forum.example.com
...
</VirtualHost>
These would usually be in the same file for a Linux Apache distribution though.
What have you used?
As Graham noted, most probably you have wrong VirtualHost configuration.
Double check that there is two different VH with right ServerName and that you don't use * (wildcard) in ServerName and ServerAlias that may overlap with subdomain or it goes AFTER subdomain.
Apache only search for first VH that matches, so if you have something like *.example.com above any other subdomain like forum.example.com would not work.
Also mod_wsgi is recommended by django documentation, consider switching to it.
I have a Zend Framework project on a local machine, and as recommended its /public subfolder is made a DocumentRoot in httpd.conf. So I access this app simply with http://localhost.
This new requirement makes me unable to access other
web apps in a former webserver root (which contains a few regular php apps and a couple of zend framework apps).
What configuration should I choose to be able to simultaneously access both ZF root-based apps and other apps like PHPMYADMIN?
You'll probably need to use some kind of VirtualHost
You have at least two solutions :
VirtualHosts based on diferent port numbers
For instance, you'd have one site on http://localhost/ (ie, default port = 80)
And another one on http://localhost:8001/
And another one on http://localhost:8802/
And so on
VirtualHosts based on different domain-names
For instance, you'd have on site on http://localhost/
And another one on http://mytestwebsite/
In the second case (the solution I always use), you will have to edit your "hosts" file, so "mytestwebsite" is an alias to your local machine -- which IP address is 127.0.0.1
Under windows, this file is located in C:\WINDOWS\system32\drivers\etc
On Linux, it's in /etc/
You'd have to add a line like these ones :
127.0.0.1 localhost
127.0.0.1 mytestwebsite
Then, in the Apache configuration, you need to create one VirtualHost per site. Something like this, I suppose :
<VirtualHost *>
ServerName mytestwebsite
DocumentRoot /home/squale/developpement/tests
<Directory /home/squale/developpement/tests>
Options Indexes FollowSymLinks MultiViews +SymLinksIfOwnerMatch
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName myothertestwebsite
DocumentRoot /.../myothertestwebsite
<Directory /.../myothertestwebsite>
Options Indexes FollowSymLinks MultiViews +SymLinksIfOwnerMatch
AllowOverride All
</Directory>
</VirtualHost>
(Needs tunning / configuration, of course)
And you'll also probably need some directive like this one :
NameVirtualHost *
Well, this might not be the entire solution (that one depends on your server / applications) ; but I hope these few pointers will help you get to it !
If you need more help, the keyword is "VirtualHost" ;-)
Have fun !
Zend Framework probably uses a .htaccess file. If so, you might use that to add rules to leave the other apps alone. Another option is to use subdomain (eg phpmyadmin.localhost)