InternotesSharing Web Development Techniques

Blossoms

Setting up local Web Server

Posted on Friday, 28th June 2013 by admin

In Principal, you can set up a Web Server by individually finding and installing the appropriate packages. This is usually a combination of:

  • Apache Web Server
  • PHP
  • MySQL Database, or PostGreSQL, or, more recently, MariaDB

In practice, you might prefer to install one of the pre-packaged versions.

Packaged Web Servers

Several packaged servers are available. They include:

Macintosh

  • AMPSS
  • XAMPP
  • MAMP

Windows

  • AMPSS
  • XAMPP
  • WAMP

You will just need to install the package, and that should be enough. However, you will probably want to make some changes to your packages to make it work in a more practical manner.

There are two main variations you might consider:

  • Where you will store the data files
  • How you will address your site.

Storing your files is always an important matter: the usual default is is to store the data somewhere in your package folder. However, this may be an unsound practice, as you should never store important data on your system drive.

The issue becomes more crucial on the Macintosh, as storing data involves working with users and permissions. Packages are often installed inside the Applications directory, which is necessarily a protected location.

The next section describes relocating the site folder for XAMPP on the Macintosh. However, you might want to do something similar for other servers, both Windows and Macintosh. Setting Up XAMPP on the Macintosh

XAMPP on the Mac is not at all like XAMPP on Windows. For a start, it needs to be installed in the Applications folder, which is heavily protected. As such, you also need to give Apache permissions to the appropriate folder.

The following will hopefully fix the problem.

  • Create a folder in the Documents folder called htdocs
  • Edit /Applications/XAMPP/xamppfiles/etc/httpd.conf
  • Find the DocumentRoot Directive and change the following:
#DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
#<Directory "/Applications/XAMPP/xamppfiles/htdocs">
DocumentRoot "/Users/[User]/Documents/htdocs"
<Directory "/Users/[User]/Documents/htdocs">

The [User] above should be replaced with the logged in user name.

  • Find the User & Group directories and change as follows:
#User nobody
User [User]
Group nogroup

Again, the user above should be changed to the logged in user.

Resolving a Virtual Domain: hosts file

The following will resolve example sites to localhost. The benefits are

  • Gives the developer a better experience of a real web site
  • Links can use / for the site root.
  1. Edit the following file (you may need admin permissions):

    • Mac OSX /etc/hosts
    • Windows C:\windows\system32\drivers\hosts
  2. Add the following:
127.0.0.1            example.com
127.0.0.1            www.example.com
127.0.0.1            example.net
127.0.0.1            www.example.net

This will handle the name resolution. Later, we will tell Apache how to respond to the name with a virtual host.

Setting up the Virtual Host

Apache is capable of running a number of virtual servers on the same IP address. This is done by mapping a domain name to a site folder.

Although the virtual domain will resolve to localhost, Apache still needs to be given the mapping. This requires:

  • A configuration file with the appropriate mapping
  • A directive in the httpd.conf file to read the above file.

The easiest method to generate the appropriate conf file is to use a form at:

http://resources.comparity.net (Select “example”)

The instructions are as follows:

Item Value
Server XAMPP
Site Folder australia
Virtual Domain www.example.com
Document Root Mac: /Users/User/Documents/htdocs
Win: /xampp/htdocs
.conf file example.conf

The values marked (*) are clearly for the XAMPP on Mac configuration. For XAMPP on Windows, the Document Root is normally /xampp/htdocs, and for WAMP it is /wamp/www. If you leave it empty, the normal default is used.

  • Click on the [Update Link] button. Then, click on the Download link.
  • Place the newly downloaded conf file inside the extra folder. This folder is where the httpd.conf file is found. Instructions are inside the new .conf file.
  • Update httpd.conf as follows:

Find the Virtual Hosts Directive and direct it to include the new .conf file. For example:

# Virtual hosts
#Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf
Include /Applications/XAMPP/etc/extra/example.conf

It’s a lot of reading, but it doesn’t actually take much work.