Publishers of technology books, eBooks, and videos for creative people

Home > Articles

Dynamic Web Sites

This chapter is from the book

Setting Up a Local Environment for Apache/PHP

Setting up a local environment using the open-source Apache and PHP setup can be difficult and frustrating for Windows and Macintosh users who are largely unfamiliar with Unix systems. There are often multiple versions of the same software available, and there are usually multiple ways of installing and configuring it.

But the biggest reason why Windows and Macintosh users are often intimidated by Apache/PHP setup is that the way users interface with the software is fundamentally different from what they’re used to. Windows and Macintosh users are accustomed to interfacing with the computers through dialogs, windows, and wizards. To set up IIS, for example, you access a dialog through the Windows Control Panel. To configure ColdFusion, you access a special Web page, where you fill out nicely designed and well-documented Web forms. To configure PHP, Apache, and (later in the book) MySQL, you will need to open and manually edit text files buried in nested application directories. A single typo can cause the system to malfunction or cease functioning altogether (until you fix it), and you’ll be given little guidance on what to do while you are in the text document. Such is the price of open-source technologies.

This section walks both Windows and Macintosh users through the process, albeit at a slightly generalized level. The reason for this is that the exact steps are likely to change (in minor ways) between the time I write this and the time you read it.

You begin by downloading the latest versions of the Apache Web server and the PHP module software, both available for free on the Web. You should install Apache before attempting to install PHP, because PHP is dependent on a Web server.

When you download Apache, PHP, or MySQL, in addition to different versions for different operating systems, you’ll also usually see options to download the Source or the Binary files. The source code is the non-compiled code that actually runs these products. Before it can be installed and used, it must be compiled, that is converted to 0s and 1s, also known as binary code. The binary code has already been compiled for you, and it is ready to install on your system. With commercial products, such as Microsoft or Macromedia products, you never have the option of seeing the source code—what comes on the CD is the binary file. One benefit of being able to access the source code is that you can change it. However, I assume that if you are reading this book, you are probably not at a point where you want to rewrite portions of the programming code behind Apache or PHP! Also, if you get the source code, you have to go through the additional trouble of converting it to a binary file. Thus, for the purposes of this book, I strongly recommend that you take the easy way out and download the binary file.

Installing Apache for Windows

To download and install the Apache Web server for Windows, follow these instructions.

  1. Go to the following URL, read the instructions, and click the Download link: http://httpd.apache.org/

    In addition to downloading the Web server itself, this site contains numerous useful resources for Web developers.

  2. Find the Windows Binary file for the most current production release, and click its link.

    At the time of this writing, the most current production release was 2.0.54.

  3. Save the file to disk, and wait for it to download.

    At the time of this writing, the Windows binary for Apache was under 5 MB.

    The application downloads in a single installer file.

  4. Double-click the installer file, which is called something like apache_2.0.54-win32-x86-no_ssl.msi to launch the installation wizard.

    The installer file is on your hard drive, in the directory you saved in the preceding step.

  5. Complete the installation wizard. In the Server Information screen, enter localhost in both the Network Domain and Server Name fields. Leave the default For All Users option selected. Finish the remaining screens using the default setting.

    The Apache installer is entirely self-explanatory, with one exception: the Server Information screen. This screen tells Apache how developers and users alike should access the server. If you were installing Apache on a network server, you’d have to specify the computer name and domain, but since you are only installing it for use on a single machine, you can just enter localhost.

  6. If a Windows Security Alert appears, select Unblock.

    To enhance security Windows XP users who have installed Service Pack 2 (SP2) will probably see a security alert screen. Windows by default is trying to disable the server you just installed, in case it is a program run by a virus. You know the Apache Web server is legitimate, though, so you can tell Windows to let it run.

  7. To verify the successful installation of your server, open a browser and go to the following location: http://localhost/

    You should see a placeholder page, as follows.

Installing PHP in Windows

Installing PHP in Windows is slightly more complex than Apache, because the files don’t come with an installer. Instead, the files needed for the program come zipped. After you unzip them, you must manually move a few files to different locations.

  1. Go to www.php.net/downloads.php and download the latest stable (not beta) version of PHP, choosing the PHP 5.x installer version, found in the Windows Binaries section.

    At the time of this writing, the latest stable version is PHP 5.0.4.

  2. Double-click the PHP installer icon.

    The icon can be found in the directory into which you downloaded it. My version of the file is called php-5.0.4-installer.exe.

    The first screen of the installer warns that you should stop your Web server, in this case, Apache.

  3. Click the Apache icon in the taskbar (lower-right corner of your screen), and from the menu that pops up, choose Apache2 > Stop.

    The Apache icon is a smaller representation of the Apache feather logo you saw when you opened localhost in your browser and saw Apache’s default start page. Stopping the server temporarily disables it, which makes it possible for you to make changes to it (in this case, installing PHP). When you are done installing PHP, you’ll reopen Apache and make sure Apache with PHP works.

  4. Proceed through the screens accepting all the defaults, until you come to the Mail Configuration screen. In that, enter your SMTP server as well as your email address. Click Next.

    Your SMTP server is the server through which you send your email. The information is provided by your ISP. If you don’t remember it, but you are using a mail client such as Microsoft Outlook Express or Entourage, or Mac OS X Mail, you can look it up in the mail settings. For example, in Microsoft Entourage, choose Tools > Accounts, select your email account, and click Edit. In the dialog that appears, you will see your SMTP (outgoing mail server) settings.

  5. In the Server Type screen, make sure Apache is selected in the list.

    As noted earlier, PHP can run on many types of servers, but in production environments, it is usually paired with Apache.

  6. Click through the remaining screens and initialize installation.

    When you are done, you may see the following dialog, which indicates that not all the installer has been written and there are a few tasks you’ll have to complete yourself.

    Directions can be found in install.txt, though it is not clear from this message whether they mean install.txt in the PHP folder (C:\PHP) or in the Apache folder (C:\Program Files\Apache Group\Apache2\). Worry not—I’ll walk you through it here.

    As mentioned earlier, open-source applications such as Apache and PHP are often configured through plain text files, rather than elaborate dialogs and wizards. Both PHP and Apache have configuration text documents, which you can use to specify important local settings and desired behaviors.

    • For PHP, the document is called php.ini, and can be found by default at C:\PHP\BACKUP.
    • For Apache, it can by default be found at C:\Program Files\Apache Group\Apache2\conf\http.conf.

    When you installed PHP, the choices you made in the dialogs configured PHP for you, so you won’t have to deal with changing php.ini. Unfortunately, a couple changes need to be made in Apache’s configuration file, httpd.conf, and the PHP installer did not make those changes (hence the warning dialog).

    Specifically, even though you have installed PHP and it is running successfully, Apache doesn’t know it is there. As a result, if you try to browse to a PHP file on your Apache server, it won’t know to let PHP process the PHP scripts, and since it doesn’t know how to process them either, it will just send them back to your browser. But your browser also doesn’t know how to deal with the script, so it will not open the file. The operating system at that point, knowing that a file needs to be opened and that the browser, which called it, can’t do it, will look for another application to open it. It will find Dreamweaver, which registered itself as an editor for PHP files. So the file will open in Dreamweaver, and you will see the script itself.

    To solve this problem, you will go into Apache’s configuration file, httpd.conf, and make a couple of edits so it knows that whenever a PHP file is requested, it needs to let the PHP application process it.

  7. In Notepad, open httpd.conf.

    Again, the default location of this file is as follows: C:\Program Files\Apache Group\Apache2\conf\http.conf.

  8. Use the Find feature to locate the first instance of the word AddType. Scroll down 15 lines or so, and locate the space between the last AddType directive (by default it probably is AddType application/x-gzip .gz .tgz) and the next block of code, which begins # AddHandler allows you to map certain file extensions.... Press Enter or Return a few times to create some space.

    The first instance is about 75 percent of the way down the file, and it appears in a line that begins, #AddType allows you to add to or override the MIME configuration....

  9. In that space, type the following code:
    ScriptAlias /php/ "c:/php/"
    AddType application/x-httpd-php .php .php5
    Action application/x-httpd-php "/php/php-cgi.exe"
    

    These lines inform Apache that if it encounters any files ending in .php or .php5 that it should pass them off to PHP, whose executable can be found at the address provided.

  10. Save and close httpd.conf.

    PHP and Apache are now configured to talk to each other, and you are almost set to go.

  11. Return to the Apache icon in the system tray and restart the server.

    Apache restarts using the new httpd.conf settings.

Setting Up Apache in Mac OS-X

Apache comes preinstalled as the default on Mac OS X and higher, so there is literally nothing to install. You do, however, have to turn it on, because by default, the Apache Web service is stopped.

  1. Open System Preferences and click the Sharing folder.

    The Sharing folder is used to control file sharing, Web services, FTP access, printer sharing, and firewalls, among other features.

  2. In the Services tab of the Sharing folder, check the Personal Web Sharing option.

    Checking this box starts Apache. That’s it!

Setting Up PHP in Mac OS X

Like Apache itself, PHP comes preinstalled in Mac OS X, so you don’t need to install it. Also like Apache, the version of PHP that ships with Mac OS X (PHP 4.3.11) is not current. You can download and install the latest and greatest version of PHP (to do so, see the Apple developer site, mentioned in the preceding note). That requires some extra work, and it is sufficient for the purposes of this book to use the shipping version. In the remaining chapters, only one line of code won’t work in this version, and it has an easy workaround that I’ll point out when the time comes.

To use PHP on Mac OS X, you need to turn it on. In contrast to turning on the Apache Web server, you can’t just click a check box, though; you need to type some code in the Apache Web server’s configuration file, httpd.conf.

  1. In the Terminal window, log in as the root user (using su) and open the file httpd.conf in a text editor.

    Typically, this file can be found in the following location: /etc/httpd/httpd.conf.

  2. Search or scroll until you see a number of lines that begin with LoadModule or #LoadModule.

    As the name suggests, LoadModule causes extensions, including PHP, to be activated on the Apache server.

    The hash mark (#) that precedes some lines signals a comment. That is, the interpreter ignores any lines preceded by a hash mark.

  3. Look through the LoadModule section for the following line of code. If it exists and is preceded with a # sign, remove that sign. If no such line exists, type it in at the end of the LoadModule block, exactly as you see below.
    LoadModule php4_module libexec/httpd/libphp4.so
    

    This is one of two lines in httpd.conf that activates PHP in Apache.

  4. Shortly after the block of LoadModule lines, you’ll see a block of lines that all begin AddModule or #AddModule. The following line of code needs to appear in that block. It may already be there, commented; if so, remove the comment (#) preceding it. If the line of code is not there at all, then type it at the end of the AddModule block.
    AddModule mod_php4.c
    

    Between this and the LoadModule line entered in the previous step, we have given Apache enough information to activate PHP.

  5. Search or scroll much later in the file, until you see a group of AddType statements.

    The AddType group is a little harder to spot, but you can find it by searching for the comment that precedes it, as shown in the following screen shot.

  6. Verify that the following two lines of code appear in this AddType group, and that they are not commented. If they are not there, type them in yourself.
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    

    These two lines tell Apache to treat all files ending with the .php extension as PHP files, and likewise to treat all .phps files as PHP source files. PHP source files actually display the PHP code in the HTML document, complete with code coloring, rather than interpreting and outputting them. This option is useful for debugging.

  7. Save and close httpd.conf.

    You have now configured Apache to work with PHP. Before you can use it, though, you must restart Apache.

  8. To restart Apache, return to the Services tab of the System Preferences, select Personal Web Sharing from the list, click the Stop button, and then click it again once it becomes the Start button.

    Your system is now configured to process and serve PHP files.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020