Hosting multiple local sites with XAMPP - php

I'm new to using XAMPP so this may be simple to some people.
I have a few php projects that I would like to be able to debug locally and view in the browser (not concurrently, but without having to change config files/copy project folders each time I want to work on a different project).
On IIS, you could set up multiple sites to serve from your machine, and I'm looking for something similar in XAMPP. When using IIS, I added multiple records to the Windows hosts file so I could access the locally hosted sites by typing friendly web-style addresses (like http://myproject1.dev)
Thanks.

Greg, you're almost there--you need (like Moses said) to setup virtual hosts.
So if your Windows hosts file has
127.0.0.1 localhost
127.0.0.1 mysite-dev.com
127.0.0.1 anothersite-dev.com
Your virtual hosts file (httpd-vhosts.conf) might look like:
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName mysite-dev.com
DocumentRoot "C:/sites/mysite-dev"
<Directory "C:/sites/mysite-dev">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName anothersite-dev.com
DocumentRoot "C:/sites/anothersite-dev"
<Directory "C:/sites/anothersite-dev">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Don't forget to restart the web server after you make any changes.

I would like to make an additional in terms of up to date information.
XAMMP uses port 80 by default and we are able to publish 1 website. I also use IIS for .Net projects. In this respect, I set the port to XAMMP except the 80 port. So I avoid a conflict.
When we want to publish more than one website, we should do the following operations to httpd.conf (this is the current name).
1. Setting the ports
Find the #Listen expression in the httpd.conf file.
Change Listen 80 to Listen 8000 (or whatever else you want)
Listen 8000
If you need 3 different websites, type the others, including 1 definition on each line, as follows.
Listen 8001
Listen 8002
Listen 8003
2. Define the file paths of sites accessed through ports
Again, find in the httpd.conf file.
Identify the folders of each website as follows.
As you would see, I've created 3 directories called 8000, 8001, 8002 and 8003 under the htdocs directory within the XAMMP directory.
<VirtualHost *:8000>
 DocumentRoot "C:\XAMPP\htdocs\8000"
 ServerName localhost:8000
<\ VirtualHost>
<VirtualHost *:8001>
 DocumentRoot "C:\XAMPP\htdocs\8001"
 ServerName localhost:8001
<\ VirtualHost>
<VirtualHost *:8002>
 DocumentRoot "C:\XAMPP\htdocs\8002"
 ServerName localhost:8002
<\ VirtualHost>
<VirtualHost *:8003>
 DocumentRoot "C:\XAMPP\htdocs\8003"
 ServerName localhost:8003
<\ VirtualHost>
Restart your Apahche server on XAMMP.
You can now view your 3rd site, such as http://localhost:8003 or http://192.168.1.1:8003/.
Hope to be useful.

This question was asked almost ten years ago, and the answers above are a bit dated. Note that XAMPP has a "How-To" for virtual hosts avilable off the dashboard, when you install it.
From the "Welcome to XAMPP for Windows" page (localhost/dashboard, the default when you first load localhost) click on the "HOW-TO" Guides in the top menu bar. From there, look for the link "Configure Virtual Hosts" which will lead you to the localhost page "http://localhost/dashboard/docs/configure-vhosts.html"
In a nutshell, the process involves editing the "httpd-vhosts.conf" file (typically in C:\XAMPP\apache\conf\extra) and replacing the contents of that file with something like this:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/"
ServerName localhost
</VirtualHost>
# vhosts - note sample entry from XAMPP how-to throws an error, so try this:
<VirtualHost *:80>
DocumentRoot "C:/Users/jdoe/Documents/dev.mysite.com/htdocs"
ServerName mysite.local
<Directory "C:/Users/jdoe/Documents/dev.mysite.com/htdocs">
Require all granted
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Additional vhosts (including SSL hosts) can be had by cloning the entry, and modifying DocumentRoot and ServerName directives and port numbers (e.g. 443 for TLS (SSL)). You can find tutorials on the web for creating and signing your own certificate, if you want to go that route.
The final step is to get your Windows machine to point your browser to the Apache host for your virtual domain (e.g. above, http://mysite.local). Using a text editor (Notebook will do) as administrator append the following entry onto your hosts file, which lives here:
C:\Windows\System32\drivers\etc\hosts
Append this entry to the hosts file:
127.0.0.1 mysite.local
IMPORTANT - you must restart your Windows machine or the new host will not respond. Some documentations will tell you just to restart the browser and Apache server, but I've found that's not sufficient.
IME, the hosts system and Apache directives can be particular, so be patient. You may need to rebuild configs, restart Apache, and restart your machine more than once.

Related

How do you create virtual hosts (that also use SSL) on localhost with MAMP and Apache?

Suppose you have a local project at /Users/yourname/Sites/example and want to be able to use both http://example.local and https://example.local to reach it on your Mac, using MAMP (I'm using MAMP version 6.6).
Read the answer for the steps to take.
1) Add your custom domain to the hosts file
Open the Finder and choose the Go to folder command from the Go menu. Enter /private/etc/hosts as the path; this will open a Finder window where you will find the hosts file.
Open the hosts file and map your custom domain to 127.0.0.1 (localhost):
127.0.0.1 localhost
127.0.0.1 example.local
Now, when you visit http://example.local, the browser will redirect you to localhost and show your list of local sites (which is not what we want yet, but hey, this is just the first step).
2) Configure MAMP to use Apache's and MySql's default ports
Open MAMP, click on Preferences, then go the Ports tab. By default, MAMP uses port 8888 for Apache and 8889 for MySQL, but it gives you the option to use Apache's default port (80) as well as MySQL's default port (3306): click the 80 & 3306 button to use these ports rather than 8888 and 8889.
3) Enable virtual hosts in Apache
Go to and open the /Applications/MAMP/conf/apache/httpd.conf file. Look for this line:
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Uncomment the line by removing the asterisk at the beginning: uncommenting the line enables virtual hosts. (If the line is already uncommented, you're good to go for this step).
4) Configure Apache to listen to port 80
While you have /Applications/MAMP/conf/apache/httpd.conf open, also look for this line:
Listen 8888
and change it to
Listen 80
Listen 443
In the previous step, you told MAMP to use port 80 for Apache, so the first line tells Apache to listen to port 80 rather than port 8888; the second line tells Apache to also listen to requests incoming on port 443: this is the port typically used by SSL connections, so it is an essential step to be able to reach your local site at https:// besides http://.
5) Create a SSL certificate for your local site
You need a SSL certificate to be able to reach your site at https://example.local as well. This can be easily done using the mkcert tool. You can read the instructions on Github, but basically it's about installing the tool, then running:
$ mkcert example.local
This generates two files: a certificate (example.local.pem) and a certificate key (example.local-key.pem).
Move these two files somewhere within your project. For example, you could create a hidden folder at the root of your project called .crt and put them in there.
6) Create a virtual host for your local site
Finally, it's time to actually create a virtual host for your local site. Go to and open the /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file and include the following:
// These first two lines tell Apache to check for virtual hosts
// whenever a request comes in on port 80 (http://) or 443 (https://)
NameVirtualHost *:80
NameVirtualHost *:443
// This first directive tells Apache to serve the files at "/Users/yourname/Sites/example"
// whenever "http://example.local" is visited
<VirtualHost *:80>
ServerName example.local
DocumentRoot "/Users/yourname/Sites/example"
</VirtualHost>
// This second directive tells Apache to serve the files at "/Users/yourname/Sites/example"
// whenever "https://example.local" is visited, and provides paths to the certificates
// we previously created
<VirtualHost *:443>
ServerName example.local
DocumentRoot "/Users/yourname/Sites/example"
SSLEngine On
SSLCertificateFile "/Users/yourname/Sites/example/.crt/example.local.pem"
SSLCertificateKeyFile "/Users/yourname/Sites/example/.crt/example.local-key.pem"
</VirtualHost>
That's it! Now you can view your site at both http://example.local and https://example.local
(Remember to adapt the paths to wherever your project and certificates are located though).
7) Need virtual hosts for multiple local sites?
Want to add virtual hosts for more local sites you have, eg. for a site you want to reach at http://test.local?
Go to /private/etc/hosts and add 127.0.0.1 test.local
127.0.0.1 localhost
127.0.0.1 example.local
127.0.0.1 test.local
Generate SSL certificates with mkcert test.local (like we did earlier on step 4).
Finally, update the virtual hosts file as follows:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerName example.local
DocumentRoot "/Users/yourname/Sites/example"
</VirtualHost>
<VirtualHost *:443>
ServerName example.local
DocumentRoot "/Users/yourname/Sites/example"
SSLEngine On
SSLCertificateFile "/Users/yourname/Sites/example/.crt/example.local.pem"
SSLCertificateKeyFile "/Users/yourname/Sites/example/.crt/example.local-key.pem"
</VirtualHost>
// Basically, copy and paste the virtual hosts for the example project
// and update the domain and paths accordingly
<VirtualHost *:80>
ServerName test.local
DocumentRoot "/Users/yourname/Sites/test"
</VirtualHost>
<VirtualHost *:443>
ServerName test.local
DocumentRoot "/Users/yourname/Sites/test"
SSLEngine On
SSLCertificateFile "/Users/yourname/Sites/test/.crt/test.local.pem"
SSLCertificateKeyFile "/Users/yourname/Sites/test/.crt/test.local-key.pem"
</VirtualHost>
8) Bonus: add extra configuration to your virtual hosts
The VirtualHost directive can accept a number of extra instructions that tell Apache what to do when a certain request is received.
For instance, suppose our example project is a Node application that can be reached at http://localhost:3000, but we want to use http://example.local to access it. We would have to tell Apache to forward any request made to http://example.local to http://localhost:3000.
NameVirtualHost *:80
NameVirtualHost *:443
// This line loads a module that enables proxying (it's turned off by default)
LoadModule proxy_http_module modules/mod_proxy_http.so
// The ProxyPass and ProxyPassReverse lines tell Apache
// to forward the request to http:// and https://localhost:3000
<VirtualHost *:80>
ServerName example.local
DocumentRoot "/Users/yourname/Sites/example"
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
<VirtualHost *:443>
ServerName example.local
DocumentRoot "/Users/yourname/Sites/example"
SSLEngine On
SSLCertificateFile "/Users/yourname/Sites/example/.crt/example.local.pem"
SSLCertificateKeyFile "/Users/yourname/Sites/example/.crt/example.local-key.pem"
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>

What is wrong in the deploy of this Laravel application? Need I an effective domain instead the vhost used on my local environment?

I am pretty new in PHP and moreover in Laravel and I am pretty desperate trying to deploy a Laravel 5.4 application that works fine on my local environment on my Linux remote server.
So I think that it is something related to virtual host configuration or something like this (maybe also something related to the .htaccess file)
In my local environment (I am using XAMPP on Windows) I have setted this virtual host into the C:\xampp\apache\conf\extra\httpd-vhosts.conf file:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/HotelRegistration/public"
ServerName laravel.dev
</VirtualHost>
So opening the laravel.dev URL I obtain the standard Laravel homepage (I have yet not replaced it with a landing page).
Then if I open this URL: http://laravel.dev/registration
I obtain the user registration page developed by me, this because I have this route into my web.php file into my project:
Route::resource('/registration', 'RegistrationController');
Then into my RegistrationController.php there is this method showing the resources/views/registration/index.blade.php view
public function index(){
return view('/registration/index');
}
All works fine.
Now I have uploaded this Laravel website into my remote Linux server, into this folder: /var/www/html/HotelRegistration
But now my problem is that in this remote environment I have not virtual host (correct me if I am doing wrong assertion: from what I have understand the virtual host is used on the local environment to simulate a domain that Laravel need to point to the public folder, is it this reasoning correct?)
Anyway, this is the URL of the public folder of my deployed web site on my remote server:
http://89.36.211.48/HotelRegistration/public/
As you can see opening it the Laravel landing page is correctly shown, the problem is that I can access to the previous registration page, the only way that I have found is to open this URL:
http://89.36.211.48/HotelRegistration/public/index.php/registration
but it is pretty horrible and above all when the registration form is submitted it is generated a POST request toward this URL http://89.36.211.48/registration that end into a 404 Not Found error.
So I think that it depend by the fact that in this remote server I can't use a virtual host that simulate a domain (as I have on my local environment), but I am not sure about it.
What can I do to solve the situation? Do you think that using a effective domain (something like: www.myregistration.com) that points to this directory of my remote server http://89.36.211.48/HotelRegistration/public/ I can solve this problem?
You need to configure your domain in your server and need to reconfigure the apache. I'm considering you are having apache2 server so here you can do:
Step 1 Go to the apache2 folder cd /etc/apache2
Step 2 You can see sites-available folder go inside it cd sites-available
Step 3 Make a new file name it laravel.dev.conf
Step 4 Write down the following sudo nano laravel.dev.conf
Step 5 Write down the following option:
<VirtualHost *:80>
ServerAdmin webmaster#laravel.dev
ServerName laravel.dev
ServerAlias www.laravel.dev
DocumentRoot /var/www/html/laravel.dev/public/
ErrorLog /var/www/html/laravel.dev/logs/error.log
CustomLog /var/www/html/laravel.dev/logs/access.log combined
<Directory /var/www/html/laravel.dev/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
Step 6 Now go to this folder/create a new one cd /var/www/html/laravel.dev
Step 7 Copy/Install your laravel application here.
Step 8 Now you can enable your site by typing sudo a2ensite laravel.dev.conf
Step 9 Now restart the apache2 sudo service apache2 restart
Now you can have proper access to your domain. Hope this helps.
Since you are using XAMPP
Add the following into your VirtualHost Directive:
<Directory "LINUX PATH TO /HotelRegistration/public">
AllowOverride All
</Directory>
Your final VirtualHost Directive should look like:
<VirtualHost *:80>
DocumentRoot "LINUX PATH TO /HotelRegistration/public"
ServerName 89.36.211.48
<Directory "LINUX PATH TO /HotelRegistration/public">
AllowOverride All
</Directory>
</VirtualHost>
After the configuration changes, restart Apache then you are good to go.

Virtual Host Not Behaving Properly

This problem has been bugging me for days and I really need an answer.
I have a previous WAMP server in which I have configured properly so I can set up virtual hosts for applications that I build. It served me well for months.
Recently, there's this software, in which I think is a Malware, that run in my pc. The software was removed and I scanned the pc and there were no viruses.
I re-installed WAMP server and configured it again. It went well but the virtual hosts are not behaving properly.
These are what I have declared:
In httpd-vhost:
<VirtualHost *:80>
ServerAdmin #removed
DocumentRoot "c:/wamp/www/Thesis"
ServerName pixtcha.dev
<Directory "c:/wamp/www/Thesis">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin #removed
DocumentRoot "c:/wamp/www/mvc-pe-system/web"
ServerName copers.com
<Directory "c:/wamp/www/mvc-pe-system/web">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
</Directory>
</VirtualHost>
In my hosts file:
127.0.0.1 pixtcha.dev
127.0.0.1 copers.com
The thing is, I am able to access the pixtcha.dev properly and there were no problems, while the copers.com is inaccessible, when I type it, it redirects me to some website from the web. I just don't understand why this happens. I even tried flushing the dns if there were corrupted local files but I just can't figure it out.
Also, I have tried using the server names from my previous virtual host and they are all inaccessible too. They are giving me:
http://copers.edu.ph is not available
So I resorted to using new server names, but I don't understand why can't I use the previous server names from my previous WAMP configuration. :(
What do you think are the problems? And how to solve them?
Please help. Thanks in advance.
Ok a couple of things that might help here.
As Apache 2.4 is IPV4 and IPV6 aware you need to change your HOSTS file like this so if the browser decides to use IPV6 it knows where to find your domains. I wish I knew what controls the broswers decision, but I dont.
127.0.0.1 localhost
127.0.0.1 pixtcha.dev
127.0.0.1 copers.com
::1 localhost
::1 pixtcha.dev
::1 copers.com
Dont forget to restart the dnscache or reboot after changing this file.
From a command windows, started using the 'Run as Administrator' option, do these 2 commands to restart the dnscache.
net stop dnscache
net start dnscache
Also when you create Virtual Hosts the default host, as defined in httpd.conf, is ignored, so you need to add a VHOST definition for localhost.
Also you are using old Apache 2.2 syntax in your VHOST definitions, incorrectly as well as it happens, and this should be changed to Apache 2.4 syntax
Also it is better to stick to lower case for directory names, Windows does not care but if you ever move code to a Unix live server it may cause confusion.
# Should be the first VHOST definition so that it is the default virtual host
# Also access rights should remain restricted to the local PC and the local network
# So that any random ip address attack will recieve an error code and not gain access
<VirtualHost *:80>
DocumentRoot "c:/wamp/www"
ServerName localhost
ServerAlias localhost
<Directory "c:/wamp/www">
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin #removed
DocumentRoot "c:/wamp/www/thesis"
ServerName pixtcha.dev
<Directory "c:/wamp/www/thesis">
Options Indexes FollowSymLinks
AllowOverride all
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin #removed
DocumentRoot "c:/wamp/www/mvc-pe-system/web"
ServerName copers.com
<Directory "c:/wamp/www/mvc-pe-system/web">
Options Indexes FollowSymLinks
AllowOverride all
Require local
</Directory>
</VirtualHost>
Check and make sure that your scripts (PHP?) in your copers.com root (index.php?) are not redirecting to these outside URLs, or to URLs further inside your local site that would redirect outside as well.
Also in Chrome, you can open the Network tab of Developer Tools to see what actual web requests are being made, and in what order.

Set static Laravel URL to domain name I purchased

I need to set my url for my site to appear as mydomain.com/ and have apache recognize that as the webroot.
Currently my url looks like:
###.###.##.##/laravel/public
and I want it to become:
mydomain.com/
This is the first site I've worked on from scratch, so I'm not completely sure what information I should include here.
Current apache httpd.conf:
NameVirtualHost *:80
NOTE: NameVirtualHost cannot be used without a port specifier
(e.g. :80) if mod_ssl is being used, due to the nature of the
SSL protocol.
VirtualHost example:
Almost any Apache directive may go into a VirtualHost container.
The first VirtualHost section is used for requests without a known
server name.
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot /var/www/html/foo_bar/public
ServerName foo.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
<Directory /var/www/html/foo/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
</VirtualHost>
First you'll need to edit your httpd.conf (usually located somewhere like /etc/apache2/conf).
Look for a line like <Directory "/var/www">. This is telling apache the root directory from which you want to work. /var/www in your case will be whatever directory laravel/public is living in on your websever currently.
If you change that line to <Directory "/var/www/laravel/public"> (assuming /var/www is where your laravel app is), save the file and restart apache, then you should be serving mydomain.com from inside laravel/public.

Creating a Virtual Host In PHP

I am trying to set up symfony on my Ubuntu system. Now as going through the installation tutorial of symfony I found to Create a virtual host for my (to be created) application. I did the same steps as bellow.
httpd.conf
NameVirtualHost 127.0.0.1:9090
Listen 127.0.0.1:9090
<VirtualHost 127.0.0.1:9090>
ServerName www.symfony.jobeet.lcl
DocumentRoot "/home/sfprojects/jobeet/web"
DirectoryIndex index.php
<Directory "/home/sfprojects/jobeet/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf /home/sfprojects/jobeet/lib/vendor/symfony/data/web/sf
<Directory "/home/sfprojects/jobeet/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
And Here is My /etc/hosts file
127.0.0.1 www.symfony.jobeet.lcl
Now I restarts my lampp server and entered www.symfony.jobeet.lcl in my browser it takes me to http://www.symfony.jobeet.lcl/xampp/ this Url. If I try without using virtual host, It works fine. also when I try www.symfony.jobeet.lcl:9090 It works. I expect it should work without giving the port number. Am I doing something wrong ?
I have created the simple bash script to simplify the process of new hosts creation. So that you don't have to do anything with config files, etc. Check if you are interested here.
Creating new host is as simple as running a command from terminal $ sudo ./newhost.sh. Then you will be asked to enter desired host name.
Change this lines
NameVirtualHost 127.0.0.1:9090
Listen 127.0.0.1:9090
<VirtualHost 127.0.0.1:9090>
to
NameVirtualHost 127.0.0.1:80
Listen 127.0.0.1:80
<VirtualHost 127.0.0.1:80>

Categories