您的位置:首页 > 运维架构 > Apache

CentOSLinux中Apache-PHP安装与服务配置

2011-04-07 09:59 363 查看
安装Apache服务器及相关组件

[root@sample ~]# yum -y install httpd\*  ← 在线安装httpd

为了使服务器开通HTTP服务后能够运行PHP编写的交互程序

[root@sample ~]# yum -y install php\*  ← 在线安装PHP

为了使PHP应用程序的执行效率大幅度提高需要安装Zend

[root@sample~]#wget http://downloads.zend.com/optimizer/3.0.1/ZendOptimizer-3.0.1-linux-glibc21-i386.tar.gz  ← 下载Zend的源代码

[root@sample ~]# tar zxvf ZendOptimizer-3.0.1-linux-glibc21-i386.tar.gz  ← 展开被压缩的源代码

[root@sample ~]# cd ZendOptimizer*   ← 进入Zend的源代码目录

[root@sample ZendOptimizer-3.0.1-linux-glibc21-i386]# ./install.sh  ← 运行安装脚本


配置HTTP(Apache)服务器


接下来,为了使服务器更安全以及更加符合实际要求,对默认的设置进行一些必要的更改。尤其在一些细节方面,越少向外界透露服务器的信息,就越能保证服务器的安全。

[root@sample ~]# vi etc/httpd/conf/httpd.conf  ← 编辑Apache的配置文件

ServerTokens OS  ← 找到这一行,将“OS”改为“Prod”(在出现错误页的时候不显示服务器操作系统的名称)
 ↓
ServerTokens Prod   ← 变为此状态

ServerSignature On  ← 找到这一行,将“On”改为“Off”
 ↓
ServerSignature Off  ← 在错误页中不显示Apache的版本

ServerAdmin root@localhost  ← 将管理员邮箱设置为自己常用的邮箱
 ↓
ServerAdmin yourname@yourserver.com  ← 根据实际情况修改默认设置
#ServerName new.host.name:80  ← 修改主机名
 ↓
ServerName www.centospub.com:80  ← 根据实际情况修改,端口号保持默认的80
Options Indexes FollowSymLinks  ← 找到这一行,删除“Indexes”,并添加“Includes”、“ExecCGI”
 ↓
Options Includes ExecCGI FollowSymLinks  ← 允许服务器执行CGI及SSI
#AddHandler cgi-script .cgi  ← 找到这一行,去掉行首的“#”,并在行尾添加“.pl”
 ↓
AddHandler cgi-script .cgi .pl  ← 允许扩展名为.pl的CGI脚本运行

AllowOverride None  ← 找到这一行,将“None”改为“All”
 ↓
AllowOverride All  ← 变为此状态,允许.htaccess

LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" combined  ← 找到这一行
 ↓
LogFormat “%h %l %u %t \”%!414r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" combined  ← 改为此状态(添加“!414”到规则中,对于过长的日志不记录)

AddDefaultCharset UTF-8  ← 找到这一行,在行首添加“#”
 ↓
#AddDefaultCharset UTF-8  ← 不使用UTF-8作为网页的默认编码
AddDefaultCharset GB2312  ← 并接着添加这一行(添加GB2312为默认编码)

<Directory “/var/www/icons”>  ← 找到这一个标签,并在标签中更改相应选项
  Options Indexes MultiViews  ← 找到这一行,将“Indexes”删除
    ↓
  Options MultiViews   ← 变为此状态(不在浏览器上显示树状目录结构)

[root@sample ~]# rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html  ← 删除测试页

启动HTTP服务

[root@sample ~]# chkconfig httpd on  ← 设置HTTP服务自启动

[root@sample ~]# chkconfig –list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off   ← 确认2–5为on的状态就OK

[root@sample ~]# /etc/rc.d/init.d/httpd start  ← 启动HTTP服务
Starting httpd:              [ OK ] ← 启动成功会出现OK

如果启动失败的话,会出现错误信息。原因可能是因为httpd.conf文件编辑过程中的失误,请检查httpd.conf。

对HTTP服务进行简单测试

[root@sample ~]# echo hello >> /var/www/html/index.html  ← 建立测试页

删除刚刚建立的测试页

[root@sample ~]# rm -f /var/www/html/index.html  ← 删除测试页

对HTTP服务进行全面测试

[1] 对HTML格式网页正确显示的测试

[root@sample ~]# vi /var/www/html/index.html  ← 建立测试页,内容如下:

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″>
<title>Hello,World!</title>
<body>
Hello,World!
</body>
</html>

在浏览器中输入“http://服务器IP地址”或者“http://你的域名”,如果出现“Hello,World!”,并且浏览器读取编码为简体中文,就OK。

[2] 对CGI的支持进行测试

[root@sample ~]# vi /var/www/html/test.cgi  ← 建立CGI测试页,内容如下:

#!/usr/bin/perl
print “Content-Type: text/html\n\n”;
print “<html><body>”;
print “Hello,World!CGI is working!<br>”;
print “</body></html>”;

[root@sample ~]# chmod 755 /var/www/html/test.cgi   ← 然后将CGI测试文件属性设置为755

在浏览器中输入“http://服务器IP地址/test.cgi”或者“http://你的域名/test.cgi”,如果正确显示“Hello,World!CGI is working!”,说明对于CGI的支持没有问题。

[3] 对PHP的支持进行测试

[root@sample html]# vi /var/www/html/test.php  ← 建立PHP测试文件,内容如下:

<?php
phpinfo();
?>

在浏览器中输入“http://服务器IP地址/test.php”或者“http://你的域名/test.php”后,正确的显示出了服务器上PHP的详细信息,说明对PHP可以正确的支持。

[4] 对SSI进行测试

[root@sample ~]# vi /var/www/html/test.shtml  ← 建立SSI测试页,内容如下:

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″>
<title>Hello,World!</title>
<body>
TEST SSI
<!–#config timefmt=”%Y/%m/%d %H:%M:%S” –>
<!–#echo var=”DATE_LOCAL” –>
</body>
</html>

在浏览器中输入“http://服务器IP地址/test.shtml”或者“http://你的域名/test.shtml”,如果正确显示当时的日期和时间,说明对于SSI的支持没有问题。

[5] 对.htaccess的支持进行测试

[root@sample ~]# vi /var/www/html/index.shtml  ← 建立.htaccess测试用的页,内容如下:

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″>
<title>Hello,World!</title>
<body>
The name of the file is <!–#echo var=”DOCUMENT_NAME” –>
</body>
</html>

在浏览器中输入“http://服务器IP地址”或者“http://你的域名”,如果显示“Forbidden”,说明.htaccess正常。

[6]建立一个.htaccess文件,并定义相应规则,如下:

[root@sample html]# vi /var/www/html/.htaccess  ← 建立.htaccess文件,内容如下:

DirectoryIndex index.shtml

在浏览器中输入“http://服务器IP地址”或者“http://你的域名”,如果正确显示“ The name of the file is index.shtml”,说明.htaccess中的规则生效状态,OK。

Apache 日志文件

[root@sample html]#vi /var/log/httpd/error_log ← Apache 日志文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: