วันเสาร์ที่ 4 ธันวาคม พ.ศ. 2553

Multiple virtual hosts in xampp/lampp in Ubuntu

Xampp ( for Windows) or Lampp (for UNIX based systems) is an Apache MySql PHP distribution which installs quickly and offers a good solution for testing websites on local machines. I myself use it and it has never disappointed me.

When working with multiple websites, there is no need to create sub-domains to the existing local domain name "localhost". Xampp/lampp enables you to work with a real domain name like "google.com", for example, but with the files hosted on your computer. The secret is the hosts files and the virtual hosts feature.

First of all you should find out where Xampp/lampp was installed on your computer. On my computer it is /opt/lampp/ .

Next, because you should get some admin privileges, open up a terminal and type gksudo nautilus . Nautilus will help you modify some files easily, directly from GEdit.

Now, with Nautilus, browse to /opt/lampp/etc/extra (change /opt/lampp to your own directory where xampp/lampp is installed, if different from mine). Now open this file: httpd-vhosts.conf

By default there are two VirtualHost sections, each contained between the tag. We will do some changes to these sections now. The first section we should change it to point to localhost . Should look like this:


ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /opt/lampp/htdocs
ServerName localhost
ServerAlias localhost
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common

In this line DocumentRoot /opt/lampp/htdocs you should once again change /opt/lampp to the directory where xampp/lampp is installed in your system.

Next, for every other additional virtual host you will append the same block of code to your httpd-vhosts.conf file, but making some slight changes. For example, if you want to add the virtual host myhost.com , whose files are located in /opt/lampp/htdocs/myhost , you will add this code:


ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /opt/lampp/htdocs/myhost
ServerName myhost.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common

Notice that DocumentRoot and ServerName has been changed. ServerAlias is not needed anymore. Save the file.

Next you should enable virtual hosts in the configuration file, located at /opt/lampp/etc . Open the file httpd.conf with nautilus, and find this line #Include etc/extra/httpd-vhosts.conf . Remove the # char from the beginning of the line, so it remains Include etc/extra/httpd-vhosts.conf only. Save the file.

Now we should add the domain name to the hosts file in order for the domain name to point to our ip address 127.0.0.1 , but not to look for it in the DNS database. So go to /etc/ with Nautilus, also, and open this file: hosts . Now at the end of the file append the following line : 127.0.0.1 myhost.com (or change myhost.com to the name of your own virtual host name). Do the same thing for all the other virtual hosts you have. Save the file.

Now we will need to restart Xampp/Lampp in order for the changes to take effect. Open a terminal window and run this command: gksudo /opt/lampp/lampp restart ( remember to change /opt/lampp if needed).

Now you should be able to work with your virtual hosts, by going with your browser to http://myhost.com or to http://the_name_of_your_own_virtual_host.your_own_tld . Good luck!


ที่มา http://www.blogger.com/post-create.g?blogID=6066341583614699750

ไม่มีความคิดเห็น: