Window下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的运行方式,由于作者本人的水平有限,错误之处在所难免!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
