November 21, 2008

Running PHP Scripts from a Command Line

Another great aspect about PHP is that you don't even need Apache to run your scripts. If you don't need any special GUI or your script takes so long that your page is timing out before it can finish, try running it from the Command Line.

Preparation:
  • Install PHP - Of course you have to install PHP! I felt silly adding this to the list, but I felt I had to.
  • Add the PHP installation directory as a Value in the Environment Variables - To do this, Right click My Computer->Properties->Advanced->Environment Variables. Look at the bottom in the System Variables for the Variable name, Path. Select Path and click Edit. Check to make sure that your install directory is included in the Variable Value path. For example, I should see in my path, C:\PHP. If the is not, add it to the beginning of the Path and separate it with a semi-colon (C:\PHP;). This is necessary so you can just type PHP MyScript.php while you are in the appropriate script directory.


Using the Command Line

This sections is for the complete newbie to the command line.

  • To open the Command Prompt, go START->RUN->type cmd
  • From here, you will need to navigate to where your PHP script you want to run. To change directories, use the cd command. For instance, if I was running a script out of the Default Apache DocumentRoot, I would type the following: cd C:\Apache\Apache2\htdocs [enter]. At this point, you are in the same directory as the script you want to run. Now type: php MyScript.php


  • With this in mind, you could actually call your PHP script much in the same way. After you enter into the Command Prompt at the very beginning type: php C:\Apache\Apache2.2\htdocs\MyScript.php [enter]


  • If your script contains HTML or any other non-PHP elements, they will just be printed to the screen
Conclusion

The reason you added C:\PHP to the environment path is so you didn't have to navigate to the PHP installation directory to access PHP.exe. Using this strategy, you can now build batch file programs that will run your scripts for you. For instance, I wrote a script that will update thousands of records in a MySQL database. The process takes over 10 minutes and I need to run it every day. All I did was created a text file called Update_Data.bat and add one line of text to the file: php C:\Apache\Apache2.2\htdocs\MyUpdate.php Now all I have to do is click Update_Data.bat and walk away without worries of a page time out.

November 20, 2008

How To Install Apache 2.2

NOTE: The instructions that follow are for installs on a Windows Operating system. Before installing, ensure that IIS is not installed or at least running.

Installing Apache

If you are new to server side scripting, you may have heard that you need a web server. No, you don’t need to have a big corporate sized computer. A web server a computer program that is responsible for accepting HTTP requests from web clients (your browser) and returning an HTML page and the Apache HTTP Server is just what you need.


  1. Go to http://httpd.apache.org/download.cgi to get the latest released version. At the time I wrote this, Apache 2.2.10 was the latest.

  2. Download the Win32 Binary without crypto (MSI Installer) by Right clicking on the apache_2 link and select Save Target As. The other files available are not relevant to you right now.

  3. Double click the MSI Installer you just downloaded and start to run through the Install Wizard.

  4. When you get to the Server Information section, enter the following:
    • Network Domain - localhost (Not Important; Used during initialization only)
    • Server's Name - localhost (This is standard)
    • Administrator’s Email Address - YOUR EMAIL

      NOTE: The word localhost refers to your PC. When using the browser on your PC, you can either use localhost or your PC name. If you use a PC that is on the same network, you will have to use the PC name to access the website.
  5. Choose the Typical installation then change the install directory to C:\Apache\Apache2.2\. It is not necessary to change the installation directory, but in the future it will be more convenient to do so.

  6. Finish going through the Install Wizard.
  7. After the installation is complete and you see that Apache is started (look in the system tray by the clock for the Apache Monitor to be green not red)
  8. Open your favorite browser and type: http://localhost/
  9. A page that says, "It Works!" should pop up. If this does not happen, there was a problem with the installation.
  10. Go to C:\Apache\Apache2.2\conf directory (or wherever you install Apache) and open the httpd.conf file. This file contains all the information that Apache uses to be configured. Take some time and read over this file. There is great documentation all throughout it and it will give you a better understanding of what Apache can do.
  11. The main thing you need to change is the DocumentRoot (Not the ServerRoot). You will notice that right now by default, it is pointing to /htdocs. This is the starting directory for your web site. Inside you will find a file called index.html. With the Apache default configuration, if there is not a file specified in the URL (http://localhost/), Apache will return the page marked index.html. If you want to store your Web Site in a different location on your PC, you will need to change the DocumentRoot and the directive below it that is pointing to /htdocs.
    NOTE: Remove the '#' in front of ServerName (ServerName localhost), otherwise it will generate a Warning in the Apache error log everytime it restarts. (It complains that it can't resolve the Domain Name)
  12. After you have made any changes, you must restart the Apache Service for the changes to take effect.
  13. Right now, you are configured only to run HTML pages from your PC. If you are going to run PHP pages, Read the How to Install PHP 5.2 installation post.

If you have any question or comments, don't hesitate to leave a comment.

How To Rename the Apache Service

If you wrote a program or had software that refered an Apache Service Name other than Apache2.2 than this article is for you. Since the release of Apache 2.2, the Apache Service name is now Apache2.2. This caused a problem with a program I use to Stop and Start my Apache and MySQL where I accessed the service by the Service Name. To fix this, you will need to do the following:
Open a Command Prompt (START-> RUN -> type cmd -> [ok])
  1. Navigate to where you installed Apache and drill down into the \bin directory (On my PC, I would type -> cd C:\Apache\Apache2.2\bin)
  2. Once you are in the \bin directory, type httpd.exe -k uninstall (you have to uninstall to rename it)
  3. Then type httpd.exe -k install -n [Your New Service Name Goes Here]

That's it! Your program should now work properly.

How To Install PHP 5.2

NOTE: At this point you should already have Apache 2.2 installed on a Windows PC. At the time of this writing, PHP was on Version PHP 5.2.6

Installing PHP



  1. First things first, you need to download the latest version of PHP at http://www.php.net/downloads.php. This actually can be an overwhelming process for the first time PHP installer. There is 8 download options to choose from, but for now we are going to concentrate on just the first 2. The Zip Package and the Installer.
    • MSI Installer - This is the easiest way to install PHP and it will install, then load nearly every default extension dll available. This is the one I will be explaining.
    • The Zip Package - Download the zip package if you are looking to upgrade to a new minor release version. For instance, if you have PHP 5.2.5 installed, download the Zip Package, rename the Original PHP directory to PHP_old and then name the extracted zip package to PHP and copy it in place of the old version. You will then need to copy the PHP.ini file from the old to the new. This is located in the PHP root.
  2. After you have downloaded the latest MSI version, double click the MSI to start the Install Wizard.
  3. Change the Install directory to C:\PHP if you want to keep it consistent with your Apache installation
  4. If you installed Apache 2.2 earlier, you will need to select the Apache 2.2.x Module when prompted on the Web Server Setup screen
  5. You will then be prompted to locate the Apache Configuration directory. This is where the httpd.conf file is located. If you followed my Apache Installation instructions, it will be located in C:\Apache\Apache2.2\conf. (During the installation of PHP, the httpd.conf file will be updated to include the PHPIniDir and the LoadModule directives)
  6. When prompted to Choose Items to Install, click the box next to Extensions and select Entire feature will be installed on local hard drive
  7. Repeat the same for Extras
  8. Finish going through the Install wizard

CAUTION

If you read this far, you are in luck. Following the above installation instructions will inevitably end in frustration if you don't read this.

When you install PHP to the fullest extent, you will get every possible PHP extension or your hard drive and unfortunately, each one is added to the PHP.ini configuration file. This wouldn't be a problem if they would all load without errors, but this isn't the case. Many extra Extension dll's are loaded that will not run properly without their counter parts installed as well. This causes errors and you are not able to load PHP pages. You need to open the php.ini file which is located in the root directory (C:\PHP) where you installed PHP to and scroll to the bottom of the file comment out the extensions that are listed below. Also, take this time to look over the options available to setup your PHP the way YOU want it. Basically, to use the basic functionality of PHP, you don't even need to load any of the dlls. The extension dlls give you added functionality like the php_mysql.dll which gives you access to the MySQL functions. Below is a list of all the Extensions that need to be commented out with a semi colon (;):

[PHP_OCI8]
;extension=php_oci8.dll
[PHP_PDO_OCI]
;extension=php_pdo_oci.dll
[PHP_PDO_OCI8]
;extension=php_pdo_oci8.dll
[PHP_PDO_PGSQL]
;extension=php_pdo_pgsql.dll
[PHP_PDO_SQLITE]
;extension=php_pdo_sqlite.dll
[PHP_PDO_SQLITE_EXTERNAL]
;extension=php_pdo_sqlite_external.dll
[PHP_PGSQL]
;extension=php_pgsql.dll
[PHP_PSPELL]
;extension=php_pspell.dll
[PHP_SNMP]
;extension=php_snmp.dll
[PHP_SYBASE_CT]
;extension=php_sybase_ct.dll
[PHP_DOMXML]
;extension=php_domxml.dll
[PHP_IBM_DB2]
;extension=php_ibm_db2.dll
[PHP_IFX]
;extension=php_ifx.dll
[PHP_IISFUNC]
;extension=php_iisfunc.dll
[PHP_INGRES2]
;extension=php_ingres2.dll
[PHP_MAXDB]
;extension=php_maxdb.dll
[PHP_MCVE]
;extension=php_mcve.dll
[PHP_NETOOLS]
;extension=php_netools.dll
[PHP_ORACLE]
;extension=php_oracle.dll
[PHP_PDO_IBM]
;extension=php_pdo_ibm.dll
[PHP_PDO_INFORMIX]
;extension=php_pdo_informix.dll
[PHP_SAM]
;extension=php_sam.dll
[PHP_SPL_TYPES]
;extension=php_spl_types.dll
[PHP_THREADS]
;extension=php_threads.dll

November 19, 2008

How To Install MySQL 5.0

NOTE: MySQL is a free database for Personal use and Businesses to manage their OWN applications. If you are looking for a free database to distribute with your application, try using PostgreSQL.

Download MySQL Server
If you have installed the latest version of Apache and PHP, you might as well install the latest MySQL Server. The MySQL Server is the actual Engine that drives your database. At the time of this writing, MySQL 5.0 was the latest. Create a Folder on your desktop to store and organize the following files. There is always going to be Beta and Development releases. Ignore those and download the stable release. Go to http://dev.mysql.com/downloads/ and download the latest stable version of MySQL Community Server. In the Windows Download section, select the Windows Essentials package and then Right Click Download and Save Target As or click Pick a Mirror. On the following page to Pick a Mirror, select No thanks, just take me to the downloads and pick the closest location near you and click the HTTP or FTP link to download. When the window pops up, choose to save the file to your folder on your desktop.

Download MySQL GUI Tools
The MySQL GUI Tools are the utility tools that you are going to use to Manage your databases and view your tables. Go to http://dev.mysql.com/downloads/gui-tools to download the latest set of tools to your desktop.

Download MySQL Connector (optional)
You will need to download the MySQL Connector if you are planning on connecting to MySQL through a ODBC connection. You don't need this with PHP if you loaded the php_mysql.dll extension in the php.ini file.

Download MySQL Workbench (optional)
The MySQL Workbench is a great GUI tool for planning and setting up your databases and the tables that reside within.

Installing MySQL 5

NOTE: If you already have an older version of MySQL installed, it is always a good idea to back your databases up before installing the new version. In fact, your should always backup your databases on a regular basis. This can be scheduled in the MySQL Administrator.

  1. Double click on the downloaded MySQL-Essential MSI installer.
  2. The setup wizard will startup and on the second window, you will see the Setup Types. Choose CUSTOM if you want to change the install directory and then click next. Through all of my walkthroughs, I install my programs the C:\ Root and I will continue here.
  3. Change the Install Directory to D:\MySQL\MySQL Server 5.0\ and click next and finish the installation.
  4. When given the option to Configure theMySQL Server Now, leave the option selected and click finish.

MySQL Configuration

If you unchecked the box at the end of the installation or you need to access the Configuration Wizard at a later date, go to C:\MySQL\MySQL Server 5.0\bin and click on MySQLInstanceConfig.exe

  • Select Detailed Configuration - Take the time to read through the different options available. This will help to setup the best optimal setup possible for you. If you are unsure of what to pick, just leave the default selection selected and click next. Remember that you can always change your mind later and revisit this Configuration Wizard.
  • Root Password - The word 'root' refers to the default administrator user. Setup this password to whatever you wish but make sure it is one that you will remember. You need 'root' to get you started later. If you decide to Enable root access from remote machines, PC's on your network will be able to access your MySQL databases with the user name root and the password you set for root.
  • Click Next and then click Execute to start the configuration. If for some reason the security settings do not take effect, you will need to close the window and reopen the Configuration Wizard and Reconfigure the Server.

Installing MySQL GUI Tools

  • Double click on the MySQL GUI Tools MSI and run through the setup wizard.
  • Change the install directory to C:\MySQL\MySQL Tools for 5.0\
  • Select to do the Complete Setup

MySQL Tools
After you install these tools, you should be able to access them through START->Programs->MySQL. If not, you can create shortcuts from where you installed them.

  • MySQL Administrator - This utility is what you will use to setup users, databases and other Administrative functions you will need to do.
  • MySQL Query Browser - The query browser is what you will use to view your databases and their tables.
  • MySQL Migration Toolkit - Use this tool to migrate existing databases from various vendors to MySQL Databases.
  • MySQL System Tray Monitor - You may want to use this tool while you are working on your databases to help monitor resource activity