Create a site with WordPress: Configuring Apache for Virtual Hosts


Antonio Lodesani
YNOT EUROPE – The previous article showed how to set up the Apache rewrite module so as to enable WordPress to change the URLs of the pages the CMS creates.

This article is dedicated to all those intending to implement several versions of the same website or more than one website. In this case, you will need to set up what are called “virtual hosts.” Virtual hosts run as if they are independent instances of the server, but all operate on a single Apache installation.

We can reduce the virtual host creation process to two simple steps to be repeated for the number of sites that we operate on the same PC.

Step 1: To allow Apache to distinguish between different versions of the same site, you must set a unique name for each version. Locally, we can choose whatever name we like even if that won’t be the name of the site when you finally make it live on the web. If we wanted to create a site called sitodiprova.com, we could choose a name like local.sitodiprova.com replacing, as you can see, the implied www prefix in the original with the “local” string in the trial site.

At this point you will need to communicate the name you have chosen to the operating system. When you type the address of a website in the navigation bar of a web browser, the browser queries the DNS (Dynamic Naming System) server network to identify the site. Before querying the DNS, a search is made on a file inside the operating system that is identified as the hosts file. Only if the name you seek matches an IP address in this file is the server queried. In our case, we will modify the hosts file to tell it that if we try to find local.sitodiprova.com, the browser should look in our local PC itself, and not on the web.

With Notepad, open the file hosts. You should find it in this path: C:\windows\system32\drivers\etc\. At the end of the file, add the line 127.0.0.1 local.sitodiprova.com. If you wish to add more than one local site, add one similar line for each site, changing name each time. For example, you could add lines for local.sitodiprova.com, local.domain2.com and local.myfave.com. Each one will be “mapped” to 127.0.0.1, which is your local machine’s IP address.

Step 2: Now you must set the Apache httpd.conf file to recognize the new site. Open the file (as described in the previous article) and paste the following lines at the bottom:
[FONT=Courier New][INDENT][php]NameVirtualHost 127.0.0.1


DocumentRoot “C:\wamp\www”
ServerName localhost


DocumentRoot “C:\wamp\www\sitodiprova.com”
ServerName local.sitodiprova.com
ErrorLog logs/sitodiprova_error.log
[/php][/INDENT][FONT=Tahoma]After editing, save the whole. You will notice that beyond the basic localhost Apache we created a folder inside the www the location where Apache will look for the file when you type local.sitodoprova.com into your browser. You also have created a folder where error logs for the webserver will be kept.

Important: For proper operation, you must create the folder sitodiprova.com in the path C:\wamp\www\.

If you followed all the steps correctly, you have successfully created your first virtual host. To test its operation, create a file called phpinfo.php in the folder C:\wamp\www\sitodiprova.com. The file should contain only the following string:[/FONT][INDENT][php][/php][/INDENT][/FONT]Now type local.sitodiprovacom/phpinfo.php in your browser. If you get a PHP information page, you have completed all the steps in the right way.

In future articles, we will see how to configure MySQL and phpMyAdmin to set up the database installation for WordPress.

This article was written for YNOT Europe by Eng. Antonio Lodesani. For more information, visit ingoccupati.blogspot.com.

About the Author

admin

YNOT Admin wields his absolute power without mercy. When he's not busy banning spam comments to hell he enjoys petting bunnies and eating peanut butter. He recommends everyone try the YNOT Mail (ynotmail.com) email marketing platform and avoid giving their money to mainstream services that hate adult companies.

Visit Website

Comments are closed.