apache virtualhost - localhost and development alias/servername - apache

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

Related

Different urls point to different directories

If I go for http://example.com then its pointing to /var/www/html
Now I am in need that if I go for http://example.com/dashboard then it will point to /var/www/example/public. Or If this not possible then /var/www/html/example/public would also be okay.
And again I need that if I go for http://wildcardsubdomain.example.com/ then also it will point to /var/www/example/public. Or If this not possible then /var/www/html/example/public would also be okay.
How can I make so?
I have tried with this but /dashboard not working:
ServerName example.com
# Listen for virtual host requests on all IP addresses
UseCanonicalName Off
#dynamic subdomain provisioning
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
#WORKING
ServerName user.example.com
ServerAlias *.example.com
DocumentRoot /var/www/example/public
</VirtualHost>
<VirtualHost *:80>
#NOT WORKING
ServerName www.example.com/dashboard
ServerAlias *.example.com/dashboard
DocumentRoot /var/www/example/public
</VirtualHost>
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
For dashboard I could solve that using the Alias Directive
<VirtualHost *:80>
DocumentRoot /var/www/html
Alias /dashboard /var/www/example/public
<Directory /var/www/example/public>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

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/

vHosting on Apache2.2 won't work properly

So, I set up two virtual hosts on my apache and now, I can't access the server via localhost anymore.
Here's my vhosts.conf:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#serv.net.local
DocumentRoot "W:/www"
<Directory "W:/www/">
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
ServerName serv.net.local
ErrorLog "logs/serv.net.local-error.log"
CustomLog "logs/serv.net.local-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#symfony.net.local
DocumentRoot "W:/www/symfony/web"
<Directory "W:/www/symfony/web/">
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
ServerName symdev.net.local
ServerAlias symfony.net.local
ErrorLog "logs/symdev.net.local-error.log"
CustomLog "logs/symdev.net.local-access.log" common
</VirtualHost>
All domains are in my hosts-file and are properly resolved. But I can only access the second vHost and get a connection reset error when trying to access localhost or serv.net.local. Same for using 127.0.0.1...
Any ideas whats wrong with this config?
According to discussion in comments, VirtualHost DocumentRoot was not readable because it contained no index, and directory listing was disabled, so fixing it solved the issue.

XAMPP Windows Apache VirtualHost 403 Forbidden

I had XAMPP running fine with virtual hosts on my winodws 8 machine until I was forced to restart my computer (windows update). After restarting I've noticed my virtual hosts weren't working any more. Instead of trouble shooting too much I decided to reinstall XAMPP (currently have XAMPP 3.1.0 running. Which is runnign Apache v 2.4.3)
I have my windows host file edited to redirect sitename.localhost to 127.0.0.1 and this is what I have in my httpd-vhost.conf:
NameVirtualHost *
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\Users\USER\Documents\sitename"
ServerName sitename.localhost
<Directory "C:\Users\USER\Documents\sitename">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Every time I try to access http://sitename.localhost I get a 403 Access Forbidden error. Any idea what I'm doing wrong?
Try adding Require all granted to the Directory configuration:
NameVirtualHost *
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\Users\USER\Documents\sitename"
ServerName sitename.localhost
<Directory "C:\Users\USER\Documents\sitename">
Order deny,allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
Also, try to set reading permissions to the web site folder for Authenticated users.
This will list out as index.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Users\USER\Documents\sitename"
ServerName projectname.dev
ServerAlias projectname.dev
<Directory "C:\Users\USER\Documents\sitename">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
could you test this code pls instead of yours?
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Users\USER\Documents\sitename"
ServerName sitename.localhost
<Directory "C:\Users\USER\Documents\sitename">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
btw, do you have index.html/php file in folder "C:\Users\USER\Documents\sitename", don't you?

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>