Easy to use PHP + mysq + IIS configuration

Source: Internet
Author: User
Tags php mysql php windows mysql command line root access
Configure PHP and MySQL on Windows (transfer)

Step 1: Install the application server role on Windows Server 2003 r2

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. (Figure 1)

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. (Figure 2)

Application Server roles include IIS and Asp.net

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 PHP and 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.

MySQL

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.

Essence: 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 essence 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. It must be manually installed.
In this article, install the essence edition. In this case, I named the downloaded file as a mysql-essential-5.0.24a-win32.msi and saved it on the desktop.

PHP

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.

Step 3: Install MySQL

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

Install MySQL

The first window of the installer is representative. You must select the installation type you want to execute. I want to install MySql in the C:/program files (x86)/MySQL Server 5.0 directory. In a 32-bit system, the target directory is C:/program files/MySQL Server 5.0. (Figure 3)

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

MySQL Configuration

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. (Figure 4)

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. (Figure 5) If your server is dedicated to databasesWorkMySQL 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 ).

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 (figure 6), which is suitable for general use.

Next, you need to define InnoDB tablespace settings. I chose to use the default settings. (Figure 7)

As shown in figure 8 below. 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.

We generally access the MySQL database through TCP/IP. By default, MySQL uses port 3306 for communication. You can disable TCP/IP or modify the port number on the configuration page in Figure 9. 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.

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

In the next window of the Configuration Wizard, the system asks if you wantMySQLInstall as a Windows service and whether the service should be started along 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. (Figure 11)

NetworkServers andDatabaseSecurity 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. (Figure 12)

Security Settings are the final configuration items in the Wizard. Once the configuration is complete, the wizard prepares the configuration and writes the configuration information to the C:/program files [{x86}]/MySQL Server 5.0/My. ini file, and then starts the database service.

Now, you have created a MySQL database server that runs normally. Now we turnPHP.

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.Work.

Add C:/PhP5 to the system PATH variable. (Figure 13) in 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.

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 IIS to run in 32-Bit mode (for 64-bit windows only)

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.
Go to the C:/inetpub/adminscripts folder from the command prompt window. If Windows is installed on a disk other than drive C, use the corresponding drive letter.
In the adminscripts folder, enter the following command: cscript adsutil. vbs set w3svc/apppools/enable32bitapponwin64 1. The value 1 of enable32bitapponwin64 tells windows to run W3SVC in 32-Bit mode, while the value 0 indicates 64-bit.
Enter iisreset.
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 Tools --InternetInformation 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.
Browse to the C:/PhP5/php5isapi file.
Click OK.
Select the "enable extension Settings" check box. (Figure 14)
Click OK.

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 the "Browse" button next to the running column and find the C:/PhP5/php5isapi. dll file.
Enter ". php" in the extension input box ". (Figure 15)

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.

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 ". (Figure 16)

Finally, in IIS, make sure that you have added a default PHP page, such as index. 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. On the contrary, if a user accesses a directory containing a file named index. 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 ". (Figure 17)

Click OK.
Click OK. If you get information that affects a subnode, click OK.

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, access http: // {yourserver}/phpinfo. php on the task machine, or access http: // 127.0.0.1/phpinfo. php from the server.
If an error occurs, the configuration is incorrect.
You should get a window similar to this. (Figure 18)

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.

Step 5: test the MySQL network 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. * towebuser @ localhostidentified 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.

Create a file named dbtest. php under the Network root directory (generally C:/inetpub/wwwroot.
Paste the following PHP code in this file:
<?

$ Username = "webuser ";

$ Password = "password ";

$ Database = "sample ";

$ Hostname = "localhost ";

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

$ 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 the web browser to http: // {yourserver}/dbtest. php. You will get a window similar to window 19.

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: info-contact@alibabacloud.com 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.