Using PHP Offline

To run a PHP script offline or to convert HTML to PHP and test your site locally on Windows or Linux you must have a local server (Apache), a PHP interpreter and a way to communicate with a database (MySQL).

How to install Apache, PHP and MySQL on Windows

The simplest is to use EasyPHP 1.7 (2003 version) that installs apache 1.3.27 - php 4.3.3 - mysql 4.0.15 - phpmyadmin 2.5.3 (apache 1.3.33 - php 4.3.10 - mysql 4.1.9 and phpmyadmin 2.6.1 with EasyPHP 1.8 - 2005 update -).
Usually no problem, except when you shut off Windows having set the option Start servers as services.
easyphp folder A few important files or folders:
- C:\Program Files\EasyPHP1-7\apache\php.ini
This file allows you to modify the configuration of PHP.
The other configuration files can be edited by a right click on EasyPHP icon icône.

- C:\Program Files\EasyPHP1-7\www
This is the default folder for your local site.

PHP 4 or 5 configuration

Open php.ini.
To find errors and problems, you have to replace
display_errors = Off by display_errors = On,
display_startup_errors = Off by display_startup_errors = On
and log_errors = Off by log_errors = On and set the name of the file where errors will be logged (for instance error_log = "c:\Program Files\easyphp1-7\php\error_php.log").
If you want to use PHP 3 scripts, you will also have to replace register_globals = Off by register_globals = On.
This is not a good idea as register_globals will be turned off by in the next version of PHP. So the best is to get used to PHP 4 and modify the scripts thanks to the logged or displayed error messages.
You shouldn't have to modify the other parameters.

Apache 1.3 configuration

If everything works fine and if you have your local site in C:\Program Files\EasyPHP1-7\www, no problem.
If your local site is elsewhere, two solutions:
1. If you want to test your local site with a robots.txt file, cookies or if you use the referrer ("HTTP_REFERER"), replace DocumentRoot "c:/Program Files/EasyPHP1-7/www" by DocumentRoot "c:/site" in the configuration file (right click on EasyPHP icon or C:\Program Files\EasyPHP1-7\apache\conf\httpd.conf)
and <Directory "c:/Program Files/EasyPHP1-7/www"> by Directory "c:/site">.
Typing 127.0.0.1 or localhost in the address bar of your browser will generate your home page.

2. If not, add an Alias with the Administration menu (if MSIE is not your default browser, you will have to type http://127.0.0.1/home/ in the address bar) or manually in the configuration file (right click on EasyPHP icon or C:\Program Files\EasyPHP1-7\apache\conf\httpd.conf):
In the Aliases part, add
    Alias /site "c:/site"

    <Directory "c:/site">
        Options FollowSymLinks Indexes
        AllowOverride None
        Order deny,allow
        allow from 127.0.0.1
        deny from all
    </Directory>
If you have another site, or want to run PHP scripts, add
    Alias /site2 "c:/site2"

    <Directory "c:/site2">
        Options FollowSymLinks Indexes
        AllowOverride None
        Order deny,allow
        allow from 127.0.0.1
        deny from all
    </Directory>
Typing 127.0.0.1/site or localhost/site in the address bar of your browser will generate your home page, 127.0.0.1/site2/your_script.php will run the script.

MySQL configuration

If your data must not be in the default folder - datadir = c:/Program Files/EasyPHP1-7/mysql/data/ (datadir=${path}/mysql/data with version 1.8) -, you can modify this setting (right click on EasyPHP icon or search for the configuration file my.ini - remplace for example by datadir = c:/data/).
In all the cases, phpMyAdmin (127.0.0.1/mysql/) will allow you to work with the databases without any modification to its configuration file.

With WampServer, Mov'AMP, XAMP or another installer

With Apache 1.3, the same files (they sometimes have a different name: mywamp.ini for WampServer) must be modified to test and debug PHP locally.
For Apache 2, see how it is done with Linux once the configuration file is located.
topTop of the page

Running a PHP script offline

Start Apache and MySQL servers.
According to what has been done during the Apache configuration, type http://127.0.0.1/alias_name/(file_name) in the address bar of your favourite navigator (with IE6 you must connect or untick File/work offline).
Normally, the home page (or file_name) is displayed.
topTop of the page

HTML to PHP conversion

To begin, you can rename the file index.html into index.php.
Then start Apache and MySQL servers.
According to what has been done during the Apache configuration, type http://127.0.0.1/ (or http://127.0.0.1/alias_name) in the address bar of your favourite navigator (with IE6 you must connect or untick File/work offline).
Normally, the home page is generated.
Once everything is OK, you have to modify the extensions in all the pages with a search and replace utility.
inforapid search and replace can do this job.
So, replace .html or .htm
Search
by .php in all the site pages except for the links toward the .html page intended to indexing.
Replace
Then you have to rename all the pages (except the .html page).
Now you can check the links and look for errors.
Once done, you can add PHP scripts.

In order to verify the scripts, you can use:

- either WinHttrack (for different browsers, generated file syntax, links and partly javascript),

- or, for MSIE 6, Wysigot that mirrors the local site, thus allowing you to find errors or warnings in the file error_log specified in the PHP 4 configuration (or in the pages) and link problems in the Apache error.log file.
Each time you refresh the mirror, as any modified file is in bold letters and as any modification is highlighted, you can see the effect of any insertion or modification in the whole site (the free version does not allow you to check the syntax of the generated files).

Finally, upload the site.
topTop of the page

What about Linux ?

I tested three distributions and had difficulties with all of them.
As a matter of fact, you have to install MySQL package, and the package dealing with links between PHP and MySQL, then have httpd and mysql services started.
Once everything set, you have to download phpMyAdmin in the version your host server uses: php or php3.
PhpMyAdmin must be decompressed in a folder (as for me /echange/phpMyAdmin-2.6.0).
According to your Apache version, the alias is not declared in the same file:

- with Apache 1.3

The file httpd.conf is located in the folder /etc/httpd/conf/
Normally the Aliases part is there (or it may be in an included file) and you just have to add
    Alias /phpmyadmin/ "/echange/phpmyadmin-2.6.0/"

    <Directory "/echange/phpmyadmin-2.6.0/">
        Options FollowSymLinks Indexes
        AllowOverride None
        Order deny,allow
        allow from 127.0.0.1
        deny from all
    </Directory>
to access the databases by typing http://localhost/phpmyadmin/ in the address bar of any browser.
In this file we do the same as with Windows to add an alias for our site or the modification of DocumentRoot.
In the User part, two solutions :
  1. we replace apache by our user name in the file.
  2. we declare in control pannel that we belong to the group apache or that we are allowed to use apache (and MySQL).
If AddDefaultCharset has been set, check if it is the one you use, if it is not you can change it or comment the line so that your browser can use another character encoding.
The modification of the other parameters shouldn't be necessary.

- with Apache 2

The file httpd2.conf is in the folder /etc/httpd/conf/
Normally the Aliases part is in an included file (either /etc/httpd/conf/commonhttpd.conf, or in the folder /etc/httpd/conf.d/ ) and you just have to add in the file declaring the "alias"
    Alias /phpmyadmin/ "/echange/phpmyadmin-2.6.0/"

    <Directory "/echange/phpmyadmin-2.6.0/">
        Options FollowSymLinks Indexes
        AllowOverride None
        Order deny,allow
        allow from 127.0.0.1
        deny from all
    </Directory>
to access the databases by typing http://localhost/phpmyadmin/ in the address bar of any browser.
If you want to modify DocumentRoot you have to do it there for <Directory ... > and then in the file httpd2.conf for DocumentRoot.
The modification of the other parameters can be done by following the apache 1.3 procedure either in httpd2.conf or in commonhttpd.conf.

PHP and MySQL configuration

As we did for Windows we have to modify the file /etc/php.ini and if necessary the file /etc/my.cnf (copied from /usr/share/mysql/my-medium.cnf or /usr/share/mysql/my-large.cnf in which we will add datadir = /full_path in the part [mysqld]). We will also have to tick mysql in the user's group.
topTop of the page

With javascript

W3C XHTML 1.0
W3C CSS