windows7下nginx+php+mysql开发环境搭建
<h2>导言:</h2><div>近来,nginx以其高性能吸引大批web开发者,各大门户网站大多都采用nginx作为web服务器,关于nginx的介绍,网上已有大量文 章,笔者就不在此赘述,关于linux上nginx的搭建网上也有大量文章,但在windows上却为数不多,今天就为大家简单介绍windows7环境 下nginx+php+mysql组合的搭建,希望对各位有所帮助。所有软件包均是最新稳定版。</div><div>php5.4.8</div><div>nginx1.2.5</div><div>mysql5.5.28<div style="page-break-after: always;"><span style="display: none;"><!--more-->& nbsp ;</span></div><p> </p></div><h2>1.下载相关安装包</h2><p>下载nginx :Stable version nginx/Windows-1.2.5</p><p>http://nginx.org/en/download.html 也可点此处直接下载http://nginx.org/download/nginx-1.2.5.zip</p><p> </p><p>下载php: VC9 x86 Non Thread Safe (2012-Oct-17 22:45:27)这里我们选择非线程安全版</p><p>http://windows.php.net/download/#php-5.4 也可点此处直接下载http://windows.php.net/downloads/releases/php-5.4.8-nts-Win32-VC9-x86.zip</p><p> </p><p>下载RunHiddenConsole</p><p>http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip</p><p> </p><p>下载MySQL MySQL Installer 5.5.28</p><p>http://www.mysql.com/downloads/installer/ 也可点此处直接下载http://cdn.mysql.com/Downloads/MySQLInstaller/mysql-installer-community-5.5.28.3.msi</p><p> </p><h2>2.安装</h2><h3>安装nginx</h3><p>在D盘下建立名为server的目录 D:server</p><p>解压nginx-1.2.5.zip至D:servernginx-1.2.5</p><p>找到下面的nginx.exe,在命令行下运行nginx.exe (注意目录中不能出现中文字符)</p><p>启动nginx</p><div><div><div>[plain]view plaincopy</div></div></div><p> </p><div><ol> <li>D:servernginx-1.2.5>nginx </li></ol></div><p> </p><p>打开任务管理器,切换到进程选项卡,出现如下进程,证明nginx已经启动。</p><p></p><p>在浏览中键入:http://127.0.0.1,看到下图所示证明nginx正常工作。</p><p></p><p>配置网站根目录</p><p>在D:server下建立www目录</p><p>在www目录下新建一个index.html文件,文件内容如下:</p><div><div><div>[html]view plaincopy</div></div><ol> <li><html> </li> <li><head> </li> <li></head> </li> <li><body> </li> <li>test is ok ! </li> <li></body> </li></ol></div><p> </p><div><ol> <li></html> </li></ol></div><p> </p><p> </p><p>修改D:servernginx-1.2.5confnginx.conf </p><p>location / {
#root html;
root D:/server/www; #注意路径分隔符为斜杠
index index.html index.htm;</p><p>}</p><p>重新加载nginx 配置</p><div><div><div>[html]view plaincopy</div></div></div><p> </p><div><ol> <li>D:servernginx-1.2.5>nginx -s reload </li></ol></div><p> </p><p>在浏览中键入:http://127.0.0.1</p><p>看到 test is ok !说明工作正常</p><p> </p><p>停止nginx:</p><div><div><div>[plain]view plaincopy</div></div></div><p> </p><div><ol> <li>D:servernginx-1.2.5>nginx -s stop </li></ol></div><p> </p><p> </p><p>显示nginx 帮助</p><div><div><div>[plain]view plaincopy</div></div></div><p> </p><div><ol> <li>D:servernginx-1.2.5>nginx -h </li></ol></div><p> </p><p> </p><h3>安装php</h3><div>解压php-5.4.8-nts-Win32-VC9-x86.zip 到D:serverphp</div><div>解压RunHiddenConsole.zip ,复制RunHiddenConsole.exe到D:serverphp</div><div>重命名php目录下的php.ini-development 为php.ini</div><div> </div><div>启动php-cgi</div><div><div><div><div>[plain]view plaincopy</div></div></div><div><ol> <li>D:serverphp>RunHiddenConsole.exe php-cgi.exe -b 127.0.0.1:9000 -c D:serverphpphp.ini </li></ol></div>使用命令</div><div><div><div><div>[plain]view plaincopy</div></div></div><div><ol> <li>D:serverphp>netstat -a </li></ol></div></div><div>出现</div><div>TCP 127.0.0.1:9000 HongJunWen-PC:0 LISTENING</div><div>说明php-cgi启动成功。</div><div> </div><h3>使nginx能将php请求交给php处理</h3><div>修改nginx配置文件</div><div>location ~ .php$ {<br /> #root D:/server/www;<br /> fastcgi_pass 127.0.0.1:9000;<br /> fastcgi_index index.php;<br /> fastcgi_param SCRIPT_FILENAME D:/server/www$fastcgi_script_name;
include fastcgi_params;
}</div><div>重新加载配置文件</div><div> </div><div>在www目录下新建index.php,文件内容如下</div><div><div><div><div>[php]view plaincopy</div></div><ol> <li><?php </li></ol></div><div><ol> <li>echo phpinfo(); </li></ol></div>在浏览器中打开http://127.0.0.1/index.php</div><div>出现php环境信息页面说明配置成功。</div><div> </div><h3>安装MySQL5.5</h3><p>(注意:需要安装.net framework 4.0.30319,已经安装有的,跳过此步) 下载地址:http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe </p><div>打开mysql-installer-community-5.5.28.3</div><div>安装路径选择D:serverMySQL</div><div>数据文件路径D:serverMySQLdata</div><div>其他默认即可</div><div> </div><div>启动mysql</div><div><div><div><div>[plain]view plaincopy</div></div></div></div><p> </p><div><div><ol> <li>D:serverMySQLMySQL Server 5.5bin>mysqld </li></ol></div></div><p> </p><div>用netstat -a 命令查看,出现如下信息,说明mysql启动成功。</div><div> TCP 0.0.0.0:3306 HongJunWen-PC:0 LISTENING</div><div> </div><h3>PHP支持Mysql</h3><div>打开php.ini</div><div>找到以下两行:<div><div><div>[plain]view plaincopy</div></div><ol> <li>;extension_dir = "ext" </li> <li>……………… </li> <li>;extension=php_mysql.dll </li></ol></div>分别将这两行前面的分号去掉,重启php-cgi。使修改后的配置生效。</div><div> </div><div>在www目录下新建mysql_test.php,内容如下:</div><div><div><div><div>[php]view plaincopy</div></div><ol> <li><?php </li> <li>$con = mysql_connect("localhost","root",""); </li> <li>if (!$con){ </li> <li> die('Could not connect: ' . mysql_error()); </li> <li>} else { </li> <li> echo 'Database connected successfully'; </li> <li>} </li></ol></div>$con<div>打开http://127.0.0.1/mysql_test.php</div><div>显示如下:</div><div>Database connected successfully</div>说明php已经支持mysql<h3>一键启动所有服务</h3> 在windows下习惯了wamp的朋友,可能会很不习惯这种一个程序一个程序单个启动的方式,这对我们这种懒人来说,不是个好方法。下面我们写两个脚本</div><div>这两个脚本分别完成所有服务的启动与关闭。</div><div>首先将RunHiddenConsole.exe复制到D:servernginx-1.2.5下</div><div> </div><div>在D:server下新建一个名为start_all_services.bat的批处理文件,此文件内容如下:</div><div><div><div><div>[php]view plaincopy</div></div><ol> <li>@echo off </li> <li>echo starting php-cgi... </li> <li>D:serverphpRunHiddenConsole.exe php-cgi.exe -b 127.0.0.1:9000 -c D:serverphpphp.ini </li> <li>echo starting nginx... </li> <li>cd nginx-1.2.5 </li> <li>RunHiddenConsole.exe D:servernginx-1.2.5nginx </li> <li>echo starting mysql... </li> <li>"D:serverMySQLMySQL Server 5.5binmysqld" </li></ol></div><div><ol> <li>exit </li></ol></div>(注意:第8行需要加双引号,对于路径中包含空格的情况,需要用双引号引起来。否则会出错。)
</div><div>在D:server下新建一个名为stop_all_services.bat的批处理文件,此文件内容如下:</div><div><div><div><div>[plain]view plaincopy</div></div><ol> <li>@echo off </li> <li>echo stopping nginx... </li> <li>taskkill /F /IM nginx.exe > nul </li> <li>echo stopping php-cgi... </li> <li>taskkill /F /IM php-cgi.exe > nul </li> <li>echo stopping mysql... </li> <li>taskkill /F /IM mysqld.exe > nul </li></ol></div></div><div>需要启动只需双击start_all_services.bat</div><div>需要关闭只需双击stop_all_services.bat</div><div>怎么样,是不是很简单。</div><div> </div><div>至此windows7下nginx+php+mysql的搭建已经完成,现在尽情投入到php的的开发中吧。</div>