apache常见问题(安装,配置认证,二级目录,虚拟目录,代理,HTML跳转代码)


YUM安装PHP+apache

yum install php     //会自动安装httpd
yum install php-gd php-mcrypt php-mbstring php-mysql //安装一些php扩展支持

安装apache带Module

./configure --prefix=/opt/apache --enable-mods-shared=most
make
make install

其它用户启动apache

chmod u+s httpd
chown root httpd
注:chmod命令+s:在文件执行时把进程的属主或组ID置为该文件的文件属主。u+s后其他用户都可享有文件属主的权限。通过chown将文件属主调整为root,这样其他用户即可以root权限操作该文件。

某个目录编码设置

<Directory /var/www/ivggamevideo/>
...
IndexOptions Charset=GB2312
...
</Directory>

Apache Option选项

Options:开启或关闭站点不同部分功能的主要工具之一,灵活运用这条指令可以让你牢牢控制住在每个目录中允许什么功能或不允许什么功能。option[+|-]选项
option +ExecCGI 开启在指定范围内执行CGI程序的功能。
option +FollowSymlinks 出现在apache所提供目录中的符号链接都会忽略掉。
option +Indexes 使那些没有index.html文件的目录能够产生自动索引。
option +Multiview 摒弃大部分你对客户端所获内容的控制,而完全听从客户端的指令,当Multiview开启后,Apache将搜索和被请求的资源匹配的文件目录,并动态为这个资源创建一个类型映射。例:请求一个
http://www.example.com/testing/index的URL,Apache将在testing目录里面搜索名字为index开头的文件。同时假设使用某一条指令,并使用任何一个添加到这个文件上的关联。

AllowOverride参数详解

通常利用Apache的rewrite模块对 URL 进行重写的时候, rewrite规则会写在.htaccess 文件里。但要使 apache 能够正常的读取.htaccess 文件的内容,就必须对.htaccess 所在目录进行配置。从安全性考虑,根目录的AllowOverride属性一般都配置成不允许任何Override,即:AllowOverride None 在 AllowOverride 设置为 None 时, .htaccess 文件将被完全忽略。当此指令设置为 All 时,所有具有 “.htaccess” 作用域的指令都允许出现在 .htaccess 文件中。而对于 URL rewrite 来说,至少需要把目录设置为:AllowOverride FileInfo

隐藏apache版本信息

ServerTokens Prod
ServerSignature Off

修改apache默认字符编码 - AddDefaultCharset utf-8,把其修改成AddDefaultCharset gbk - 把AddDefaultCharset utf-8(这里也可能是其他字符集)修改成AddDefaultCharset off,也就是关掉默认使用的字符集,这样apache就可以根据网页中的meta信息来选择 - 二级目录单独设置字符集:IndexOptions Charset=GB2312

取消默认页面,开启目录目录浏览

开启目录浏览,关闭noindex
vi /etc/httpd/conf.d/welcome.conf
Options Indexes
IndexOptions FancyIndexing HTMLTable ScanHTMLTitles FoldersFirst NameWidth=85 DescriptionWidth=128 IconWidth=16 IconHeight=16 VersionSort Charset=UTF-8
# ErrorDocument 403 /error/noindex.html

配置基本认证

AllowOverride None
改成
AllowOverride AuthConfig
AuthName "test user"
AuthType Basic
AuthUserFile /test/.htpasswd
require valid-user
/opt/apache/bin/htpasswd -c /test/.htpasswd test

chcon -t httpd_sys_content_t /u01 失败的话
chcon -h system_u:object_r:httpd_sys_content_t /u01
chcon -R -h root:object_r:httpd_sys_content_t /u01/*

配置代理请求

NameVirtualHost *:80
<VirtualHost *:80>
ServerName issue.xxxx.net
ProxyIOBufferSize 8192
ProxyRequests Off
ProxyVia Full
ProxyPass / http://www.xxxx.net:8080/
</VirtualHost>
<VirtualHost *:80>
ServerName wiki.xxxx.net
ProxyIOBufferSize 8192
ProxyRequests Off
ProxyVia Full
ProxyPass / http://www.xxxx.net:8081/
# ProxyPass / ajp://issue.xxx.com:8089/ smax=5 max=20 ttl=120 retry=300
</VirtualHost>

配置二级目录

vi /etc/httpd/conf/httpd.conf
Alias /test "/test"
<Directory "/test">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
===============
<VirtualHost *:80>
    ServerName assistant.myivg.com
    DocumentRoot "/home/ivg_logo_dev/web"
<Directory "/home/ivg_logo_dev/web">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
   Alias /api/game "/home/gm_console/game_events/api"
<Directory "/home/gm_console/game_events/api">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
    ErrorLog "/var/log/httpd/80-error_log"
    CustomLog "/var/log/httpd/80-access_log" common
</VirtualHost>

禁止使用IP访问网站

namevirtualhost *:80
<VirtualHost *:80>
ServerName starsliao.com
DocumentRoot C:/com/httpd/htdocs/
</VirtualHost>
<VirtualHost *:80>
ServerName www.starsliao.com
DocumentRoot C:/com/httpd/htdocs/
</VirtualHost>
<virtualhost *:80>
servername 77.88.99.66
<location />
order deny,allow
deny from all
</location>
</virtualhost>
<virtualhost *:80>
servername 192.168.79.8
<location />
order deny,allow
deny from all
</location>
</virtualhost>

配置虚拟目录

NameVirtualHost *:80
<VirtualHost *:80>
ServerName issue.xxxx.net
DocumentRoot /var/www/html/jira
</VirtualHost>
<VirtualHost *:80>
ServerName wiki.xxxx.net
DocumentRoot /var/www/html/wiki
</VirtualHost>
<VirtualHost *:80>
ServerName www.xxxx.net
ServerAlias *
DocumentRoot /var/www/html/
</VirtualHost>

apache+svn 优化参数:

Timeout 180
KeepAlive on
MaxKeepAliveRequests 0
KeepAliveTimeout 30
MaxRequestsPerChild  4000

允许指定IP用户访问网站

<LocationMatch "^/(?i:(?:xampp|phpmyadmin|security|licenses|webalizer|server-status|server-info))">
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/8 \\
                   192.168.1.31 192.168.1.32 192.168.1.50 192.168.1.51 192.168.1.36 192.168.1.200

        ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</LocationMatch>

HTML跳转代码

不隐藏URL:
<html>
<meta http-equiv="refresh" content="0;url=http://www.XXXX.net:8080">
</html>
隐藏URL:
<html>
<frameset framespacing="0"border="0"rows="0" frameborder="0">
<framename="main"src="http://www.xxxx.net:8081" scrolling="auto"noresize>
</frameset>
</html>
带进度条:

<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<title>testweb</title>
</head>
<body>
<form name=loading>
<P align=center><FONT face=Arial color=#0066ff size=4>Loading...<FONT>
<INPUT style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-WEIGHT: bolder; PADDING-BOTTOM: 0px; COLOR: #0066ff; BORDER-TOP-style: none; PADDING-TOP: 0px; FONT-FAMILY: Arial; BORDER-RIGHT-style: none; BORDER-LEFT-style: none; BACKGROUND-COLOR:white; BORDER-BOTTOM-style: none" size=46 name=chart>
<BR>
<INPUT style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; COLOR: #0066ff; BORDER-BOTTOM:medium none; TEXT-ALIGN: center" size=47 name=percent>
<script language="javascript">
var bar=0
var line="||"
var amount="||"
count()
function count(){
bar=bar+2
amount =amount + line
document.loading.chart.value=amount
document.loading.percent.value=bar+"%"
if (bar<99){
setTimeout("count()",100);
}else{
window.location = "http://www.xxx.net:8080";
}
}
</script>
</P>
</form>
</body>
</html>