天天看點

Windows 下 PHP 環境搭建小結

機子上的 PHP 的版本太老了,是以就又重新搭建了相關的開發環境,使用了最新的 PHP 5.3.5,總結一下:

1,安裝 XAMPP:

這種安裝是最簡單的了,直接通路官網:http://www.apachefriends.org/zh_cn/xampp.html

下載下傳 适用于 Windows 的 XAMPP 即可,安裝及配置方法寫的一明二白;

2,分開安裝 Apache,PHP,MySQL:

> Apache

官網:http://httpd.apache.org/

下載下傳最新版:http://www.motorlogy.com/apachemirror//httpd/binaries/win32/httpd-2.2.17-win32-x86-openssl-0.9.8o.msi

即:httpd-2.2.17-win32-x86-openssl-0.9.8o.msi

點選安裝:我選的是針對所有使用者的安裝,預設端口是 80 端口;

安裝好後把 182 行的 ServerName 之前的 # 去掉,改為:

ServerName localhost:80      

一般情況下,46 行的 Listen 80 ,監聽 80 端口,和 ServerName 的端口一緻;

可以使用自帶的 Apache 服務監測工具 Monitor Apache Servers 來管理服務,也可以使用 cmd 指令來啟動和停止服務:

net start apache2.2
net stop apache2.2      

如果你的電腦裡還裝有老版本的 Apache 的話,Monitor Apache Servers 也會監測到它。

開啟服務後,在浏覽器中鍵入:localhost,如果顯示“It Works!”的話 則安裝成功。

> PHP

官網:http://windows.php.net/download/

在下載下傳的時候注意看右方的 Which version do I choose?

If you are using PHP with Apache 1 or Apache2 from apache.org you need to use the VC6 versions of PHP

If you are using PHP with IIS you should use the VC9 versions of PHP

VC6 Versions are compiled with the legacy Visual Studio 6 compiler

VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability. The VC9 versions require you to have the Microsoft 2008 C++ Runtime (x86) or the Microsoft 2008 C++ Runtime (x64) installed

Do NOT use VC9 version with apache.org binaries

VC9 versions of Apache can be fetched at Apache Lounge. We use their binaries to build the Apache SAPIs.      

它告訴你應該選擇什麼版本的 PHP,因為使用的是 Apache, 故選擇:VC6 x86 Thread Safe

其實上方還有一個:VC6 x86 Non Thread Safe 版本,我們最好選擇第一個版本(線程安全版本),網上的資料是:

先從字面意思上了解,Thread Safe 是線程安全,執行時會進行線程(Thread)安全檢查,以防止有新要求就啟動新線程的 CGI 執行方式而耗盡系統資源。Non Thread Safe 是非線程安全,在執行時不進行線程(Thread)安全檢查。
      再來看 PHP 的兩種執行方式:ISAPI 和 FastCGI。
      ISAPI 執行方式是以 DLL 動态庫的形式使用,可以在被使用者請求後執行,在處理完一個使用者請求後不會馬上消失,是以需要進行線程安全檢查,這樣來提高程式的執行效率,是以如果是以 ISAPI 來執行 PHP,建議選擇 Thread Safe 版本;
      而 FastCGI 執行方式是以單一線程來執行操作,是以不需要進行線程的安全檢查,除去線程安全檢查的防護反而可以提高執行效率,是以,如果是以 FastCGI 來執行 PHP,建議選擇 Non Thread Safe 版本。
      官方并不建議你将Non Thread Safe 應用于生産環境,是以我們選擇Thread Safe 版本的PHP來使用。      

下載下傳連結:http://windows.php.net/downloads/releases/php-5.3.5-Win32-VC6-x86.zip

即:php-5.3.5-Win32-VC6-x86.zip

解壓到認為合适的檔案夾裡,之後就要配置 Apache 支援 PHP 了,看看我的檔案結構:

Windows 下 PHP 環境搭建小結

也可以作為預期的檔案結構來整理,webRootDoc 作為根目錄(需要配置 Apache 的 DocumentRoot 配置)。

解壓之後開始配置 Apache 來支援 PHP;

> 配置 Apache 支援 PHP

打開 Apache 的配置檔案 httpd.conf ,加入:

LoadModule php5_module D:/newVersionPhp/php-5.3.5/php5apache2_2.dll
AddType application/x-httpd-php .php3 .php
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .po .py .pl .hu

# PHPIniDir
PHPIniDir "D:\newVersionPhp\php-5.3.5"      

上面的 PHPIniDir 規定了php.ini 配置檔案的搜尋目錄,這樣就不用把 php.ini 放入 C:/windows 中了。

配置好了以後使用 phpinfo() 函數來檢測是否安裝成功。

> MySQL

官網:http://dev.mysql.com/downloads/

我下載下傳的是:

MySQL Community Server(Current Generally Available Release: 5.5.9)MySQL Community Server is a freely downloadable version of the world\'s most popular open source database that is supported by an active community of open source developers and enthusiasts.DOWNLOAD

下載下傳的話貌似需要免費注冊一個帳号,注冊吧!别猶豫!全名:Mysql-5.5.9.msi

提供了傻瓜式的安裝。

之後啟動服務和關閉服務類似于 Apache:

net start mysql
net stop mysql      

> 配置 PHP 支援 MySQL

我們使用 $php_path 來表明你的 PHP 的安裝目錄:

1) 将 $php_path\libmysql.dll 拷入 windows 的 system32 目錄中;

2) 将 $php_path\ext\php_mysql.dl l 拷到 $php_path\ 下;

3) 重命名:$php_path\php.ini-production 為 php.ini ,這樣的話 php.ini 才投入使用。

4) 設定 php.ini 的 extension_dir :

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"
extension_dir = "D:/newVersionPhp/php-5.3.5/ext"      

5) 去掉 948 行之後每一行 extension 之前的分号。

> 測試 MySQL

可以寫一個簡單的腳本來測試是否已經配置好 PHP 和 MySQL:

<?php

$db_host     = \'localhost\';
$db_database = \'test\';
$db_username = \'root\';
$db_password = \'123\';
$connection  = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
	die ("Could not connect to the database: <br />". mysql_error());
}
else{
	echo "connected successfully!";
}

?>      

> 安裝 PEAR 擴充

剛開始使用的可能是老版本的 go-pear.php 來安裝的,安裝一直出錯,最後在一個論壇上找到了原因:

QUOTE(Ric @ Jan 26 2011, 08:41 AM)

It looks as if go-pear.php v1.1.2 remains the latest version (as per Pear web-site).

However core and other elements have been updated while go-pear has been neglected.

Solution is to edit file:

UniServer\home\admin\www\plugins\pear\

go_pear.php

Locate this section:

CODE

\'PEAR.php\'             => \'http://svn.php.net/viewvc/pear/pear-core/branches/PEAR_1_4/PEAR.php?view=co\',

\'Archive/Tar.php\'      => \'http://svn.php.net/viewvc/pear/packages/Archive_Tar/tags/RELEASE_1_3_2/Archive/Tar.php?view=co\',

\'Console/Getopt.php\'   => \'http://svn.php.net/viewvc/pear/pear-core/branches/PEAR_1_4/Console/Getopt.php?view=co\',

Change as shown below:

CODE

\'PEAR.php\'             => \'http://svn.php.net/viewvc/pear/pear-core/branches/PEAR_1_6/PEAR.php?view=co\',

\'Archive/Tar.php\'      => \'http://svn.php.net/viewvc/pear/packages/Archive_Tar/tags/RELEASE_1_3_3/Archive/Tar.php?view=co\',

\'Console/Getopt.php\'   => \'http://svn.php.net/viewvc/pear/pear-core/branches/PEAR_1_6/Console/Getopt.php?view=co\',

經過修改後的 go_pear.php 沒有問題了,安裝成功:

Starting installation ...
Loading zlib: ok

Bootstrapping Installer...................
Bootstrapping PEAR.php............(remote) ok
Bootstrapping Archive/Tar.php............(remote) ok
Bootstrapping Console/Getopt.php............(remote) ok

Extracting installer..................
Downloading package: PEAR.............ok
Downloading package: Structures_Graph....ok

Preparing installer..................
Updating channel "doc.php.net"
Update of Channel "doc.php.net" succeeded
Updating channel "pear.php.net"
Channel "pear.php.net" is up to date
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded

Installing selected packages..................
Downloading and installing package: PEAR.............ok
Installing bootstrap package: Structures_Graph.......ok
Downloading and installing package: Archive_Tar-stable.......ok
Downloading and installing package: Console_Getopt-stable.......ok
Downloading and installing package: PEAR_Frontend_Web-beta.......ok

Writing WebFrontend file ... ok

Installation Completed !
	Note: To use PEAR without any problems you need to add your
PEAR Installation path (D:\newVersionPhp\PEAR/PEAR)
to your include_path.

Using a .htaccess file or directly edit httpd.conf would be working solutions
for Apache running servers, too.

For more information about PEAR, see:
PEAR FAQ
PEAR Manual

Thanks for using go-pear!      

檔案下載下傳連結:https://files.cnblogs.com/catprayer/go-pear_new.rar

直接解壓放入根目錄運作即可,按照提示來安裝。