Configure main domain and sub domain with diffrent resourcs in apache server - apache

I have an issue to creating a sub domain in apache server.
Normally we can do that by following way.
<VirtualHost *:80>
ServerName www.maindomain.com
ServerAlias *.maindomain.com
DocumentRoot "/home/abc/xyz"
<Directory "/home/abc/xyz">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
In above example both main and sub domain share same Directory.
But my requirement is main domain have its own resource and rest of sub domains have other resource.
I want to do something like this.
<VirtualHost *:80>
ServerName www.maindomain.com
DocumentRoot "/home/abc/xyz"
<Directory "/home/abc/xyz">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName xyz.maindomain.com
ServerAlias *.maindomain.com
DocumentRoot "/home/ghi/pqr"
<Directory "/home/ghi/pqr">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
Above thing didn't work for me

I think you can use apache VirtualDocumentRoot.

Does the NameVirtualHost directive (for example, NameVirtualHost *:80) appear in your Apache configuration file, before the virtual hosts? If not, it needs to be there.

Related

XAMPP virtualhost site with all configs not working

I am trying to run site my.com locally on XAMPP's apache server. Here my configurations in httpd-vhosts.conf file in the path ...\XAMPP\apache\conf\extra:
<VirtualHost *:80>
DocumentRoot "D:/IDEs/XAMPP/htdocs"
ServerName localhost
<Directory "D:/IDEs/XAMPP/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/IDEs/Websites/my.com"
ServerName my.com
<Directory "D:/IDEs/Websites/my.com">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Also, I remembered to add two lines in the hosts file, with is located in C:\Windows\System32\drivers\etc:
127.0.0.1 localhost
127.0.0.1 my.com
However, when I enter via link 'my.com' with enabled apache server, I've got only page with folder directories, which are in htdocs XAMPP's folder.
I have checked a lot of videos and stackoverflow questions, but nothing helped.
I would be pleased if you could help me with solving this issue.
first you should arrange directories on httpd.conf
example:
<Directory "C:/SERVER~1/web/site1">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "C:/SERVER~1/web/site2">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
and then arrange vhosts.conf like this
example :
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.site1.com
DocumentRoot "C:/SERVER~1/web/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName site1.com
DocumentRoot "C:/SERVER~1/web/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot "C:/SERVER~1/web/site2"
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
DocumentRoot "C:/SERVER~1/web/site2"
</VirtualHost>
After five days-night of reading, searching, trying, I finnally fould the solution and done it in some steps:
First of all, forget about every file, and concentrate on the httpd-ssl.conf which is located in D:\IDEs\XAMPP\apache\conf\extra.
Second:
find the line "SSL Virtual Host Context" which has been commented. And below, find the line <VirtualHost _default_:443>.or simmilar. Then change it to <VirtualHost *:443>.
Third, change document root to the path of xampp's htdocs, for example, "D:/XAMPP/htdocs". Also, change ServerName from www.example.com:443 or similar, to localhost.
Here we done.
And Finally, after line </VirtualHost>, insert code
<VirtualHost *:443>
DocumentRoot "D:/IDEs/Websites/my.com"
ServerName my.com
<Directory "D:/IDEs/Websites/my.com">
Require all granted
</Directory>
</VirtualHost>
Change Document Root and Directory paths to your website location. And Servername to your server name.
RESTART APACHE
it works for me.
I also understood than some website uses SSL on localhost while the others not.
SO if you have exception like Apache/2.4.37 (Win32) OpenSSL/1.0.2p PHP/7.0.33 Server at my.com Port 443 you shold add your virtualhost configs into httpd-ssl.conf file.
If you got like ... Port 80 add yourl virtualhost configs into httpd-vhosts.conf

apache virtualhost - localhost and development alias/servername

I'm trying to configure my apache vhosts file to have a localhost/something hostname and "alias" hostnames. I'm working with google api's currenctly and they are not accepting custom aliases as url's, so I can't make it work with my custom url's. Any thoughts of what to do? My current config that's not working:
<VirtualHost 127.0.0.1:80>
ServerName localhost/go
ServerAlias localhost/go
DocumentRoot "D:/username/Web/server.dev/go"
</VirtualHost>
<Directory "D:/username/Web/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
<VirtualHost *:80>
ServerName api.server.dev
ServerAlias api.server.dev
DocumentRoot "D:/username/Web/server.dev/api"
</VirtualHost>
##... more custom urls with subdomains cut out because it's unnecessary
<VirtualHost *:80>
ServerName adstrck.server.dev
DocumentRoot "D:/username/Web/server.dev/adstrck"
</VirtualHost>
### ALL OTHERS ###
<VirtualHost *:80>
ServerName www.server.dev
ServerAlias server.dev *.server.dev
DocumentRoot D:/username/Web/server.dev
</VirtualHost>
When I'm trying to access 127.0.0.1/go or localhost/go I get an internal server error.
Maybe what you want is something like this
<VirtualHost 127.0.0.1:80>
ServerName localhost
ServerAlias server.dev *.server.dev
DocumentRoot "D:/username/Web/server.dev"
</VirtualHost>
<Directory "D:/username/Web/server.dev">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
Then use a url like http://localhost/go to view the site.
Depending on your OS/browser, you may be able to add a development subdomain to localhost. E.g.
<VirtualHost *:80>
ServerName dev1.localhost
## rest of your config
## e.g. ServerAlias my.website.on.the.internet.com
DocumentRoot /var/www/dev1
</VirtualHost>
<VirtualHost *:80>
ServerName dev2.localhost
DocumentRoot /var/www/dev2
</VirtualHost>
# Default / catch-all
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
I then pointed my browser to dev1.localhost and that resolved to dev1 and likewise for dev2.localhost and localhost by itself resolved to the default apache page.
This resolved my similar problem. Tested on Apache in a Debian WSL. Worked on Windows Chrome, failed on Windows Firefox. Based on this SO: https://stackoverflow.com/a/35124491

Hosting multiple website on my EC2 instance is not working properly

I'm trying to host multiple website on Amazon Ec2 linux, but it is only show the home dashboard website. I try as following virtual host configuration in httpd.conf too.
DocumentRoot "/var/www/html/SITE-1"
ServerName www.myexample1.com
DocumentRoot "/var/www/html/SITE-2"
ServerName www.myexample2.com
If i put like http://10.123.12.12/SITE-1 and http://10.123.12.12/SITE-2 it's getting the right page
But if i given with the DNS name the default page is only loading up, the two different site is not loading up
It looks like your document root is still being set to /var/www/html and you may not have the proper VirtualHost containers. Try something like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/var/www/html/SITE-1"
ServerName www.myexample1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/SITE-2"
ServerName www.myexample2.com
</VirtualHost>
<Directory "/var/www/html/SITE-1">
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/html/SITE-2">
AllowOverride None
Order allow,deny
Allow from all
</Directory>
If you need to do SSL, you'll need to include port 443 and some other options.

Two apps on Apache server with URI

I need to install a server with Apache 2.2 on Linux and I need to do two VirtualHosts differentiated by URI.
But with only one domain name and one ip address. And I can't use Alias.
I tried something like that but that doesn't work :
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName localhost/app1
ServerAlias www.localhost/app1
<Directory /var/www/app1>
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/app2
ServerName localhost/app2
ServerAlias www.localhost/app2
<Directory /var/www/app2>
Allow from all
</Directory>
</VirtualHost>
Thank you to the first answer here, it's working : https://serverfault.com/questions/588841/two-apps-on-apache-server-with-uri
I put the answer here if one day the link doesn't work :
What you could do is set up a reverse proxy to different virtual hosts listening only on loopback.
You would get in your www.localhost virtualhost:
<VirtualHost *:80>
DocumentRoot /var/www/
ServerName localhost
ServerAlias www.localhost
ProxyPassReverse /app1/ http://webapp1.local/
ProxyPassReverse /app2/ http://webapp2.local/
</Virtualhost>
And create two virtualhosts for the apps:
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/app1
ServerName webapp1.local
<Directory /var/www/app1>
Allow from all
</Directory>
</Virtualhost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/app2
ServerName webapp2.local
<Directory /var/www/app2>
Allow from all
</Directory>
</Virtualhost>
Make sure to add webapp1.local and webapp2.local to your /etc/hosts file.
Since you have only a single domain name and only a single ip address available there is no means for the apache server to distinguish which host is meant. Therefore there is noo sense in defining VirtualHosts here.
However you certainly can place two apps in separate folders inside your DocumentRoot:
ServerName whatever-your-domain.is
DocumentRoot /var/www
<Directory /var/www/app1>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/app2>
Order allow,deny
Allow from all
</Directory>
Then you'd call those apps by their paths:
apps1: http://whatever-your-domain.is/app1
apps2: http://whatever-your-domain.is/app2
Don't forget to take care of requests to the "main folder" of that single host: /var/www which can be reached by http://whatever-your-domain.is/

Using a directory in VirtualHost ServerName

I'm currently using name-based virtual host configuration, to server about 5 different websites from the same IP address, just like in the apache documentation:
<VirtualHost *:80>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
Is it possbile to have something like:
<VirtualHost *:80>
ServerName www.domain.tld/folderpath
DocumentRoot /www/software
</VirtualHost>
The webpages in this folder are using a different software stack, and I'd like to keep it nicely separate. I tried the method above but it didn't work.
It's not possible the way you show - a VirtualHost is always just a host. But you could use an Alias.
<VirtualHost *:80>
ServerName www.domain.tld
DocumentRoot /www/domain
Alias /folderpath /www/software
</VirtualHost>
Is it possible to have a different vhost for each application like that:
<VirtualHost *:80>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.tld
Alias otherApp /www/otherApp
</VirtualHost>
I add to the alias.conf file (on a windows machine).
Remember that if it outside the 'document root' path, you'll need permissions
<IfModule alias_module>
#### FolderPath ####
Alias /folderpath "E:/any/path/you/like"
<Directory "E:/any/path/you/like">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#### Another ####
Alias /another "E:/another/different/path"
<Directory "E:/another/different/path">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</IfModule>