---
title: "linux 下 Apache的启动与PHP的测试"
author: "hae"
date: "2014-05-20T10:25:16Z"
tags: []
categories: ["编程语言"]
slug: "linux-apache-php"
draft: false
lastmod: "2014-05-20T10:25:16Z"
id: 2068540
url: "https://hae.iteye.com/blog/2068540"
---

在linux下启动Apache。

启动的方法很简单，用传统的方式来处理即可：

 

```
[root@linux ～]# /etc/init.d/httpd start  <==启动
[root@linux ～]# /etc/init.d/httpd stop   <==关闭 WWW
```

 

另外，其实Apache也自行提供一个Script，以供用户使用，即apachectl程序。

 

```
[root@linux ～]# /usr/sbin/apachectl start  <==启动
[root@linux ～]# /usr/sbin/apachectl stop   <==关闭 WWW 
```

 

一般建议您稍微记一下apachectl程序，因为很多认证、会考等都会用到，而且它也是Apache默认提供的一个管理指令。下面来看看有没有成功启动：

 

```
[root@linux ～]# netstat -tunl | grep ':80'
tcp   0      0 :::80    :::*    LISTEN
# port 80启动。接下来看看日志文件

[root@linux ～]# tail /var/log/httpd/error_log
[Mon Nov 06 00:38:42 2006] [notice] Apache/2.0.52 (CentOS) configured 
-- resuming normal operations
```

有时可能会启动出错, No such file or directory: httpd: could not open error log file /etc/httpd/logs/error\_log.

如下所示:

![](http://dl2.iteye.com/upload/attachment/0097/3089/97303fd5-a4ac-3ea5-9dee-2194ed9bdf6e.png)  
  

解决办法: 删除/etc/httpd/logs文件,重新创建logs文件即可(建议是备份该logs文件,然后新建)

 

这样即可成功启动Apache。接下来测试一下能否看到网页，首先看/var/www/html有没有数据？怎么没有呢，没关系，因为CentOS帮我们编写了一个测试页，所以您还需要在浏览器上输入这台主机的IP，如图18-3所示。

 

[![](http://new.51cto.com/files/uploadimg/20071205/1424450.jpg)](http://new.51cto.com/files/uploadimg/20071205/1424450.jpg) 图18-3 Apache主网页的测试界面

 

如果看到如图18-3所示的界面，恭喜您，您的Apache已经顺利启动了。可以开始制作自己的网页了。但如果想要知道有没有成功驱动PHP模块，那可以到/var/www/html目录下建立一个简单的文件：

 

```
[root@linux ～]# vi /var/www/html/phpinfo.php
phpinfo();
?>
```

 

“”是PHP模块的网页嵌入式程序语法，在这中间的就是PHP的程序代码。“phpinfo()”是特殊的PHP函数，可以显示出目前WWW服务器内PHP模块的相关核心数据。建立文件后立刻通过浏览器再查看此文件，效果如图18-4所示。

[![](http://new.51cto.com/files/uploadimg/20071205/1424451.jpg)](http://new.51cto.com/files/uploadimg/20071205/1424451.jpg) 图18-4 Apache网页的PHP测试界面