apache

HTTP:hypertext transfer protocol:超文本傳輸協議

超鏈接:

web:
http/0.9:僅純文本(超鏈接),ASCII
HTML:hypertext mark language,超文本標記語言
<h2>Title</h2>

browser:客戶端

1.1.1.1:web,a.html
2.2.2.2:web,a.html

URI:uniform resource indentifier,統一資源標識符,全局範圍內
統一:路徑格式上的統一

URL:uniform resource locator,統一資源定位符

protocol://HOST:port/path/to/file

http://www.magedu.com/download/linux.tar.gz

web資源:http://www.magedu.com/logo.gif
多個資源很可能被整合爲一個html文檔

web對象:

HTTP方法:
GET

http/1.0:PUT,POST,DELETE

MIME:multipurpose internet mail extension,多用途互聯網郵件擴展

SMTP:simple mail transmission protocol,純文本
MIME:將非文本數據在傳輸前重新編碼爲文本格式,接收方能夠用相反的方式將其重新還原爲原來的格式,還能夠調用相應的程序來打開此文件
base64

協議首部:
image/jpeg

動態效果:ActiveX
Java,applet,jre

動態網頁:服務器端存儲的文檔非HTML格式,而是編程語言開發的腳本,腳本接受參數之後在服務器運行一次,運行完成之後會生成HTML格式的文檔,把生成的文檔發給客戶端

web:index.php

web --》procotol --》 php(運行index.php)

http

index.html:
引用N個web對象,URL

動態網頁:包含靜態內容和動態內容
動態內容部分才需運行

緩存:

阻塞
非阻塞

IP:
source ip
destination ip
TCP:
source port
destination port

http報文:請求報文,響應報文

http首部:
GET /2.html
Host:www.magedu.com(虛擬主機)

URL:http://www.magedu.com/2.html

請求報文語法:
<method><request-URL> <version>
<headers>

<entity-body>

響應報文語法:
<version><status> <reason-phrase>
<headers>

<entity-body>

狀態代碼:
1xx:純信息
2xx:"成功"類的信息(200,201,202)
3xx:重定向類的信息(301,302,304)
4xx:客戶端錯誤類的信息(404)
5xx:服務器端錯誤類的信息

請求報文:
GET / HTTP/1.1
Host:www.magedu.com
Connection: keep-alive

響應報文:
HTTP/1.1 200 ok
x-powered-by: PHP/5.2.17
Vary:accept-encoding,cookie,user-agent
cache-control:max-age=3,must-revalidate
content-length:6931

web服務器的主要操作
1.建立連接
2.接收請求
3.處理請求
4.訪問資源
5.構建響應
6.發送響應
7.記錄日誌

5s:10 image,3 css,5html
18資源

http,tcp:三次握手,四次斷開

http/1.1:
增強了緩存的功能
長連接

單進程/單線程
多進程/多線程

事件驅動
通知

c/s
c:clinet agent(browser,spider)
s:server

client-》request-》server
URL
server-》response-》client

HTTP Method
GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECTION

server操作
server模型
http,MPM:
prefork
work
event

Client:
IE
Firefox
Chrome
Opera
Safari

Server:
Apache-->httpd
IIS
nginx
lighttpd
thttpd

應用程序服務器:
IIS
tomcat(apache,JSP,open source)
websphere(IBM,JSP,commodity)
weblogic(Oracle,JSP,commodity)
JBoss(RedHat)

www.netcraft.com:各種web服務器所佔的份額

Apache: 17,
NCSA,httpd
A patchy server = apache

FSF:GUN,GPL
ASF:Apache software foundation
web:httpd
tomcat
Hadoop

www.apache.org
web:http://httpd.apache.org

httpd:
web server,open source
2.2,2.4,2.0,1.3

httpd:
事先創建進程
按需維持適當的進程
模塊化設計,核心比較小,各種功能都模塊添加(包括php)
支持運行配置,支持單獨編譯模塊
支持多種方式的虛擬主機配置

虛擬主機:
物理服務器
web程序也只有一個
卻可以服務多個不同的站點
socket ip:port
基於ip的虛擬主機;
基於端口的虛擬主機;
基於域名的虛擬主機;

protocol://HOST:port/path/to/source
Method URL version
header

body

GET /download/linux.tar.bz2 HTTP/1.0
Host: www.magedu.com

支持https協議(mod_ssl)
支持用戶認證
支持基於ip或主機名的ACL
支持每目錄的訪問控制
支持URL重寫,/image/a.jpeg,/bbs/images/abc.jpeg

RedHat(httpd):
rpm包
源碼編譯

httpd:SELinux(事先讓其處於permssive,disabled)

httpd:
/usr/sbin/httpd(MPM:prefork)
httpd:root,root(master process)
httpd:apache,apache(worker process)
/etc/rc.d/init.d/httpd
port:(80/tcp),(ssl:443/tcp)
/etc/httpd:工作跟目錄,相當於程序安裝目錄
/etc/httpd/conf:配置文件目錄
主配置文件:httpd.conf
/etc/httpd/conf.d/*.conf
/etc/httpd/modules:模塊目錄
/etc/httpd/logs--》/var/log/httpd:日誌目錄
日誌文件有兩類:訪問日誌access_log,錯誤日誌:err_log

/var/www
html:靜態頁面
cgi-bin:動態內容

CGI:common gateway interface,通用網關接口
client --》httpd(index.cgi)--》spawn process(index.cgi)--》httpd --》client
Perl,Python,Java,(servlet,jsp),php

fastcgi:就是httpd主進程創建一批空閒的進程放在這裏,等用戶請求動態內容的時候,直接調用這個空間進程去響應。

程序:指令和數據
數據,數據庫服務
CPU-bound

apache
php
mysql
LAMP

httpd:
directive value
指令不區分字符大小寫
value則根據需要有可能要區分

LoadRunner

MPM:multi path modules,多處理模塊
mpm_winnt
prefork(一個請求用一個進程響應)
worker(一個請求用一個線程響應,(啓動多個進程,每個進程生成多個線程))
event(一個進程處理多個請求)

httpd -l
httpd.worker -l
httpd.event -l
httpd -M
vim /etc/sysconfig/httpd

安裝配置:
yum install httpd httpd-manual
rpm -ql httpd
service httpd start
netstat -tunlp | grep ':80'
ps aux | grep 'httpd'
cd /etc/httpd/conf.d
mv welcome.conf welcome.conf.bak
cd /var/www/html
vim a.html
<html>
<title>hello world</title>
<h1>haha</h1>
welcome to our website
</html>

cd /etc/httpd/conf
vim httpd.conf
<IfModule prefork.c>
</IfModule>
Listen 80
Listen 172.16.100.1:8080

LoadModule

DocumentRoot "/var/www/html"

URL路徑跟本地文件系統路徑不是一碼事兒,URL是相對於DocumentRoot的路徑而言

<Directory>
options
None:不支持任何選項
indexes:允許索引目錄
FollowSynLink:允許訪問符號鏈接指向的原文件
Includes:允許支持服務端包含(SSI)
ExecCGI:允許執行CGI腳本
All:支持所有選項

AllowOverride AuthConfig
AuthType Basic
AuthName "Restricted Site..."
AuthUserFile "/etc/httpd/conf/htpasswd"
AuthGroupFile "/etc/httpd/conf/htgroup"
Require valid-user
或者 Require user hadoop
或者 Require group myusers

Order deny,allow
Allow from all

</Directory>

Order:用於定義基於主機的訪問功能的,IP,網絡地址或主機定義訪問控制機制
Order allow,deny
allow from
deny from

192.168.0.0/24

Order allow,deny
allow from 192.168.0.0/24

Order deny,allow
Deny from 192.168.0.0/24

elinks http://172.16.100.1
-dump
-source

htpasswd -c -m /etc/httpd/conf/htpasswd hadoop
htpdasswd -m /etc/httpd/conf/htpasswd tom
httpd -t

vim /etc/httpd/conf/htgroup
myusers: hadoop tom

地址的表示方式:
IP
network/netmask
HOSTNAME:www.a.com
DOMAINNAME;magedu.com
Partial IP:172.16,172.16.0.0/16

vim /etc/httpd/conf/htgroup
myusers:hadoop tom

#UserDir disable
UserDir public_html

su - hadoop
mkdir public_html
cd public_html
vim index.html
chmod o+x public_html
service httpd restart

hadoop
http://172.16100.1/~tom/

#AccessFileName .htaccess

%h:客戶端地址
%l:訪問用戶名
%u:認證登陸的用戶名
%t:登陸時間
%r:請求報文第一行
%s:狀態碼
%b:響應報文大小
%{Referer}i:從哪個頁面跳轉到這個頁面的

PV:page view,每天的頁面訪問量
UV:user view,每天的獨立ip訪問量

/web/html
bbs/images

/www/forum bbs
http://172.16.100.1/bbs/images/logo.jpeg

定義別名:可以把客戶端訪問的目錄定義在與根不用的路徑下面
mkdir /bbs/forum

vim /etc/hhtpd/conf/httpd.conf
Alias /luntan "/bbs/forum"或者Alias /luntan/ "/bbs/forum/"
vim /bbs/forum/index.html

/luntan目錄不需要存在

回顧:
定義網站文檔目錄
訪問選項:options
基於主機的訪問控制
基於用戶或組的訪問控制
用戶個人站點
錯誤日誌
日誌格式
訪問日誌 PV UV
路徑別名

apache虛擬主機:
apache:服務器,Host,物理主機

虛擬主機:
apache服務:
服務於多個不同的站點

apache:
中心主機
虛擬主機
基於IP
IP1:80
IP2:80
基於端口
IP:80
IP:8080
基於域名
IP:80
主機名不同
www.magedu.com
www.a.org
www2.a.org
www.b.net

apache 2.2
NameVirtualHost

apache 2.4

ServerName
ServerAlias
DocumentRoot /www/a.org/
<Directory "/www/a.org">
Option
Allowoverride

</Directory>
Alias
Errorlog
CustomLog

<Location "/images">

</Location>
ScritAlias

/www/test
www.a.org/images/

CGI:Common Gateway Interface

客戶端動態
服務器端動態

id -un:顯示當前用戶名

得先取消中心主機,註釋中心主機的DocumentRoot即可
虛擬主機的定義:
<VirtualHost Host>

</VirtualHost>

基於IP,HOST的寫法
HOST
IP1:80
IP2:80

基於端口:
HOST
IP:80
IP:8080

基於域名:
*:80
ServerName 不同

vim httpd.conf
#DocumentRoot "/var/www/html"

vim /conf.d/virtual.conf
<VirtualHost 172.16.100.1:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
</VirtualHost>

<VirtuaHost 172.16.100.2:80>
ServerName www.a.org
DocumentRoot "/www/a.org"
</VituaHost>

mkdir /www/magedu.com
vim index.html
<title>MageEdu</title>
<h1>magedu.com</h1>

mkdir /www/a.org
vim index.html
<title>A</title>
<h1>a.org</h1>

ip addr add 172.16.100.2/16 dev eth0
ip add show

vim httpd.conf
Listen 80
Listen 8080

vim /conf.d/virtual.conf
<VirtuaHost 172.16.100.1:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
</VituaHost>

<VirtuaHost 172.16.100.2:80>
ServerName www.a.org
DocumentRoot "/www/a.org"
</VituaHost>

<VirtuaHost 172.16.100.1:8080>
ServerName www.b.net
DocumentRoot "/www/b.net"
</VituaHost>

mkdir /www/b.net
vim index.html
<title>B</title>
<h1>b.net</h1>

vim /conf.d/virtual.conf
NameVirtualHost 172.16.100.2:80

<VirtuaHost 172.16.100.1:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
CustomLog "/var/www/httpd/magedu.com/access_log" combined
</VituaHost>

<VirtuaHost 172.16.100.2:80>
ServerName default
DocumentRoot "/www/default"
</VituaHost>

<VirtuaHost 172.16.100.2:80>
ServerName www.a.org
DocumentRoot "/www/a.org"
CustomLog "/var/www/httpd/a.org/access_log" combined
<Directory "/www/d.gov">
Options none
AllowOverride authconfig
AuthType basic
AuthName "Restrict area."
AuthUserFile "/etc/httpd/.htpasswd"
Require User tom
</Directory>
</VituaHost>

<VirtuaHost 172.16.100.2:80>
ServerName www.d.gov
DocumentRoot "/www/d.gov"
<Directory "/www/d.gov">
Options none
AllowOverride none
Order deny,allow
Deny form 172.16.100.177
</Directory>
</VituaHost>

<VirtuaHost 172.16.100.1:8080>
ServerName www.b.net
DocumentRoot "/www/b.net"
</VituaHost>

mkdir /www/d.gov
vim index.html
<title>D</title>
<h1>d.gov</title>

mkdir /www/default
vim index.html
<h1>default</h1>

vim /etc/hosts(在哪個客戶端訪問就在哪裏域名解析)
172.16.100.1 www.d.gov
172.16.100.1 www.a.org

mkdir /var/www/httpd/a.org/access_log
mkdir /var/www/httpd/magedu.com/access_log

htpasswd -c -m /etc/httpd/.htpaswwd tom

vim httpd.conf
<Location /server-status>
SetHandler server-status
Order allow,deny
Allow from all
</Location>

基於openssl的https服務配置

httpd -M:查看http所已經加載的模塊

yum install mod_ssl
rpm -ql mod_ssl

在172.16.100.8上面創建證書頒發機構:

cd /etc/pki/CA:所有操作均在此目錄下進行
生成私鑰:
( umask 077;openssl genrsa -out private/cakey.pem 2048)

生成自簽證書:
vim ../tls/openssl.cnf
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName_default = Henan
localityName_default = Zhengzhou
O.organizationName_default = MageEdu
organizationUnitName_default = Tech

openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655

vim ../tls/openssl.cnf
[ CA_default ]
dir = /etc/pki/CA

mkdir certs crl newcerts
touch index.txt
echo 01 > serial

在web服務器上面172.16.100.1:
cd /etc/httpd/
mkdir ssl
cd ssl

生成密鑰:
(umask 077; openssl genrsa 1024 > httpd.key)

生成向證書頒發機構簽署請求:
openssl req -new -key httpd.key -out httpd.csr

把簽署請求拿到證書頒發機構去簽署:
scp httpd.csr 172.16.100.8:/tmp

在172.16.100.8上面進行簽署:
openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt -days 3650

拿回已經簽署好的證書:
在172.16.100.1上面進行
scp 172.16.100.8:/tmp/httpd.crt ./

在172.16.100.1上面配置使用證書:
cd /etc/httpd/conf.d/
vim ssl.conf
<VirtualHost 172.16.100.1:443>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
SSLEngine on
SSLCertiFicateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
</VirtualHost>

service httpd restart

客戶端要檢查web服務器端的證書是否可信,要拿着證書頒發機構的證書去驗證
scp -r 172.16.100.8:/etc/pki/CA/cacert.pem ./

mv cacert.pem cacert.crt
然後安裝證書,windows雙擊安裝,Linux不知道

https://hello.magedu.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章