Apache連線保持相關參數
使用Require設定項實現存取控制,按先後順序限制
可用,,,設定段中
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">
Require not ip 192.168.100.100 '//表示不允許ip192.168.100.100存取'
Require all granted
</Directory>
</VirtualHost>
...省略內容
命令基本格式
'//htpasswd命令是httpd自帶的'
htpasswd -c /etc/httpd/conf/abc webadmin '//爲使用者webadmin建立密碼檔案'
New password:'//輸入密碼'
Re-type new password:'//重複輸入密碼'
cat /etc/httpd/conf/abc
webadmin:加密密碼
舉例
[root@localhost abc]# htpasswd -c /etc/httpd/conf/aaa ddd
New password:
Re-type new password:
Adding password for user ddd
[root@localhost abc]# cat /etc/httpd/conf/aaa
ddd:$apr1$zwIklPGy$ZINaIkXsSq36oIk2lAiiJ1
'//新增第二個使用者'
[root@localhost abc]# htpasswd /etc/httpd/conf/aaa bbb
New password:
Re-type new password:
Adding password for user bbb
[root@localhost abc]# cat /etc/httpd/conf/aaa
ddd:$apr1$zwIklPGy$ZINaIkXsSq36oIk2lAiiJ1
bbb:$apr1$q7OdS8z1$ELGg2a1AhHP.QfXJXfQTQ1
'//設定基本格式'
<Directory "/var/www/html">
AuthName "DocumentRoot" '//受保護的領域名稱'
AuthType Basic '//認證型別'
AuthUserFile /etc/httpd/conf/qwe '//使用者認證賬號檔案'
Require valid-user '//要求通過認證才能 纔能存取'
</Directory>
舉例
[root@localhost abc]# vim vhost.conf
...省略內容
<VirtualHost 192.168.197.100:80>
DocumentRoot "/var/www/html/ccc"
ServerName www.ccc.com
Errorlog "logs/www.ccc.com.error_log"
Customlog "logs/www.ccc.com.custom_log" common
<Directory "/var/www/html">
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /etc/httpd/conf/aaa
Require valid-user
</Directory>
</VirtualHost>
...省略內容
設定網站的日誌檔案轉交給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"
CustomLog "| /usr/sbin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined
VMware軟體
一臺centos7虛擬機器
一臺Windows虛擬機器
WindowsDNS解析地址指向centos7
centos7安裝bind和httpd
[root@localhost opt]# vim /etc/named.conf
options {
listen-on port 53 { any; }; '//修改成any'
...省略內容
allow-query { any; }; '//修改成any'
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "dabao.com" IN {
type master;
file "dabao.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost dabao.com.zone
[root@localhost named]# vim dabao.com.zone
'//尾行修改爲下面 下麪內容'
www IN A 192.168.197.143
[root@localhost named]# systemctl start named
[root@localhost named]# vim /etc/httpd/conf/httpd.conf
'//修改此兩行內容'
Listen 192.168.197.143:80
#Listen 80
'//修改爲指定域名'
ServerName www.dabao.com:80
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start httpd.service
[root@localhost ~]# mount.cifs //192.168.100.3/ccc /opt
Password for root@//192.168.100.3/ccc:
[root@localhost ~]# df -h
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf awstats-7.6.tar.gz -C /mnt
[root@localhost opt]# cd /mnt
[root@localhost mnt]# ls
awstats-7.6
[root@localhost mnt]# mv awstats-7.6/ /usr/local/awstats
[root@localhost mnt]# ls
[root@localhost mnt]# 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/
[root@localhost tools]# ls
[root@localhost tools]# ./awstats_configure.pl
> /etc/httpd/conf/httpd.conf '//第一次互動輸入此路徑'
y '//第二次互動輸入y'
www.dabao.com '//第三次互動輸入指定域名'
'//第四五六次互動直接回車'
[root@localhost tools]# cd /etc/httpd/conf
[root@localhost conf]# vim httpd.conf
'//省略內容,直接到檔案末尾修改此設定'
<Directory "/usr/local/awstats/wwwroot">
Options None
AllowOverride None
# Order allow,deny
# Allow from all
Require all granted
</Directory>
[root@localhost conf]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.dabao.com.conf
[root@localhost awstats]# vim awstats.www.dabao.com.conf
...省略內容
'//將下面 下麪兩行內容修改'
LogFile="/var/log/httpd/access_log"
...省略內容
DirData="/var/lib/awstats"
[root@localhost awstats]# cd /var/lib
[root@localhost lib]# ls
[root@localhost lib]# mkdir awstats
[root@localhost lib]# systemctl restart httpd.service
嘗試存取awstats網頁
使用剛剛互動生成的網址存取,將其中的localhost修改爲指定域名
[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
...省略內容
[root@localhost tools]# ./awstats_updateall.pl now
...省略內容
[root@localhost tools]# crontab -e
'//新增一下內容'
*/3 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
[root@localhost tools]# systemctl start crond
You have new mail in /var/spool/mail/root
[root@localhost tools]# cd /var/www/html
[root@localhost html]# vim aws.html
<html>
<head>
<meta http-equiv=refresh content="0;url=http://www.dabao.com/awstats/awstats.pl?config=www.dabao.com">
</head>
<body></body>
</html>
[root@localhost ~]# vim /etc/http/conf/xxx/vhost.conf
1 <VirtualHost *:80> '//針對本伺服器所有IP地址'
2 DocumentRoot "/var/www/html/xxx" '//指定站點目錄'
3 ServerName www.xxx.com '//指定域名'
4 Errorlog "logs/www.xxx.com.error_log" '//指定錯誤日誌路徑'
5 Customlog "logs/www.xxx.com.custom_log" common '//指定存取日誌路徑'
6 <Directory> "/var/www/html">
7 Require all granted '//設定網頁允許所有主機存取,也可以通過denied設定拒絕存取'
8 </Directory>
9 </VirtualHost>
生產環境中,伺服器不止兩臺
每台伺服器都有虛擬主機對應各自的網站
通過排程伺服器調取伺服器中的網站資源
VMware軟體
一臺Windows虛擬機器,IP地址爲192.168.197.133
一臺centos7虛擬機器,IP地址爲192.168.197.141
centos7安裝bind和httpd服務
[root@localhost ~]# yum install bind httpd -y
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/named.conf
...省略內容
options {
listen-on port 53 { any; }; '//修改爲any'
listen-on-v6 port 53 { ::1; };
...省略內容
allow-query { any; }; '//修改爲any'
...省略內容
[root@localhost ~]# vim /etc/named.rfc1912.zones
'//新增一下兩端內容'
...省略內容
zone "dabao.com" IN {
type master;
file "dabao.com.zone";
allow-update { none; };
};
zone "erbao.com" IN {
type master;
file "erbao.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 dabao.com.zone
[root@localhost named]# vim dabao.com.zone
...省略內容
'//末尾新增內容'
www IN A 192.168.197.141
[root@localhost named]# cp -p dabao.com.zone erbao.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir baobao
[root@localhost conf]# ls
baobao httpd.conf magic
[root@localhost conf]# cd baobao
[root@localhost baobao]# ls
[root@localhost baobao]# vim vhost.conf
1 <VirtualHost *:80>
2 DocumentRoot "/var/www/html/dabao"
3 ServerName www.dabao.com
4 Errorlog "logs/www.dabao.com.error_log"
5 Customlog "logs/www.dabao.com.custom_log" common
6 <Directory> "/var/www/html">
7 Require all granted
8 </Directory>
9 </VirtualHost>
10
11 <VirtualHost *:80>
12 DocumentRoot "/var/www/html/erbao"
13 ServerName www.erbao.com
14 Errorlog "logs/www.erbao.com.error_log"
15 Customlog "logs/www.erbao.com.custom_log" common
16 <Directory> "/var/www/html">
17 Require all granted
18 </Directory>
19 </VirtualHost>
[root@localhost baobao]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir dabao erbao
[root@localhost html]# ls
dabao erbao
[root@localhost html]# echo "this is dabao" > dabao/index.html
[root@localhost html]# echo "this is erbao" > erbao/index.html
[root@localhost html]# yum install tree
[root@localhost html]# tree /var/www/html/
/var/www/html/
├── dabao
│ └── index.html
└── erbao
└── index.html
2 directories, 2 files
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
'//找到此行'
IncludeOptional conf.d/*.conf
Include conf/baobao/vhost.conf '//新增此行內容'
[root@localhost html]# systemctl start httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# systemctl start httpd
[root@localhost html]# cd /etc/httpd/conf
[root@localhost conf]# ls
baobao httpd.conf magic
[root@localhost conf]# cd baobao
[root@localhost baobao]# ls
vhost.conf
[root@localhost baobao]# vim vhost.conf
'//在第二個主機設定後新增80'
<VirtualHost *:8080> '//多寫一個80'
[root@localhost baobao]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.197.141:80
Listen 192.168.197.141:8080 '//找到監聽地址,開啓IPv4監聽,設定監聽地址,新增一個8080埠的監聽地址'
#Listen 80 '//關閉IPv6監聽'
[root@localhost baobao]# systemctl restart httpd.service
[root@localhost baobao]# vim vhost.conf
<VirtualHost 192.168.197.141:80> '//將*修改爲第一張網絡卡IP地址'
<VirtualHost 192.168.197.180:80> '//將*修改爲第二張網絡卡IP地址'
'//將他們的域名都註釋掉'
[root@localhost baobao]# vim /etc/httpd/conf/httpd.conf
...省略內容
Listen 192.168.197.141:80
Listen 192.168.197.180:80
#Listen 80
...省略內容
[root@localhost baobao]# systemctl restart httpd