How do I set the entered subfolder as root? - apache

I have setup VirtualHost and it works good when I use the domain (ex. www.website_1.se). The problem is that the DocumentRoot that is set for the VirtualHost is not applied when I visit the website through localhost (ex. localhost/website_1.se).
My root folder looks like this:
website_1.se, website_2.se, website_3.se
And my conf:
<VirtualHost *:80>
ServerAdmin admin#website_1.se
ServerName website_1.se
ServerAlias www.website_1.se
DocumentRoot "C:/xampp/htdocs/website_1.se"
</VirtualHost>
Inside website_1.se, I have html-files with src attributes, some of them starts with '/', which refers to the root.
So if I have a src="/images/file.jpg" inside the index file of "website_1.se", and enter "localhost/website_1.se, Apache will try to load that image from localhost/images/file.jpg instead of localhost/website_1.se/images/file.jpg
So my question is. How can I use '/' in paths, so that it works the same way for both localhost and VirtualHost (domain)?
EDIT (I ask the question again, better and clearer this time hopefully.)
I need to be able to host multiple websites on my apache server.
I have managed to do it with the help of vhost.
For example:
<VirtualHost *:80>
ServerName site_1.se
ServerAlias www.site_1.se
DocumentRoot "C:/xampp/htdocs/site_1.se"
</VirtualHost>
<VirtualHost *:80>
ServerName site_2.se
ServerAlias www.site_2.se
DocumentRoot "C:/xampp/htdocs/site_2.se"
</VirtualHost>
So when I enter www.site_1.se I get to localhost(htdocs)/site_1.se. And when I enter www.site_2.se I get to localhost(htdocs)/site_2.se and so on. Perfect, that's the point.
Now here is the problem. These folders (site_1.se & site_2.se) contains html-files, and in these files I have links with addresses that starts with '/', which refers to the root.
So for example if have: <img src="/images/file.png">
inside a html-file in site_1.se, Apache tries to locate the image in:
localhost/site_1.se/images/file.png (if entering www.site_1.se)
or
localhost/images/file.png (if entering localhost/site_1.se)
So this obviously becomes a problem when developing in localhost and using '/' at the beginning of paths. Because when you then visit the page from the domain name, the links are wrong.
So I'm wondering how can I set up apache, so I can specify paths starting with '/', and get the same root no matter how I visit the site?

for localhost configuration listen on different ports so as to distinguish between the different sites. You need to tell Apache to listen on those ports also.
Listen 80
Listen 81
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/site_1.se"
Require all granted
</VirtualHost>
<VirtualHost *:81>
DocumentRoot "C:/xampp/htdocs/site_2.se"
Require all granted
</VirtualHost>

Related

virtual host not able to access file outside document root

I have setup virtual hosts for separate projects on my localhost on my macOS. Here is the httpd-vhosts.conf file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "/Users/Sammy/Sites"
ServerName 127.0.0.1
ServerAlias localhost
ErrorLog "/private/var/log/apache2/localhost-error_log"
CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Sammy/Sites/workspace/abc/public_html"
ServerName abc
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Sammy/Sites/workspace/xyz/public_html"
ServerName xyz
</VirtualHost>
I have also updated the list of hosts. The problem is, when I access my sites using this link, The sites are not able to load the css/js files & images present outside the 'public_html' directory, while it works if I open it via localhost path..
I come from an ubuntu environment, The apache directory structure is quite older on mac! can anyone help me with this..
Thanks
The sites are not able to load the css/js files & images present outside the 'public_html' directory
That is rather the point.
The DocumentRoot is the root of the website. Everything under it is part of the site.
By putting something in the DocumentRoot you are giving it a URL and making it available to be requested over HTTP.
An HTTP server isn't designed to expose every private file on your whole hard disk to the world. That would be awful.
If you want to give a file a URL, then put it in the DocumentRoot (or look at the Alias directives).

NamedVirtualHost in apache configuration

I have a unix system whose actual name is "ech-10.45.25.12"
i have installed apache server in it.
Now i need to configure it in such a way that the two applications running in the same machine in tomcat in two different ports should be accessed by the same domain.
ie., i have two applications running in the same machine under different port
http://ech-10.45.25.12:8080/issuetracker/
http://ech-10.45.25.12:8180/dashboard/
I would like to name this server(ech-10.45.25.12) as devjunior.mycompany.com
The following is the configuration i have made in httpd.conf
Listen 80
Listen 8080
Listen 8180
NameVirtualHost ech-10.45.25.12:80
NameVirtualHost ech-10.45.25.12:8080
NameVirtualHost ech-10.45.25.12:8180
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:8080>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:8180>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
i know i am doing a major mistake
But i should be able to access the applications by using the following urls
http://devjunior.mycompany.com/issuetracker
http://devjunior.mycompany.com/dashboard
Should i create ANY directories under any folders any where in the system
Please tell that also.
You configured only the names. So you've configured Apache to listen for:
http://devjunior.mycompany.com:8080
http://devjunior.mycompany.com:8180
You can:
Configure 2 domains with namevirtualhost without using ports. this is the most elegant way of doing what you want
Configure a single domain that points to a single directory on the filesystem with 2 links for the diferrent applications. This works with php mostly or pure html pages. With more complex applications you could incur in a lot of headache..
Domain and port. Like you've done. But you can access only by http://devjunior.mycompany.com:8080/issuetracker and http://devjunior.mycompany.com:8180/dashboard
Solution 1
You can use different domains or subdomains (which are cookie friendly in an eventuality of single sign on).
Listen 80
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName dashboard.devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName issuetracker.devjunior.mycompany.com
DocumentRoot /www/domain-8180
</VirtualHost>
Solution 2 is left as an excercise for the reader... :P
Here is what i did to make it work.
Though the change of name in etc/hosts file did nothing in my intranet, so i used the actual name of the machine which is ech-10.45.25.12
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName ech-10.45.25.12
ProxyPreserveHost on
ProxyPass /issuetracker http://ech-10.45.25.12:8080/issuetracker
ProxyPass /dashboard http://ech-10.45.25.12:8180/dashboard
</VirtualHost>
Also dont forget to add the "proxyName" & "proxyPort" attribute to the tag in tomcat's server.xml

Apache Virtual Host using mod_vhost_alias

I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.
To do this I set the following in my 000-default.conf file in the sites-available folder.
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
This worked great.
Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.
Can someone please indicate to me what I am doing wrong?
Full Code Example In apache2/sites-available/000-default.conf:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot.
VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).
Edit:
Now you need some other changes:
- ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4).
- Move the Mass-Virtualhost as first, so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).
I have figured out how to do this, and decided to post the solution here for anyone else sitting with a similar problem:
SO to setup apache2, using mod_vhost_alias to have all domains point to a generic folder with the same name, but specific domains to point elsewhere, this is what you need to do.
In your 000-default.conf site config file, write the following code:
UseCanonicalName Off
Then add the following block for each specific domain you want to point to a specific folder, replacing example.com with your domain name:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.*
DocumentRoot path/to/your/folder
</VirtualHost>
Then add the next block to point all other generic domains to a generic folder:
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias www.*
VirtualDocumentRoot path/to/your/folder/%2+
</VirtualHost>
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias *
VirtualDocumentRoot path/to/your/folder/%1+
</VirtualHost>
The first block will direct all domains, starting with www. to a folder matching the name after the www.
The second block is to direct the same domains, when no www. is specified, to the same folder.
For more information on the dynamic mass virtual host options to use in the document root, go to: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

configure httpd.conf to support same IP and different PATH ,with one default page

I am configure the httpd.conf to support same IP addr and different virtualHost.
<VirtualHost *:80>
DocumentRoot /var/www/
ServerName AA </VirtualHost>
<VirtualHost *:80>
ServerPath /var/www/
ServerName BB
</VirtualHost>
Now, I can access the webpage by
http://192.168.20.100/AA
and
http://192.168.20.100/BB
This is almost done, but I need the final step.If the user access the IP address, I want to show them the default AA, that is when user access http://192.168.20.100/ , the content of http://192.168.20.100/AA will be showed.
I give it a lot of tries, but none works. The following ways i have tried:
1) configure the global DocumentRoot in httpd.conf
2) set the serverName to empty to "/"
Have anyone met the same situation?

Hosting two websites from the same webserver

On my webserver, I want to serve several websites just based on domain name.
For example, I want a webserver that will serve "mycoolsite.com" and "badstuff.org".
I pointed both sites at the same IP address. In httpd-vhosts, I made two entries:
<VirtualHost *:80>
DocumentRoot /www/mycoolsite
ServerName www.mycoolsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/badstuff
ServerName www.badstuff.org
</VirtualHost>
But when I go to badstuff.org, I get served mycoolsite.com! Why is this happening?
Apache Doc
I also know that when I use MAMP, I have to adjust my "hosts" file too. Is this relevant?
Have you added the NameVirtualHost directive before the vhosts declaration ?
NameVirtualHost *:80
<VirtualHost *:80>
...
</VirtualHost>
You may want to check your configuration by command:
$/usr/local/apache2/bin/httpd -S
If everything is OK, try to check again by restarting the server.