软件下载
Apache: http://www.apachehaus.com/cgi-bin/download.plx
PHP: http://windows.php.net/download
MySQL: http://dev.mysql.com/downloads/mysql
下载后的文件
httpd-2.4.23-x64-vc11.zip
php-5.6.24-Win32-VC11-x64.zip
mysql-5.7.14-winx64.zip
注意:Apache和PHP的VC11的意思是软件是通过VisualStudio2012编译的,这要保持一致,不然可能出现无法预料的问题。所以要么同时是VC9,要么同时是VC11或者其它版本。
分别解压放到某个目录,如D:\PHPEnvironment
Apache24
php-5.6.24
mysql-5.7.14
Apache安装
打开D:\PHPEnvironment\Apache24\conf\httpd.conf
修改
Define SRVROOT "/Apache24"
为
Define SRVROOT "D:/PHPEnvironment/Apache24"
然后,以管理员方式打开命令行工具
C:\Windows\system32>d:
D:\>cd PHPEnvironment\Apache24\bin
D:\PHPEnvironment\Apache24\bin>httpd -k install
D:\PHPEnvironment\Apache24\bin>httpd -k start
最后,在浏览器输入http://localhost
,如果显示如下内容,则说明Apache安装成功
Welcome,
That's right! If you are seeing this it means that the web server installed at this site is working properly, but has not yet been configured.
...
当然,如果觉得每次切换目录比较麻烦,你可以把Apache
的bin
目录添加到环境变量Path
中去。
PHP安装
复制D:\PHPEnvironment\php-5.6.24
目录下的php.ini-development
文件,重命名为php.ini
。然后打开文件,找到
; extension_dir = "ext"
在该行下面添加
extension_dir = "D:/PHPEnvironment/php-5.6.24/ext"
Apache与PHP整合
打开D:\PHPEnvironment\Apache24\conf\httpd.conf
,在文件末尾添加
LoadModule php5_module D:/PHPEnvironment/php-5.6.24/php5apache2_4.dll
PHPIniDir D:/PHPEnvironment/php-5.6.24
AddType application/x-httpd-php .php .html
然后,以管理员方式打开命令行工具重启Apache
D:\PHPEnvironment\Apache24\bin>httpd -k restart
之后,在D:\PHPEnvironment\Apache24\htdocs
目录下新建phpinfo.php
。添加如下代码
<?php
phpinfo();
最后,浏览器访问http://localhost/phpinfo.php
,如果显示类似内容则说明整合成功
PHP Version 5.6.24
System Windows NT DESKTOP-VO4IA7V 6.2 build 9200 (Windows 8 Professional Edition) AMD64
Build Date Jun 22 2016 12:08:08
Compiler MSVC11 (Visual C++ 2012)
Architecture x64
...
MySQL安装
复制D:\PHPEnvironment\mysql-5.7.14
目录下的my-default.ini
文件,重命名为my.ini
。修改如下代码
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
为
# These are commonly set, remove the # and set as required.
basedir = D:\PHPEnvironment\mysql-5.7.14
datadir = D:\PHPEnvironment\mysql-5.7.14\data
port = 3306
# server_id = .....
其中server_id
暂时不用配置。然后,以管理员方式打开命令行工具
C:\Windows\system32>d:
D:\>cd PHPEnvironment\mysql-5.7.14\bin
D:\PHPEnvironment\mysql-5.7.14\bin>mysqld install
D:\PHPEnvironment\mysql-5.7.14\bin>mysqld --initialize-insecure
D:\PHPEnvironment\mysql-5.7.14\bin>net start mysql
最后,输入mysql -u root -p
然后回车,默认密码为空,所以继续回车,看到如下内容则说明安装成功
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 252
Server version: 5.7.14 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
如果对MySQL命令行管理不太熟悉,可以使用可视化管理工具,如Navicat for MySQL
等等。
注意:安装MySQL的时候,在命令行工具中需要手动切换到其bin
目录进行安装。如果在此之前就把bin
目录配置到环境变量Path
中,然后命令行工具打开后直接进行安装的话,可能会出一些问题。当然,安装完成后配置到环境变量中,然后执行MySQL相关的命令,是没有问题的。
安装完毕
至此,环境基本上搭建完毕,我们可以写个小程序测试下。在D:\PHPEnvironment\Apache24\htdocs
目录下新建test.php
,输入如下代码
<?php
$dsn = 'mysql:dbname=mysql;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
echo 'Connection succeeded';
在浏览器访问http://localhost/test.php
,显示如下内容则测试成功
Connection succeeded
如果提示
Connection failed: could not find driver
说明php_pdo_mysql
扩展没打开。打开D:\PHPEnvironment\php-5.6.24\php.ini
,找到
;extension=php_pdo_mysql.dll
改为
extension=php_pdo_mysql.dll
最后重启Apache即可
D:\PHPEnvironment\Apache24\bin>httpd -k restart
常用配置
设置DirectoryIndex
为index.php
打开D:\PHPEnvironment\Apache24\conf\httpd.conf
,可看到默认配置
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
修改成如下内容
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
然后
D:\PHPEnvironment\Apache24\bin>httpd -k restart
这样,我们访问一个目录的时候,就默认先访问index.php
,如果不存在,则访问index.html
,如果还不存在,就直接显示目录了。比如我们访问http://localhost
,如果index.php
存在,就相当于访问了http://localhost/index.php
。
测试环境
windows10
httpd-2.4.23-x64-vc11
php-5.6.24-Win32-VC11-x64
mysql-5.7.14-winx64