Apache配置必配基础

一、绑定域名到子目录

在httpd.conf文件末尾添加

#不同的域名对应到的目录
<VirtualHost *:80>
    DocumentRoot "D:\wamp\www\batsing"
    ServerName www.batsing.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "D:\wamp\www\getchar"
    ServerName www.getchar.cn
</VirtualHost>
#本地访问
<VirtualHost *:80>
    DocumentRoot "D:\wamp\www"
    ServerName localhost
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "D:\wamp\www"
    ServerName 127.0.0.1
</VirtualHost>
#IP访问,主要用于开发环境手机连接WIFI访问电脑的
#服务器上一般不要允许IP访问
<VirtualHost *:80>
    ServerName 192.168.1.129
    DocumentRoot "D:\wamp\www"
</VirtualHost>

 二、Apache 外部访问(如WIFI),出现403错误

打开httpd.conf文件,找到

Require local

改成

#   Require local
    Require all granted

即是注释掉允许本机访问,改成允许外部访问,然后重启Apache

三、使Apache支持 .htaccess URL重写

打开httpd.conf文件

查找 

#LoadModule rewrite_module modules/mod_rewrite.so

把#去掉;
查找

# AllowOverride controls what directives may be placed in .htaccess files.

把它下面的 AllowOverride None改成 AllowOverride all
重启Apache,即可支持URL重写

四、Linux 下 Apache基本操作命令

基本的操作方法:
如果apache安装成为linux的服务的话,可以用以下命令操作:

service httpd start #启动
service httpd restart #重新启动
service httpd stop #停止服务

不是linux服务的话:
本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况

apahce启动命令:
推荐/usr/local/apache2/bin/apachectl start apaceh启动
apache停止命令
/usr/local/apache2/bin/apachectl stop   停止
apache重新启动命令:
/usr/local/apache2/bin/apachectl restart 重启
要在重启 Apache 服务器时不中断当前的连接,则应运行:
/usr/local/sbin/apachectl graceful

五、服务器上禁止IP(和其它非法域名)访问

#禁止IP所有非法域名直接访问
<VirtualHost *:80>
    ServerAlias *
    <Location />
        Order Allow,Deny
        Deny from all
    </Location>
</VirtualHost>

六、多域名绑定到同一个目录

<VirtualHost *:80>
    DocumentRoot "D:\wamp\www\getchar"
    ServerName www.getchar.com
    ServerAlias getchar.com
    ServerAlias www.getchar.cn
    ServerAlias getchar.cn
#    DirectoryIndex index.php #默认首页
    ErrorDocument 400 /errpage/400.html #各种错误页
    ErrorDocument 403 /errpage/403.html
    ErrorDocument 404 /errpage/404.html
    ErrorDocument 405 /errpage/405.html
</VirtualHost>

如果没有特别要求,也可以绑定移动版 m.getchar.com 等到同一目录

七、手机浏览器自动跳转到移动版

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.batsing.com$ [NC]  # 要限定PC版的域名,不然会使手机端产生重定向循环错误
RewriteCond
%{HTTP_USER_AGENT} "Mobile" [NC] # 含Mobile字眼的浏览器(包括微信、UC移动、QQ移动、Safari移动、安卓原生等) RewriteRule ^(.*)$ http://m.batsing.com$1 [R=301,NC,L] #跳转到移动端对应的地址 # RewriteRule ^(.*)$ http://m.batsing.com [R=301,NC,L] #跳转到移动端的首页

说明:以上所有的配置中,都是使用shell语法,.htaccess保存立即生效,httpd.conf需要重启Apache才能生效。

你可能感兴趣的:(Apache配置必配基础)