Running PHP Locally
Print This Post
| One Comment | 480 views
While setting up PHP on my local machine, I came across a great tutorial that provides step-by-step instructions for configuring Apache2 web server, PHP, and MySQL in Windows. With permission, I am reprinting the tutorial here.
Special thanks to Ricocheting.com for allowing me to replubish the tutorial here. Make sure to check out the site for additional resources.
For this tutorial I used this setup:
- Windows XP Pro – SP2
- Apache 2.2.11 for win32
- ActivePerl 5.10
- PHP 5.2
- MySQL 5.1
- And installed everything on C:
Apache 2.2.X
1. Download & Unpack
Go to http://www.apache.org/dist/httpd/binaries/win32/ and choose a mirror.
You want the latest win32-x86-no_ssl.msi “MSI Installer Package”
release.My file was named: apache_2.2.11-win32-x86-no_ssl.msi
NOTE:
Apache sometimes moves the “quick download” link to the win32 binary.
The long way to officially find it is Download > Other files > binaries
folder (not the “Binary Releases” info link at the top) > win32
folder > then the latest win32-x86-no_ssl.msi release
2. Install
When you install Apache, you’ll get a prompt for “Server Information.”
Here is the settings I used:Network Domain: localhost
Server Name: localhost
Admin Email: (your any email)
[checked]: for All Users, on Port 80, as a Service
3. Starting/Stopping Apache
After installing, Apache2 automatically starts. The start icon in the System
Tray means it started. The stop icon means the “Monitor Apache Servers”
is running, but Apache2 isn’t started.You can easily start/stop/restart Apache and Apache2 via that icon in your
System Tray. If you get “The requested operation has failed!” error
while starting apache use the “Test Configuration” shortcut in the
Start Menu to find the error (if the text window pops up then closes before
you can read it, your config file is fine).
4. Testing
Now the ultimate test. To see if it’s serving. Open your browser and head
to: http://127.0.0.1/ or http://localhost/If it shows the It works! you have your server software installed and running.
5. Making Apache point to your files
Using Notepad open C:/Program Files/Apache Software Foundation/Apache2.2/htdocs
(in the start-menu there should also be a “Apache HTTP Server 2.2 >
Configure Apache Server > Edit the Apache httpd.conf Configuration File”
shortcut) and search for DocumentRoot. Change it from something like DocumentRoot
“C:/Program Files/Apache Software Foundation/Apache2.2/htdocs” to
the location where your HTML files and site are located. In my case: DocumentRoot
“C:/public_html”NOTE:
The first time i installed Apache2, i accidentally changed ServerRoot. Make
sure you change the correct line which is DocumentRoot and which is down about
line 150.
6. Then scroll down about one page and change:
<Directory “C:/Program Files/Apache Software Foundation/Apache2.2/htdocs”>
to point to the same location you set DocumentRoot to in the last step. Restart
apache to make the changes take effect
ActivePerl 5.10
1. Download
Download the latest version of ActivePerl from http://www.activestate.com/store/activeperl/download
(if it asks you to register, you can just leave the form blank and hit “Continue”).Get the Windows (x86) MSI version. My file was named: ActivePerl-5.10.0.1004-MSWin32-x86-287188.msi
2. Install
On the “Setup” screen (where you choose what features to install
– should be about the 2nd or 3rd screen in. I tend to miss it every time
and have to go back) change the install location (hit the little “browse”
button). I highly recommend that you install perl to a directory like:c:usr
In someone else’s words (very good advice)
If you will be using Perl CGI programs and want to maintain some level of
portability between both Linux machines and Windows machines, you will want
to install Perl to the same location on your Windows machine that it is on
most Linux machines.For example, on a standard Linux machine, Perl is located at /usr/bin/perl
and so every Perl program that I write begins with #!/usr/bin/perl. So, when
I install Perl on a Windows machine, instead of installing it in the default
location (which is E:perl for ActivePerl) I install it in E:usr so that
the Perl executable is located at /usr/bin/perl. This allows me to write code
on my Windows machine, then move it (without making any changes) to a Linux
machine and have it run there. And vice versa.[Check] Add Perl to the PATH environment variable
[Check] Create Perl file extension association
The rest should be grayed out and read-only, but if not, leave them unchecked
3. Activating CGI
Using Notepad (or other text editor) open httpd.conf (should be start-menu
shortcut “Apache HTTP Server 2.2 > Configure Apache Server > Edit
the Apache httpd.conf Configuration File”) and search for Options Indexes
FollowSymLinks (about line 190) when you find it add ExecCGI to the end so
it looks like Options Indexes FollowSymLinks ExecCGI
4. [OPTIONAL] Enabling CGI in any directory
If you want to use CGI outside the C:/Apache2_2/cgi-bin/ ScriptAliased directory,
you will need to uncomment the following line: #AddHandler cgi-script .cgi
becomes AddHandler cgi-script .cgi (remove the #) I also added .pl behind
.cgi so ‘perl’ extension is also treated as cgi files.If you will be creating your own cgi-bin, you will want to comment out: ScriptAlias
/cgi-bin/ “C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/”
so it becomes #ScriptAlias /cgi-bin/ “C:/Program Files/Apache Software
Foundation/Apache2.2/cgi-bin/”
5. Finding your location to perl
If you do not know where your perl.exe installed to, go to Start -> Search
and type in a search for perl.exe This location is the path to perl you put
on the top of all your cgi scripts. If you listened to my advice in the “Install”
step, the path should be close to: C:/usr/bin/perl.exeSome notes
For the perl path C:/usr/bin/perl.exe all of these are/were valid. I prefer
the last one because of the Linux <<>> Windows portability.#!C:/usr/bin/perl.exe
#!C:/usr/bin/perl
#!/usr/bin/perl.exe
#!/usr/bin/perl
6. Testing CGI
If you if you uncommented (removed the # symbol) the line AddHandler cgi-script
.cgi in step #4, then create a file in your document_root called hello.cgi
and put these three lines in it (if you did not comment/disable it, put the
CGI file in C:/Apache2_2/cgi-bin/):#!/usr/bin/perl
print “Content-type:text/htmlnn”;
print “hello world”;
7. Restart Apache if it is already running.
Now go to http://localhost/cgi-bin/hello.cgi (or wherever you put the file)
and run the script.
8. If you get a hello world in your browser, CGI is running.
If you get a 500 error, go to the last entry in Apache error log (Apache
HTTP Server 2.2 > Review Server Log Files > Review Error Log shortcut
in the Start Menu) to see exactly what caused this error.
PHP 5
1. Download & Unpack
Download and install PHP from http://www.php.net/downloads.php, you should
grab the newest 5.x.x Windows Binaries zip package that will work on apache.My file was named: php-5.2.9-1-Win32.zip
2. Unzip php. In my case, I unzipped to:
C:php
3. Rename C:phpphp.ini-dist it to php.ini
4. Edit your php.ini
Open php.ini in a text editor and scroll down about halfway through the
file and look for doc_root then change it to point to whatever your Apache
DocumentRoot is set to. In my case: doc_root = “C:public_html”Scroll down about 7 more lines and change the extension_dir from extension_dir
= “./” to the location of the ext directory after you unzipped PHP.
in my case: extension_dir = “C:phpext”If you are going to be using your server for testing, i recommend (this is
optional) you search and change the error reporting to give you info on any
errors, notices, or bad coding. If you want to enable this type of stuff,
search for error_reporting for and change:error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
to
error_reporting = E_ALL | E_NOTICE | E_STRICT
5. Editing Apache Conf File
Using Notepad open httpd.conf (should be start-menu shortcut “Apache
HTTP Server 2.2 > Configure Apache Server > Edit the Apache httpd.conf
Configuration File”). Either at the very beginning or end of the file
add the following lines: (NOTE: be sure to change BOTH C:/php parts to the
directory you installed your php to)LoadModule php5_module “C:/php/php5apache2_2.dll”
AddType application/x-httpd-php .php
PHPIniDir “C:/php”
Note: If you installed the older Apache 2.0, instead of the above lines,
you will need to use the lines listed on the bottom step of the Apache 2.0
tutorial. 6.[OPTIONAL] Editing Apache Conf File (part 2)
To get apache to automatically look for an index.php, search httpd.conf for
DirectoryIndex (about line 212) and add the files you want apache to look
for when a directory is loaded (if it doesn’t find any of these files, it
displays folder contents). Mine looks like:<IfModule dir_module>
DirectoryIndex index.php index.html default.html
</IfModule>
7. Testing
Restart Apache if it is already running (if it doesn’t start or you get
errors, use your Apache “Test Configuration” shortcut in the Start
Menu to see why).To test your PHP simply create a test.php file in your Apache “DocumentRoot”
folder (E:public_html in my case). In your test.php file, type these 3 lines
and then load the file in your browser like http://localhost/test.php (you
should get a whole long list of php variables, settings, etc):<?php
phpinfo();
?>
8. Documentation Suggestion
One weird thing I have noticed about PHP is that it does not come with documentation
of any kind. If you are a php developer/programmer, I suggest you download the
documentation. Downloads can be found on http://www.php.net/download-docs.php
and I personally recommend the “English – Windows HTML Help” (chm)
version as the search is so handy, although they all contain the same information.
MySQL 5.1
1. Download MySQL v5.1
Download MySQL from http://dev.mysql.com/downloads/mysql/5.1.html Just make
sure you get a Windows Essentials (x86) binary version. (after you click the
“pick a mirror,” if it asks you to login, scroll down a little and
click the “No thanks, just take me to the downloads!” link under
the login form)My file was named: mysql-essential-5.1.33-win32.msi
2. Install MySQL v5.1
Run the MSI file and use the following settings
Typical Setup
- Skip Sign-Up
- make sure “Configure the MySQL Server now” is checked
- “Detailed Configuration”
- “Developer Machine”
- “Multifunctional Database”
- “InnoDB Tablespace Settings” – leave everything default
- “Decision Support (DSS)/OLAP”
- make sure “Enable TCP/IP Networking” is checked and leave the
port number at 3306 (at this point, if you have a firewall, it will usually
try to access itself on the localhost)Leave “Enable Strict Mode” checked
- “Standard Character Set”
- check “Install As Windows Service” and I would recommend leaving
“Launch the MySQL Server automatically” checked.- enter your root password and I would recommend leaving “Enable root
access from remote machines” unchecked- then hit “execute” and it’ll install and set it up.
3. Getting PHP5 to work with MySQL – Official Info
Unfortunately PHP5 removed built-in support for MySQL. To get it to work,
the easiest way is to copy the mysql library file by hand. Open the folder
you unzipped your PHP to. Copy the libmysql.dll file (should be located like
C:phplibmysql.dll ) into your Window’s System folder (usually C:WindowsSystem32
although might be C:WinNTSystem or something).Then open up your php.ini in a text editor and search for ;extension=php_mysql.dll
and remove the ; infont of that line.Restart Apache and see if you get any errors. If it complains about “php_mysql.dll”
either your extension directory isn’t correct or windows can’t find libmysql.dll
4. Testing MySQL
Testing MySQL is not exactly easy. However, here are the common connection
strings for PHP. I recommend downloading phpMyAdmin and using it to create
and manage your databases, etc.PHP Connection test
<?php
// hostname or ip of server (for local testing, localhost should work)
$dbServer=’localhost’;
// username and password to log onto db server
$dbUser=’root’;
$dbPass=”;
// name of database
$dbName=’test’;
$link = mysql_connect(”$dbServer”, “$dbUser”, “$dbPass”)
or die(”Could not connect”);print “Connected successfully<br>”;
mysql_select_db(”$dbName”) or die(”Could not select database”);
print “Database selected successfully<br>”;
// close connection
mysql_close($link);
?>
Remote Acess
1. Getting your Server Location
Unfortunately unless you have a static IP with your internet connection,
your IP address (and server URL) will change every time you connect to the
internet, or your modem gets reset etc. To get your current IP (Internet Protocol)
number do one of the following:Windows 95, 98:
Start -> Run -> type in WINIPCFG when the window pops up use the top
pull down menu to select the adapter you use to connect to the internet.
I had an Ethernet [network] and PPP [dial-up] adapter listed, and by default
everytime it’s sitting on the network adapter.Windows 2000, XP:
Start -> Run -> type in cmd (this starts DOS/Command prompt). Type
in ipconfig and you should get a short list of settings with your IP address
displayed. (if you want to see a complete list: ipconfig /all but you don’t
usually need that much info)Now open the browser and type in http://your.number (like http://66.229.93.208).
Now anybody can access your site by that number till your IP number changes
(usually when you get disconnected from the internet it will change).Now open the browser and type in http://your.number (like http://66.229.93.208).
Now anybody can access your site by that number till your IP number changes.Shortcut:
Well, unless you are using a proxy, your IP and server is 66.229.93.208
2. Getting a Dynamic DNS Provider
If your IP changes each time you log onto the internet, you might consider
getting a free Dynamic DNS provider. They work by giving you a static name
like http://yourname.DNSprovidername.com. Then each time you connect to the
internet, the provider will update so your URL will point to your ever changing
IP number. This is a great service, and check out the Dynamic DNS primer for
a better explanation of dynamic DNS, what it is, and how it works.
3. LAN IP
If “winipcfg” or “ipconfig” from above returned a number
that was in the 192.168.x.x range, that IP is your LAN (Local Area Network)
IP. Only computers that are sharing your internet connection can access your
server using that number. In which case you’ll need to setup your router/modem
to forward your “outside” IP like 66.229.93.208 to your 192.168.x.x
LAN ip.
4. Router Issues
If you have a router or share your high-speed connection with multiple computers,
you will likely need to log into your router and configure it to forward Port
80 to your computer. How you do that depends on your router. The basic info
to get you started:
- Manufacturer Login IP Default username Default Password
- Netgear 192.168.0.1 admin password
- D-Link 192.168.0.1 admin
- Linksys 192.168.1.1
- admin
Look for something under “Virtual Servers” or “Forwarding.”
Type of info it typically askes:
- Private IP: (enter your LAN ip. should be something like 192.168.x.x)
- Protocol Type: TCP
- Private Port: 80
5. Public Port: 80
If you still need help, check google or your router manual as I usually do
not answer questions relating to this.
Security
1. Firewalls
Get yourself a firewall. A good free one is Zone Alarm that monitors outbound
as well as inbound traffic.
2. FTP Server
I previously recommended the trial version of G6/BulletProof FTP server
until one of you readers pointed out a much better server. Now I highly recommend
using the totally free GuildFTPd. Although it hasn’t been updated in a while,
it’s still a great windows based program with tons of options.
3. Mail POP3/SMTP Server
This is also a reader suggested program. MailEnable is a mail server for
Win32 systems. The Standard (free) version has no ads and no forced signatures.
The program has no limits on the number of domains or on the number of accounts.
A minor drawback is it does not support web mail (hotmail-like interface)
but that can be replaced with a simple perl/php script.4. MySQL data manipulation
Because of frequent requests, i’m going to give a simple 1-2-3 on how to
install phpMyAdmin so you can use it to edit your MySQL databases and tables.
If you want to install it “correctly” you should follow their official
docs. If you want to get it working fast, do this:1. Download version 2.x (3.x will only work with mysql5+). there are “quick
downloads” links at the top left2. Extract it somewhere in your document_root (the folder where your html
“web files” are). It creates it’s own sub-folder which you might
want to rename after extracting3. Rename the ugly long-tailed phpMyAdmin-2.9.2-all-languages-utf-8-only folder
it created to something shorter like: phpmyadmin4. Edit the file phpmyadmin/libraries/config.default.php ignoring all the
“do not edit” warnings and scroll down to about line 150. The lines
you want to fill out are:$cfg['Servers'][$i]['user'] = ‘root’; // MySQL user
$cfg['Servers'][$i]['password'] = ”; // MySQL password
5. Load the sucker up in your browser like http://localhost/phpmyadmin/
(or wherever you installed it to)

















Thanks kel.