標籤:oca 錯誤 文檔 start 實驗 virtual 定義 var 其他
Apache伺服器
一 簡介
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:
#tail -f access_log 動態查看檔案
1日誌切割 apache內建日誌裡面內建日誌切割
2日誌輪替 linux內建日誌管理logrotate.conf加入
#vim /etc/logrotate.conf
/usr/local/apache2/logs/access_log{
daily
rotate 30
}
#logrotate -f /etc/logrotate.conf 手動運行
#cd /usr/local/apache2/logs/
#ls
所有日誌都要進行日誌輪替
四 設定檔
vi /root/.bashrc
alias sta=’/usr/local/apache2/bin/apachectl start’
alias sto=’/usr/local/apache2/bin/apachectl stop’
source /root/.bashrc
#sto
#sta
注意:apache設定檔嚴格區分大小寫
1 針對主機環境的基本配置
ServerRoot apache主目錄
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>
修改子設定檔
#vim /usr/local/apache2/extra/httpd-autoindex.conf
29 Alias /www/ "/usr/local/apache2/www/"
30 <Directory "/usr/local/apache2/www/">
31 Options Indexes
32 AllowOverride None
33 Require all granted
34 </Directory>
3)建立/usr/local/apache2/www目錄
#mkdir /usr/local/apache2/www
4)重啟服務 測試
#sto
#sta
測試 192.168.172.251/www/
4 使用者認證
限制特定目錄,只有指定使用者可以訪問。
1) 建立需要保護的目錄
使用別名,在系統位置建立目錄,然後保護
mkdir -p /share/soft
2)修改設定檔,允許許可權檔案生效
vi /usr/local/apache2/etc/httpd.conf
453 Include etc//extra/httpd-autoindex.conf
#vim /usr/local/apache2/etc/extra/httpd-autoindex.conf
37 Alias /soft/ "/share/soft/"
38 <Directory "/share/soft/">
39 Options Indexes
40 AllowOverride All
41 Require all granted
42 </Directory>
Alias /soft/ "/share/soft/"
<Directory "/share/soft">
Options Indexes
AllowOverride All #開啟許可權認證檔案.htaccess
Require all granted
</Directory>
重啟apache
3)在指定目錄建立許可權檔案
建立/share/soft/.htaccess許可權檔案
#vim /share/soft/.htaccess
1 AuthName "172PHP"
2 AuthType basic
3 AuthUserFile /share/apache.passwd
4 require valid-user
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 再添加更多使用者時
重啟服務 測試
#sto
#sta
測試 192.168.172.251/soft/
5 虛擬機器主機
1)分類
基於IP的虛擬機器主機: 一台伺服器,多個IP,搭建多個網站
基於連接埠的虛擬機器主機: 一台伺服器,一個ip,搭建多個網站,每個網路使用不同連接埠訪問
基於名字的虛擬機器主機: 一台伺服器,一個ip,搭建多個網站,每個網站使用不同網域名稱訪問
2)步驟:
① 解析實驗網域名稱
192.168.172.251 www.sina.com
192.168.172.251 www.sohu.com
C:\WINDOWS\system32\drivers\etc\hosts windows
/etc/hosts Linux
② 規劃網站主目錄
#mkdir /share/sina/
#mkdir /share/sohu/
#vim /share/sina/index.html
#vim /share/sohu/index.html
/share/sina--------------www.sina.com
/share/sohu ------------ www.sohu.com
③ 修改設定檔
vi /usr/local/apache2/etc/httpd.conf
465 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>
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>
<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>
35 <VirtualHost 192.168.172.251>
36 ServerAdmin [email protected]
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.172.251>
44 ServerAdmin [email protected]
45 DocumentRoot "/share/sohu/"
46 ServerName www.sohu.com
47 ErrorLog "logs/sohu-error_log"
48 CustomLog "logs/sohu-access_log" common
49 </VirtualHost>
重啟服務 測試
#sto
#sta
測試 www.sina.com www.sohu.com
6 rewrite 重寫功能 URL
在URL中輸入一個地址,會自動跳轉為另一個
1)網域名稱跳轉 www.sina.com ------> www.sohu.com
開啟虛擬機器主機,並正常訪問
# vi /usr/local/apache2/etc/httpd.conf
147LoadModule rewrite_module modules/mod_rewrite.so
#開啟重寫模組,記得重啟apache
修改設定檔,使sina目錄的.htaccess檔案生效
# vi extra/httpd-vhosts.conf
<Directory "/usr/local/apache2/htdocs/sina">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
#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>
vi /usr/local/apache2/htdocs/sina/.htaccess
建立/share/sina/.htaccess
#vim /share/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
1 RewriteEngine on
2 RewriteCond %{HTTP_HOST} www.sina.com
3 RewriteRule .* http://www.sohu.com
重啟服務 測試
#sto
#sta
測試 www.sina.com -> 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
#vim /share/sina/.htaccess
1 RewriteEngine on
2 RewriteRule index(\d+).html index.php?id=$1
#cd /share/sina
#vim index.php
<?php
echo "yemian rewrite";
?>
測試 www.sina.com/index5.html
7 常用子設定檔
httpd-autoindex.conf apache系統別名
httpd-default.conf 線程式控制制 *
httpd-info.conf 狀態統計網頁
httpd-languages.conf 語言編碼 *
httpd-manual.conf apache協助文檔
httpd-mpm.conf 最大串連數 *
httpd-multilang-errordoc.conf 錯頁面 *
httpd-ssl.conf ssl安全通訊端訪問
httpd-userdir.conf 使用者主目錄配置
httpd-vhosts.conf 虛擬機器主機
8. apache服務實驗筆記