Apache服务器配置详解

配置文件 /usr/local/apache2/etc/httpd.conf 主配置文件
/usr/local/apache2/etc/extra/httpd-*.conf 子配置文件

网站根目录 /usr/local/apache2/htdocs/

日志文件目录

/usr/local/apache2/logs/
/var/log/

# tail -f access_log 动态查看日志文件

日志处理
# vim /etc/logrotate.conf

 35 /usr/local/apache2/logs/access_log {
 36         daily
 37         rotate 30
 38 }
 39 
 40 /usr/local/apache2/logs/error_log {
 41         daily
 42         rotate 30
 43 }

# logrotate -f /etc/logrotate.conf
# cd /usr/local/apache2/logs/
# ls

命令别名 alias
# vim /root/.bashrc

alias sto='/usr/local/apache2/bin/apachectl stop'
alias sta='/usr/local/apache2/bin/apachectl start'

# source /root/.bashrc
# sto
# sta

实验1 目录别名 Alias
1)修改配置文件
# vim /usr/local/apache2/etc/httpd.conf

454 Include etc//extra/httpd-autoindex.conf

2)修改子配置文件httpd-autoindex.conf
# vim /usr/local/apache2/etc/extra/httpd-autoindex.conf

 29 Alias /www/  "/usr/local/apache2/www/"
 30 
 31 <Directory "/usr/local/apache2/www/">
 32     Options Indexes
 33     AllowOverride None
 34     Require all granted
 35 </Directory>

# cd /usr/local/apache2/
# mkdir www

# sto
# sta

3)测试 192.168.159.251/www/

实验2 用户认证
1)建立认证目录
# mkdir -p /share/soft/

2)修改配置文件
# vim /usr/local/apache2/etc/httpd.conf

454 Include etc//extra/httpd-autoindex.conf

# vim /usr/local/apache2/etc/extra/httpd-autoindex.conf

 29 Alias /soft/  "/share/soft/"
 30 
 31 <Directory "/share/soft/">
 32     Options Indexes
 33     AllowOverride All
 34     Require all granted
 35 </Directory>

3)建立.htaccess文件
# vim /share/soft/.htaccess

  1 AuthName " PHP"
  2 AuthType basic
  3 AuthUserFile /share/apache.passwd
  4 require valid-user

4)添加用户
# /usr/local/apache2/bin/htpasswd -c /share/apache.passwd t1
# /usr/local/apache2/bin/htpasswd -m /share/apache.passwd t2

# sto
# sta
5)测试 192.168.159.251/soft/

实验3 虚拟主机
1)域名解析 文件解析
C:\Windows\System32\drivers\etc\hosts
192.168.159.251 www.sina.com
192.168.159.251 www.sohu.com

2)规划网站根目录
# mkdir /share/sina/
# mkdir /share/sohu/
# vim /share/sina/index.html
# vim /share/sohu/index.html

3)修改配置文件
# vim /usr/local/apache2/etc/httpd.conf

466 Include etc//extra/httpd-vhosts.conf

# vim /usr/local/apache2/etc/extra/httpd-vhosts.conf

<Directory "/share/sina">
    Options Indexes
    AllowOverride None
    Require all granted
</Directory>

<Directory "/share/sohu">
    Options Indexes
    AllowOverride None
    Require all granted
</Directory>

<VirtualHost 192.168.159.251>
    ServerAdmin webmaster@sina.com
    DocumentRoot "/share/sina"
    ServerName www.sina.com
    ErrorLog "logs/sina-error_log"
    CustomLog "logs/sina-access_log" common
</VirtualHost>

<VirtualHost 192.168.159.251>
    ServerAdmin webmaster@sohu.com
    DocumentRoot "/share/sohu"
    ServerName www.sohu.com
    ErrorLog "logs/sohu-error_log"
    CustomLog "logs/sohu-access_log" common
</VirtualHost>

# sto
# sta

4)测试 www.sina.com www.sohu.com

实验4 rewrite 重写 重定向

www.sina.com -> www.sohu.com

1)修改配置文件
# vim /usr/local/apache2/etc/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

2)修改子配置文件
# vim /usr/local/apache2/etc/extra/httpd-vhosts.conf

 24 <Directory "/share/sina">
 25     Options Indexes FollowSymLinks
 26     AllowOverride All
 27     Require all granted
 28 </Directory>

3)建立/share/sina/.htaccess文件
# vim /share/sina/.htaccess

  RewriteEngine on
  RewriteCond %{HTTP_HOST} www.sina.com
  RewriteRule .*  http://www.sohu.com

# sto
# sta

4)测试 www.sina.com -> www.sohu.com

网页文件跳转
# vim /share/sina/.htaccess

   RewriteEngine on
   RewriteRule index(\d+).html  index.php?id=$1

# vim /share/sina/index.php

测试: www.sina.com/index5.html