Linux(八)Apache伺服器

來源:互聯網
上載者:User

標籤:

一 簡介

  1 www:world wide web 全球資訊網

    http 協議: 超文字傳輸通訊協定 (HTTP)

    HTML語言: 超文本標識語言

  2 URL:統一資源定位 協議+網域名稱:連接埠+網頁檔案名稱
    http://www.sina.com.cn:80/11/index.html

  3搭建www的伺服器的方法
    windows      IIS  +asp+SQLserver
         Internet Information server
    Linux  apache+mysql+php

二安裝
  1、lamp源碼安裝
    生產環境,技術要求高,複雜
  2、rpm包安裝 yum安裝
    httpd
    mysql
    mysql-server
    php
    php-devel
    php-mysql

三相關檔案

  apache設定檔
    源碼包安裝:/usr/lcoal/apache2/etc/httpd.conf
          /usr/local/apache2/etc/extra/*.conf

  rpm包安裝:/etc/httpd/conf/httpd.conf

  預設網頁儲存位置:
    源碼包:/usr/local/apache2/htdocs/
    rpm包安裝:/var/www/html/

  日誌儲存位置
    源碼包:/usr/local/apache2/logs/
    rpm包: /var/log/httpd/
    rpm包預設使用Tlog程式 /var下都會輪替 源碼包才需要設定
  Tlog:
    1日誌切割 apache內建日誌裡面內建日誌切割
    2日誌輪替 linux內建日誌管理logrorate.conf
      加入/usr/local/apache2/logs/access_log{
          daily
          rotate 30
        }
      logrotate -f /etc/logrotate.conf

    所有日誌都要進行日誌輪替

四設定檔

  vi /root/.bashrc
    alias sta=’/usr/local/apache2/bin/apachectl start’
    alias sto=’/usr/local/apache2/bin/apachectl stop’

  source /root/.bashrc

  注意:apache設定檔嚴格區分大小寫

  1針對主機環境的基本配置

    ServerRootapache主目錄
    Listen監聽連接埠
    LoadModule載入的相關模組

      User
      Group使用者和組
      ServerAdmin管理員郵箱
      ServerName伺服器名(沒有網域名稱解析時,使用臨時解析。不開啟)
      ErrorLog "logs/error_log錯誤記錄檔
      CustomLog "logs/access_log" common正確訪問日誌
      DirectoryIndex index.html index.php預設網頁檔案名稱,優先順序順序
      Include etc/extra/httpd-vhosts.conf子設定檔中內容也會載入生效

  2首頁目錄及許可權

    DocumentRoot "/usr/local/apache2//htdocs"
  首頁目錄

    <Directory "/usr/local/apache2//htdocs">
      #Directory關鍵字定義目錄許可權

      Options Indexes FollowSymLinks
      #options
        None:沒有任何額外許可權
        All: 所有許可權
        Indexes:瀏覽許可權(當此目錄下沒有預設網頁檔案時,顯示目錄內容)
        FollowSymLinks:准許軟串連到其他目錄
      AllowOverride None
      #定義是否允許目錄下.htaccess檔案中的許可權生效
        None:.htaccess中許可權不生效
        All:檔案中所有許可權都生效
        AuthConfig:檔案中,只有網頁認證的許可權生效。

      Require all granted存取控制清單 403錯誤 404錯誤

#定義此目錄的允許存取權限
例1:僅允許IP為192.168.1.1的主機訪問
Require all denied
Require ip 192.168.1.1

例子2.僅允許192.168.1.0/24網路的主機訪問
Require all denied
Require ip 192.168.1.0/24

例子3.禁止192.168.1.2的主機訪問,其他的都允許訪問,
<RequireAll>
Require all granted
Require not ip 192.168.1.2
</RequireAll>

例子4.允許所有訪問,
Require all granted

例子5.拒絕所有訪問,
Require all denied


  3目錄別名 用途 擴充網站目錄,增加伺服器,使用次層網域,使用目錄別名
      子設定檔名etc/extra/httpd-autoindex.conf

          Alias /icons/   "/usr/local/apache2//icons/"
         apache以為在這裡  實際目錄位置
    定義別名    /icons/----
      http://192.168.1.253/icons/

      <Directory "/usr/local/apache2//icons">
        Options Indexes MultiViews
        AllowOverride None
        Require all granted
      </Directory>

  4使用者認證
  限制特定目錄,只有指定使用者可以訪問。

    1)建立需要保護的目錄

       使用別名,在系統位置建立目錄,然後保護

       mkdir -p /share/soft

    2)修改設定檔,允許許可權檔案生效
      vi /usr/local/apache2/etc/httpd.conf
      Alias /soft/ "/share/soft/"

      <Directory "/share/soft">
        Options Indexes
        AllowOverride All        #開啟許可權認證檔案.htaccess
        Require all granted
      </Directory>

    重啟apache

    3)在指定目錄建立許可權檔案
    cd /share/soft

    vi .htaccess#不區分大小寫
      AuthName "50 docs"          #提示資訊
      AuthType basic              #加密類型
      AuthUserFile /share/apache.passwd    #密碼檔案,檔案名稱自訂。
      require valid-user            #允許密碼檔案中所有使用者訪問

  4)建立密碼檔案,加入允許訪問的使用者。使用者和系統使用者無關
    /usr/local/apache2/bin/htpasswd -c /share/apache.passwd test1
      -c 建立密碼檔案,只有添加第一個使用者時,才能-c
    /usr/local/apache2/bin/htpasswd -m /share/apache.passwd test2
      -m 再添加更多使用者時

5虛擬機器主機


1)分類
  基於IP的虛擬機器主機:一台伺服器,多個IP,搭建多個網站
  基於連接埠的虛擬機器主機:一台伺服器,一個ip,搭建多個網站,每個網路使用不同連接埠訪問
  基於名字的虛擬機器主機:一台伺服器,一個ip,搭建多個網站,每個網站使用不同網域名稱訪問

2)步驟:
  ①解析實驗網域名稱
    www.sina.com
    www.sohu.com
  C:\WINDOWS\system32\drivers\etc\hosts    windows
  /etc/hosts                          Linux


  ②規劃網站主目錄
      /share/sina--------------www.sina.com
      /share/sohu ------------ www.sohu.com

  ③ 修改設定檔
    vi /usr/local/apache2/etc/httpd.conf
    Include etc//extra/httpd-vhosts.conf
    #開啟虛擬機器主機設定檔
    vi /usr/local/apache2/etc/extra/httpd-vhosts.conf

 

<Directory "/usr/local/apache2/htdocs/sina">
  Options Indexes
  AllowOverride None
  Require all granted
</Directory>

<Directory "/usr/local/apache2/htdocs/sohu">
  Options Indexes
  AllowOverride None
  Require all granted
</Directory>

<VirtualHost 192.168.150.253>
#注意,只能寫ip
ServerAdmin [email protected]
#管理員郵箱
DocumentRoot "/usr/local/apache2/htdocs/sina"
#網站主目錄
ServerName www.sina.com
#完整網域名稱
ErrorLog "logs/sina-error_log"
#錯誤記錄檔
CustomLog "logs/sina-access_log" common
#訪問日誌
</VirtualHost>

<VirtualHost 192.168.150.253>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs/sohu"
ServerName www.sohu.com
ErrorLog "logs/sohu.com-error_log"
CustomLog "logs/sohu.com-access_log" common
</VirtualHost>

6rewrite重寫功能 URL
在URL中輸入一個地址,會自動跳轉為另一個

  1)網域名稱跳轉www.sina.com ------> www.sohu.com

    開啟虛擬機器主機,並正常訪問

    [[email protected] ~]# vi /usr/local/apache2/etc/httpd.conf
    LoadModule rewrite_module modules/mod_rewrite.so
    #開啟重寫模組,記得重啟apache

      修改設定檔,使sina目錄的.htaccess檔案生效

      [[email protected] etc]# vi extra/httpd-vhosts.conf

<Directory "/usr/local/apache2/htdocs/sina">
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>

  vi /usr/local/apache2/htdocs/sina/.htaccess
    RewriteEngine on                #開啟rewrite功能    
    RewriteCond %{HTTP_HOST} www.sina.com  #把以www.sina.com 開頭的內容賦值給HTTP_HOST變數
    RewriteRule .* http://www.sohu.com      #.* 輸入任何地址,都跳轉到http://www.sohu.com

  2)網頁檔案跳轉
    vi /usr/local/apache2/htdocs/sina/.htaccess
    RewriteEngine on
    RewriteRule index(\d+).html index.php?id=$1
    #輸入index(數值).html時,跳轉到index.php檔案,同時把數值當成變數傳入index.php

 

7常用子設定檔

  httpd-autoindex.confapache系統別名

  httpd-default.conf線程式控制制*

  httpd-info.conf 狀態統計網頁

  httpd-languages.conf語言編碼*

  httpd-manual.confapache協助文檔

  httpd-mpm.conf 最大串連數*

  httpd-multilang-errordoc.conf錯頁面*

  httpd-ssl.conf ssl安全通訊端訪問

  httpd-userdir.conf使用者主目錄配置

  httpd-vhosts.conf虛擬機器主機

 

Linux(八)Apache伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.