Apache virtual host set up for remote access - apache

I set up a virtual host on computer A by doing this:
Open apache config file (httpd.conf), insert lines as below:
<VirtualHost 127.0.0.1:80>
<Directory "{$path}/www/design-report/public/">
Options Indexes FollowSymLinks
AllowOverride FileInfo
Order deny,allow
Allow from all
</Directory>
ServerName weeklyreport.abc.com
ServerAlias www.weeklyreport.abc.com 127.0.0.1
DocumentRoot "{$path}/www/design-report/public/"
</VirtualHost>
Then I added a line as below to system32/drivers/etc/hosts:
127.0.0.1 weeklyreport.abc.com
By now on computer A I'm able to access weeklyreport.abc.com just into the right folder. I want to access this address from another computer named B.
So I changed the hosts file on B by adding A's IP like this:
192.xxx.5.xx weeklyreport.abc.com
Now I can access weeklyreport.abc.com from computer B.
But the problem is, it isn't the right folder. By this IP I can only access {$path}/www, not "{$path}/www/design-report/public/".
How can I fix this? Did I miss anything?

You missed the fact that VirtualHost definition tells apache to make virtual host only on given IP. It doesn't match for the external IP. You either need to defined second virtual host or use NameVirtualHost. For more details see Apache docu
HTH,Jan

Related

WAMP Server cannot be accessed through local network [duplicate]

So I set up a few virtual hosts with unique urls and they work just fine on the desktop. However, when I connect a mobile device on the network, it can't seem to access anything properly but the default localhost virtualhost and that's only when it's the only virtualhost I have up.
My setup and coding is pretty much this except with a different site title
wamp server 3.0 virtual host on another device
and while that solution redirects me to my unique url, it has a lack of images on a default wordpress website.
Has anyone managed to get mobile devices fully accessing links other than on localhost?
Since I posted the answer you referenced, I have decided upon a simpler solution.
What the actual problem is
Because we cannot fiddle with the configuration of a phone like we can with a PC, the phone can never find the domain name we create in our Virtual Host definition on the Server machine, because it does not exist in any DNS Server for it to locate the IP Address in, and a DNS Server is the only place a phone can look, unless it is jail broke.
If you wanted to access one of your Virtual Hosts domains from another PC you could just add a line like this into the HOSTS file on the other PC like this.
192.168.0.10 example.local
But you cannot do that on a phone/tablet.
What Apache expects to be able to asssociate a request to a Vhost
When we create an Apache Virtual Host, we are actually telling Apache to look at the domain name on the incoming connection and match that domain name to a ServerName that exists in one of our multiple Virtual Hosts definitions.
But if we use for example example.local as our virtually hosted domain when we attempt to connect to that from our phone, the phone does a DNS Lookup and does not find that domain and therefore cannot get its ip address.
The simplest way to get round this is:
Assuming we do not have access to adding record to a DNS Server we have to come up with a different solution.
The simplest of these is to use the IP Address of the PC running the WAMPServer(Apache) server and a specific port number. So thats a different port number for each of our sites we want to use from a phone.
So how do we do this
Add the new listening port to httpd.conf like so after the 2 existing Listen statements
WAMPServer 3: Do this using the menus, not by doing a manual edit on httpd.conf
right click wampmanager-> Tools -> Add listen port for Apache
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen [::0]:80
Listen 0.0.0.0:8000
Listen [::0]:8000
Suggested httpd-vhosts.conf file
#
# Virtual Hosts
#
# Always keep localhost, and always first in the list
# this way a ramdom look at your IP address from an external IP
# maybe a hack, will get told access denied
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp/www
<Directory "c:/wamp/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# The normal Vhost definition for one of our sites
<VirtualHost *:80>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# Access example.dev from phone for testing
<VirtualHost *:8000>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
# assuming yoursubnet is 192.168.0.?
# allow any ip on your WIFI access
Require ip 192.168.0
</Directory>
</VirtualHost>
Restart Apache from wampmanager after completing these edits.
Now you test this from the WAMPServer PC by using the ServerName i.e example.dev and from the phone using the ip of the PC running WAMPServer with the port number i.e. 192.168.0.10:8000
Apache will find the correct code to serve from both requests.
If you want more than one Virtual Host to be accessible from your phone you just duplicate this idea and change the port number for each new site, lets say you would use 8001,8002,8003 etc. For as many sites as you want to access.
You may also have to amend your firewall to allow access on http on port 8000, or whatever port you pick to use

virtual host subdomain not working properly, points me to main domain

<VirtualHost *:80>
ServerName db.mydomain.co
#ServerAlias db.mydomain.co
DocumentRoot /var/www/db/public
<Directory /var/www/db/public>
AllowOverride All
</Directory>
ErrorLog /var/www/db/error.log
CustomLog /var/www/db/requests.log combined
It's the config file I created in cong.d directory. I also changed the host file on my PC pointing mydomain.co to the server IP address. After I restarted my apache server, I go to db.mydomain.co which gives me db.mydomain.co’s server DNS address could not be found. But ironically, mydomain.co shows me the subdomain content and I won't be able to access the website hosted on my main domain. I'm using the distribution made by Amazon, and it's based on CentOS I think.
I really have no idea what's going on here. Any helps appreciated.

Prevent access to files through ip address - apache 2.4

I have asked a similar question before
Restrict access to directories through ip address
at that time the problem was solved for apache 2.2. Recently I re-installed the OS (to Debian 8) and it comes with apache 2.4.
I want to restrict access to files - when the request comes "by" IP. Mainly if in the browser I try to open http://192.168.252.178/test/image.jpg it should show error - 403 forbidden. Directory test is in www directory of apache. However I should be able to access that image if I type http://www.example.com/image.jpg - considering that example.com points to that test directory.
With apache version 2.2 I would simply put this lines in my default site config file - and the problem was solved
<Files ~ ".+">
Order allow,deny
Deny from all
</Files>
Now, trying the same thing does not work: I am getting 403 forbidden even if I try to open any site by the domain name.
Considering the changes in 2.4 I also tried this, but again getting the the same 403 forbidden when trying to open some site.
<Files ~ ".+">
Require all denied
</Files>
My goal is to prevent any kind of access to directories and files - if they are being accessed through ip address. I have also this lines in my default site's config to prevent the directory access and this works fine.
<Directory /home/username/www>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
So, the question is - how to prevent file access through IP address. Also I need to achieve this by apache config, by htaccess is not a solution for me. And I need to achieve this for all the directories/files inside www recursively, so specifying the exact file names and/or directories is not a solution either.
Thanks
When you use name based virtual hosts, the main server goes away. Apache will choose which virtual host to use according to IP address (you may have more than one) and port first, and only after this first selection it will search for a corresponding ServerName or ServerAlias in this subset of candidates, in the order in which the virtual hosts appear in the configuration.
If no virtual host is found, then the first VHost in this subset (also in order of configuration) will be choosen. More.
I mention this because it will be important you have only one type of VirtualHost directive:
<VirutalHost *:80>
or
<VirtualHost 123.45.67.89:80>
I'll use the wildcard in the example.
You need a directory like /var/www/catchall with a file index.html or similar, as you prefer.
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
# It will be used as the catchall.
ServerName 123.45.67.89
# Giving this DocRoot will avoid any request based on IP or any other
# wrong request to get to the other users directories.
DocumentRoot "/var/www/catchall"
<Directory /var/www/catchall>
...
</Directory>
</VirtualHost>
# Now you can add as usuall the configuration for any other VHost you need.
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site2.com
DocumentRoot "/home/username1/www"
<Directory /home/username1/www>
...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
DocumentRoot "/home/username2/www"
<Directory /home/username2/www>
...
</Directory>
</VirtualHost>
Debian specific :
For Debian, you ideally put one VHost configuration per file, and put the file in the /etc/apache2/sites-available directory.
Name the files as you like, only the file containing the catchall vhost should be named something like 000-catchall, because they will be read in alphabetic order from the /etc/apache2/sites-enabled directory.
Then you disable Debian's usual default site :
a2dissite 000-default
and you enable the new catchall site and the other VHosts if needed :
a2ensite 000-catchall
An ls /etc/apache2/sites-enabled command should show the catchall as the first of list, if not change its file name so that it will always be the first. Restart Apache: service apache2 restart
Of course you could do all this changes in the original default VHost config file, but I usually prefer keep an original model.

Accessing virtual host from computer on same local network

I am trying to make a setup so that I can access my website on a virtual host in computer A from computer B. Both A and B are on the same network.
I am using xampp on Win 7.
So here is as the problem goes computer A(server) has a virtual host configuration as follows in the httpd-vhosts.conf file.
NameVirtualHost project:81
<VirtualHost project:81>
DocumentRoot "D:/work/website"
ServerName project:81
<Directory "D:/work/website">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
(using port 81 as port 80 has IIS running, dont know much about these things however)
this configuration works fine on the local machine(server). i.e project:81 in the address bar of the browser opens up the website as it should.
Now on computer B(client) I changed the hosts file to contain the IP of the server along with the name of the virtual host like:-
192.168.1.7 project
now when I enter project:81 on the client browser .. it takes me to the server but its not taking me to the virtual host directory, instead it takes to the default directory .. i.e in my case is
C:\xampp\htdocs
Now I am stuck and unable to make the client to point to the current destination.
So can anybody suggest what I am doing wrong here or something else I need to do in order to have access to the correct virtual host site from the client machine.
Thanks in advance for any help
Ok So Seto El Kahfi's reply to my very old question led me to do some more research and reading on Apache's website.
So what I got is this, my NameVirtualHost directive was improper.
So Instead of this,
NameVirtualHost project:81
<VirtualHost project:81>
DocumentRoot "D:/work/website"
ServerName project:81
<Directory "D:/work/website">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
What I had to do was this.
NameVirtualHost *:81
<VirtualHost *:81>
DocumentRoot "D:/work/website"
ServerName project
<Directory "D:/work/website">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Notice the ' * ' , I could have used an IP Address there too.(In this case my server's(machine A) local IP) both work. Now all I had to do is enter "project:81" on the client machine, and I get what my eyes wished to see.. :)
Few things I got from this.
1) How to use NameVirtualHost(or what it's purpose basically is.). Read More here
http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost
This one is also good http://www.thegeekstuff.com/2011/07/apache-virtual-host/
2)You can use this via command line:
httpd -D DUMP_VHOSTS
to know how your virtual hosts are setup(will also give you some warnings regarding precedence if something's wrong with your setup)
3)Other's gesture to help you makes you help yourself.. :) So keeping helping and rocking.
Have you try to include the port at your client host's file?
192.168.1.7:81 project

Apache: see named virtual hosts from LAN

I use some Virtual Hosts on Apache to speed up development. My configurations look like this:
<VirtualHost 127.0.0.1>
ServerName my_server.dev
DocumentRoot "my_root"
<Directory "my_public_files">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName another_server.dev
DocumentRoot "another_root"
<Directory "other_public_files">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
and so on. I also add myserver.dev and another_server.dev to the hosts files, so putting one of those address into the browser takes me to my development environment.
For testing purposes. I would like to be able to access these Virtual Hosts from ohter machines on my LAN. I can access the main Host just by putting the server local IP, but I don't know how to access other Virtual Hosts.
I think I could do this by assigning a different port to each Host, but this becomes uncomfortable after a while. Any chance to access the Virtual Hosts by name on the LAN?
You have to modify the hosts file on all computers in your LAN, so that they know that another_server.dev should directed to your local server. Otherwise, dns lookup will be made and fail as the domain doesn't really exists.
You must access the server by name, not by IP.
so, machines on you LAN should to know, where is the "another_server.dev", therefore you have to add into hosts-file line like:
10.0.0.1 another_server.dev my_server.dev
(replace 10.0.0.1 with your machine IP)
after this the machines on LAN can access your server with http://my_server.dev