Apache服務簡單使用

一、安裝

# yum install httpd -y
設置開機自啓動
# chkconfig --level 35 httpd on
啓動Apache服務
# /etc/init.d/httpd start
啓動Apache
打開瀏覽器訪問:http://ip
默認頁面
服務目錄 /etc/httpd
配置文件/etc/httpd/conf/httpd.conf
網站數據目錄/var/www/html
訪問日誌 /var/log/httpd/access_log
錯誤日誌 /var/log/httpd/error_log
配置文件簡要說明:
ServerRoot 服務目錄
ServerAdmin 管理員郵箱
User 運行服務用戶
Group 運行服務用戶組
ServerName 網站服務域名
DocumentRoot 網站數據目錄
Listen 監聽地址和端口號
DirectoryIndex 默認索引頁面
ErrorLog 錯誤日誌文件
CustomLog 訪問日誌文件
Timeout 網頁超時時間
Include 加載項

二、開啓個人用戶主頁功能

1.修改配置文件的mod_userdir(第360行)或者直接在/etc/httpd/conf.d增加userdir.conf配置文件

# vi /etc/httpd/conf/httpd.conf

<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
UserDir enable cy
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
# 
UserDir public_html

</IfModule>

mod_userdir

2.創建個人用戶網站數據

切換普通用戶
# su - cy
創建數據目錄
# mkdir -p public_html
寫入首頁文件內容:
# echo "This is cy's test web"> public_html/index.html
授權
# chmod -Rf 755 /home/cy/public_html

3.增加密碼安全驗證

設置密碼
# htppasswd -c /etc/httpd/passwd cy
修改配置文件的features Allow(302行)或者直接在/etc/httpd/conf.d/userdir.conf添加配置

# vi /etc/httpd/conf/httpd.conf

<Directory "/home/*/public_html">
AllowOverride all
authuserfile /etc/httpd/passwd
authname "my privately web"
authtype basic
Require user cy
</Directory>

features Allow
訪問地址:http://ip/~username
訪問地址

三、虛擬網站主機功能

1.基於IP地址

1.單網卡設置多個IP
2.分別創建網站數據目錄
# mkdir -p /var/www/html/252
# mkdir -p /var/www/html/253
# mkdir -p /var/www/html/254
3.寫入主頁文件
# echo "IP:172.27.9.252"> /var/www/html/252/index.html
# echo "IP:172.27.9.253"> /var/www/html/253/index.html
# echo "IP:172.27.9.254"> /var/www/html/254/index.html
4.配置基於IP的虛擬主機

在/etc/httpd/conf.d/添加vhosts.conf配置文件

<VirtualHost 172.27.11.252>
ServerAdmin root@localhost
DocumentRoot "/var/www/html/252"
ServerName 172.27.11.252
<Directory "/var/www/html/252">
    AllowOverride none
    Require all granted
</Directory>
</VirtualHost>
<VirtualHost 172.27.11.253>
ServerAdmin root@localhost
DocumentRoot "/var/www/html/253"
ServerName 172.27.11.253
<Directory "/var/www/html/253">
    AllowOverride none
    Require all granted
</Directory>
</VirtualHost>
<VirtualHost 172.27.11.254>
ServerAdmin root@localhost
DocumentRoot "/var/www/html/254"
ServerName 172.27.11.254
<Directory "/var/www/html/254">
    AllowOverride none
    Require all granted
</Directory>
</VirtualHost>

重啓Apache服務
# /etc/init.d/httpd restart
訪問地址:
http://ip1/
http://ip2/
http://ip3/

2.基於主機名

1.配置IP地址與hosts文件
# cat /etc/hosts

hosts文件

# mkdir -p /var/www/html/cy1
# mkdir -p /var/www/html/cy2
# mkdir -p /var/www/html/cy3
# echo  "cy1.moxiaokai.com"> /var/www/html/cy1/index.html
# echo  "cy2.moxiaokai.com"> /var/www/html/cy2/index.html
# echo  "cy3.moxiaokai.com"> /var/www/html/cy3/index.html

在/etc/httpd/conf.d/添加vhosts2.conf配置文件

<VirtualHost 172.27.11.252>
ServerAdmin root@localhost
DocumentRoot "/var/www/html/252"
ServerName "cy1.moxiaokai.com"
<Directory "/var/www/html/252">
    AllowOverride none
    Require all granted
</Directory>
</VirtualHost>
<VirtualHost 172.27.11.252>
ServerAdmin root@localhost
DocumentRoot "/var/www/html/253"
ServerName "cy2.moxiaokai.com"
<Directory "/var/www/html/253">
    AllowOverride none
    Require all granted
</Directory>
</VirtualHost>
<VirtualHost 172.27.11.252>
ServerAdmin root@localhost
DocumentRoot "/var/www/html/254"
ServerName "cy3.moxiaokai.com"
<Directory "/var/www/html/254">
    AllowOverride none
    Require all granted
</Directory>
</VirtualHost>  

重啓Apache服務
# /etc/init.d/httpd restart
訪問地址:
http://domain1/
http://domain2/
http://domain3/

3.基於端口

這裏不再贅述,和上述兩個配置差不多,請自行研究。

四、Apache訪問控制

可以基於主機名、IP地址及客戶端特徵做Apache網站訪問控制,更多的配置請自行研究啦 。
常用的指令:order、allow、deny、satisfy
按順序匹配規則並執行
設置僅IE瀏覽器訪問:

    <Directory "/var/www/html/cy1">
    SetEnvIf User-Agent "Internet Explorer" ie
    Order allow,deny
    Allow from env=ie
    </Directory>

設置僅允許172.27.8.252通過:

    <Directory "/var/www/html/cy1">
    Order allow,deny
    allow from 172.27.8.252
    deny from all
    </Directory>
發佈了91 篇原創文章 · 獲贊 132 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章