Configure and integrate PHP and MySQL classic tutorials on Windows

Source: Internet
Author: User
Tags configuration settings php and mysql php mysql php windows mysql command line root access

PHP and MySQL are a powerful combination of dynamic content. Over the years, these two products have crossed their original application stage. Now, some of the busiest websites in the world are also applying them. Although they were originally open-source software and only run on Unix/Linux, they have been able to run on windows after a considerable period of development.

In this article, I will gradually show you how to install and configure these products in a Windows environment. For some of you, the information provided in this article may be too simple, but this article aims to completeArticleProvide step-by-step guidance on the objectives proposed by the title.

This special software combination is generally referred to as WIMP (Windows/IIS/MySQL/PHP). It is similar to lamp (Linux/Apache/MySQL/PHP) in the Linux field ).

My lab system runs 64-bit Windows Server 2003 Enterprise Edition.

Step 1: in Windows Server 2003 r2Install application server on the SystemRole

To illustrate this example, I will use IIS as a network server. You can also choose a combination of PHP, MySQL, and Apache without IIS, but I will not use them in this example.

To install the application server role on the system, click "Start Menu" and select "Manage Server ". In the Management Server main window, click "add or delete a role. (FigureA)


Install the application server role on the server so that it can be used as the website host.

Windows searches for existing roles on the server and provides a complete list of roles for installation. (FigureB)

Figure B

Application Server roles include IIS and

During server role installation, the system may require you to insert a Windows Server disc. The required files are stored in CD 1.

After the installation is complete, the system updates the Management Server window to reflect the added roles.

Step 2: Download PHPAnd MySQL

At this time, the server is ready to support web pages. However, since PHP or MySQL has not been installed, the webpage is still static. Before installing PHP and MySQL, you must download their windows version from their respective websites.


First, describe how to install MySQL. At the end of this article, MySQL 5.1 is still in the testing stage, and MySQL 5.0 is widely used. Even if I want to install MySQL on 64-bit windows, I still want to download its 32-bit version. After all, PHP needs to be installed on the same server, but it is not very common on 64-bit hardware, which means it must also use 32-bit IIS, so there is no way to fully implement 64-bit. MySQL has three versions: Excellent version, full version, or no installation version.

    • Excellent Edition: The essence version is an MSI Installation File designed to meet the needs of most users.
    • Full Version: As you expected, the full version includes more components than the excellent version, including a set of evaluation tools.
    • No installation required: This version is the same as the full version, but does not contain the Configuration Wizard or installer. You must install it manually.

In this article, install the essence edition. In this case, I name the downloaded fileMysql-essential-5.0.24a-win32.msiAnd save it on the desktop.


To increase the dynamic effect of web pages, especially when you want to extract information from databases such as MySQL, it is necessary to use languages such as PHP. PHP has two different versions: 4. x series and 5. x series. If you want to use scripts in the old website, you may need to use PHP 4.x series because the 5.x series version has been modified. Because I want to build a brand new website, I use the latest version. At the end of this article, the latest version is 5.1.6.

The download file name

Step 3: Install MySQL

The installation process is quite simple, but I provide readers with one-screen guidance on the installation and configuration process.


InstallProgramThe first window is representative. You must select the installation type you want to execute. I want to perform a typical installation. It installs MySQL to the C: \ Program Files (x86) \ mysql \ MySQL Server 5.0 directory. In a 32-bit system, the target directory is c: \ Program Files \ mysql \ MySQL Server 5.0. (FigureC)

Figure C

Which installation do you want to perform?

I do not describe the summary window. Next is the important step.


After the installation is complete, the MySQL Configuration Wizard will be started immediately. It will ask you some questions and learn how to apply the server. According to your answer, the MySQL Configuration Wizard creates a configuration file for the database server, which contains the adjustment parameters of the running server.

The first step in the configuration process is to determine how to configure the server. Do you want the system to ask you many questions about server applications, or do you want MySQL to use a pre-defined configuration file and then perform manual operations? This example is described in detail below. (FigureD)

Figure D

Do you want to perform a detailed analysis or just use a predefined configuration?

To allocate appropriate Ram and other resources to the database, the MySQL Configuration Wizard asks you how to apply the server. (FigureE) If your server is dedicated to database work, MySQL will use as many Ram and other resources as possible. Because my test server is both a database and a network server, I chose a server machine ).

Figure E

Which server will you use?

In addition, the Configuration Wizard asks you another question, which helps you adjust to the best system for your purpose. In my example, I chose to use the multi-function database option (FigureF.

Figure F

How do you use the database?

Next, define the systemInnoDB tablespace. I chose to use the default settings. (FigureG)

Figure G

Select InnoDB configuration options

As shown below:FigureH. Next, let's determine how many concurrent connections are established with the MySQL database. Do not underestimate this data. If the connection is used up, other users accessing your website cannot access the database. However, this data should not be too large, because other database connections also require other resources. A balance must be created.

Figure H

The number of database connections is an important resource consideration.

We generally access the MySQL database through TCP/IP. By default, MySQL uses port 3306 for communication. You can disable TCP/IP or modifyFigureIConfigure the port number on the page. On this page, you can also select enable strict mode, so that MySQL will not allow small syntax errors. If you are a newbie, I suggest you cancel the standard mode to reduce the trouble. However, if you are familiar with MySQL, try to use the standard mode because it can reduce the possibility of harmful data entering the database.

Figure I

TCP/IP and standard mode configuration settings.

I will not explain too much in the next window. (FigureJ) Since I only use English, I select the standard character set.

Figure J

Select a character set.

In the next window of the Configuration Wizard, the system asks if you want to install MYSQL as a Windows service and whether the service should be started with the server. In addition, you must decide whether to add the MySQL command to the Windows path. I made a wise choice to enable the MySQL service along with the operating system and choose to modify the system path. (FigureK)

Figure K

Select the environment option.

Security of network servers and databases is also an important factor. Similarly, you should set a secure root password for the database server. If not necessary, prohibit remote root access and do not create an anonymous account. (FigureL)

Figure L

Provides some security camouflage for database servers.

Security Settings are the final configuration items in the Wizard. Once the configuration is complete, the wizard prepares the configuration and writes the configuration informationC: \ Program Files [{x86}] \ mysql \ MySQL Server 5.0 \ My. iniFile, and then start the database service.

Now, you have created a MySQL database server that runs normally. Now we turn to PhP.

Step 4: install and configure PHP

This may be the most difficult part of the process. Collaboration between PHP and IIS 6 is sometimes a challenge. The following is a complete description.

Install PHP

The first task is to install PHP from the compressed file downloaded earlier. I usually install PHP in the C: \ PhP5 folder. Complete these steps in the system:

    • Create a folder named PhP5 on drive C.
    • Decompress the downloaded file to this location. The built-in decompression tool of Windows Server 2003 R2 can successfully complete this task.

Believe it or not, this is the entire "Installation Process" of PhP5 ". Now, it is more difficult to configure PHP.

Configure PHP

Several steps are required for PHP to work with IIS in windows.

    • Add c: \ PhP5 to the system PATH variable. (FigureMIn this way, the system can more easily locate the required php dll file.

      • Select Start and right-click on my computer.
      • Select properties from the shortcut menu that appears.
      • Select an advanced tag in the Properties window.
      • Click environment variable.
      • Double-click the PATH variable in the system Variable list.
      • Add "; C: \ PHP" at the end of the PATH variable ". (No quotation marks !)
      • Restart the server.

Figure M

Modify the system path to include the PHP Directory.

    • Copy the c: \ PhP5 \ PHP. ini-Dist file to c: \ PhP5 \ PHP. ini, which is the configuration file of PHP. -The Dist version is only a sample.
    • Now, create a system variable named phprc and allocate it to the path of the php. ini file. If you choose to do this, instead of creating a registry key to complete the same task, make sure that your phprc variable only contains the php. ini path, not the path and PHP. ini file name.
    • Finally, to make PHP and MySQL work together, you must make some modifications to c: \ PhP5 \ PHP. ini.
      • Open c: \ PhP5 \ PHP. ini in your most popular text editor.
      • Find the extension_dir configuration option and ensure that this line is: extension_dir = "./EXT", which tells PHP where to find other modules to be loaded. When using MySQL and PHP, you need at least one module.
      • Find the line "; Extension = php_mysql.dll" and delete the semicolon (;). In the php. ini file, the semicolon is used to annotate the configuration line. In this example, I want to decompile the lines of the PHP MySQL module.
      • At the bottom of the previous line, add: "extension = php_mysqli.dll ". This module activates some other functions of the new MySQL version.
    • Enter the "iisreset" command at the command prompt to re-load IIS and PHP with the new configuration file.

Configure IISIn 32Run in bit mode (only for 64Windows)

The official PHP windows binaries cannot be used in 64-bit windows architecture, but you can still run it on 64-bit windows. To achieve this, you must run IIS 6 in 32-bit simulation mode. On your Windows Server, perform the following operations:

    • Enter the command prompt.
    • Transfer from Command Prompt windowC: \ Inetpub \ adminscriptsFolder. If Windows is installed on a disk other than drive C, use the corresponding drive letter.
    • InAdminscriptsIn the folder, enter the following command: cscript adsutil. vbs set w3svc/apppools/enable32bitapponwin64 1. Value 1 of enable32bitapponwin64 tells windows to run W3SVC in 32-Bit mode, and value 0 indicates 64-bit.
    • InputIisreset.
    • Restart the server.

Configure IIS

After the PHP configuration is complete, you must also tell IIS how to use the new service before you start to apply it. Otherwise, if you try to load the PHP file from the IIS server to a Web browser, you will only get the wrong result.

There are two ways to run php -- Application ISAPI in IIS, or in the form of CGI extension. Because the application ISAPI generally achieves better performance, we apply this method in this article. First, we add the php isapi extension to IIS.

    • Start-management tool-Internet Information Service (IIS) manager.
    • Select the server name-network service extension.
    • In the right box, click "Add a new network service extension ..." .
    • Enter "php ISAPI" in the "extension" of the result window ".
    • Click "add.
    • BrowseC: \ PhP5 \ php5isapiFile.
    • Click OK.
    • Select the "enable extension Settings" check box. (FigureN)
    • Click OK.

Figure N

Specify the new extension in the php5isapi. dll file of the c: \ PhP5 folder.

Add a new extension (. php)

    • Start-management tool-Internet Information Service (IIS) manager.
    • Right-click the URL and select Properties From the shortcut menu that appears.
    • In the Properties window, select the "home directory" tab.
    • Click "Configure.
    • Click "add.
    • In the Add/edit application extension ing window, Click Browse next to the running column and findC: \ PhP5 \ php5isapi. dllFile.
    • Enter ". php" in the extension input box ". (FigureO)
    • Under the Action (verbs) Title, select the "limited to" option and enter "Get, post, and head" in the text box ".
    • Click OK to return to the main window of IIS manager.

Figure O

The PHP extension tells IIS how to handle. PHP page requests.

At the same time, in IIS, make sure that you have set a website license to execute the script:

    • Start-management tool-Internet Information Service (IIS) manager.
    • Right-click the URL and select Properties From the shortcut menu that appears.
    • In the Properties window, select the "home directory" tab.
    • In "application settings", change the "License execution" option to "script only ". (FigureP)

Figure P

Modify the settings in IIS to run the script.

Finally, in IIS, make sure that you have added a default PHP page, as shown in figureIndex. php. In this way, you do not need to specify the complete path and PHP script name for a specific folder on the network server. Conversely, if a user accesses a directory that contains a directory namedIndex. php, ISS will automatically load it.

    • Start-management tool-Internet Information Service (IIS) manager.
    • Right-click the URL and select Properties From the shortcut menu that appears.
    • In the Properties window, select the file tag.
    • Click "add.
    • In the "add content" window, enter "index. php ". (FigureQ)
    • Click OK.
    • Click OK. If you get information that affects a subnode, click OK.

Figure Q

If there is no page on it, load the directory page.

Test PHP

Now we have tested PHP to ensure that it can use IIS to implement dynamic pages.

    • Create a phpinfo. php file under the IIS network root directory (generally c: \ Inetpub \ wwwroot.
    • Paste a line of text in this file: <? Phpinfo () ;?>. This is actually a complete PHP script used to output all the configurations of the PHP-based network server.
    • Now, accessHttp: // {your server}/phpinfo. php, Or access from the serverHttp: // php.
    • If an error occurs, the configuration is incorrect.
    • You should get a window similar to this. (FigureR)
    • Make sure that there is a section in phpinfo of MySQL. If no, return and ensure that you have modified the php. ini file correctly to load the MySQL module.

Figure R

The PHP test page contains a complete PHP configuration overview.

Step 5: Test MySQLNetwork Connection

To check whether the installation is successful, you need a sample database to perform another test. In this article, I will use the MySQL command line tool to create a small sample database. its only purpose is to ensure that the PHP/MySQL connection is normal.

To create this sample database:

1. Create a real database first. Run the following command:
Mysqladmin-uroot-P ******** create sample
Note:: If an error occurs, make sure that the password is correct (-u parameter specifies the user name,-p parameter is in MySQL
The password allocated in the configuration phase .)

2. Now, create a table and add some data. In addition, execute the following commands from the command line:

    • C:> mysql-uroot-P ******** sample
      In this case, replace * with the same password *. The remaining steps are completed on the MySQL command interface.
    • At the MySQL prompt, enter
      Create Table sample_table (firstname varchar (30), middlename varchar (30), lastname varchar (30); mysql> insert into sample_table values ("Scott", "David ", "Lowe"); mysql> insert into sample_table values ("Ralph", "John", "Malph ");
    • Note:: The semicolon in the preceding step is extremely important.
    • Using the root user account to access the database from the network server is a major security threat. Therefore, you should create a separate account for this purpose. Enter the following command in the MySQL interface:
      Grant all on sample. * To webuser @ localhost identified by 'Password ';
    • Enter quit to exit the MySQL interface.

Next, you need a PHP script under the network server, which can read the content of this new database. If this is done successfully, you have a running IIS/PHP/MySQL system.

    • Under the network root directory (generallyC: \ Inetpub \ wwwroot) To createDbtest. php.
    • Paste the following PHPCode:


$ Username = "webuser ";

$ Password = "password ";

$ Database = "sample ";

$ Hostname = "localhost ";

$ Mysqlconnection = mysql_connect ($ hostname, $ username, $ password) or die ("unable to connect to MySQL database !! ");

$ Mysqlselecteddb = mysql_select_db ($ database, $ mysqlconnection) or die ("cocould not set the database !! ");

$ Mysqlrecordset = mysql_query ("select * From sample_table ");

Print "First name-". "middle name-". "Last name <br> ";

While ($ myrow = mysql_fetch_array ($ mysqlrecordset, mysql_assoc ))


Print $ myrow ['firstname']. "-". $ myrow ['midlename']. "-". $ myrow ['lastname']. "<br> ";


Mysql_close ($ mysqlconnection );

? >

Save the file and point it to http: // {yourserver}/dbtest. php.FigureS.

Figure s

If you see these three names, your configuration is successful.

A better idea

I have never said that the installation and configuration process is very simple. However, if you follow these steps, you can build a normal MySQL/PHP system in a short period of time. I am very happy to accept your remarks or suggestions for simplifying this process. If I receive good suggestions, I will post them in another article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.