search
HomeBackend DevelopmentPHP TutorialWindow下PHP三种运行方式图文详解_php技巧

PHP能不能成功的在Apache服务器上运行,就看我们如何去配置PHP的运行方式。

PHP运行目前为止主要有三种方式:

a、以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成到Apache服务器,以同一个进程运行。

b、以CGI的方式运行,CGI英文叫做公共网关接口,就是Apache在遇到PHP脚本的时候会将PHP程序提交给CGI应用程序(php-cgi.exe)解释,解释之后的结果返回给Apache,然后再相应请求的用户。

c、以FastCGI的方式运行。这种形式是CGI的加强版本,CGI是单进程,多线程的运行方式,程序执行完成之后就会销毁,所以每次都需要加载配置和环境变量fork-and-execute(创建-执行)。而FastCGI则不同,FastCGI 像是一个常驻 (long-live) 型的 CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去 fork 一次。FastCGI进程管理器自身初始化,启动多个CGI解释器进程 (在任务管理器中可见多个php-cgi.exe)并等待来自Web Server的连接。下面我就分边配置这三种运行方式:

1、无论上述哪种方式运行下面的配置通常都会加上,解压PHP安装包到c:/PHP5/,重命名PHP.ini-recommend文件为PHP.ini,分别寻找如下字段编辑,去除前面的分号(注意不要去错分号,好多是注释信息,仔细辨认)。

复制代码 代码如下:

error_reporting = E_ALL //开启报错,便于程序员查错 line 342
display_errors = On //显示错误 line 373
extension_dir = "C:/php5/ext" //php的扩展选项文件所在的目录 line 542
date.timezone = Asia/shanhai //时区配置  line 716


2、以模块的方式运行,在Apache(C:/Program Files/Apache Software Foundation/Apache2.2/conf)的配置文件里添加如下配置

复制代码 代码如下:

LoadModule php5_module "C:/php5/php5apache2_2.dll" //大约line 127
PHPinidir "C:/php5/php.ini"
//修改配置
DirectoryIndex index.html index.php//追加index.php
AddType application/x-httpd-php .php //line 408左右添加


接着我们在apache的根目录下面,默认C:/Program Files/Apache Software Foundation/Apache2.2/htdocs新建php文件index.php,编辑添加如下代码:

复制代码 代码如下:

phpinfo();
?>


然后我们在地址栏输入http://localhost/会出现如下界面:注意红色部分和配置的关系

3、cgi的方式运行,需要做如下的配置首先php配置文件
复制代码 代码如下:

cgi.force_redirect = 0 //本来是 1 并且去掉注释符号;


修改apache的配置,去掉原来的模块配置

复制代码 代码如下:

AddType application/x-httpd-php .php
LoadModule php5_module "C:/php5/php5apache2_2.dll"
PHPinidir "C:/php5/php.ini"


=>加入以下配置
复制代码 代码如下:

AddHandler cgi-script .cgi // line 396


然后我们在目录C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin新建一个cgi文件test.cgi编写如下代码:

复制代码 代码如下:

#!c:/php5/php-cgi.exe

php php phpinfo();
?>


 

如果同时打开多个则会有很多php-cgi.exe,并且在执行完成之后消失:

4、PHP的fastcig方式运行,首先需要去下载fastcgi模块,默认是没有带这个模块的,而cgi是自带的;下载地址http://httpd.apache.org/mod_fcgid/;解压复制其中的mod_fcgid.so和mod_fcgid.pdb,接下来做如下的配置:

复制代码 代码如下:

LoadModule fcgid_module modules/mod_fcgid.so // line 128追加
FcgidInitialEnv PHPRC "c:/php5" //php配置文件 line 129追加
AddHandler fcgid-script .php //添加句柄 即后缀 什么样的文件需要fastcgi解释 line 407追加
FcgidWrapper "c:/php5/php-cgi.exe" .php //解释器路径 line 408
Options Indexes FollowSymLinks ExecCGI //line 221 追加 ExecCGI 意思是目录允许执行CGI脚本


是否成功安装,只需要做个PHP文件检查就可以了,我们就用上面的index.php文件:

终于把PHP的运行方式讲完了,初学者一定要亲自试验一下,这篇教程具有一定的难度,要反复思考才能理解其中的意思,涉及到一些专业知识,当然你暂时可以直接使用模块运行方式,等自己对PHP有了一定的理解之后可以再回头看看这篇文章,今天我花了几个小时的时间整理PHP的运行方式,由于作者本人的水平有限,错误之处在所难免!
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.