Apache連線保持相關參數
KeepAlive
KeepAlive Timeout
MaxKeepAliveRequests
部署keepalive優化連線
[root@promote ~]# yum install -y httpd
[root@promote ~]# vim /etc/httpd/conf/httpd.conf
KeepAlive on
#KeepAlive off
KeepAliveTimeout 600
[root@promote ~]# systemctl restart httpd.service
使用Require設定項實現存取控制,按先後順序限制
可用< Location >、< Directory >、< Files >、< Limit >設定段中
Require設定項的常見語法
Require all granted
Require all denied
Require local
Require [not] host <主機名或域名列表>
Require [not] ip <IP地址或網段列表>
'//使用not禁止存取時要將其置於<RequireAll></RequireAll>容器中,並在容器中指定相對應的限制策略'
範例:
[root@localhost ~]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir abc
[root@localhost conf]# ls
abc httpd.conf magic
[root@localhost conf]# cd abc
[root@localhost abc]# vim vhost.conf
<VirtualHost *:80>
...省略內容
<Directory "/var/www/html">
<RequireALL> '***只要裏面有require all和not ,外面就要加標籤,否則重新啓動httpd服務會報錯***'
Require not ip 192.168.100.100 '//表示不允許ip192.168.100.100存取'
Require all granted
<RequireALL>
</Directory>
</VirtualHost>
...省略內容
[root@localhost ~]# cd /usr/local/httpd/
[root@localhost httpd]# htpasswd -c /etc/httpd/conf/.user jerry '-c 新建使用,再次建使用者則不需要; 新建密碼檔案'
New password:
Re-type new password:
Adding password for user webadmin
[root@localhost httpd]# cat /usr/local/httpd/confl.user '確認用戶數據庫檔案'
webadmin:$apr1$L53Ws/Y2$3L4xhs4zZKDbJb.9p 1Ng.
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
......
<Directory "/usr/local/httpd/htdocs">
......
AuthName "DocumentRoot" '受保護的領域名稱'
AuthType Basic '認證型別'
AuthUserFile /etc/httpd/conf/.user '使用者認證賬號檔案'
Require valid-user '要求通過認證才能 纔能存取'
</Directory>
[root@localhost ~ ]# systemctl restart httpd
'#安裝軟體'
[root@localhost ~]# yum -y install httpd
'#檢視是否有htpasswd'
[root@localhost ~]# which htpasswd
/usr/bin/htpasswd
'#建立密碼'
[root@localhost httpd]# htpasswd -c /etc/httpd/conf/.user jerry
'#檢視一下密碼'
[root@localhost httpd]# cat /usr/local/httpd/confl.user
'#修改組態檔'
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
...... '將Require all granted註釋掉'
AuthName "DocumentRoot" '受保護的領域名稱'
AuthType Basic '認證型別'
AuthUserFile /etc/httpd/conf/.user '使用者認證賬號檔案'
Require valid-user '要求通過認證才能 纔能存取'
</Directory>
'#啓動服務,關閉核心防護,關閉防火牆'
[root@localhost httpd]# systemctl start httpd
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
瀏覽器驗證:
設定網站的日誌檔案轉交給rotatelogs分割處理
設定格式爲
ErrorLog "| rotatelogs 命令的絕對路徑 -l 日誌檔案路徑/網站名-error_%Y%m%d.log 86400" '//which rotatelogs命令檢視絕對路徑,%Y%m%d表示年月日,86400表示一天的秒數'
CustomLog "| rotatelogs 命令的絕對路徑 -l 日誌檔案路徑/網站名-access_%Y%m%d.log 86400" combined
例如
[root@localhost logs]vim /etc/httpd/conf/httpd.conf
ErrorLog "| /usr/sbin/rotatelogs -l logs/error_%Y%m%d.log 86400"
實際生產環境中,一個伺服器絕大多數對應N個子域名站點,爲了方便統一管理,可以用虛擬主機的方式進行設定,並用網站名標識日誌檔案
一臺centos 7.6虛擬機器,一臺win 10虛擬機器
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# which rotatelogs
/usr/sbin/rotatelogs '日誌的路徑'
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 14.0.0.44:80 '修改監聽埠爲虛擬機器的地址,埠號爲80 '
#Listen 80 '將ipv6的埠註釋掉'
.......
ServerName www.yyc.com:80 '域名設定爲www.yyc.com'
[root@localhost ~]# ls /var/log/httpd/ '沒開啓httpd服務時,是空目錄'
[root@localhost ~]# systemctl start httpd.service '開啓httpd服務'
[root@localhost ~]# ls /var/log/httpd/ '開啓httpd服務後,就會生成兩個日誌檔案'
access_log error_log
[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# cat access_log '沒有人存取,日誌就是空的'
開啓一個win10客戶機,將地址設定爲如下:
DNS伺服器設定爲虛擬機器的IP地址
開啓網頁搜尋14.0.0.44,會出現apache的測試網頁
[root@localhost httpd]# cat access_log '重新整理幾次網頁後,會出現海量日誌'
14.0.0.100 - - [15/Jul/2020:00:45:34 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
14.0.0.100 - - [15/Jul/2020:00:45:34 +0800] "GET /noindex/css/open-sans.css HTTP/1.1" 200 5081 "http://14.0.0.44/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
......
做日誌分割:182行修改 217行,修改雙引號內內容
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
182 ErrorLog "| /usr/sbin/rotatelogs -l logs/www.yyc.com.error_%Y%m%d.log 86400"
217 CustomLog "| /usr/sbin/rotatalogs -l logs/www.yyc.com.access_%Y%m%d.log 86400" combined
[root@localhost httpd]# systemctl restart httpd
[root@localhost httpd]# httpd -t '檢查語法'
Syntax OK
[root@localhost httpd]# ls /var/log/httpd/ '此時沒有error_log'
access_log error_log www.yyc.com.error_20200715.log
[root@localhost httpd]# ls /var/log/httpd/ '進入win10重新整理一下就會自動生成'
access_log error_log www.yyc.com.access_20200715.log www.yyc.com.error_20200715.log
[root@localhost httpd]# date '檢視當前日期'
2020年 07月 15日 星期三 01:27:12 CST
[root@localhost httpd]# date -s 08/05/20 '可以修改'
2020年 08月 05日 星期三 00:00:00 CST
[root@localhost httpd]# systemctl restart httpd '重新啓動httpd服務'
[root@localhost httpd]# ls /var/log/httpd/ '進入日誌目錄檢視'
access_log www.yyc.com.access_20200715.log www.yyc.com.error_20200805.log
error_log www.yyc.com.error_20200715.log
[root@localhost httpd]# ls /var/log/httpd/ 'win10重新整理網頁,就會出現設定的時間的日誌檔案'
access_log www.yyc.com.access_20200715.log www.yyc.com.error_20200715.log
error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
ErrorLog "l cronolog命令的絕對路徑 日誌檔案路徑/網站名-error-%Y%m%d.log'
......
CustomLog "I cronolog命令的絕對路徑 日誌檔案路徑/網站名-%Y%m%d.log" combinec
一臺centos 7.6虛擬機器,一臺win 10虛擬機器
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
cronolog-1.6.2-14.el7.x86_64.rpm rh
[root@localhost opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm
[root@localhost opt]# which cronolog
/usr/sbin/cronolog
[root@localhost opt]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log www.yyc.com.access_20200715.log www.yyc.com.error_20200715.log
error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
[root@localhost httpd]# rm -rf www*
[root@localhost httpd]# ls
access_log error_log
改規則:
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
ErrorLog "| /usr/sbin/cronolog logs/www.yyc.com.error_%Y%m%d.log"
......
CustomLog "| /usr/sbin/cronolog logs/www.yyc.com.access_%Y%m%d.log " combined '不能加-l,不然 ls /var/log/httpd/不會產生log,重新整理網頁 ls /var/log/httpd/不會產生error_log'
[root@localhost httpd]# systemctl restart httpd
[root@localhost httpd]# ls /var/log/httpd/
access_log error_log www.yyc.com.error_20200805.log
[root@localhost httpd]# ls /var/log/httpd/ 'win10重新整理網頁,會自動產生log日誌'
access_log error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
[root@localhost httpd]# date -s 05/20/20 '修改日期'
2020年 05月 20日 星期三 00:00:00 CST
[root@localhost httpd]# systemctl restart httpd
[root@localhost httpd]# ls /var/log/httpd/ 'win10重新整理網頁,會自動生成修改後的日期日誌'
access_log www.yyc.com.access_20200520.log www.yyc.com.error_20200520.log
error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
環境部署
VMware軟體
一臺centos7虛擬機器
一臺Windows虛擬機器
環境準備
WindowsDNS解析地址指向centos7
centos7安裝bind和httpd
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
設定dns服務
[root@localhost ~]# yum install bind httpd -y
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
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";
allow-query { any; };
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "yyc.com" IN {
type master;
file "yyc.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost yyc.com.zone
[root@localhost named]# vim yyc.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 14.0.0.44 '原有刪掉加一行'
[root@localhost named]# systemctl start named
安裝apache服務
[root@localhost named]# vim /etc/httpd/conf/httpd.conf
Listen 14.0.0.44:80
#Listen 80
......
ServerName www.yyc.com:80
[root@localhost named]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
<h1>this is test web</h1>
安裝AWStats
[root@localhost html]# rz -E '脫入軟體包'
rz waiting to receive.
[root@localhost html]# ls
awstats-7.6.tar.gz index.html
[root@localhost opt]# tar zxvf awstats-7.6.tar.gz
[root@localhost opt]# ls
awstats-7.6 awstats-7.6.tar.gz rh
[root@localhost opt]# mv awstats-7.6 /usr/local/awstats
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
awstats bin etc games include lib lib64 libexec sbin share src
[root@localhost local]# cd awstats/
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/ 'tools工具'
[root@localhost tools]# ls
awstats_buildstaticpages.pl awstats_updateall.pl httpd_conf nginx xslt
awstats_configure.pl dolibarr logresolvemerge.pl urlaliasbuilder.pl
awstats_exportlib.pl geoip_generator.pl maillogconvert.pl webmin
進行安裝:
[root@localhost tools]# ./awstats_configure.pl '進行安裝'
Config file path ('none' to skip web server setup):
> /etc/httpd/conf/httpd.conf '宣告路徑,會把設定直接寫入檔案'
......
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y '輸入y'
......
Your web site, virtual server or profile name:
> www.yyc.com
'其他全部是回車'
(多了一個檔案/etc/awstats/awstats.www.yyc.com.conf,後面要改,http://localhost/awstats/awstats.pl?config=www.yyc.com
這是域名)
[root@localhost tools]# vim /etc/httpd/conf/httpd.conf
'大G到文末,awstatsd 的檔案目錄和設定都被寫進去了'
<Directory "/usr/local/awstats/wwwroot">
Options None
AllowOverride None
# Order allow,deny '新增註 加注釋'
# Allow from all '新增註 加注釋'
Require all granted '新增一行'
</Directory>
[root@localhost tools]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.yyc.com.conf
[root@localhost awstats]# vim /etc/awstats/awstats.www.yyc.com.conf
LogFile="/var/log/httpd/access_log" '改名存取日誌檔案位置access_log'
...
DirData="/var/lib/awstats" 'awstats預設不存在(需要建立)'
[root@localhost awstats]# cd /var/lib/
[root@localhost lib]# ls '檢視檔案,並沒有組態檔裡的awstats目錄,需要自己建立'
AccountsService colord fwupdate libvirt ntp rpm-state tuned
alsa cs games lldpad os-prober rsyslog udisks2
alternatives dav gdm logrotate PackageKit samba unbound
authconfig dbus geoclue machines plymouth selinux upower
bluetooth dhclient gssproxy misc polkit-1 setroubleshoot vmware
boltd dnsmasq hyperv mlocate postfix sss xkb
certmonger flatpak initramfs net-snmp pulse stateless yum
chrony fprint ipa-client NetworkManager rpcbind systemd
color fwupd iscsi nfs rpm tpm
[root@localhost lib]# mkdir awstats
[root@localhost lib]# ls
AccountsService color fwupd iscsi nfs rpm tpm
alsa colord fwupdate libvirt ntp rpm-state tuned
alternatives cs games lldpad os-prober rsyslog udisks2
authconfig dav gdm logrotate PackageKit samba unbound
awstats dbus geoclue machines plymouth selinux upower
bluetooth dhclient gssproxy misc polkit-1 setroubleshoot vmware
boltd dnsmasq hyperv mlocate postfix sss xkb
certmonger flatpak initramfs net-snmp pulse stateless yum
chrony fprint ipa-client NetworkManager rpcbind systemd
[root@localhost lib]# systemctl restart httpd '重新啓動apache服務'
進入win10網頁搜尋:http://www.yyc.com/awstats/awstats.pl?config=www.yyc.com
(複製剛剛的域名,將localhost改爲www.yyc.com),此時沒有任何存取記錄
[root@localhost lib]# cd /usr/local/awstats
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/
[root@localhost tools]# ls
awstats_buildstaticpages.pl awstats_updateall.pl httpd_conf nginx xslt
awstats_configure.pl dolibarr logresolvemerge.pl urlaliasbuilder.pl
awstats_exportlib.pl geoip_generator.pl maillogconvert.pl webmin
[root@localhost tools]# ./awstats_updateall.pl now '更新數據'
再重新整理網頁,就會有存取記錄
###此時使用者存取網頁,記錄需要人工輸入 ./awstats_updateall.pl now 命令進行更新數據,非常麻煩。
[root@localhost tools]# crontab -e '不加使用者就是當前使用者'
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now '每五分鐘更新一次'
[root@localhost tools]# cd /var/www/html/
[root@localhost html]# ls
index.html
[root@localhost html]# vim count.html
<html>
<head>
<meta http-equiv=refresh content="0;url=http://www.yyc.com/awstats/awstats.pl?config=www.yyc.com">
<head>
<body></body>
</html>
'實現自動跳轉功能'
開啓網頁搜尋www.yyc.com/count.html ,會自動跳轉至http://www.yyc.com/awstats/awstats.pl?config=www.yyc.com頁面,方便很多