I'm using Apache, PHP (MAMP) in OSX environment, and Symfony framework for develpment.
Symfony requires the use of Virtual Host for development locally, so I've added these lines on my /etc/apache2/hosts file:
127.0.0.1 frontenda.dev
127.0.0.1 frontendb.dev
Then at my /Applications/MAMP/conf/apache/extra/httpd.vhosts.conf, I've added these lines:
<VirtualHost *:80>
ServerName frontenda.dev
DocumentRoot /Users/geo/Sites/frontenda/www/
</VirtualHost>
<VirtualHost *:80>
ServerName frontendb.dev
DocumentRoot /Users/geo/Sites/frontendb/www/
</VirtualHost>
I can open those folders from my browser by these urls http://frontenda.dev or http://frontendb.dev. My question is what should I do (if this is even possible) to use IP address instead of frontenda.dev or frontendb.dev? What I mean with that is, let say my machine ip 192.168.1.144, can I do 192.168.1.144/frontenda or even assigning different port number for different folder?
You can use Alias to map different paths to different directories
Alias /frontenda /Users/geo/Sites/frontenda/www
Alias /frontendb /Users/geo/Sites/frontendb/www
I've had a better answer. I get it working using xip.io from 37signals http://xip.io/. You can do like this:
10.0.0.1.xip.io resolves to 10.0.0.1
www.10.0.0.1.xip.io resolves to 10.0.0.1
mysite.10.0.0.1.xip.io resolves to 10.0.0.1
foo.bar.10.0.0.1.xip.io resolves to 10.0.0.1
Related
I have created virtual hosts by going to C:\xampp\apache\conf\extra\httpd-vhosts.conf
then i wrote this:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/basicwebsite/public"
ServerName basicwebsite.dev
</VirtualHost>
and then i went to hosts file in C:\Windows\System32\drivers\etc and wrote this:
127.0.0.1 localhost
127.0.0.1 basicwebsite.dev
then I stopped the apache server and again start it, normally it should have open the laravel application that we are making but when i open browser and typed basicwebsite.dev it showed an error in my browser it shows this
Screenshot of my page when i type the link
it says this site cant provide secure connection.
but it shouldn't have shown this , it should have shown the page.
What to do here?
Laragon provides this feature very well. It is easy to setup virtual host with it beacuse it provides Pretty URLs using
Auto Virutal Hosts
Learn more about laragon here
.dev is blocking by browser. Instead of that use .loc or something else
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
Yesterday i have created a new site on my Codero Dedicated Server. and i have not assigned any domain name yet to the ip address.
Currently i am able to access my site with http://MySiteIpAddress/~username but i want to access it with only http://MySiteIpAddress/. Is there any way to do it. I don't know if that's possible by htaccess rules, so any hints are appreciated.
You want to setup a virtual host and point it to the folder you want.
See this other post for more details.
You need to do several steps in order to make this work.
1.) Update the hosts file. On Windows XP, you can find it under c:\WINDOWS\system32\drivers\etc\. You should already see the first
line from below, it takes care of your mentioned other project. - add
the additional ones to make any requests to the mentioned virtual
hosts routed back to your own machine.
127.0.0.1 localhost
127.0.0.1 foo-bar.com
127.0.0.1 abcdef.com
127.0.0.1 qwerty.com
2.) Update the vhosts file in Apache configuration. Under your XAMPP folder, add the following to
apache\conf\extra\httpd-vhosts.conf and if needed change the ports
(i.e. if you use 8080 instead of port 80).
<VirtualHost *:80>
DocumentRoot C:/xampplite/htdocs/foo-bar/
ServerName www.foo-bar.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot C:/xampplite/htdocs/abcdef/
ServerName www.abcdef.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot C:/xampplite/htdocs/qwerty/web/
ServerName www.qwerty.com
</VirtualHost>
3.) Do a quick configuration check. Open {XAMPP-folder}\apache\conf\httpd.conf your file and make sure that
the following part is not commented out by a preceeding # character:
Include conf/extra/httpd-vhosts.conf
4.) Restart XAMPP.
... and you should be all setup now. Your other project should
be accessible at the URI you mentioned if you just put it under
C:/xampplite/htdocs/my-project/.
I am running WAMP and using CodeIgniter for my project and have this on my vhost:
<VirtualHost *:80>
ServerAdmin admin#yahoo.com
DocumentRoot "C:/wamp/www/myproject/assets"
ServerName myproject.dev
ErrorLog "logs/myproject.dev-error.log"
CustomLog "logs/myproject.dev-access.log" common
</VirtualHost>
Now to access this, I added this line on windows/system32/drivers/etc/hosts:
127.0.0.1 myproject.dev
Now for the other computers on the network, I have to edit the hosts file of EACH computer so they can access my virtual host. (yes of course I have to use my ip address instead 127.0.0.1 for other computers)
Now my question is, is there a way that they can access my project by only using my ip address on the browser's address bar like this?
http://192.168.1.112/myproject
I mean there are 100 users that will access that project and it's a big hassle if I edit each one's hosts file. Like adding something to .htaccess, or to the routes of CodeIgniter, or to the <virtualHost>
Note:
By the way, when we are still NOT using Codeigniter (plain PHP codes), this is not a problem. But because of Codeigniter's structure, we can't do it anymore.
Can you just add a DNS entry that points to your IP address and set that as the ServerName that apache responds to?
Alternatively you can do virtual hosting based on IP address and port as described here:
http://httpd.apache.org/docs/2.2/vhosts/ip-based.html
In summary you should be able to do:
<VirtualHost 192.168.1.112:8000>
ServerAdmin admin#yahoo.com
DocumentRoot "C:/wamp/www/myproject/assets"
ServerName myproject.dev
ErrorLog "logs/myproject.dev-error.log"
CustomLog "logs/myproject.dev-access.log" common
</VirtualHost>
And have people access it via
http://192.168.1.112:8000/myproject
But, don't forget to add a Listen directive for port 8000 (or whatever you choose) if you use IP-based Virtual hosts
It might work of you create an alias called /myproject in wamp server and point the document root to 'C:/wamp/www/myproject/assets'
Make sure you have set your wamp server status to online by selecting 'Put Online' in wamp server system tray icon.
My Apache "httpd-vhosts.conf" looks like this::
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost laravel.dev:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost learninglaravel.dev:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
and my "...system32/drivers/etc/hosts" also looks like this::
127.0.0.1 localhost
127.0.0.1 localhost
// I added the following entries. The first two entries above was already there
127.0.0.1 laravel.dev
127.0.0.1 learninglaravel.dev
When i enter "learningLaravel.dev" and "laravel.dev" into the browser, they work fine as expected. But i have other folders in my "www" folder that i use them to learn PHP and i want to be able to access the files in those folders directly from the browser like say "localhost/test/me.php". But anytime i enter such address the browser goes to the second entry in the vhosts-conf file [which prints a laravel error meaning that it can't find the file]. It seems that the first entry in the vhosts-conf file is not working and Apache bypasses it to the second entry. The first entry is suppose to be the catch all entry. I tried to swap the second and third entries to see how it will behave but it always direct the browser to the second entry instead of the catch all (first entry) that is suppose to handle addresses likes "localhost/test/me.php"
Anytime i enter only "localhost" into the browser, it goes straight to the second entry instead of say printing the contents of the "www" folder.
How do i solve this problem? thanks.
It seems the problem comes from the way you use the VirtualHost directive.
Using a fully qualified domain name for the IP address of the virtual host is not recommended. It is misleading how it works. Name based virtual hosts determine the host through the ServerName directive, and not through the FQDN in the VirtualHost directive (<VirtualHost FQDN:80>). In fact this is seen as <VirtualHost 127.0.0.1:80>
What happens is your case is documented in the VirtualHost doc, last 2 paragraphs (just before "Security"), quoted:
When a request is received, the server first maps it to the best
matching based on the local IP address and port
combination only. Non-wildcards have a higher precedence. If no match
based on IP and port occurs at all, the "main" server configuration is
used.
If multiple virtual hosts contain the best matching IP address and
port, the server selects from these virtual hosts the best match based
on the requested hostname. If no matching name-based virtual host is
found, then the first listed virtual host that matched the IP address
will be used. As a consequence, the first listed virtual host for a
given IP address and port combination is the default virtual host for
that IP and port combination.
So when you ask for localhost/somedir, the server will try to find from the non-wildcards VHosts declarations, but do not find any with corresponding host name (ServerName), and so it chooses as "default" the first VHost with IP:Port, and not the first with *:Port.
To solve your problem, try to use <VirtualHost *:80> in all three vhost declarations :
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
And reload / restart Apache.
(My only doubt about this is why Nasreddine could make a working test case with your setup.)