Home php教程 php手册 在 WIN 平台上让你的 Apache 2.0.45 支持 PHP

在 WIN 平台上让你的 Apache 2.0.45 支持 PHP

Jun 21, 2016 am 09:08 AM
apache browser conf httpd php

apache

前言
如果你对使用 Apache 缺乏信心。或者你已经在使用 apache 2 但是不能让他和 PHP 一起工作(apache不能解析你的.php脚本)。不要怕,读完本文后,所有的问题都会迎韧而解的。

我将如何使 Apache 和 PHP 共同合作的方法步骤归纳总结了一下。其实,这么比其他人所说的简单得多。我很惊讶为什么在这之前没有人写一个针对这个问题的操作指南,好让每个人都能够更好地理解和使用 Apache 2.0 。凭着个人的兴趣,我分析了 Apache 2.0,特别地,为了获得最新的 Apache服务器,本文所用的,是2.0.45版。

现在,对任何对升级有顾虑的人来说,把服务器升级到 Apache 2.0,比升级微软的IIS要安全得多,并且,还有一大堆有用的工具。在本文章中,我将向大家演示如何配置你的 httpd.conf 文件来使你的服务器能够处理 .php 和.phps 脚本。 接下来我们要一起来配置apache 2,当我们请求 .php 页面时才不会显示源代码或提示下载而不显示我们想看到的页面。


我们要做的工作包括:

下载和安装apache 2.0.45
配置httpd.conf文件
使apache2运行.php和.phps文件
这篇文章是针对Windows 2000/Me/XP用户的。WIN95/98 和 NT4.0 用户如果没有 msi installer 可能需要先从微软下载并安装它。之所以我要强调 Msi 安装程序,是因为本文是针对初次安装 Apache 2 的人的。如果你已经在此之前安装了 Apache,那么你可以直接开始安装了。但也要需要注意本文的一些步骤,特别是当你打算安装PHP 4.3.1的时候。好了,让我们开始吧。


下载apache2

如果你已经安装了apache 1.3,请先别卸载它,先把它停了。在安装了apache2并且PHP脚本工作后,你就可以安全的卸载 apache1.3 了,卸载请一定注意备份好你的 httpd.conf 文件,这样,你就只需改动很少的地方,就可以把它用到新的服务器上了。

对机器上没有安装过apache或安装了其他服务器的人,请先去apache.org下载apache2.0.45(http://nagoya.apache.org/mirror/httpd/binaries/win32/)。确信你下载的.msi文件是5.3M大小。运行下载的.msi文件安装apache。你的服务器就会自动启动。如果你有防火墙,请确保防火墙允许对 Apache 的访问。

你应该注意apache服务监视器(一个绿灯,在任务栏里面),标志着apache在工作。要确保它在工作,只要把鼠标箭头移动到监视器上,它将会告诉你它在工作。你的任务栏应该和下图差不多:



有人也许想知道为什么会显示 “running 1 out of 2 apache services”,那是因为你可能有不止一个版本的apache在工作。大多数人不会这么干,但我的机器上的确有两个 apache 服务器。我的 1.3.27 停止运行了,而 apache 2 则正在运行中。下图是我的机器的情况:



注意,在apache1.3.27 的监视器上有个红灯,而在apache2的上面有个绿灯。第一次安装 apache 的人只能看见一个,因为你只装了一个。

好了。Apache服务器已经基本安装完毕了。现在让我们去下载PHP的源代码,因为我们想让 PHP 和 APACHE 合作,去php.net下载最新版的PHP(目前为PHP 4.3.2 http://www.php.net/downloads.php)。如果你已经安装了PHP,检查sapi文件下是否有php4apache2.dll文件,如果有,你可以跳过下面的步骤。

我们需要下载 php-4.3.2-Win32.zip 和 php-4.3.1-installer.exe 两个文件,许多人要问为什么都要下载。
这样做是让你偷懒,这样就可以不需要建立文件夹,不需要把文件夹名字从 php-4.3.1-Win32 改为PHP。我们先使用installer.exe来安装,它将为你建立PHP文件夹,你可以节省更多的时间。运行installer.exe,根据你的喜好安装PHP,完成后,解压zip文件,然后找到压缩包中的 sapi 文件夹。把该文件夹复制你刚才安装好的PHP目录。(例如:C:PHP)然后我们便可以删除php-4.3.2-win32文件夹,因为现在已经不再需要它了(有点卸磨杀驴的感觉,haha)。把 PHP文件夹中的php4ts.dll复制到你的系统目录:在XP下是WINDOWSSystem32,在2K下是WINNTsystem32。下面我们开始修改conf文件。



让 Apache 解析 php 页面

用你喜欢的文本编辑器打开httpd.conf文件,让我们加几行进去,这样我们Apache 就可以解析 .php 和.phps文件了。我们需要注意conf文件的两个部分,第一部分是 Dynamic Shared Object (DSO) Support list,第二部分是 cgi-bin directory

首先让我们找到如下所示的 DSO 列表.

# Dynamic Shared Object (DSO) Support
#
# to be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
这还有许多,一直找到最后一行:

#LoadModule ssl_module modules/mod_ssl.so
上面一行是该列表的最后一行,按回车产生一个空的行,在这个空行内加入下面这行:

LoadModule php4_module c:/php/sapi/php4apache2.dll
在关闭conf文件之前,我们需要再加入两行。找到CGI-BIN目录,为了节省时间,可以这样找:按CTRL+F,(译者注:如果你是在记事本而不是写字板里编辑这个文本,那么,应该用快捷键F3)查找 ,在第二个后面两次回车,以便加入下面两行:

AddType application/x-httpd-php .php .phtml .php3 .php4
AddType application/x-httpd-php-source .phps
添加好后,保存你的conf文件,并重新启动你的apache服务器。高级用户现在就可以测试你们激动人心的PHP脚本了。一旦成功运行,那么,你的工作就结束了。对于初次安装的用户,你需要把PHP文件保存在你的默认的路径里:c:Program FilesApache GroupApache2htdocs。创建一个新的文本文档,将下列代码添加进去,保存为 date.php。

$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
echo "$month $mday, $year";
?>
现在来测试上述代码(试试在浏览器的地址栏里输入:localhost/date.php,然后敲回车),应该显示当前的月、日期、年。如果输出正确,就表明PHP已经在你的服务器上开始工作了。为了满足那些想要看源代码的人的愿望,你就再创建一个文本文档,将下列代码添加进去吧:

$browser = getenv("HTTP_USER_AGENT");
?>

You are using the web browser.



<?php echo $title; ?>
把它保存为 browser.phps,并另存为 browser.php 。在测试了这两个例子后,你会发现结果是不同的。browser.php将显示你的浏览器版本,如:
You are using the Mozilla/4.0 (compatible; MSIE 6.0; Windows 98) web browser.
而 browser.phps 则显示了你的源代码。


总结

现在,Apache 2.0.45 应该可以处理你的 .php 和 .phps 网页了。希望一切都在顺利运行。现在,你拥有了一个全新的服务器了。:P



Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

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: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

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.

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

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 vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

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.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

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.

See all articles