Upload Share and Earn

Thursday, September 6, 2007

Installing PHP under Windows: Further Configuration of WAMP

There are 4 steps involved in doing this configuration

(1)Installing PHP under Windows: Further Configuration of WAMP

In his last article, Matthew showed us how to successfully install and configure WAMP (Windows, Apache, MySQL and PHP) to setup a development environment. In this next piece, learn how to make use of the Apache and PHP configuration files to further enhance your server needs.
In the previous tutorial, your WAMP system on your home PC is best for use as a development server only. You may be able to host a personal home page or hobby page, but of course this would only be available when your computer is on and online. Bearing in mind our decision to only use it as a development server we will have a specialised configuration.

In an attempt to make code developed on our server portable to the widest possible range of configurations, for other peoples’ servers, we will deliberately have a strict configuration, with many options turned off, this will force us to use 'better' programming techniques thus allowing anyone with Apache and PHP to run our code.

First we are going to configure Apache, I will attempt to justify my choices for any configuration changes that I make, as well as briefly explain other possible options. This will allow you to choose your own options should your opinion differ from mine.


2)configure Apache


Installing PHP under Windows: Further Configuration of WAMP - Configuring Apache
(Page 2 of 4 )

To configure Apache the file we will be editing is httpd.conf. Despite the extension this is just an ordinary text file; so open it in notepad for editing either by navigating through the folders (C:Program FilesApache GroupApache2confhttpd.conf) or via the shortcut on the Start menu (Start > All Programs > Apache > Config > Edit httpd Config File).

Apache will treat any lines starting with a hash, or pound, sign (#) as comments, and will ignore them, so a good way to test a setting would be to comment out the old line with a '#' and type your new setting. That way if you make any mistakes it will be nice and easy to go back to the way things were (delete the new line and uncomment the old one.) There will be many lines commented out, much of it will be text explaining various bits and pieces to you, you should read the first few paragraphs (before Section 1). This should explain a couple of points about directory paths, and different slashes. If there is something you don't understand, don't worry as I will be taking you through it all. Remember if you mess up big time the default configuration settings are stored in a backup file (httpd.default.conf). If there are any lines that I don't talk about, don't change them unless you know what you are doing. Finally remember, none of these changes will be apparent until Apache is restarted.
I would recommend changing one line at a time, saving the file and testing it by clicking start > All Programs > Apache > Configure > Test Configuration. If you briefly see a DOS (Command Line) box that disappears, then everything is ok. If the box stays it will tell you where it found an error.

Let’s move on to the various directives (configurations/options).

Line: Listen 80

This tells Apache which port to listen for requests on. The default is port 80. If you change the port from default, for example to 8080 or 8008 (both common choices), then any URLs must be validated (e.g. http://www.localhost:8080/ or http://localhost:8080/test.php). As you could imagine typing this for every URL would get quite annoying, quite quickly, so unless you have a real reason to change it, such as IIS compatibility issues, don't.

Line: LoadModule

The Various LoadModule commands tell Apache to load various external modules. Don't touch these unless you know what you are doing, If you remember when we first installed WAMP in the first tutorial, we added a LoadModule line for PHP (LoadModule php4_module php4apache2.dll). That's because PHP is an external module that Apache needs to load for PHP scripts to work. Move the PHP LoadModule line from the end of the file, where you put it on install, to the end of the LoadModule block.

Line: ServerAdmin

Make sure the email address you wish to be contacted on is here. Should any error page crop up this will allow people to contact you.. This is not really a major issue on a development server, however, should anything go wrong this would let people know who the server belongs to.

Line: ServerName :

localhost:80 will do for this one; for similar reasons to ServerAdmin.

Line: DocumentRoot

This tells Apache where your web documents are stored. You can leave this as is, or change it to something you prefer (c:webdocs might be quicker to browse to than C:/Program Files/Apache Group/Apache2/htdocs). Whatever you do, make sure the directory exists and that you copy all your files to it.

Line: AddType

Back when you installed PHP and added the LoadModule line you should have also added an AddType (AddType application/x-httpd-php .php) line. AddType tells Apache which extensions to associate with which types of documents. Move the PHP AddType line from the bottom of the file to the bottom of the AddType block. If you wish you could change the PHP AddType line to something like (AddType application/x-httpd-php .php .htm). This will associate .htm file types with PHP, so you can include some php scripts in htm files.

We will be adding another AddType line (AddType application/x-httpd-php-source .phps). This line will make the extension .phps a PHP Source file, which will display the PHP code with special coloured formatting (once we turn script highlighting on in PHP). You will probably find this quite useful for debugging.

Line: ErrorDocument

These allow you to create your own documents to handle errors. For instance, if a page is missing you usually get an ugly page with a message similar to '404 Not Found'. Well, you can make your own error pages. This will be more fully covered in a future tutorial; however, for the moment uncomment the line ('# ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var') by removing the '#' and change the .html.var extension to .html. In your document folder create a new directory called 'error' then create an html page called HTTP_NOT_FOUND. This page should contain a message telling the user that the page they clicked on could not be found. You should also include a link to your main page. Now, after Apache is restarted, whenever you go to a page that doesn't exist (http://localhost/bad/page) you will get your own pretty error page instead of the standard ugly one.

That's about all you have to worry about for Apache. As I said before, I recommend that you change one setting at a time, test the configuration (Start > All Programs > Apache > Configure > Test Config), and, if the black screen disappears, fine, if it stays, take note of any errors and correct whatever you may have done wrong. Remember that no changes will take effect until Apache has been restarted (Start > All Programs > Apache > Control > Restart).


3)PHP
(Page 3 of 4 )

Now we are going to edit php.ini in the Apache folder (one up from the conf folder). Anything following the semi-colon character (;) is commented out, to uncomment it, simply delete the semi-colon.

short_open_tag = On
This should be set to off. This will not allow us to use the short style tags. Although there are ways to get around this, this setting will force us to code in the 'proper' and more portable way ().

asp_tags = Off
Who wants to use ASP style <% %> tags (apart from ASP developers)?

highlight.string = #DD0000
highlight.comment = #FF9900
highlight.keyword = #007700
highlight.bg = #FFFFFF
highlight.default = #0000BB
highlight.html = #000000

Simply uncomment these lines (remove the semi colon (;) before them).

error_reporting = E_ALL
Sets PHP to tell us about all errors.

display_errors = On
PHP tells us about errors rather that just sit there with a blank screen!

register_globals = Off
Stops many possible security issues, and forces the user of the superglobal arrays (ie: $_POST, $_GET, $_COOKIES, etc.).

doc_root = C:Program FilesApache GroupApache2htdocs
This path should be the same as the document root you set in Apache’s httpd.conf file.

;extension= ...
This allows you to include various PHP extensions, a bit like modules in Apache. You should only turn then on when you need them.

session.save_handler = files
Make sure that this is set to files.

session.save_path = c:/temp
Make sure that this is set to a temp directory that exists on your PC. It will be used to save data for sessions.


Installing PHP under Windows: Further Configuration of WAMP - Conclusion
(Page 4 of 4 )



That's about it, save the file and restart Apache. View the test.php file you wrote in Installing PHP Under Windows to check that everything is still functioning the way it should be. In this article, we’ve covered various aspects of the Apache and PHP configuration files. By making use of the tips above, you are bettering the chance of creating portable code that can be used across an array of system configurations

No comments:

 
eXTReMe Tracker