Web網站服務(Apache)
一、Apache簡介
Apache的主要特點
開放源代碼
跨平臺應用
支持各種Web編程語言:Apache服務器可支持的網頁編程語言包括Perl,PHP,PythonJava 等,甚至微軟的ASP技術也可以在Apache服務器中使用。
模塊化設計:其他軟件開發商可以編寫標準的模塊程序,從而添加Apache本身並不具有的其他功能。
運行非常穩定:Apache服務器可用於構建具有大負載訪問量的Web站點。
良好的安全性:Apache服務器具有相對較好的安全性,Apache的維護團隊會及時對已發現的漏洞提供修補程序,爲Apache的所有使用者提供 儘可能安全的服務器程序。
二、安裝httpd服務器
1、普通安裝
1、安裝相應的庫和頭文件
[root@localhost ~]# rpm -q httpd //查看是否安裝
[root@localhost ~]# rpm -e httpd --nodeps //卸載已安裝的程序
[root@localhost ~]# yum -y install apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre
2、源碼編譯及安裝
[root@localhost media]# tar zxf httpd-2.4.25.tar.gz -C /usr/src/
[root@localhost media]# cd /usr/src/httpd-2.4.25/
[root@localhost httpd-2.4.25]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
[root@localhost ~]# /usr/local/httpd/bin/apachectl start //啓動服務
[root@localhost ~]# /usr/local/httpd/bin/apachectl restart //重啓服務
[root@localhost ~]# /usr/local/httpd/bin/apachectl stop //停止服務
常用參數 | 說明 |
---|---|
–prefix | 指定httpd服務程序的安裝目錄 |
–enable-so | 啓用動態加載模塊支持,使httpd具備進一步擴展功能的能力 |
–enable-rewrite | 啓用網頁地址重寫,用於網站優化及目錄遷移維護 |
– enable-charset-lite | 啓動字符集支持,以便支持使用各種字符集編碼的網頁 |
–enable-cgi | 啓用CGI腳本程序支持,便於擴展網站的應用訪問能力 |
–enable-ssl | 編譯SSL模塊 |
–with-zlib | 支持ZLIB壓縮 |
–with-pcre | 支持正則表達式 |
–enable-mpms-shared=all | 以共享方式編譯的模塊 |
–with-mpm=prefork | 指明httpd的工作方式爲prefork |
3、確認安裝結果
[root@localhost ~]# ls /usr/local/httpd/
bin cgi-bin error icons logs manual
build conf htdocs include man modules
主要子目錄 | 用途 |
---|---|
/usr/local/httpd/bin | 存放httpd服務各種執行程序文件,主程序httpd、服務控制工具apachectl等 |
/usr/local/httpd/cgi-bin | 存放各種CGI程序文件 |
/usr/local/httpd/logs | 存放httpd服務的日誌文件 |
/usr/local/httpd/conf | 存放httpd服務的各種配置文件,包括主配置文件httpd.conf |
/usr/local/httpd/htdocs | 存放網頁文檔,包括首頁文件index.html等 |
/usr/loca/httpd/modules | 存放httpd服務的各種模塊文件 |
4、優化執行路徑
[root@localhost ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@localhost ~]# ls -l /usr/local/bin/httpd /usr/local/bin/apachectl
lrwxrwxrwx. 1 root root 30 6月 1 15:31 /usr/local/bin/apachectl -> /usr/local/httpd/bin/apachectl
lrwxrwxrwx. 1 root root 26 6月 1 15:31 /usr/local/bin/httpd -> /usr/local/httpd/bin/httpd
5、添加httpd系統服務
建立[service].service配置文件
[root@localhost ~]# vim /lib/systemd/system/httpd.service
[Unit]
Description = The Apache HTTP Server //設置服務單元描述字符串
After = network.target //設置服務單元的啓動先後順序(先啓動httpd.service,後啓動network.target)
[Service]
Type = forking //設置服務進程的啓動類型
PIDFile = /usr/local/httpd/logs/httpd.pid //設置服務的守護進程的PID文件
ExecStart = /usr/local/bin/apachectl $OPTIONS //設置服務啓動時執行的命令
ExecReload = /bin/kill -HUP $MAINPID //設置服務重新加載時執行的命令
KillMode = process //設置在單元停止時,殺死進程的方法。 process表示僅殺死主進程
Restart = on-failure //表示僅在進程異常退出時重啓
RestartSec = 42s //設置在重啓服務前暫停多長時間
[Install]
WantedBy = graphical.target //設置該服務所在的Target
[root@localhost ~]# systemctl enable httpd //設置httpd服務開機啓動
Created symlink from /etc/systemd/system/graphical.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
2、優化安裝
1、檢查是否已安裝rpm包httpd
[root@localhost ~]# rpm -q httpd //查看是否安裝
[root@localhost ~]# rpm -e httpd --nodeps //卸載已安裝的程序
2、編寫腳本安裝前提軟件
軟件包:https://download.csdn.net/download/g950904/12504171
[root@localhost ~]# vim httpd.sh
#!/bin/bash
cd /media
yum -y install zlib-*
tar zxf apr-1.5.2.tar.gz -C /usr/src
tar zxf apr-util-1.5.4.tar.gz -C /usr/src
tar zxf pcre-8.39.tar.gz -C /usr/src
tar zxf openssl-1.0.1u.tar.gz -C /usr/src
tar zxf httpd-2.4.25.tar.gz -C /usr/src
cd /usr/src
cd apr-1.5.2
./configure --prefix=/usr/local/apr && make && make install
cd ..
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
cd ..
cd pcre-8.39
./configure --prefix=/usr/local/pcre && make && make install
cd ..
cd openssl-1.0.1u
./config -fPIC --prefix=/usr/local/openssl enable-shared && make && make install
cd ..
cd httpd-2.4.25
./configure --prefix=/usr/local/httpd --enable-so --enable-cgi --enable-cgid --enable-ssl --with-ssl=/usr/local/openssl --enable-rewrite --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-fcgi --enable-expires --enable-deflate && make && make install
ln -s /usr/local/httpd/bin/* /usr/local/bin
[root@localhost install]# sh httpd.sh
模塊 | 說明 |
---|---|
–enable-so | 支持動態共享模塊(即打開 DSO 支持) |
–enable-rewrite | 支持 url 重寫 |
–enable-ssl | 支持 ssl |
–with-ssl=/usr/local/openssl | 指定 ssl 安裝位置 |
–enable-cgi | 啓用 cgi |
–enable-cgid | MPM 使用的是 event 或 worker 要啓用 cgid |
–enable-modules=most | 指明要靜態編譯到 httpd 二進制文件的模塊,all 或者 most,all 表示包含所有模塊,most 表示包含大部分常用模塊 |
–enable-mods-shared=most | 指明要以 DSO 方式編譯的模塊 |
–enable-mpms-shared=all | 啓用 MPM 所有支持的模式 |
–with-mpm=event | 指定啓用的 mpm 模式,默認使用 enevt 模式 |
–with-pcre=/usr/local/pcre | 支持 pcre |
–with-z=/usr/local/zlib | 使用 zlib 壓縮庫 |
–with-apr=/usr/local/apr | 指定 apr 的安裝路徑 |
–with-apr-util=/usr/local/apr-util | 指定 apr-util 的安裝路徑 |
–enable-expires | 激活彧通過配置文件控制 HTTP 的“Expires:”和“Cache-Control:”頭內容 |
–enable-deflate | 提供對內容的壓縮傳輸編碼支持,一般是 html、js、css 等內容的站點 |
3、優化鏈接
[root@localhost ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost ~]# vim /etc/init.d/httpd
#!/bin/sh
# chkconfig: 35 85 15 //聲明服務啓動級別,開機啓動順序,關機關閉順序
# description: apache 2.4.25 //服務聲明,簡要信息
......
[root@localhost ~]# chkconfig --add httpd //添加httpd到系統服務
[root@localhost ~]# chkconfig httpd on //設置服務開機自啓(等同於:systemctl enable httpd)
[root@localhost ~]# systemctl start httpd //開啓服務(等同於:service httpd start)
4、查看httpd模塊
[root@localhost ~]# httpd -V //查看版本和已裝模塊
[root@localhost ~]# httpd -l //只查看靜態編譯模塊
[root@localhost ~]# httpd -M //查看所有模塊
二、httpd服務器的基本配置
1、Web站點的部署過程
1、配置並啓動httpd服務
配置httpd服務
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
ServerName www.csnd.com:80
對配置內容進行語法檢查
[root@localhost ~]# apachectl -t
Syntax OK
啓動httpd服務
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 40420/httpd
2、部署網頁文檔
[root@localhost ~]# cat /usr/local/httpd/htdocs/index.html
<html><body><h1>It works!</h1></body></html>
3、在客戶機中訪問Web站點
4、查看Web站點的訪問情況
[root@localhost ~]# tail /usr/local/httpd/logs/access_log
192.168.1.10 - - [01/Jun/2020:15:35:49 +0800] "GET / HTTP/1.1" 200 45
192.168.1.10 - - [01/Jun/2020:15:35:49 +0800] "GET /favicon.ico HTTP/1.1" 404 209
2、httpd.conf配置文件
1、全局配置項
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
ServerRoot "/usr/local/httpd" //設置httpd服務器的根目錄。默認情況下,httpd服務器的根目錄爲httpd的安裝目錄。
Listen 80 //設置httpd服務器監聽的網絡端口號,默認爲80
User daemon //設置運行httpd進程時的用戶身份,默認爲daemon
Group daemon //設置運行httpd進程時的組身份,默認爲daemon
ServerAdmin [email protected] //設置httpd服務器的管理員E-mail地址,可以通過此E-mail地址及時聯繫Web站點的管理員
ServerName www.csdn.com //設置Web站點的完整主機名(主機名+域名)
DocumentRoot "/usr/local/httpd/htdocs" //設置網站根目錄,即網頁文檔在系統中的實際存放路徑
Directoryindex index.html index.php //設置網站的默認索引頁(首頁),可以設置多個首頁文件,以空格分開,默認的首頁文件爲index.html.
ErrorLog "logs/error_log" //設置錯誤日誌文件的路徑,默認路徑爲logs/error_logo
LogLevel warn //設置記錄日誌的級別,默認級別爲warn(警告)
CustomLog "logs/access_log" common //設置訪問日誌文件的路徑、日誌類型
PidFile "logs/httpd.pid" //設置用於保存httpd進程號(PID)的文件
AddDefaultCharset UTF-8 //設置站點中的網頁默認使用的字符集編碼,如utf-8、gb2312等
Include conf/extra/httpd-default.conf //包含另一個配置文件的內容
2、區域配置項
<Directory />
Options Indexes FollowSymLinks //控制選項,允許使用符號鏈接
AllowOverride none //不允許隱含控制文件中的覆蓋配置
Require all denied //禁止任何人訪問此區域
</Directory>
三、httpd服務的訪問控制
1、客戶機地址限制
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
<Directory "/usr/local/httpd/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
[root@localhost ~]# systemctl restart httpd
字段 | 說明 |
---|---|
Require all granted | 表示允許所有主機訪問 |
Require all denied | 表示拒絕所有主機訪問 |
Require local | 表示僅允許本地主機訪問 |
Require [not] host〈主機名或域名列表〉 | 表示允許或拒絕指定主機或域訪問 |
Require [not] ip<ip地址或網段列表〉 | 表示允許或拒絕指定P地址或網段訪問 |
(1)只希望IP地址爲173.17.17.173的主機能夠訪問/usr/local/httpd/htdocs網頁目錄下的網頁內容
Require ip 173.17.17.173
(2)只希望禁止來自兩個內網網段192.168.0.0/24和 192.168.1.0/24的主機訪問,但允許其他任何主機訪問
Require all granted
Require not ip 192.168.0.0/24 192.168.1.0/24
2、用戶授權限制
1、創建用戶認證數據文件
新建數據文件/usr/local/httpd/conf/.awspwd,其中包含一個名爲webadmin的用戶信息
[root@localhost ~]# cd /usr/local/httpd/
[root@localhost httpd]# bin/htpasswd -c /usr/local/httpd/conf/.awspwd webadmin
New password:
Re-type new password:
Adding password for user webadmin
[root@localhost httpd]# cat /usr/local/httpd/conf/.awspwd
webadmin:$apr1$y4m4WWpU$KgA4ZB5wlQgjPOdrEIxwo1
添加一個新用戶zhangsan
[root@localhost httpd]# bin/htpasswd /usr/local/httpd/conf/.awspwd zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
[root@localhost httpd]# cat /usr/local/httpd/conf/.awspwd
webadmin:$apr1$y4m4WWpU$KgA4ZB5wlQgjPOdrEIxwo1
zhangsan:$apr1$.6GiuPa6$klmZ81AiYEHGDfORn5IFs/
2、添加用戶授權配置
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
<Directory "/usr/local/httpd/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all denied //拒絕所有主機訪問
AuthName " DocumentRoot " //定義受保護的領域名稱,該內容將在瀏覽器彈出的認證對話框中顯示
AuthType Basic //設置認證的類型,Basic表示基本認證
AuthUserFile /usr/local/httpd/conf/.awspwd //設置用於保存用戶賬號、密碼的認證文件路徑
Require valid-user //要求只有認證文件中的合法用戶才能訪問。其中,valid-user表示所有合法用戶,若只授權給單個用戶,可改爲指定的用戶名(如webadmm)
</Directory>
[root@localhost ~]# systemctl restart httpd
重新啓動服務後訪問
四、構建虛擬Web主機
基於域名:爲每個虛擬主機使用不同的域名,但是其對應的IP地址是相同的。
基於IP地址:爲每個虛擬主機使用不同的域名,且各自對應的P地址也不相同。
基於端口:這種方式並不使用域名、IP地址來區分不同的站點內容,而是使用了不同的TCP 端口號,因此用戶在瀏覽不同的虛擬站點時需要同時指定端口號才能訪問。
安裝DNS服務
[root@localhost ~]# yum -y install bind bind-chroot
1、基於域名的虛擬主機
1、爲虛擬主機提供域名解析
[root@localhost ~]# vim /etc/named.conf
options {
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
......
zone "." IN {
type hint;
file "named.ca";
};
zone "web1.com" IN {
type master;
file "web1.com.zone";
};
zone "web2.com" IN {
type master;
file "web2.com.zone";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
[root@localhost ~]# vim /var/named/web1.com.zone
$ttl 86400
@ in soa web1.com. admin.web1.com. (
20200601
3h
15m
1w
1d
)
in ns ns1.web1.com.
ns1 in a 192.168.1.10
www in a 192.168.1.10
[root@localhost ~]# vim /var/named/web2.com.zone
$ttl 86400
@ in soa web2.com. admin.web2.com. (
20200601
3h
15m
1w
1d
)
in ns ns1.web2.com.
ns1 in a 192.168.1.10
www in a 192.168.1.10
重啓服務
[root@localhost ~]# systemctl restart named
2、爲虛擬主機準備網頁文檔
[root@localhost ~]# mkdir -p /var/www/html/web1
[root@localhost ~]# mkdir -p /var/www/html/web2
[root@localhost ~]# echo "www.web1.com" > /var/www/html/web1/index.html
[root@localhost ~]# echo "www.web2.com" > /var/www/html/web2/index.html
3、添加虛擬主機配置
[root@localhost ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //創建獨立的配置文件
<VirtualHost *:80> //設置web1虛擬站點區域
ServerAdmin [email protected]
DocumentRoot "/var/www/html/web1"
ServerName www.web1.com
ErrorLog "logs/www.web1.com.error_log"
CustomLog "logs/www.web1.com.access_log" common
<Directory "/var/www/html"> //設置目錄訪問權限
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80> //設置web2虛擬站點區域
ServerAdmin [email protected]
DocumentRoot "/var/www/html/web2"
ServerName www.web2.com
ErrorLog "logs/www.web2.com.error_log"
CustomLog "logs/www.web2.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf //加載獨立的配置文件
Include conf/extra/httpd-vhosts.conf
[root@localhost ~]# systemctl restart httpd //重啓服務使新配置生效
4、在客戶機中訪問虛擬Web主機
2、基於IP地址的虛擬主機
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:0 //修改IP地址
[root@localhost network-scripts]# systemctl restart network
[root@localhost ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.1.10:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/web1"
ServerName www.web1.com
ErrorLog "logs/www.web1.com.error_log"
CustomLog "logs/www.web1.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.100:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/web2"
ServerName www.web2.com
ErrorLog "logs/www.web2.com.error_log"
CustomLog "logs/www.web2.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf //加載獨立的配置文件
Include conf/extra/httpd-vhosts.conf
[root@localhost ~]# systemctl restart httpd //重啓服務使新配置生效
3、基於端口的虛擬主機
[root@localhost ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.1.10:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/web1" //正常訪問的網站目錄
ServerName www.web1.com
ErrorLog "logs/www.web1.com.error_log"
CustomLog "logs/www.web1.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.10:8080>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/web2" //後臺管理系統的網站目錄
ServerName www.web2.com
ErrorLog "logs/www.web2.com.error_log"
CustomLog "logs/www.web2.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
Listen 80 //監聽80端口
Listen 8080 //監聽8080端口
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf //加載獨立的配置文件
Include conf/extra/httpd-vhosts.conf
[root@localhost ~]# systemctl restart httpd //重啓服務使新配置生效
在客戶機中訪問虛擬Web主機
五、其他
1、HTTP返回狀態碼
返回狀態碼 | 說明 | 返回狀態碼 | 說明 |
---|---|---|---|
200 | 請求成功 | 201 | 上傳文件成功 |
301 | 永久重定向(redirect) | 302,307 | 臨時重定向(redirect) |
304 | 瀏覽器緩存 | 403 | 請求不到首頁,沒有權限 |
404 | 請求的資源在前端查明不存在 | 405 | 請求方法不支持 |
500 | 服務器的內部錯誤,程序錯誤 | 502 | 請求的資源前端有記錄指向後端數據庫,卻找不到後端資源 |
503 | 服務暫時不可用 | 504 | 請求超時 |
2、用戶訪問網站攜帶的參數,以及服務器返回的參數
1、General概況
Request URL: https://www.baidu.com/ //請求的URL地址
Request Method: GET //請求的方法(獲取)
Status Code: 200 OK //成功狀態碼
Remote Address: [2408:80f0:410c:1d:0:ff:b07a:39af]:443 //請求的地址(ipv4或ipv6)
Referrer Policy: no-referrer-when-downgrade //降級不推薦(從https降級到http)
2、Request Headers:客戶端請求頭部信息
Accept: text/html //請求的類型
Accept-Encoding: gzip, deflate, br //是否進行壓縮
Accept-Language: zh-CN,zh;q=0.9 //請求的語言
Connection: keep-alive //TCP長連接
cache-control:max-age=0 //緩存時間
Cookie: //客戶端緩存,用戶密碼等網站信息
Host:www.baidu.com/ //請求的域名
Upgrade-Insecure-Requests: 1 //啓用升級https訪問
User-Agent: Mozilla/5.0 //客戶端瀏覽器
3、Response Headers:服務器響應頭部信息
Cache-Control: private //緩存類型非公開(公開:pubLic)
Connection: keep-alive //長連接
Content-Encoding: gzip //壓縮格式gzip
Content-Type: text/html;charset=utf-8 //文檔類型
Date: Sat, 14 Mar 2020 08:48:02 GMT //響應時間
Expires: Sat, 14 Mar 2020 08:47:42 GMT //失效時間
Server: BWS/1.1 //網站服務器軟件
status:200 //狀態碼
3、HTTP統計術語
PV:頁面瀏覽量
UV:獨立客戶瀏覽量
IP:獨立ip瀏覽量