Apache configuration and application

1. Construction of Web virtual directories and user authorization restrictions

①Create user authentication data file

cd /usr/local/httpd/bin
htpasswd -c /usr/local/httpd/user zhangsan
htpasswd  /usr/local/httpd/user lisi
cat /usr/local/httpd/user

Insert picture description here
②Add user authorization configuration

vim /usr/local/httpd/conf/httpd.conf
(在最后写入以下内容)
Alias /test /var/www/html/test

<Directory "/var/www/html/test">
    AuthName "Hello!"
    AuthType Basic
    AuthUserFile /usr/local/httpd/user
    Require valid-user
   #authgroupfile /usr/local/httpd/conf/group
   #Require user zhangsan
   #Require group zhangsan
</Directory>

Insert picture description here

Alias /test /var/www/html/test     #设置虚拟目录的根目录,/test为虚拟目录名称

<Directory "/var/www/html/test">   #设置虚拟目录配置区域
    AuthName "Hello!"              #定义受保护的领域名称,会在认证对话框中显示
    AuthType Basic                 #设置认证的类型,Basic表示基本认证
    AuthUserFile /usr/local/httpd/user         #设置用于保存用户账号和密码的认证文件的路径
    Require valid-user             #开启用户认证,只有认证文件中的合法用户才能访问
   #authgroupfile /usr/local/httpd/conf/group  #设置用于保存组账号和密码的认证文件的路径
   #Require user zhangsan          #仅允许指定用户访问
   #Require group zhangsan         #仅允许指定组访问
</Directory>

③Verify user access authorization

mkdir -p /var/www/html/test
echo "<h1>this is test</h1>" > /var/www/html/test/index.html

systemctl restart httpd.service 

④Browser access verification

http://192.168.153.30/test

Insert picture description here
Insert picture description here

Two, Apache log split

Use Apache's own rotatelogs segmentation tool to automatically segment Apache logs according to the date of the day.

Apache日志文件所在文件位置:
/usr/local/httpd/logs

①Modify the httpd service configuration file and restart the service

vim /usr/local/httpd/conf/httpd.conf
-----修改273行附近该字段--------
ErrorLog "|/usr/local/bin/rotatelogs -l /var/log/httpd/error_%Y%m%d.log 86400"   #分割错误日志
-----修改308行附近该字段---------
CustomLog "|/usr/local/bin/rotatelogs -l /var/log/httpd/access_%Y%m%d.log 86400"    combined     #分割访问日志

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 
#开头的 | 为管道符号。
#-l 选项表示使用本地时间为时间基准
#86400表示一天,即每天生成一个新的日志文件

Error log:
Insert picture description here
Modified to:
Insert picture description here
Access log:
Insert picture description here
Uncommented, modified to:
Insert picture description here
②Create a directory for saving the divided log file

mkdir /var/log/httpd               
systemctl restart httpd.service    

③View the split log

vim /var/log/httpd/error_20210106.log    

Insert picture description here
④ Configure virtual host log split

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
<VirtualHost 192.168.153.30:80>
    DocumentRoot "/var/www/html/benet"
    ServerName www.benet.com
    ErrorLog "| /usr/local/bin/rotatelogs -l /var/log/httpd/benet.com-error_%F.log 86400"
   CustomLog "|/usr/local/bin/rotatelogs -l /var/log/httpd/benet.com-access_%F.log 86400"    combined
</VirtualHost>

<VirtualHost 192.168.153.30:8080>
    DocumentRoot "/var/www/html/accp"
    ServerName www.accp.com
    ErrorLog "|/usr/local/bin/rotatelogs -l /var/log/httpd/accp.com-error_%F.log 86400"
    CustomLog "|/usr/local/bin/rotatelogs -l /var/log/httpd/accp.com-access_%F.log 86400"    combined
</VirtualHost>

<Directory "/var/www/html">
    Options None
    AllowOverride None
    Require all granted
</Directory>

systemctl restart httpd.service

Insert picture description here
At this point, you can view the log in the /var/log/httpd directory
Insert picture description here

Three, AWStats analysis system

①Transfer the software package required to install AWStats to the /opt directory and decompress it

cd /opt
tar zxvf awstats-7.6.tar.gz
mv /opt/awstats-7.6 /usr/local/awstats

②Establish configuration files for the sites to be counted

cd /usr/local/awstats/tools
./awstats_configure.pl
......

Config file path ('none' to skip web server setup):
> /usr/local/httpd/conf/httpd.conf                 #输入httpd服务主配置文件的路径
Your web site,virtual server or profile name:
> www.wt.com                                       #输入要统计的站点域名
其它全部是y或者回车

Insert picture description here

Insert picture description here
③Modify the automatically generated awstats access permissions and load the CGI module (Apache2.4 or higher version needs to load the CGI module)

vim /usr/local/httpd/conf/httpd.conf

<IfModule !mpm_prefork_module>
       LoadModule cgid_ module modules/mod_cgid.so          #143行,取消注释
</IfModule>
<IfModule mpm_prefork_module>
       LoadModule cgi_module modules/mod_cgi.so             #取消注释
</IfModule>

ErrorLog "logs/error_log"                          #273行附近
CustomLog "logs/access_log" combined               #302行附近

<Directory "/usr/local/awstats/wwwroot">
Options None
AllowOverride None
#Order allow,deny               #G到最后一行,进行注释
#Allow from all                 #进行注释
Require all granted             #添加
</Directory>

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
④ Modify the site statistics configuration file

vim /etc/awstats/awstats.www.abc.com.conf

LogFile="/usr/local/httpd/logs/access_log"   #修改访问日志文件位置
DirData="/var/lib/awstats"                   #awstats目录默认不存在,需要手动创建

mkdir /var/lib/awstats

Insert picture description hereInsert picture description here
⑤Perform log analysis and set up cron scheduled tasks

systemctl restart httpd
cd /usr/local/awstats/tools/

./awstats_updateall.pl now          #更新数据(根据站点配置文件指定的日志文件路径)

Insert picture description here
⑥ Write planned tasks, update log file data every 5 minutes

crontab -e                          
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now

systemctl start crond

⑦Visit AwStats analysis system site

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
echo "192.168.153.30 www.wt.com" >> /etc/hosts
浏览器访问:
http://www.wt.com/awstats/awstats.pl?config=www.wt.com

Insert picture description here
⑧ Optimize web page address

vim /var/www/html/aws.html
<html>
<head>
<meta http-equiv-refresh content="0;url=http://www.wt.com/awstats/awstats.pl?	config=www.wt.com">
</head>
<body></body>
</html>

HTML文件结构解释:
<html> </html>:用于HTML文件结构最外层表示的标签
<head></head>:用于HTML网页内容描述信息的头标签
<body></body>:用于显示网页内容的内容标签
<meta>:定义了HTML文档中的元数据,比如针对搜索引擎和更新频度的描述和关键词。这里的http-equiv=refresh用于实现网页自动跳转
浏览器访问:
http://www.wt.com/aws.html

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/112329187