Whatterz


Configuring PHP with Apache on Windows Vista

by Simon. Average Reading Time: about 2 minutes.

Pre-requisites:

  1. Follow my instructions on installing Apache on Windows Vista
  2. And then follow my instructions on installing PHP on Windows Vista

Installing PHP with Apache on Windows Vista is a relatively simple task until you try an configure the settings in php.ini. Problems can occur and stem from the fact that when you install PHP and edit the php.ini file, you need to not only be logged in as Administrator, but run the installer and Notepad text editor as Administrator. The php.ini file also needs to be located in the Windows directory.

Below is an outline of the steps to get your PHP installation running and configured correctly.

  1. Run Notepad as Administrator. Go to All Programs > Accessories Right-click “Notepad” and select “Run as Administrator”. Open the php.ini file, in my case:
    C:\PHP5\php.ini
  2. Edit the php.ini file as necessary, e.g.:
    extension_dir = "C:\PHP5\ext"
    upload_max_filesize = 16M
    upload_tmp_dir = "C:\PHP5\upload"
    session.save_path = "C:\PHP5\session"
  3. Enable the extensions you need by deleting the semi-colon at the beginning of the line. For example, you will commonly need the following MySQL extensions:
    extension = php_mysql.dll
    extension = php_mysqli.dll
  4. Save the edited php.ini file into your Windows directory. This is very important as Vista will not read the changes but will refer to the default settings.
  5. Check that the PHP directory has been added to your computers “Environment Variables”. Click Start, right-click on Computer, select Properties > Advanced > Environment Variables. Click on PATH and select Edit. If the PHP path is there, in my case “C:\PHP5\”, all is well, otherwise add the PHP path.
    • System Properties:

      Vista System Properties

    • Environmental Variables

      Vista Environment Variables

    • Edit Environmental Variable – PATH

      Vista Edit Environmental Variable PATH

  6. Open your Apache configuration file (httpd.conf). Ensure the following lines are present (at the bottom of the file):
    LoadModule php5_module "C:\PHP5\php5apache2_2.dll"
    AddType application/x-httpd-php .php
    AcceptPathInfo on
    PHPIniDir "C:\Windows"

    If there are additional LoadModule lines, remove them, leaving only the one that relates to your version of Apache.

  7. Find the DirectoryIndex in the Apache config and append index.php, e.g.:
    <ifmodule dir_module>
    	DirectoryIndex index.html index.php
    </ifmodule>
  8. Restart the Apache service.
  9. Create a index.php file in your webroot and add the following lines:
    <?php phpinfo(); ?>
  10. Navigate to the file (e.g. http://localhost/index.php) and you will be presented with all the PHP and server settings. You’re now good to go.

This article has been tagged

, , , , , , , , , ,

Other articles I recommend

PHP.ini Permission Problems on Windows Vista

Installing PHP is a relatively simple task one would think. Indeed it is simple, but configuring the php.ini isn’t; at least not so on Windows Vista! It is infuriating when such a relatively simple task is made inordinately complicated because of the nuances of Vista permissions. What started out as a 5 minute task took a significant number of hours searching for a suitable answer on Google, and not only by myself.

Configuring Your First Local Apache Website

Apache is controlled by a series of configuration files but the one we will be dealing with here is httpd.conf. This file contains instructions on how Apache should run. Several companies offer GUI-based Apache front-ends, but it’s easier to edit the configuration files by hand.

Installing Apache on Vista

Following the purchase of a spangly new Toshiba laptop running Vista, and not one for making life simple, I decided not to run my development environment on IIS7, but rather, I wanted to install the latest version of Apache. The installation process was not a trivial matter!

  • http://www.simonwhatley.co.uk Simon

    If you use the PHP 5 installer, you’ll notice that it uses the wrong type of slash on Windows!

    For example, change “C:/PHP5/” to “C:\PHP5\” and everything works great.

  • http://blahblah.com Mike

    could you please explain more on

    Enable the extensions you need by deleting the semi-colon at the beginning of the line. For example, you will commonly need the following MySQL extensions:
    extension = php_mysql.dll
    extension = php_mysqli.dll

    because i cant find it, otherwise everything else is good.

  • http://denacht.blogspot.com Jesse

    Thank you so much. I was beginning to see another two hours ahead, but your instructions spared me of it. thanks :-)

  • http://www.alanweb.org Alan

    I just want to point out that Apache v.2.2.8 keeped crashing with PHP 5.2.6 😉 Install earlier version of PHP and thing will sing like a bird..

  • sergio

    what is my webroot or were is it.

  • http://www.simonwhatley.co.uk Simon

    @sergio your webroot is wherever you set it up in Apache.

    Mine happens to be C:\WebRoot.

    The default for Apache on Windows is:

    C:\Program Files\Apache Software Foundation\Apache2.2\htdocs

    If you want to amend the location, look in the httpd.conf file for DocumentRoot and change it to the location of your webroot. You will also need to update the following code

    <Directory "[YOUR WEBROOT]">
    [...some settings]
    </Directory>

  • sergio

    Hi again all is set, the apache works when a put http://localhost/ it returns “it works”. But my php when a put http://localhost/index.php, it returns an totally empty blank page, also when a create a html file, it ignores my php code ( writing what is in between the start stop php tags as just as it is written, this way a can only create html. Thats not good for me because i need it to practice to pass exams at uni. Of course there is always linux but a totaly dont like it due to its poor user friendly methods, and also have problems installing php.

    By the way thanks allot for your help and hope that you can take me out of this whole.

  • http://www.simonwhatley.co.uk Simon

    @sergio you need to make sure that the index.php file is referenced in the Apache configuration file:

    <IfModule dir_module>
    DirectoryIndex index.html index.htm index.cfm index.php
    </IfModule>
    

  • sergio

    its still the same thing, if that line index.php is not present as shown above it occurs an error on the webpage, but in my case using what is shown above there is no error just a blank page I am starting to think that the php install must be damaged, going to download again and retry.

    Thanks for your atention, ill give feedback when finished.

  • http://www.simonwhatley.co.uk Simon

    @sergio the Apache httpd.conf will need the php configuration lines. You will know if PHP is correctly installed because, when you open the Apache Services Monitor screen (double click on the feather icon in the system tray) you see PHP/5.2.x written in the monitor’s status bar.

    You *may* need to locate the php.ini file in the Windows directory rather than the PHP directory, although this is not always the case!

    (My work machine has the php.ini in the PHP directory, whilst my home machine would only work with it in the Windows directory. Both are Vista Business edition)

    Try using PHP 5.2.5, not the latest release. Don’t install all the extensions, only those that are needed; commonly these are the MSSQL, MySQL and GD extensions.

    Once you’ve installed a couple of extensions you can go back afterwards to install the others, since they will all be on your server. To add extra extensions is simply a case of editing the php.ini file with the extension’s name eg. extension=php_gd2.dll;

    Every change requires an Apache service restart.

  • sergio

    Hi again good afternoon, just came to give you the update of the installation, I gave up so what i did, searching in the net i tried out many other programs some just totally crashed, until i came upon php designer 2007 free for non comercial use, after install just had to direct it to the php-win.exe file in its program options, of the prevous php.msi setup just as you explained. It is so easy to use that it gives me the creaps after having to learn c language, its something to forget.

    thanks for the tips especialy that windows command line to install msi file (msiexec /i filename.msi), and of course for that experience on playing around with the config file and the php ini file.

    Compliments

    sérgio santos

  • sergio

    Hi again just not to show that i am not a quitter i retried and found where i mistake, it was on the backslash in the config file of the apache. Now it works just has you said.

  • http://www.simonwhatley.co.uk Simon

    @sergio good to see that your perseverance has been rewarded.

  • http://n/a jeff

    I am having a similar problem to what sergio experienced. PHP is installed. I have confirmed via command line prompts that it is running. When I look at the Apache monitor I see the PHP 5.2.6.

    When I run the test page with a phpinfo(); command I get nothing back. I have my php.ini file in the windows and Program Files\PHP directories.

    Any help would be appreciated.

  • http://www.simonwhatley.co.uk Simon

    @Jeff the php.ini file need only be in one directory, either the PHP or Windows directory. I would favour the PHP directory.

    Make sure the path in your httpd.conf directory points to the correct file. (Details can be found in the above post).

    Make sure the index.php file, in which you have the <?php phpinfo(); ?> is in the correct location. (See my comments above).

    Make sure your PHP install has the minimum installed extensions required by your apps. These tend to be the MySQL and GD libraries. You can always add/uncomment additional libraries in the future if and when needed.

  • http://n/a jeff

    Thanks. I am now able to run a file with the .php extension. When I try to run the following I just get the HTML portion. The PHP code does not seem to execute.

    Using PHP and HTML Together

    Using PHP & HTML Together
    Here is your PHP info:

  • jeff

    The system parsed the code…

    <html>
    <head>
    <title>Using PHP and HTML Together</title>
    </head>
    <body>
    <h1> Using PHP &amp; HTML Together </h1>
    <p>Here is your PHP info:</p>
    <?php phpinfo(); ?>
    </body>
    </html>
    

  • http://n/a jeff

    The code works and the php output is displayed only if the file suffix is php and the file type is .php. If it is .html the php code does not execute. It isn’t a big deal, but if you know what is going on and how to correct it it would be great.

    Thanks for all your help. It has saved me countless hours!

  • http://www.simonwhatley.co.uk Simon

    @Jeff the .php extension tells the webserver that PHP needs to render the content on the page, even if it doesn’t contain any PHP code. If you simply name the files with the .html extension, PHP will not render the page so any code wrapped in <?php ?> will be rendered simply as text.

  • Jason Smith

    Is anyone else having trouble with apache ignoring changes to httpd.conf? I can add a line randomly in the file “THIS ISN’T SUPPOSED TO BE HERE” and it will fail the test apache config thing, but apache completely ignores it. I’ve changed Documentroot with no effect as well so it’s not as if I’m just getting subtleties.

    Is there some way to verify that it is using the conf file at all? I get the PHP 5 byline in the status bar of monitor, so I know something is working right…

  • http://chawklit.com Jeff K

    Hi Simon,

    Thanks for all your help in installing Apache and PHP. I just purchased a book in PHP and its going to be my new hobby. My question that I have is when I added the index.php file into my webroot, the only display I received was instead of PHP and server settings. What did I do wrong along the way?

  • http://www.simonwhatley.co.uk Simon

    @jeffk it you have put the php_info function into the index.php page you will receive a display of the server settings. If you put html and text it’ll show the html and text and so on.

  • George

    Exactly the same problem as Sergio and Jeff. I had tried installing XAMPP which worked just fine on Vista, but then some friends adviced me to rather install APACHE, PHP and MySQL separately. Apache alone worked just fine, but when running the php msi following these instructions, it just doesn’t work for php. Moreover, I have problems if I want to stop or restart apache, it crashes.

    I realised the PHP installer adds the needed lines:
    LoadModule php5_module “C:\PHP5\php5apache2_2.dll”
    PHPIniDir “C:\Windows”
    (together with the same lines for older versions of apache, which I now removed). I also added:
    AddType application/x-httpd-php .php
    AcceptPathInfo on

    The Apache Monitor shows that I have both Apache/2.2.9 (Win32) PHP/5.2.6.

    It displays the html files, but it does not work for php extensions :(

    btw, why was it necessary to be logged in as admin, disable UAC and STILL run via command prompt as admin? And disable Antivirus and Windows Defender?

    Thanks!

  • http://www.simonwhatley.co.uk Simon

    @George You need to make sure the PHP extensions are installed but disabled in the php.ini file. Only enable those that you require, e.g. the database extensions. For some quirky reason, Apache *always* breaks if you enable all extensions.

    You need to disable anything that will conflict the smooth installation of PHP and Apache. So UAC and Antivirus etc have to be disabled. Even though you log in as administrator, this isn’t entirely the case thanks to Vista’s security model.

  • George

    Thank you very much Simon. Congrats for your enthusiasm to keep watching and helping out. I thought convenient to tell you how did that work out for me, so that you can help others in the future with my perhaps very common error:

    When installing PHP from the msi, just because I am still a newbie with this installation, I didn’t want to have future problems just because of saving space on Disk, so I decided to INSTALL ALL THE FEATURES, ALL THE EXTENSIONS, ALL THE EXTRAS: sacrificing disk space to avoid future nightmares just cause I forgot to include an extension. Well, the medicine was being worse than the disease itself, this was the only reason that it was not working for weeks, and I was to give up today and go back to my friendly XAMPP package.

    Moreover, I am on Vista, APACHE 2.2.9, PHP 5.2.6, and if you allow the msi to install php (this time I just selected extensions php_mysql.dll, php_mysqli.dll, the MSSQL dll one I may need, and GD2 that I don’t know what it is but I read you said we may need it in a comment. And as extras just the php documentation. Well, just with the default PHP installation and without any further tweak like modifying the httpd.conf or the php.ini at all, or even moving the php.ini into windows directory, it worked just fine! (the msi installer adds the necessary changes on the httpd.conf for you:

    #BEGIN PHP INSTALLER EDITS – REMOVE ONLY ON UNINSTALL
    PHPIniDir “C:/PHP/”
    LoadModule php5_module “C:/PHP/php5apache2_2.dll”
    #END PHP INSTALLER EDITS – REMOVE ONLY ON UNINSTALL

    Notice I left php.ini into my “C:/PHP” installation folder. Also, notice that it does work without:
    AddType application/x-httpd-php .php
    AcceptPathInfo on

    (not saying it is recommended, I’m not that far, but it works with the default installer). Also, the installer adds for you the environmental variables

    Simon, many thanks for your help, and hope my problem may help others:
    newbies just sacrifice disk space to avoid future headaches and they chose to install the whole feature, which makes php to not work, and apache to crash as you restart it or try to stop it

  • Raghu

    Hi Simon,
    I did the modification of what you said but couldn’t succeed in it. I could see .html files but when I open .php files it says “HTTP 500 Internal server error” . I am working on windows vista business edition and I installed apache_2.2.9-win32-x86-openssl-0.9.8h-r2 and php-5.2.6-win32-installer. Can you help in this?

    Thanks,
    Raghu

  • Pingback: Configuring PHP with Apache on Windows Vista « Mandar Oak()

  • John Parsons

    Hello Simon,

    I am having similar problems as the rest.

    When I try to run the index.php file all I get is the following text output:

    Another query, may or may not be related. This line exists in phpini
    extension_dir = “C:\PHP5\ext”
    but the directory doesn’t exist. Where have I gone wrong, are there no extensions?

    Thanks

    John

  • http://www.simonwhatley.co.uk Simon

    @john It looks like you chose not to install any extensions. Therefore, you can simply create the empty directory as you normally would in Windows Explorer. You can also download the extensions separately from the PHP website and put them into the ext directory. If you then want to load the extensions, you will need to add the required information to your php.ini file.

  • John Parsons

    Thanks for the swift reply,

    I used the installer and installed the lot (I thought). Which extensions must I load to get me up and running?

    John

  • John Parsons

    Thanks for the quick reply Simon,

    I followed your installation guide and thought that I had installed everything.

    Which extensions do I need to get going.

    Another question, where does the php.exe file get called from, could that be an issue?

    John

  • Stuart

    Hi Simon,

    Sorry, but I’m getting the same problem as a few others here with phpinfo.php giving me just a blank screen. Apache Monitor shows that everything [Apache 2.2.10 (Win32) PHP5.2.6] is green for go.

    I’m guessing that my problem is with forward/backward slashes, and your first comment on this page says quite clearly that one should use backslashes instead of forward slashes. But in which file? php.ini or httpd.conf?

    At the start of the httpd.conf file, there’s some text about slashes that would seem, in part, to contradict what you’ve written:
    # NOTE: Where filenames are specified, you must use forward slashes
    # instead of backslashes (e.g., “c:/apache” instead of “c:\apache”).

    Could you possibly explain when I should use forward slashes and when I should use back slashes?

  • http://www.simonwhatley.co.uk Simon

    @john I have the following extensions installed:

    [PHP_MSQL]
    extension=php_msql.dll
    [PHP_MSSQL]
    extension=php_mssql.dll
    [PHP_MYSQL]
    extension=php_mysql.dll
    [PHP_MYSQLI]
    extension=php_mysqli.dll
    [PHP_PDO_MSSQL]
    extension=php_pdo_mssql.dll
    [PHP_PDO_MYSQL]
    extension=php_pdo_mysql.dll
    [PHP_GD2]
    extension=php_gd2.dll

    They are essentially just database connectors and an image manipulation library connector (GD2).

  • http://www.simonwhatley.co.uk Simon

    @stuart The back-slash forward-slash issue seems to be a little inconsistent as I now have installations working with both!

    The blank screen could relate to where Apache is looking for your webroot, rather than PHP not functioning. Make sure the httpd.conf file has the webroot location correctly set.

  • John Parsons

    Thanks Simon,

    It’s all working. Obviously choosing to add all extensions from the installer (being too lazy to choose just what I needed) creates problems. All is well. Thanks for the straight forward instructions.

    John

  • Stuart

    Simon,

    I’ve checked the webroot location on the conf file and it would seem to be fine. Also, if I load http://localhost/index.html in my browser, I get the message “It Works!”, which suggests that Apache itself is OK.

    Here’s the closing code from my conf file. Does anything seem wrong to you? What about the need for, or the need to omit, closing slashes, such as “C:/Windows/” instead of “C:/Windows”?

    #BEGIN PHP INSTALLER EDITS – REMOVE ONLY ON UNINSTALL

    LoadModule php5_module “C:/Program Files/PHP/php5apache2_2.dll”

    ScriptAlias /php/ “C:/Program Files/PHP/”
    Action application/x-httpd-php “C:/Program Files/PHP/php-cgi.exe”

    AddType application/x-httpd-php .php
    AcceptPathInfo on
    PHPIniDir “C:/Windows/”
    #PHPIniDir “C:/Program Files/PHP/”

    #END PHP INSTALLER EDITS – REMOVE ONLY ON UNINSTALL

  • Richard Gehring

    I couldn’t get php file to parse php code. I had to remove the extra space in you php example. <?php instead of < ?php. Thanks for making a complex procedure a little simpler.

    Rich

  • michal

    Hi Simon –

    you seem to have the answers for everyone else so maybe you can help me – I had to switch to Windows Vista. I installed apache (“It works!”) and php (the test worked when I pasted the localhost/index.php in my browser – gave me all the info). Now here’s my problem the .php extension doesn’t work – when I save a file with the .php extension I don’t get that smaller than html icon and if i click on it windows have no idea what i want.

    any advice?

  • http://www.simonwhatley.co.uk Simon

    @michal, If you need to edit the php file, you’ll need to right-click on the file name, select edit and choose your preferred editing tool. Alternatively, when in your editing software, open the file using the standard method for the tool, i.e. File — Open.

    To actually view the file, you can’t simply double-click it, you’ll need to browse to it in, for example, Firefox or Internet Explorer.

  • http://www.marylayton.net Mary

    Hi Simon,

    I seem to have got Apache and PHP cooperating on my Vista-driven laptop, but would like to connect to a web-based database (the one that will be used when I upload the files to my ‘live’ site) when editing php pages on the localhost. I have tried to indicate the ‘mysql.default_host =’ (as well as the _user, and _password variables) in the php.ini to point to my sql server (ie: http://mysql.mywebsite.com) but still get the ol’ Fatal error: Call to undefined function mysql_connect(). Could the default port setting under the mysqli heading in the php.ini be affecting my abilities to point to my live database? Are there special rules/commands/workarounds I need to be looking for in order to edit files on a localhost while drawing info from a web-based mysql database? I have installed Apache 2.2, PHP 5, and MySql 5.1; the ‘live’ database is MySql Server Version 4.1.16-standard-log. My coding/technical skill level: just enough to be dangerous and break things. 😛

  • http://www.simonwhatley.co.uk Simon

    @mary The mysql_connect() error could be related to the missing DLL file in your PHP installation. Look under [path to php install]\ext to see if the php_mysql.dll and php_mysqli.dll files are present (if not, go to the PHP website and download them manually). You’ll also need to make sure that the extensions are added into your PHP.ini file. You can check what PHP has installed by creating and running a simple PHP file, including <?php phpinfo(); ?>.

    To add the extensions, you need to make sure the following exists/is uncommented:

    extension_dir = “c:\php\ext”

    Where “c:\php\ext” is the path to your PHP extensions directory (change if needed).

    You also need to add the following to PHP.ini (if not already there):

    extension=php_mysql.dll
    extension=php_mysqli.dll

    Another gotcha could be that the 3rd-party database provider doesn’t allow remote connections to the database or may have changed the default port through which connections are made.

  • http://www.marylayton.net Mary

    Bingo! It wound up being an incorrect path (C:\program files\php… when it should have been C:\php…), I noticed the discrepancy when checking the bits you mentioned. Thanks for pointing me in the right direction, Simon!

  • ANAND

    Hi,

    I installed apache as per your directions.. Thanks so much!!! But when I install php, it doesn’t show me the option of setting Apache as the server.. I have tried working things around.. Doesn’t work.. hope you could help me

  • http://www.fotoranking.pl fotoranking.pl

    For all who can’t get the php code parsed ( seems to be not recognized as script). You have to set “short_open_tag = On” in your php.ini file.

  • sergio terroso

    Hi Simon,

    Almost everything went just fine, except for the php.ini location. I cannot copy it to C:\Windows because Apache would not start and the same happens when I refer the PHPInidir parameter to my PHP installation path (C:\Program Files\PHP\). I am currently using Windows Vista Business.

    I thought it might be something related to file permissions but I copied the php.ini file to a new directory (C:\PHPINIDIR\) and changed the PhpInidir parameter in httpd.config and again it didn’t start.

    Finally I tried disabling all extensions and EUREKA! apache started. I enabled only those extensions that I will need and all works great since then. I think it would be great if you could clarify the reasons of PHP crash when all extensions are enabled upon installation.

  • http://www.simonwhatley.co.uk Simon

    @sergio I had a similar problem with the extensions killing the PHP install. You’ll have to refer to the PHP community as to why; they’ll probably say it’s because you’re using Vista! Not helpful, but the likely answer.

    However, it does make sense to only enable those extensions that you require.

  • Mike122

    Thanks a lot Simon for your online support. I
    Just for the record there is one too many space in the start tag
    should be Cheers!

  • Abhirup10

    hi,i have done all the steps…but till now when ever i try to connect
    i have this error

    Fatal error: Call to undefined function mysql_connect() in C:abhirupphp_progtest1.php
    on line 15my total code is

    <?php
        // hostname or ip of the server (for local testing localhost should work)
        $dbServer = "localhost";

        // username and password to log onto db server (what you entered in MySQL installation)
        $dbUser = "root";
        $dbPass = "abhirup";

        // name of database (MySQL by default comes with an empty database named "test")
        $dbName = "test";

        // open server connection
        $link = mysql_connect($dbServer, $dbUser, $dbPass) or die("Could not connect to server");
        echo "Connected successfully”;

        // test database connection
        mysql_select_db($dbName) or die(“Could not select database”);
        echo “Database selected successfully”;

        // close server connection
        mysql_close($link);
    ?>