10.15.07

Install PHP + MySQL on Windows 2003 (IIS + PHP + MySQL)

Posted in Programming at by chenty

1. Why Windows Server 2003 and IIS?
The optimal environment for running PHP + MySQL application is a Linux server running Apache Web Server. It requires minimal setup and assures the best performance and reliability. However, what if .NET applications are required to run on the same server? I guess the only option is to install PHP + MySQL on a windows server (Why not .NET on Linux? No, the other way has been tried, but it did not work well).

On a windows server, Apache Web Server is still the best choice to run PHP + MySQL combo, but unfortunately Apache and IIS cannot share the same port, which means only one of them will run on port 80 (even you bind 100 IP addresses on the same server). Therefore, if you want to run both PHP and .NET applications on the same server, your best bet will be IIS + PHP + MySQL.

2. Sample environment
(1) Windows Server 2003 SP2 + IIS 6.0
(2) PHP 5.2.4
(3) MySQL 5.1.22

3. Download Files
(1) PHP: http://www.php.net/downloads.php
There are several PHP packages for Windows. Make sure you download “Zip Package”. DO NOT DOWNLOAD “installer”! Too many people have problems with “installer” version and that package messes up too much.

(2) MySQL: http://dev.mysql.com/downloads/
As the time I wrote this guide, MySQL 6.0 is out as a public preview. I did not use it because there is no manual. I recommend to use a stable release for production servers.

4. Install MySQL
(1) Most tutorials online installs as the order: MySQL -> Apache -> PHP because you can test MySQL alone, and when you install PHP you can directly modify php.ini for MySQL (otherwise, you have to modify php.ini again after you install MySQL).

(2) I downloaded installer version of MySQL. Just run setup.exe and it will do all the work for you. I am lazy to write detail installation process because I have not had any problem. Everything was straight forward. If you had run in a problem, check official documentation:
http://dev.mysql.com/doc/refman/5.1/en/installing.html

(3) Test MySQL server in command-line:
a) Start -> Run -> cmd
b) mysql -u root -p
c) input your password
d) if it connects to the server successfully, you are in good shape. type “show databases;” to display all default databases. If you see the list, you are ready to move on.

5. Install PHP
(1) Unzip all the files to “C:\php” (I unzipped to “C:\Program Files\PHP” but I have seen people having problem with file path that contains “space”, so most tutorial uses “C:\php”.

(2) create a folder you want to run PHP applications, e.g. C:\inetpub\phproot. Most tutorial uses “wwwroot” which is the default IIS web directory, but I think it makes sense to use a different directory since I don’t want my application mess up with all asp files.

(3) Modify php.ini
php.ini is the most important configuration file. I will revisit this file later in this guide when I talk about setting up email and file upload. For now, we need to modify:
a) “doc_root”: set this as “c:\inetpub\phproot”;
b) “extension_dir”: set this as “c:\php\ext”;
c) uncomment lines with mysql extensions (e.g. remove “;” before “extension=php_mysql.dll”);
d) change default exection timeout, e.g. “max_execution_time = 3600″ (Note: this is very important! I have rarely seen tutorial mentioning this parameter, but the default is 30 seconds. If you did not increase this number you will get way too many 500 error and take you forever to figure out “what’s wrong with my code?” (there is nothing wrong with your code, but the server will return an error after 30 seconds).

(4) Setup IIS (using “IIS Services Manager”):
a) expand “Web Service Extensions”;
b) Action -> add a new service extension;
c) enter “PHP” as name and click “Add…”, and select “c:\php\php5isapi.dll”. (Note: many earlier tutorials recommend to use CGI mode because ISAPI is not stable for PHP. I believe things have been improved dramatically now and most new tutorials prefer ISAPI to CGI);
d) Create a new website. Set “c:\inetpub\phproot” as home directory;
e) In the same “Home Directory” tab, click “Configuration…”;
f) In the new dialog window, click “Add…”;
g) Set “c:\php\php5isapi.dll” as executable and “.php” as extension. Make sure “Script engine” option is checked.
h) Now restart your IIS Server (open “Services” in Control Panel, and restart “World Wide Web Publishing Service”)

(5) Run a test script for php setting:
Create a new file in “c:\inetpub\phproot” and name it “test.php”. Open it with notepad and type:

  1. < ?php
  2. ?>

Run this script, e.g. type “http://localhost/test.php” in a web browser. If you see the script display a whole page of server configuration like this one, that means your server is supporting PHP now. Scroll down a little bit in the page, and make sure it has sections for “mysql” (In the sample I provided above, it has a section for mysql).

Note: If you did not get the expected result unfortunately, there are many good posts on troubleshooting. Troubleshooting is beyond the scope of my guide.

============================================

Now you got a good server running PHP + MySQL under IIS, and most tutorial will stop here. However, it is far from over for a production server! In my case, the application I run requires:
(1) backward compatibility of old instruction sepraration;
(2) sending email using mail();
(3) users can upload files.

If you just follow my guide above, non of these features will function properly!

(1) Proper instruction separation for PHP should use “< ?php" as open tag and "?>” as close tag. However, many legacy code has other format, e.g. “< ?" to open and "?>” to close. The best practice is to modify the code for standard format! Nonetheless, PHP can still support “< ?" opening by set "short_open_tag=1" in php.ini.

(2) PHP supports file upload from client to server, but PHP file upload feature is not enabled under IIS by default.

You need setup two folders on your server. One is the folder where you want the upload files to store, and the other one is the folder for temporary upload files (PHP handles upload in two separate steps: it first uploads the file to the temp folder, and then use "move_uploaded_file()" function to move from temp folder to final destination. For a good example, check official doc http://ca3.php.net/features.file-upload).

Here is an example on setup procedures:
a) create two folders "c:\inetpub\phproot\uploads" and "c:\inetpub\phproot\upload_temp";
b) In "IIS Service Manager", right click on the folder and click "Properties". In "Directory" tab, make sure you enable "write" permission for both folders;
c) Open php.ini, uncomment and edit this line: upload_tmp_dir = "C:\inetpub\phproot\upload_temp\";
d) restart the service and file upload feature should be enabled.

(3) If you google "php mail IIS", you find many many posts from people who have trouble to make mail() function work. I have talked about this issue in another guide I wrote.

10.09.07

How to make PHP mail () function work on Windows

Posted in Programming at by chenty

mail() is a useful and powerful function in PHP, but many developers got problems to make mail() work properly on Windows. Many these problems were related to headers and different line feed formats between Windows and Linux. However, this post does not address these problems. The problem I encountered last week was how to make mail() function to send even the simplest mail on Windows.

The server setup:
Windows Server 2003 Standard Edition (SP2)
Apache 2.2
PHP 5.2.4
MySQL 5.1

run a test script with phpinfo() to confirm the server settings. There were several lines that relate to mail():
smtp = localhost
smtp_port = 25
sendmail_from = yourname@yourdomain.com
sendmail_path =

Windows and Linux handle mail() differently. Linux uses “sendmail” whereas Windows employs SMTP. You can refer to PHP manual for more information:
http://ca3.php.net/function.mail

In this post, I will only cover Windows. Windows Server 2003 comes with IIS 6.0, which supports SMTP. If you are a .NET programmer, you will find mail features working out of box. Unfortunately, this was not the case for PHP. You can try to run the following script to check if mail() works:

  1. if(mail(‘test@test.com’,‘test subject’,‘test message’))
  2. {
  3.   echo(‘ok’);
  4. }
  5. else
  6. {
  7.   echo(‘not ok’);
  8. }

If the script returns ‘not ok’ when you run the code, that means mail() function could not send the mail through SMTP. So what was the problem? A good way to demonstrate the problem is run an email client (e.g. Outlook Express) and setup a new account using “localhost” as SMTP. When you try to send a new message, you will receive an error similar to this one:

Server Response: ‘550 5.7.1 Unable to relay for yourname@yourdomain.com’, Port: 25, Secure(SSL): No, Server Error: 5500

This is the reason your PHP script cannot send the mail: by default, IIS restricts all SMTP relay! In order to solve the problems, you need to grant access to your server IP:
1. Open “IIS Manager”;
2. Right click “Default SMTP Virtual Server” and select “Properties”;
3. In “Access” Tab, click “Relay…”;
4. Check “only the list below” and click “Add” to add the server IP.

Now, if you open Outlook Express and create a new message, the mail will be sent out properly. Now, run the test script I wrote above, the script should return ‘ok’ and you should receive the mail in your mailbox amazingly!