Archive for the ‘Tutorials’ Category

Setting up Apache and PHP

January 31, 2008

Glossary:

  1. Introduction
  2. Setting up a directory
  3. Downloading and Installing Apache
  4. How to create an account w/ hostname on dyndns.com
  5. Downloading and Installing PHP
  6. Configuring Apache
  7. Downloads
  8. Final Words

0. Introduction:

I’ve noticed that there have been a good amount of people curious as to create a free web server. This tutorial will teach you how to set up your own local web server. I will try my best to write this tutorial with the beginner in mind, making easy to follow steps.

Many people are under the assumption that they need to pay for web hosting in order to learn and test their HTML and PHP scripts.

Agenda – In this tutorial I will show you how to install Apache HTML server and PHP onto windows xp, as well as setting up a nice working environment.

1. Setting up a directory:

First, you will need to choose a place to put Apache and PHP or any other programs in the future. You will want to choose path with lots of space to avoid future problems. Possibly consider a second partition on your hard drive. It does not matter what you name the web server directory’s folder, but i would suggest something along the lines of ‘WebServer’ to avoid confusion. In your web server folder add two more folders and name them “Apache” and “PHP”

2a. Downloading and Installing Apache:

First, download the windows binary files from here – Download – The Apache HTTP Server Project (note: i will add the apache installer along with the php installer at the end of this tutorial)

Once you have downloaded the binary files we are ready to install. Double click on the installer, read the agreements and data, click next and agree to everything on each screen. Once you reach the fourth screen, you will see this:

Here is an explanation of what each of these mean, taken from Using Apache with Microsoft Windows – Apache HTTP Server

Network Domain. Enter the DNS domain in which your server is or will be registered in. For example, if your server’s full DNS name is server.mydomain.net, you would type mydomain.net here.

Server Name. Your server’s full DNS name. From the example above, you would type server.mydomain.net here.

Administrator’s Email Address. Enter the server administrator’s or webmaster’s email address here. This address will be displayed along with error messages to the client by default.

For whom to install Apache Select for All Users, on Port 80, as a Service – Recommended if you’d like your new Apache to listen at port 80 for incoming traffic. It will run as a service (that is, Apache will run even if no one is logged in on the server at the moment) Select only for the Current User, on Port 8080, when started Manually if you’d like to install Apache for your personal experimenting or if you already have another WWW server running on port 80.

You now have a couple options:

- if you plan on using apache just for testing scripts then you can enter localhost for network domain and server name and anything you would like for the email.

- if you would like to use apache as a local area network server, you should set your router to assign the computer you are installing Apache onto have a static LAN ip (if it doesn’t already have one. for more information on static LAN ip’s check out this link – LAN101 – The TCP/IP Network : Hands-on How-To)

- If you would like your server to be accessible through the internet, you will need to have a static IP or set up a domain name server (DNS). If you do not have a static IP, you can set up your computer to have a static local IP stated previously, and use a Dynamic DNS service (Dynamic DNS – Wikipedia, the free encyclopedia) such as DynDNS — Dynamic DNS, E-Mail Delivery and Other Services to keep your IP up to date. If you own a good router, it will allow you to automaticly update the dynamic DNS when the router gets an IP address. You will have to set up port forwarding so that all traffic is forwarded to port 80 on your computer static IP address. Enter the DNS domain name you have set up for network domain and server name, and a suitable email for the third entry. Choose the option “for All users, on Port 80, as a service — Recommended.” and press next.

For people not sure on how to use DynDNS — Dynamic DNS, E-Mail Delivery and Other Services i will give a brief explination on how to set it up.

2b. How to create an account w/ hostname on dyndns.com:

1) create an account using a real email and a easy to remember user name and password.

2) once logged in, under “account>my services>add host service” choose a host name, tick the checkbox for wildcard, for service type choose “host with ip address”, and for ip address click ‘”use auto detected IP address” then create your host.

Once Apache is finished installing you should be prompted with windows security alert from windows firewall asking you if you would like to continue blocking or unblock Apache, choose to unblock it.

This happens because Apache is attempting to open a port on your computer and asks if this is acceptable with you.

Now, open up your web browser (make sure Apache is running), you can check in your icon tray to see if Apache is running or not. Type in the name of your server (localhost, domain name that you made with dyndns.com, or your IP address). If all has went well then you should see a blank page that says “it works!” in big bold text.

If everything is working, you are ready to move on. If not, its hard to tell; there can be multiple issues. Your best bet would be to uninstall Apache and restart the process.

3. Downloading and Installing PHP:

You may want to consider reading the PHP documentation for a background. May come in handy if you get stuck or are confused about something. PHP: Installation on Windows systems – Manual

Start off by going to the php.net download section. (keep in mind that i will add alternate downloads at the end of the article if anyone has some issues) PHP: Downloads

Go for the PHP 5.x installer under the “Windows Binaries” section. Place the installer into your PHP folder you made at the beginning of this tutorial. For PHP to work with Apache from any command prompt, you will need to add PHP to the environment variable. To do this, right click on “My Computer” and select “Properties.” Select the advance tab in the window that pops up and click on the “environment Variable” button near the bottom of the window.

In the window, select path variable line from the “System Variable” menu, and click edit. At the end of the variable value textbox, type a semicolon (;) then the FULL path of your PHP folder. If you followed my tutorial exactly, then your path should be “;C:\WebServer\PHP” or “;D:\WebServer\PHP” depending on where you put it.

Hit OK in all windows until each one is closed then restart your computer. Pick back up here once you have restarted.

Now that you have restarted you can test to see if PHP was successfully installed by going to start>run>cmd then hit OK. In the command prompt enter in “php -v” and hit Enter. If you updated the environment variable correctly you should see something about the PHP version along with the company.

Although, if you get an error like “php is not recognized as an internal or external command, operable program or batch file” then you did something wrong and should recheck the steps.

Now, open the php folder and search for the file “php.ini-recommended” to “php.ini” The settings are pretty much exactly what they should be at. If you plan on using Apache and PHP for testing, it would be a good idea to open up “php.ini” in a text editor such as notepad and change “display_errors = off” to “display_errors = on” and “log_errors = off” to “log_errors = on”

Make the following changes to the php.ini file:

Line 512: doc_root = C:\WebServer\Apache2\htdocs

Line 519: extension_dir = C:\WebServer\PHP

Save the php.ini file and close it. For more information on configuring PHP refer to this article – PHP: Runtime Configuration – Manual

4. Configuring Apache:

We currently have Apache and PHP working separately, but we would like them to work together. In the Apache2\conf folder open up the httpd.conf file in notepad.

1) At the top of the LoadModule section, add this line:

LoadModule php5_module “C:/WebServer/PHP/php5apache2.dll”

note: those are forward slashes, not backslashes.

2) In the AddType section, add this line:

AddType application/x-httpd-php .php .html

PHP can paraphrase other file types, i’ve just added PHP and HTML so you can include PHP into .html documents.

3) Lastly, add this line anywhere that seems perceptible:

PHPIniDir “C:/WebServer/PHP/”

Now, save the httpd.conf file and start apache or restart it if it is already running.

5. Downloads:

note: please forgive me for using rapidshare for the links. if you would like me to use another site please request it and i will glady upload to that site.

Apache HTML Server:
RapidShare: 1-Click Webhosting

PHP Binary:
RapidShare: 1-Click Webhosting

6. Final Words:

Despite the long tutorial, it was well worth it. You now have a very good environment for creating and testing websites and web applications. Hopefully this long tutorial wasn’t too painful. Enjoy your new web server. Hopefully this cleared up all the confusion that people have been having whilst trying to create a web server.

I would like to thank Apache and PHP for providing free tools to enable us to create our web servers. I would also like to thank wikipedia for giving accurate definitions on some phrases that people may not be familiar with.

Regards,

The Indian Guy