http 介紹
httpd是Apache超文本傳輸協議(HTTP)服務器的主程序。被設計爲一個獨立運行的後臺進程,它會創建一個處理請求的子進程或線程的池。html
httpd 特性linux
httpd經常使用的配置
在httpd中實現虛擬主機web
所謂的虛擬主機是指在經過在配置文件中提供不一樣的配置,從而能夠實如今同一臺物理服務器上提供多個站點的訪問路徑,實現方式有三種,分別是:apache
IP地址相同,監聽的端口不一樣,經過不一樣的端口號來訪問
IP地址不一樣,端口能夠相同,經過不一樣的IP來訪問
主機名不一樣,端口號和IP地址能夠相同,經過不一樣的主機名稱來訪問。
使用虛擬主機的前提是關閉中心主機功能,即將主配置文件中的DocumentRoot這一指令註釋。vim
Apache服務器默認在80端口監聽
一臺機器能夠有1到65535號端口,一個端口表明2個字節windows
Netstat -an 該命令用來查詢本機器有哪些端口正在被監聽服務器
Netstat -anb 該命令用來查詢本機器有哪些端口正在被監聽及其對應的應用程序
端口中的1-1024號叫作有名端口,這些端口通常不要用,他們已經分配好了負載均衡
Apache如何配置端口:
Apache軟件的端口是在httpd.conf文件中配置的,該文件在Apache目錄下的conf文件下。在該文件中能夠修改端口,修改後從新啓動Apache,就生效。ide
控制訪問法則模塊化
法則 | 功能 |
---|---|
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 文件