Apache CGI PHP MySQL
- What and Who is this Tutorial for?
- Do I want "server-side thingamajiggies" from my website?
- Let's Go!
What and Who is this Tutorial for?
This is intended for the dial-up user, who pays for their access per minute lets say, but is applicable to anyone who does a lot of coding and designing for their site, and would like to test it immediately, instead of going through all the hassle of uploading etc.
If you have a website which you use with server-side thingamajiggies in it (PHP, Perl CGI, SHTML, mySQL etc.) then you will know that having to upload every time you change your code to test it is not only:
- totally in-e-fficient
- totally unnecessary
Wouldn't it be great if you could turn your own computer into a mini web server of its own, that would allow you to use your browser just like a real site to test it? Well, get ready folks, you can!
This tutorial is all about easily installing and setting up a fantastic web server just like your web host has. In fact, it'll be practically identical, but you have the easy option of not having to spend hours and hours pondering and configuring it for security etc. After all, you're the only one who's going to be using it!
See my other article on how to install Apache PHP Gettext on Windows for a shorter, more graphical help.
Do I want "server-side thingamajiggies" from my website?
There are two "sides" to this:
- client side
- server side
There's a whole lot of arguments about the differences, pros, cons etc. for client side and server side, and if you're that interested, you can look it up on a search engine...
It's also useful if using pre-made scripts like message boards, so you can test modifications and style changes without the risk of screwing up your site.
Not many, just a few differences between you and your hosts servers:
- if you're using windows, and your host is using Unix (most likely) you'll have to disable flocking, just # out any flock statements (if you don't know what this is, then you don't have to worry about it...)
- you might have to change absolute paths for your scripts, although if you use $PHP_SELF etc. for forms then there are no worries there... (again, if you don't get this, you don't need to)
A few words before we start, on the syntax of this tutorial (read colors):
Perl/HTML/Apache code, altered Perl/HTML/Apache code, directories, files
The main source for this tutorial was an article in the TLC Perl Articles website.
Right, let's begin! What do you need to do?
- Install Apache Web Server (necessary)
- Install Active PERL (optional - for Perl CGI users)
- Install PHP (optional - for PHP users)
- Install mySQL (optional - for mySQL users)
After each step, you can test each relevant component.
Install Apache Web Server (top)
Wass this "Apache" business then? Well, Apache is the name of a supa-doopa webserver which most hosts use. A webserver is the thing that intercepts peoples requests (ie. i wanna see index.html) and gives them back useful stuff (ie. <html><head> etc.).
Why Apache? Well, like I said, Apache is used by the majority of hosts, and if you use a different server yourself (read on) you'll have to make sure you configure all your files so they are useable with Apache (not that that will be a huge job).
What other servers are there? Lots, Windows has built in IIS, but that really sucks - Personal Web Server etc. They just... suck. besides, Apache is open source as well, which is even better (although not very helpful for us in this tutorial).
So, this is how you easily install Apache on your computer.
NOTE: I use Windows XP, which is basically NT, so there may be differences between what I describe and what you see, in reference to services etc. If you're using Linux, well, if you are using Linux, you should be smart enough to figure it out yourself...
Download Apache (back to Apache top)
You can download lots of of Apache versions, not just the Windows ones. There are various Windows versions in MSI (Microsoft Installer) format (you should have this if Windows 2000 and above, and can download it from Microsoft for Windows 95 & 98).
The latest version available as of time of writing is 2.0.39 so if you're reading this and a newer version has come out, just apply everything later on equally. To minimise download times, choose the version which has no_ssl in it and don't get the one with src in it (this means no source included nor secure server stuff, which means a smaller file and the quicker to get this working)
Download Apache Win32 Platform (ie. windows users)
Download Apache All Platforms (unix etc. - there's lots!)
Remember to choose the latest version, with no src and no_ssl in it.
Install It (back to Apache top)
Run the install program, and click through, you should see a "Server Information" page. Now:
- Server Name: localhost
- Server Address: localhost
- Administrator Email: local@host
You get the idea - anything that asks you for server address, use localhost, email can be anything you like.
localhost = 127.0.0.1 which is the IP address for your computer (local... host). This means that when you type in localhost in your browser, you're trying to connect to yourself - when you have everything installed properly, this is the address you'll use to connect to yourself (http://localhost). You'll get an error if you try it and you've not a webserver running...
Keep all the defaults for paths and options, it will install to: c:\program files\Apache Group\Apache2 which you can change to c:\apache if you like, as it will be quicker to type when editing...
That should be done fairly quickly. Just click yes to most stuff. Make sure you don't run it yet, 'cos we need to change a teeny weeny bit of stuff in it.
If you are using Windows 2000, XP (ie NT) then Apache will install itself as a service which will run automatically in the background when you start your computer - a little icon will be in the system tray for stopping and starting the server (useful when you configure it later.)
If you are using Windows 98 or 95, it won't run as a service and you'll have to start the server manually via the start menu => programs option, it will pop up a nasty console box, but you'll have to live with it.
Modify it (back to Apache top)
Before you run it (make sure it's stopped if it first. If you got hasty and ran it already, you can Control-C the Apache box if you're on Windows 98 or just click on the Apache symbol on the system tray and tell it to stop the server if on NT (XP & 2000)) you have to modify it a little bit. Which is easy to do, and won't take long. Apache 2.xxx is easier and has less to modify...
Open up the httpd.conf file in Notepad or something from the c:\program files\Apache Group\Apache2\conf directory, or from the Start Menu option => Configure Server.
You should a whole bunch of these: # and some writing 'n' stuff. Scroll down until you see:
<Directory "C:/Program Files/Apache Group/Apache2/htdocs">
Then find just below that:
Options Indexes FollowSymLinks MultiViews
and change it to:
Options Indexes FollowSymLinks MultiViews +Includes
These make sure SSI and .htaccess work for you. If you don't want 'em, don't add 'em!
Scroll down a bit, and make sure:
DirectoryIndex index.html index.html.var
DirectoryIndex index.html index.html.var index.shtml index.php
if you want your index file to be a php, you can add all sorts of things here, like home.htm etc. This means that when someone types in http://www.somesite.com they will get http://www.somesite.com/index.html or /index.php etc. If you don't set a valid option and have a file like mymainpage.htm then you'll get an error message (or worse) a directory listing...
Scroll down a whole lot more and find:
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"
Now if you have a cgi-bin in your public_html folder like:
then you need to change this to:
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/htdocs/cgi-bin/"
ie. the htdocs is the equivalent of your public_html folder... If you have no idea what I'm talking about, just leave it.
Some hosts have "anywhere CGI" enabled, so you can have this cgi-bin folder... other hosts have a cgi-bin they give you which isn't in the public_html directory: all in all, it's best to mimic your host's server as much as poss so it's all dead easy for you to use.
If you want to use PHP (if you don't, skip this), then we might as well add that it now, seeing as we are in the right place - add:
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php .php3
Action application/x-httpd-php "/php/php.exe"
just below the ScriptAlias /cgi-bin/ bit. Note that c:/php is the directory we're going to install PHP into, if you want to choose a different one, go ahead and change it, but remember for later.
Back to CGI... change:
<Directory "C:/Program Files/Apache Group/Apache2/cgi-bin">
Allow from all
(just below the script alias) to
<Directory "c:/program files/apache group/apache2/htdocs/cgi-bin">
Allow from all
This makes sure CGI is executed in the cgi-bin directory
Scroll to find:
#AddHandler cgi-script .cgi
and change it to:
AddHandler cgi-script .cgi .pl
This makes sure .pl and .cgi are executed.
#AddOutputFilter INCLUDES .shtml
AddOutputFilter INCLUDES .shtml
This makes sure SHTML is server-parsed.
You're done! Save the file and continue...
Test!(back to Apache top)
First, some explanations of the folder structure in your c:\program files\apache group\apache2, the only ones you need to care about are:
- cgi-bin (if you changed this to htdocs/cgi-bin you can delete this folder)
- conf (this has that httpd.conf in it)
- htdocs (this is where are your html, php and stuff goes - like your public_html folder)
- logs (contains errors.log - which is great if you have a naff script which doesn't work, it will tell you why...)
- manual (contains the Apache manual, with loads of information if you're interested)
Right, first, start up Apache, either going into the Start Menu -> Apache httpd Server -> Start Apache in Console or by using the nifty system tray icon for NT users (start server/service)
For Win98 and 95: You should get a DOS box, saying: Apache/2.0.29 (Win32) running... if you get an error message, Apache have included a FAQ for when it doesn't work in the manual, above.
For XP, 2000 and NT: You won't see much, but the system tray icon should have a "play" icon on it...
You must have Winsock 2 installed too. Not necessary for any user above Win95. A search on Google for Winsock 2 should reveal some download locations.
Now the test. Open your browser and type in the address box: http://127.0.0.1/ - you should get an Apache page. If so - congratulations! You have your own Apache Web Server. If not, check back on above. A reboot helps if you just installed Winsock 2. You are seeing the default page which comes with the server, called index.html.xx in the htdocs folder.
If you changed DirectoryIndex to something obscure, you'll get an error message - if you get a 404 message - that means the server works! You just didn't put anything into it yet...
Now you can clear out the htdocs directory ready for your own files...
Install Perl (top)
Now you have Apache installed, you need to install Perl, as Apache doesn't come with it - but it's really simple...
Again, there are lots of versions for different OS, but we're going to use ActivateState Perl for Win32. With MSI.
Download ActiveState Perl - All formats
Remember to download the latest version, as it gets updated pretty frequently.
Run the program you downloaded, accept all the license agreements etc. Keep the current settings, but change the root (ie the first tab) install directory to c:\usr - it will tack on the bin\perl bit so your path to perl will be: #!/usr/bin/perl which is pretty much exactly the same as it is on most hosts! Isn't that handy? This done, click next and untick the Create Perl file extension assosciation - this means when you open a .pl file it won't run perl, but continue as it did before. I have it run notepad so I can edit files.
Having installed it you can test Perl works:
Let's see if Apache with Perl works! Copy and paste the following file and save it in your cgi-bin directory (c:\program files\Apache Group\Apache2\htdocs\cgi-bin etc.) as test.cgi
print "Content-type: text/html\n\n";
print "Hello World";
Run it, and cross your fingers! Note: http://127.0.0.1/cgi-bin/test.cgi is the path in your web browser.
If it works - congratulations! If not, check the above, and if you're really struggling, ask for help in our forums. If you get an error message, check the ever-useful errors.log file for more info...
Install PHP (top)
This is easy, make sure you followed the steps in editing the httpd.conf file as described at the beginning (adding ScriptAlias /php/ "c:/php/" etc.).
Ok, goto http://www.php.net and goto the downloads page. Goto your respective OS, and for Windows users, you want the ZIP package, NOT the installer version.
When it's downloaded, just extract the zip file to the directory you want (I used c:\php in the httpd.conf file).
Goto your php directory, and rename the php.ini-dist to php.ini and move it to your conf directory in the Apache folder. You don't need to change it, as in PHP 4+ mySQL support is built in.
This is easy, make a file called phpinfo.php with:
Save it, and stick it in the htdocs folder.
Type http://localhost/phpinfo.php in your browser and hit enter. You should get a nice page full of info - it worked! If not, you may need to restart your server, and if still you're having problems, look at the errors.log file for more info.
Install mySQL (top)
Goto http://www.mysql.com and goto the download area, download the file for your OS. It's very simple.
Run the setup file, install it where you like, doesn't make much difference.
Like I said before, for XP, 2000, NT users, like Apache, you get mySQL as a background service which automatically loads a traffic signal thingie in the system tray. Clicking on this will give you the mySQLadmin program - which is great.
I haven't tried mySQL on Windows 95 or 98, but you should run the mysqld.exe file in the c:\mysql\bin directory to start it. Then use the winmysqladmin.exe file for the mySQLadmin program.
If you're doing this, I'm taking a chance that you know enough about mySQL to configure your own my.ini, if not, the admin program has a tab for you, which will even do it for you!
You might want to change (in the "my.ini setup" tab of the admin program):
I won't go into details over writing php databases, but a quick test is to fire up the mysql.exe in the c:\mysql\bin directory, which should log you on:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 to server version: 3.23.51-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
ok, now type the following:
mysql> create database mydb; Query OK, 1 row affected (0.08 sec)
mysql> use mydb; Database changed mysql>
If you got as far as that, it worked! I'll leave it up to you do write the message board...
A few things to bear in mind:
- the directory where your HTML content is is \htdocs. Just copy your files there
- if having problems, check out the help pages! Apache and Perl come with extensive documentation. \manual contains Apache documentation and \usr\html contains Perl documentation. PHP and mySQL have online documentation at their sites.
- If you have any comments, suggestions, improvements, or find any bugs, post in our forums!
- Have fun!
What I've done with Apache, Perl, PHP and mySQL
- Installed Ikonboard locally (lots of # out flock commands). Tested out "hacks" which tend to screw up online. Tested mods and style changes
- Copied my site to htdocs and cgi-bin. Coded it.
- Learnt PHP and mySQL
- Used super doopa databases instead of rubbishy flat file databases
- Saved a HUGE amount of online time
If you have any problems with the above, think it's dumb, or think it's great, email me, or post a question in the discussion forums.