apache(服務器軟件)

apache基礎

apache:www.apache.org --->主頁中的project項目標題中提供了apache組織發佈的各種平臺軟件。
apache:是取自“a patchy server”的讀音,意思是充滿補丁的服務器。
發佈了很多開源的應用程序。如大數據庫的平臺軟件hadoop、zookeeper等。

軟件包名稱:httpd
服務端口:80/tcp 443/tcp
查apache的配置文件:rpm -qc httpd
配置文件: /etc/httpd/conf/httpd.conf 主配置文件
/etc/httpd/conf.d/*.conf 子配置文件
/etc/httpd/conf.d/welcome.conf 默認配置文件
數據目錄:/var/www/html .html

練習:安裝並啓動httpd軟件。
rpm -q httpd  httpd-manual
yum  -y  install  httpd   httpd-manual
systemctl restart httpd && systemctl enable  httpd
lsof -i:80 或 netstat  -atunlp |grep  :80

練習:安裝命令行界面的網頁瀏覽器軟件,訪問網站。
yum  -y install curl   elinks
curl  127.0.0.1  看到的網站的html源代碼
elinks 127.0.0.1  看到網頁內容,按q鍵退出瀏覽器軟件

練習:創建一個index.html的主頁。訪問網站。
data  > /var/www/html/index.html
free  >> /var/www/html/index.html
lsblk >> /var/www/html/index.html

curl 127.0.0.1 或elinks 127.0.0.1

URl:統一資源標識,定位了一切網上的資源。列子 www.qf.com/index.html
URL:統一資源定位器。列:http://www.baidu.com/xinwen/1.html
www. 萬維網
www 主機名
qf.com 域名

客戶端
圖形化界面:firefox   瀏覽器
命令: elinks  links
elinks 192.168.1.250

#echo  qf.com  > /var/www/html/index.html

當服務器沒有welcome.conf和*.html文件時,web server會顯示、/var/www/html這個目錄中的目錄結構
*//apache的主配置文件:
 vim  /etc/httpd/conf/httpd.conf

 httpd.conf配置文件的組成:
 global全局設置:
 mlodules模塊設置:
 VirtualHost虛擬主機設置:
 Directory目錄設置(控制訪問權限-基於IP、用戶認證):
 File文件控制訪問控制:
 .....

31 ServerRoot "/etc/httpd" 服務器的根目錄(即apache軟件的根目錄)
41 #Listen 12.34.56.78:80
42 Listen 80 啓用80端口的監聽
56 Include conf.modules.d/*.conf 包含的子配置文件,此目錄是存放apache的模塊程序
66 User apache 程序的執行者,用lsof -i :80 或 ps aux | grep apache可查
67 Group apache 程序的執行組,用top -u apache -->按f鍵-->方向鍵選group,並空格-->Esc鍵
86 ServerAdmin root@localhost 網站服務器管理員的郵箱

102 <Directory /> 根目錄訪問權限設置
103 AllowOverride none
104 Require all denied 要求全部拒絕,即拒絕訪問/根目錄下的任何文件
105 </Directory>

119 DocumentRoot "/var/www/html" 文檔根目錄,即默認的網站主頁目錄
124 <Directory "/var/www"> 對/var/www目錄訪問權限設置
125 AllowOverride None
126 # Allow open access:
127 Require all granted 需要所有授權,即允許任何人訪問
128 </Directory>

131 <Directory "/var/www/html">
144 Options Indexes FollowSymLinks
151 AllowOverride None
156 Require all granted
157 </Directory>

163 <IfModule dir_module> 設置dir_module模塊。If是Interface接口的縮寫
164 DirectoryIndex index.html index.php index.jsp 指定目錄的索引(即主頁)文件
165 </IfModule>

171 <Files ".ht*"> 文件訪問設置
172 Require all denied
173 </Files>

182 ErrorLog "logs/error_log"
189 LogLevel warn

191 <IfModule log_config_module> 設置log_config_module日誌配置模塊
196 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"% {User-Agent}i\"" combined
197 LogFormat "%h %l %u %t \"%r\" %>s %b" common
198
199 <IfModule logio_module>
201 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \ "%{User-Agent}i\" %I %O" combinedio
202 </IfModule>

217 CustomLog "logs/access_log" combined
218 </IfModule>

220 <IfModule alias_module> 設置alias_module別名模塊
231 # Example:
232 # Alias /webpath /full/filesystem/path 定義/full/.../path路徑的別名爲/webpath
247 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
248
249 </IfModule>

255 <Directory "/var/www/cgi-bin">
256 AllowOverride None
257 Options None
258 Require all granted
259 </Directory>
260
261 <IfModule mime_module>
266 TypesConfig /etc/mime.types
277 #AddEncoding x-compress .Z
278 #AddEncoding x-gzip .gz .tgz
283 AddType application/x-compress .Z
284 AddType application/x-gzip .gz .tgz
305 AddType text/html .shtml
306 AddOutputFilter INCLUDES .shtml
307 </IfModule>

316 AddDefaultCharset UTF-8 默認的網站字符編碼爲UTF-8(支持中文)
317
318 <IfModule mime_magic_module>
324 MIMEMagicFile conf/magic
325 </IfModule>

348 EnableSendfile on
353 IncludeOptional conf.d/*.conf

apache有兩種運行模式(進程和線程)

穩 prefork model 基於進程(公司常用) 即一個apache進程啓動一個線程來響應客戶的請求
快 worker model 基於線程(公司少用) 即一個apache進程啓動多個線程來響應客戶的請求

通俗理解:將進程理解成“超市”,將線程理解成超市中的“收銀臺”。

基於進程:由主進程創建子進程,每一個進程只有一個線程,內存是不可以共享,系統開銷大,但是一個線程壞掉了不會影響其他線程

基於線程:一個進程中會產生多個線程,內存中的某個區域是可以共享的,創建線程數度快,開銷小,但是一個線程出現問題時,其他線程都死掉。[以武俠小說中的分身術來理解,即發分身術招式的是進程(本尊),招式中分身是線程]

集羣環境:一般都會使用進程模式,不需要某一臺主機單獨承擔大量的訪問

httpd模式切換:
centos 7的操作:
cat -n /etc/sysconfig/httpd 請看第10行內容
10 # /etc/httpd/conf.modules.d/00-mpm.conf.

vim /etc/httpd/conf.modules.d/00-mpm.conf
6 LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
12 #LoadModule mpm_worker_module modules/mod_mpm_worker.so
18 #LoadModule mpm_event_module modules/mod_mpm_event.so
說明:apache默認是採用進程模式來運行的,即以上的第6行代碼。如需使用多線程模式運行,需要將第6行註釋掉並且將第12行啓用。然後重啓httpd服務,再用httpd -V 查apache的運行模式信息。輸出結果的部分內容如下:
Server MPM: worker(線程模式)
threaded: yes (fixed thread count)
forked: yes (variable process count)

---
centos 6的操作(httpd模式切換):
vim /etc/sysconfig/httpd //切換成線程模式
HTTPD=/usr/sbin/httpd.worker
/etc/init.d/httpd restart (centos 6的操作) 或 systemctl restart httpd (centos 7的操作)

<IfModule prefork.c> 進程模塊
StartServers 8 初始建立的進程數(1個父進程,8個工作進程)
MinSpareServers 5 最小空閒的進程數
MaxSpareServers 20 最大空閒的進程數
ServerLimit 256 服務器最大併發連接限制(默認256)
MaxClients 256 服務器最大併發訪問量
MaxRequestsPerChild 4000 每個子進程在其生命週期內允許響應的最大請求數,達到進程會被殺死,如果爲0則用不結束,一個程序結束後
</IfModule>

httpd線程查詢
[root@node11 ~]# ps -ef | grep httpd

apache虛擬主機

apache虛擬主機功能:在一臺apache網站服務器發佈多個網站站點。
網站虛擬主機三種技術:
1.基於ip地址 每個網站有一個ip地址
2.基於端口 所有網站僅用一個ip,端口不一樣
3.基於主機名(域名) 所有網站僅用一個ip,但是主機名稱不一樣

最常用的是基於主機名,基於ip地址不常用,因爲申請ip需要資金

配置apache實現虛擬主機
基於ip的虛擬主機:
思路: 1.給服務器的網卡設置多個ip地址。
             2.創建網站主頁目錄及index.html主頁文件。
             3.在httpd服務器軟件的/etc/httpd/conf.d目錄中創建虛擬主機的配置文件,給每個ip地址綁定一個網站。
             4.訪問測試:訪問不同的ip地址,得到不同網站頁面。

實施參考:
1.給服務器的網卡設置多個ip地址。(臨時設置多個ip,重啓network服務是時ip會丟失)
ifconfig ens33:1 192.168.11.21/24 up
ifconfig ens33:2 192.168.11.22/24 up
ifconfig ens33:3 192.168.11.23/24 up
ip a 或 ifconfig

2.創建網站主頁目錄及index.html主頁文件。
mkdir -pv /baidu/{www,map,music}
echo www.baidu.com >/baidu/www/index.html
echo map .baidu.com > /baidu/map/index.html
echo music.baidu.com > /baidu/music/index.html

3.在httpd服務器軟件的/etc/httpd/conf.d目錄中創建虛擬主機的配置文件,給每個ip地址綁定一個網站。
cd /etc/httpd/conf.d(子配置文件)
ls
rpm -ql httpd | grep vhosts查httpd軟件的所有文件名列表,但匹配vhosts關鍵詞的文件
cat /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
tail /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf > /etc/httpd/conf.d/baidu.conf(創建baidu.conf配置文件)
-----基於ip的虛擬主機
vim /etc/httpd/conf.d/baidu.conf 修改後的內容如下

 <VirtualHost 192.168.11.21:80>  指定虛擬主機的ip號和端口號
ServerAdmin [email protected]   服務器管理員郵箱
DocumentRoot "/baidu/www/"  網站主頁根目錄
ServerName www.baidu.com  網站的域名
ErrorLog "/var/log/www.baidu.com-error_log"
CustomLog "/var/log/httpd/www.baidu.com-access_log" common

</VirtualHost>

<Directory "/baidu/www"> 目錄設置 在/etc/httd/conf/httpd.conf可以找到源代碼
AllowOverride None

Allow open access:

Require all granted  需要所有授權,即允許任何人訪問

</Directory>

<VirtualHost 192.168.11.22:80>
ServerAdmin [email protected]
DocumentRoot "/baidu/map/"
ServerName www.map.com
ErrorLog "/var/log/www.map.com-error_log"
CustomLog "/var/log/httpd/www.map.com-access_log" common
</VirtualHost>

<Directory "/baidu/map">
AllowOverride None

Allow open access:

Require all granted

</Directory>

4.訪問測試:訪問不同的ip地址,得到不同的頁面。
systemctl restart httpd
curl 192.168.11.21 結果是www.baidu.com
curl 192.168.11.22 結果是map.baidu.com

----基於端口的虛擬主機
vim /etc/httpd/conf.d/baidu.conf修改內容如下

 Listen 81   啓用81端口的監聽

Listen 82 啓用82端口的監聽
<VirtualHost 192.168.11.21:81> ip和下面的ip相同 但端口不同
ServerAdmin [email protected]
DocumentRoot "/baidu/www/"
ServerName www.baidu.com
ErrorLog "/var/log/www.baidu.com-error_log"
CustomLog "/var/log/httpd/www.baidu.com-access_log" common
</VirtualHost>

<Directory "/baidu/www">
AllowOverride None

Allow open access:

Require all granted

</Directory>

<VirtualHost 192.168.11.21:82>
ServerAdmin [email protected]
DocumentRoot "/baidu/map/"
ServerName www.map.com
ErrorLog "/var/log/www.map.com-error_log"
CustomLog "/var/log/httpd/www.map.com-access_log" common
</VirtualHost>

<Directory "/baidu/map">
AllowOverride None

Allow open access:

Require all granted

</Directory>

訪問測試:訪問相同ip的不同端口,得到不同網頁界面
systemctl restart httpd
curl 192.168.11.21:81 輸出結果是www.baidu.com
curl 192.168.11.21:82 輸出結果是map.baidu.com


基於域名的虛擬主機:

Listen 81
Listen 82
<VirtualHost 192.168.11.21:80>
ServerAdmin [email protected]
DocumentRoot "/baidu/www/"
ServerName www.baidu.com
ErrorLog "/var/log/www.baidu.com-error_log"
CustomLog "/var/log/httpd/www.baidu.com-access_log" common
</VirtualHost>

<Directory "/baidu/www">
AllowOverride None

Allow open access:

Require all granted

</Directory>

<VirtualHost 192.168.11.21:80>
ServerAdmin [email protected]
DocumentRoot "/baidu/map/"
ServerName www.map.com
ErrorLog "/var/log/www.map.com-error_log"
CustomLog "/var/log/httpd/www.map.com-access_log" common
</VirtualHost>

<Directory "/baidu/map">
AllowOverride None

Allow open access:

Require all granted

</Directory>

並改掉 vim /etc/hosts
加入這一行192.168.11.21 www.baidu.com map.baidu.com
systemctl restart httpd
curl www.baidu.com 輸出結果爲www.baidu.com
curl map.baidu.com 輸出結果爲map.baidu.com

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