Apache的詳細配置

http 協議名
apache 軟件名 nginx

httpd (http daemon;http服務)

web 服務器


survey.netcraft.net --此網站會有每個月份的世界上網站使用的WEB服務器的使用率統計

官網:
www.apache.org



IBM apache軟件基金會 redhat oracle
websphere tomcat jboss weblogic



==========================================================================================================
學習環境配置:
準備一臺全新的虛擬機(centos7.3平臺),準備好本地yum源

# yum install httpd\* --安裝軟件包


# rpm -qa |grep httpd
httpd-manual-2.4.6-45.el7.centos.noarch
httpd-2.4.6-45.el7.centos.x86_64
httpd-devel-2.4.6-45.el7.centos.x86_64
httpd-tools-2.4.6-45.el7.centos.x86_64



主配置文件
ls /etc/httpd/conf/httpd.conf


啓動服務
默認端口:80
啓動前驗證沒有進程佔用80,若是有,先停掉或kill掉佔用的進程
# lsof -i:80
# netstat -ntlup |grep :80


# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd


啓動完成後,再驗證是不是你要啓動的進程佔用了80
# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 2312 root 4u IPv6 30417 0t0 TCP *:http (LISTEN)
httpd 2313 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)
httpd 2314 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)
httpd 2315 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)
httpd 2316 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)
httpd 2317 apache 4u IPv6 30417 0t0 TCP *:http (LISTEN)

# netstat -ntlup |grep :80
tcp6 0 0 :::80 :::* LISTEN 2312/httpd

======================================================================================================


例一:創建網站主頁(主頁文件優先於welcome歡迎頁面)
在網站根目錄下 創建一個主頁文件

echo 'main page' > /var/www/html/index.html 主頁文件的默認路徑

http://127.0.0.1/ --就能夠看到main page的信息,也就是直接訪問到主頁

把主頁文件寫成html標籤的格式以下:
<html>
<head>
<title>筆記站點</title>
</head>
<body>
<center><h1>歡迎來個人網站!@_@</h1></center>
</body>
</html>



例二:修改網站根目錄

# vim /etc/httpd/conf/httpd.conf

119 DocumentRoot "/www" --修改網站根目錄爲/www

131 <Directory "/www"> --把這個也對應的修改成/www



# mkdir /www
# echo "new main page" > /www/index.html

# systemctl reload httpd




--注意,不要開啓selinux,不然換新的家目錄的話,客戶端訪問會被拒絕



例三:修改主頁類型或加多個主頁類型並調整優先級


# vim /etc/httpd/conf/httpd.conf

164 DirectoryIndex index.php index.html

--使用瀏覽器訪問,發現沒有返回主頁信息,由於上面的意思爲:先找網站家目錄下的index.php,不存在話,再找index.html,都不存在,就表示找不到主頁

# echo 'php main page' > /www/index.php

客戶端測試會優先訪問index.php



例四:對php支持的簡單討論

# cat /www/index.php --把例三的index.php主頁文件改爲下面的一段簡單代碼
<?php
echo "hello world"
?>

客戶端測試訪問不到hello world,而是一個空白頁面(說明沒有php解釋器來解釋它)
解決方法:
# yum install php\* --要安裝php解釋器
# systemctl restart httpd

客戶端再測試,顯示的是hello world(說明php解釋成功)



例五:把家目錄裏的文件作成列表的形式

條件1.
Options Indexes FollowSymLinks --相應目錄的參數控制裏要有indexes參數
條件2.
沒有主頁文件
條件3.
符合上面兩個條件,就會訪問到redhat的歡迎頁面

vim /etc/httpd/conf.d/welcome.conf --註釋掉這個歡迎頁面,或者是刪除它,或者mv更名







例六
關於apache的標籤,容器(訪問控制)

Directory (目錄) Files(文件) Location (位置,url)
DirectoryMatch (目錄,匹配regex)
Files(文件,匹配regex)
Location (位置,url,匹配regex)


<Directory "/www">
Options indexes FollowSymLinks --容許列表,符號連接
AllowOverride None --不使用.htaccess控制
Require all granted --容許全部人訪問; Require all deined 拒絕全部人訪問
</Directory>



訪問控制寫法簡單總結(基於apache2.4版本):
Require all denied
Require ip 10.1.1.1 10.1.1.2 --拒絕全部,但容許10.1.1.1和10.1.1.2


<RequireAll>
Require all granted
Require not ip 10.1.1.1 10.1.1.2 --容許全部,但拒絕10.1.1.1和10.1.1.2
</RequireAll>



例七:對apache家目錄作一個基本驗證功能

1,
# vim /etc/httpd/conf/httpd.conf

<Directory "/www">
Options indexes FollowSymLinks
AllowOverride All --把None改成All(None表示此目錄的驗證功能不生效,All表示生效)
Require all granted
</Directory>


2,
# vim /www/.htaccess   --對哪一個目錄進行驗證,就在哪一個目錄下創建此文件

authname "please input your username and password! @_@"
authtype basic
authuserfile /etc/httpd/userpasswd
require valid-user


3,
# htpasswd -c /etc/httpd/userpasswd aaa  --建立此文件,並加入一個用戶,自定義密碼,注意此用戶與系統普通用戶無關(文件不存在,第一次建立須要加-c參數)
New password:
Re-type new password:
Adding password for user aaa


# htpasswd /etc/httpd/userpasswd bbb  --再增長一個用戶
New password:
Re-type new password:
Adding password for user bbb


4,#systemctl restart httpd


5,客戶端使用瀏覽器訪問測試



練習:針對一個子目錄(非家目錄)單獨作驗證




例八:關於Files和FilesMatch標籤 --支持正則匹配,一個是文件匹配,一個是目錄匹配

拒絕全部目錄下的1文件被訪問
<Files ~ "1">
Require all denied
</Files>



練習:網站下全部目錄(包括子目錄)以aaa開頭的文件都不能被訪問

<FilesMatch ~ "^aaa">
Require all denied
</FilesMatch>


練習:網站下全部目錄(包括子目錄)以.txt結尾的文件都不能被訪問

<FilesMatch ~ ".txt$">
Require all denied
</FilesMatch>


練習:網站下全部.gif .jpeg .png的圖片都被拒絕

<FilesMatch ~ "\.(gif|jpeg|png)$">
Require all denied
</FilesMatch>



例九:關於Location和LocationMatch的討論

<Location "/aaa">
Require all denied
</Location>

表示elinks http://IP/aaa這個訪問路徑會被拒絕


<LocationMatch "^/aa">
Require all denied
</LocationMatch>

表示elinks http://IP/aaa或http://IP/aab這樣以aa開頭的訪問路徑會被拒絕



例十:apache的alias跳轉

# vim /etc/httpd/conf/httpd.conf
Alias /yum "/yum" --前面/yum是location /yum ;後面的/yum是服務器根目錄下的yum子目錄

<Directory "/yum">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Require all granted
</Directory>


# systemctl restart httpd


客戶端elinks http://IP/yum 就能夠訪問到服務器的/yum目錄的文件




例十一:
錯誤頁面

準備一個圖片放到家目錄,好比一個404bg.jpg


# vim /etc/httpd/conf/httpd.conf --修改主配置文件,指向你的這個圖片路徑
ErrorDocument 404 /404bg.jpg

# systemctl restart httpd


客戶端測試,故意訪問一個不存在的頁面,觸發404錯誤,就會顯示上面的圖片



======================================================================================================


虛擬主機 --用apache或nginx就能夠作

一臺服務器跑多臺web服務


虛擬主機(用apache或nginx就能夠作) --》 VPS(virtual private server虛擬專用服務器)(虛擬化) --》 雲服務器 (雲計算)



mkdir /www/aaa
mkdir /www/bbb
echo "aaa main page" > /www/aaa/index.html
echo "bbb main page" > /www/bbb/index.html




例十二:基於ip的虛擬主機
# vim /etc/httpd/conf/httpd.conf --把下面一段直接加到最後的空白處

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/aaa
ServerName 10.1.1.2
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/bbb
ServerName 10.1.1.5
</VirtualHost>


# ifconfig eth0:0 10.1.1.5/24 --基於ip的虛擬主機,你這臺apache服務器得有訪問它們的多個ip,沒有的能夠虛擬出來

# systemctl restart httpd

客戶端訪問測試
elinks -dump http://10.1.1.2/ --獲得aaa main page
elinks -dump http://10.1.1.5/ --獲得bbb main page




例十三:基於port的虛擬主機
# vim /etc/httpd/conf/httpd.conf

listen 80 --默認有這一句,不須要再加
listen 8080 --這一句沒有,須要加上這一句


<VirtualHost *:80>
DocumentRoot /www/aaa
ServerName 10.1.1.2
</VirtualHost>

<VirtualHost *:8080>
DocumentRoot /www/bbb
ServerName 10.1.1.2
</VirtualHost>

# systemctl restart httpd

客戶端訪問測試
elinks -dump http://10.1.1.2/ --獲得aaa main page
elinks -dump http://10.1.1.5:8080/ --獲得bbb main page


例十四:基於域名的虛擬主機

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/aaa
ServerName news.web.com
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/bbb
ServerName sports.web.com
</VirtualHost>

# systemctl restart httpd


dns作CNAME,也就是news.web.com和sports.web.com都指向apache服務器ip(這裏過程省略),若是不想作dns,能夠直接在客戶端/etc/hosts裏綁定就能夠了


客戶端訪問測試
elinks -dump http://news.web.com/ --獲得aaa main page
elinks -dump http://sports.web.com/ --獲得bbb main pagephp

相關文章
相關標籤/搜索