http 介紹
httpd是Apache超文本傳輸協議(HTTP)服務器的主程序。被設計爲一個獨立運行的後臺進程,它會建立一個處理請求的子進程或線程的池。
httpd 特性
- 高度模塊化:Core+Modules
- DSO:Dynamic Shared Ojbect動態加/卸載
- MPM:Multi-processing Module多路處理模塊
- 虛擬主機:IP,Port,FQDN
- CGI:通用網關接口
- 反向代理(和正向代理的區別:正向代理距離客戶端近,加速客戶端的訪問速度;反向代理距離服務器近,可以做調度,把client的請求轉發到websrv集羣中的某一個client<--->proxy(cache)<--->reverse proxy<--->websrv1,websrv2,sebsrnv)
- 負載均衡
- 路徑別名
- 雙向認證
- 支持第三方模塊
httpd常用的配置
在httpd中實現虛擬主機
所謂的虛擬主機是指在通過在配置文件中提供不同的配置,從而可以實現在同一臺物理服務器上提供多個站點的訪問路徑,實現方式有三種,分別是:
IP地址相同,監聽的端口不同,通過不同的端口號來訪問
IP地址不同,端口可以相同,通過不同的IP來訪問
主機名不同,端口號和IP地址可以相同,通過不同的主機名稱來訪問。
使用虛擬主機的前提是關閉中心主機功能,即將主配置文件中的DocumentRoot這一指令註釋。
Apache服務器默認在80端口監聽
一臺機器可以有1到65535號端口,一個端口代表2個字節
Netstat -an 該命令用來查詢本機器有哪些端口正在被監聽
Netstat -anb 該命令用來查詢本機器有哪些端口正在被監聽及其對應的應用程序
端口中的1-1024號叫做有名端口,這些端口一般不要用,他們已經分配好了
Apache如何配置端口:
Apache軟件的端口是在httpd.conf文件中配置的,該文件在Apache目錄下的conf文件下。在該文件中可以修改端口,修改後重新啓動Apache,就生效。
控制訪問法則
法則 | 功能 |
---|---|
Require all granted | 允許所有主機訪問 |
Require all deny | 拒絕所有主機訪問 |
Require ip IPADDR | 授權指定來源地址的主機訪問 |
Require not ip IPADDR | 拒絕指定來源地址的主機訪問 |
Require host HOSTNAME | 授權指定來源主機名的主機訪問 |
Require not host HOSTNAME | 拒絕指定來源主機名的主機訪問 |
Require not ip | 拒絕指定ip主機訪問 |
httpd編譯安裝
實驗環境說明:
主機名 | IP |
---|---|
[root@yanyinglai3 ~] | 192.168.47.12.24 |
準備環境,將防火牆和selinux
[root@yanyinglai3 ~]# setenforce 0
[root@yanyinglai3 ~]# systemctl stop firewalld
安裝開發環境
[root@yanyinglai3 ~]# yum groupinstall "Development Tools"
創建apache組和用戶apache
[root@yanyinglai3 ~]# groupadd -r apache
[root@yanyinglai3 ~]# useradd -M -s /sbin/nologin -g apache apache
[root@yanyinglai3 ~]# id apache
uid=1000(apache) gid=996(apache) 組=996(apache)
安裝相關的軟件包
[root@yanyinglai3 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
·下載並安裝apr-1.4和apr-util-1.4+
[root@yanyinglai3 ~]# cd /usr/src/
[root@yanyinglai3 ~]# yum -y install wget
[root@yanyinglai3 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
[root@yanyinglai3 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
解壓下載安裝apr-1.4和apr-util-1.4+的壓縮包
[root@yanyinglai3 ~]# tar xf apr-1.6.3.tar.bz2
[root@yanyinglai3 ~]# tar xf apr-util-1.6.1.tar.bz2
[root@yanyinglai3 ~]# ls
anaconda-ks.cfg apr-1.6.3 apr-1.6.3.tar.bz2 apr-util-1.6.1 apr-util-1.6.1.tar.bz2
進入apr-1.6.3將修改configure配置文件
[root@yanyinglai3 ~]# cd apr-1.6.3/
[root@yanyinglai3 apr-1.6.3]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" //將此行加入註釋,或者刪除此行
編譯安裝
[root@yanyinglai3 apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@yanyinglai3 apr-1.6.3]# make && make install
[root@yanyinglai3 apr-1.6.3]# cd /usr/src/apr-util-1.6.1
[root@yanyinglai3 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@yanyinglai3 apr-util-1.6.1]# make && make install
編譯安裝httpd
[root@yanyinglai3 ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@yanyinglai3 ~]# ls
anaconda-ks.cfg httpd-2.4.34.tar.bz2
[root@yanyinglai3 ~]# tar xf httpd-2.4.34.tar.bz2
[root@yanyinglai3 ~]# cd httpd-2.4.34/
[root@yanyinglai3 httpd-2.4.34]# ./configure --prefix=/usr/local/apache \
> --sysconfdir=/etc/httpd24 \
> --enable-so \
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --with-zlib \
> --with-pcre \
> --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util/ \
> --enable-modules=most \
> --enable-mpms-shared=all \
> --with-mpm=prefork
[root@yanyinglai3 httpd-2.4.34]# make && make install
當相同IP不同端口時
[root@yanyinglai3 ~]# vim /etc/httpd24/httpd.conf
//找到ServerName www.example.com:80 取消#號註釋
//找到Listen 80 在下面添加不同端口 Listen 81
//在最後面添加下面如下內容
<VirtualHost 192.168.47.12:80>
DocumentRoot "/usr/local/apache/htdocs/yan"
ErrorLog "logs/yan/error_log"
CustomLog "logs/yan/access_log" combined
<Directory "/usr/local/apache/htdocs/yan">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.47.12:81>
DocumentRoot "/usr/local/apache/htdocs/yyl"
ErrorLog "logs/yyl/error_log"
CustomLog "logs/yyl/access_log" combined
<Directory "/usr/local/apache/htdocs/yyl">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
[root@yanyinglai3 ~]# tail -25 /etc/httpd24/httpd.conf
[root@yanyinglai3 ~]# cd /usr/local/apache/logs/ //建立與httpd主配置文件相同路徑的目錄
[root@yanyinglai3 logs]# mkdir yan
[root@yanyinglai3 logs]# mkdir yyl
[root@yanyinglai3 logs]# cd /usr/local/apache/htdocs/ //在網站存放目錄下 也創建與之相同的目錄
[root@yanyinglai3 htdocs]# mkdir yan
[root@yanyinglai3 htdocs]# mkdir yyl
[root@yanyinglai3 htdocs]# chown -R apache.apache /usr/local/apache/htdocs/
//給網站存放的目錄更改屬主屬組爲apache
[root@yanyinglai3 htdocs]# echo 'hello yan' > yan/index.html
[root@yanyinglai3 htdocs]# echo 'hello yyl' > yyl/index.html
[root@yanyinglai3 htdocs]# cd /usr/local/apache/bin/
[root@yanyinglai3 bin]# ./apachectl start
[root@yanyinglai3 bin]# ./apachectl -t
客戶端驗證
不同IP相同端口
[root@yanyinglai3 bin]# vim /etc/httpd24/httpd.conf
<VirtualHost 192.168.47.12:80>
DocumentRoot "/usr/local/apache/htdocs/yan"
ErrorLog "logs/yan/error_log"
CustomLog "logs/yan/access_log" combined
<Directory "/usr/local/apache/htdocs/yan">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.47.13:80>
DocumentRoot "/usr/local/apache/htdocs/yyl"
ErrorLog "logs/yyl/error_log"
CustomLog "logs/yyl/access_log" combined
<Directory "/usr/local/apache/htdocs/yyl">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
[root@yanyinglai3 bin]# ip addr add 192.168.47.13/24 dev ens32
建立與編輯文件對應的臨時ip
[root@yanyinglai3 ~]# pkill httpd
[root@yanyinglai3 ~]# /usr/local/apache/bin/httpd
[root@yanyinglai3 ~]# ss -antl
[root@yanyinglai3 ~]# cd /usr/local/apache/bin/
[root@yanyinglai3 bin]# ./apachectl start
httpd (pid 62137) already running
[root@yanyinglai3 bin]# ./apachectl -t
Syntax OK
客戶端檢測
.相同IP相同端口不同域名
[root@yanyinglai3 ~]# vim /etc/httpd24/httpd.conf
<VirtualHost 192.168.47.12:80>
ServerName www.yanyinglai.com:80
DocumentRoot "/usr/local/apache/htdocs/yan"
ErrorLog "logs/yan/error_log"
CustomLog "logs/yan/access_log" combined
<Directory "/usr/local/apache/htdocs/yan">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.47.12:80>
ServerName www.yyl.com:80
DocumentRoot "/usr/local/apache/htdocs/yyl"
ErrorLog "logs/yyl/error_log"
CustomLog "logs/yyl/access_log" combined
<Directory "/usr/local/apache/htdocs/yyl">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
root@yanyinglai3 ~]# pkill httpd
[root@yanyinglai3 ~]# /usr/local/apache/bin/httpd
[root@yanyinglai3 ~]# ss -antl
[root@yanyinglai3 ~]# cd /usr/local/apache/bin/
[root@yanyinglai3 bin]# ./apachectl start
httpd (pid 62137) already running
[root@yanyinglai3 bin]# ./apachectl -t
Syntax OK
客戶端檢測
在windows電腦上修改, C:\Windows\System32\drivers\etc 文件