首页 > 代码库 > 【Linux】Apache服务配置

【Linux】Apache服务配置

一. URL 统一资源定位符

    http://www.sina.com.cn:80/admin/index.html

二. 环境安装

    LAMP 源码包编译安装  版本可以自定义 

    生产环境  安全 稳定 

    开发环境        

    LAMP 二进制包安装 yum 

三. 相关文件

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

   网页文件默认保存位置
              /usr/local/apache2/htdocs/

   日志保存位置 
              /usr/local/apache2/logs/

   日志处理(切割轮替)
        vim /etc/logrotate.conf
        /usr/local/apache2/logs/access_log {
            daily
            rotate 30
        }

        /usr/local/apache2/logs/error_log {
            daily
            rotate 30
        }

    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  目录别名    扩展网站目录  增加服务器

    1.修改主配置文件
    vim /usr/local/apache2/etc/httpd.conf
    453 Include etc//extra/httpd-autoindex.conf

    2.配置子配置文件
    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>

     3.建立www目录 
      mkdir /usr/local/apache2/www/
      vim /usr/local/apache2/www/index.html
       hello  /usr/local/apache2/www/         

     4.重启服务  测试 

        sto
        sta

        测试  192.168.183.251/www/

实验2 虚拟主机 

    1.域名解析  (文件解析)  (windows)
      C:\Windows\System32\drivers\etc\hosts

     192.168.183.251    www.sina.com
     192.168.183.251    www.sohu.com

    2.网站域名规划
      mkdir -p /share/sina/
      mkdir  /share/sohu/
      vim /share/sina/index.html
      vim /share/sohu/index.html

    3.修改配置文件
     vim /usr/local/apache2/etc/httpd.conf
     465 Include etc//extra/httpd-vhosts.conf

    4.修改子配置文件
     vim /usr/local/apache2/etc/extra/httpd-vhosts.conf
      23 <Directory "/share/sina/">
      24       Options Indexes
      25       AllowOverride None
      26       Require all granted
      27 </Directory>
      28     
      29 <Directory "/share/sohu/">
      30       Options Indexes
      31       AllowOverride None
      32       Require all granted
      33 </Directory> 

      35 <VirtualHost 192.168.183.251>
      36     ServerAdmin webmaster@sina.com
      37     DocumentRoot "/share/sina/"
      38     ServerName www.sina.com
      39     ErrorLog "logs/sina-error_log"
      40     CustomLog "logs/sina-access_log" common
      41 </VirtualHost>
      42 
      43 <VirtualHost 192.168.183.251>
      44     ServerAdmin webmaster@sohu.com
      45     DocumentRoot "/share/sohu/"
      46     ServerName www.sohu.com
      47     ErrorLog "logs/sohu-error_log"
      48     CustomLog "logs/sohu-access_log" common
      49 </VirtualHost>

    5.重启服务  测试
    sto
    sta

    测试  www.sina.com   www.sohu.com


    实验3 rewrite 重写/重定向  
        www.sina.com -> www.sohu.com   (301 永久重定向)

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

    147 LoadModule rewrite_module modules/mod_rewrite.so

    2.修改子配置文件(虚拟主机文件)
    vim /usr/local/apache2/etc/extra/httpd-vhosts.conf
     23 <Directory "/share/sina/">
     24       Options Indexes FollowSymLinks
     25       AllowOverride All
     26       Require all granted
     27 </Directory>

    3.建立权限文件.htaccess
     vim /share/sina/.htaccess
       1 RewriteEngine on  
       2 RewriteCond %{HTTP_HOST} www.sina.com
       3 REwriteRule .* http://www.sohu.com

    4.重启服务  测试
      sto
      sta

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


      网页文件跳转
      1.修改.htaccess           index().html    index.php
      vim /share/sina/.htaccess
       1 RewriteEngine on  
       2 REwriteRule index(\d+).html index.php?id=$1

      2.建立index.php 
       vim /share/sina/index.php
         1 <?php  echo "rewrite" ?>

      3.重启服务 测试
      sto
      sta

      测试  www.sina.com/index5.html

【Linux】Apache服务配置