1:Apache特点:
功能强大,配置简单,速度快,应用广泛,性能稳定可靠,并可做代理服务器或负载均衡来使用。
2:Apache的应用场景
1)使用Apache运行静态HTML网页,图片(处理静态小文件能力不及Nginx)。
2)使用Apache结合PHP引擎运行PHP,Perl,Python,等程序,LAMP被称之为经典组合。
3)使用Apache结合Tomcat/Resin运行JSP,JAVA等程序,称为中小企业的首选。
4)使用Apache做代理,负载均衡,rewrite规则过滤等等。
3:查看Apache版本
rpm -qa httpd
卸载Apache
rpm -e httpd-2.4.6-67.el7.centos.6.x86_64
强制卸载Apache
rpm -e --nodeps httpd-2.4.6-67.el7.centos.6.x86_64
4:Apache 添加模块。
[root@devops-zabbix httpd-2.2.34]# pwd
/home/hubo/httpd-2.2.34
[root@devops-zabbix httpd-2.2.34]# cd /application/apache
[root@devops-zabbix apache]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@devops-zabbix apache]# cd conf/
[root@devops-zabbix conf]# ls
extra httpd.conf magic mime.types original
进入主配置文件
[root@devops-zabbix conf]# grep -i documentroot httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/application/apache2.2.27/htdocs"
# This should be changed to whatever you set DocumentRoot to.
# access content that does not live under the DocumentRoot.
[root@devops-zabbix conf]# cd ../
[root@devops-zabbix apache]# cd htdocs/
[root@devops-zabbix htdocs]# cat index.html
<html><body><h1>It works!</h1></body></html>
[root@devops-zabbix htdocs]# rm -f index.html
[root@devops-zabbix htdocs]# vim index.html
脚本
<html>
<head><title>hubo,s blog</title></head>
<body>
Hi,I an hubo. Myblog address is
<a href="http://blog.csdn.net/hubo2017"targe=_parent>http://blog.csdn.net/hubo2017</a>
</body>
</html>
5:平滑重启
[root@devops-zabbix conf]# ../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
Syntax OK
[root@devops-zabbix conf]# ../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
6:虚拟机主机:部署多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,不同的IP。
需要虚拟知己功能。
一句话,一个http服务要配置多个站点,就需要虚拟主机。
虚拟机分类:
1)基于域名 *****
2)基于端口
3)基于ip
域名: 站点目录
www,hubo.org /var/html/www
blog.hubo.org /var/html/blog
bbs.hubo.org /var/html/bbs
[root@devops-zabbix html]# mkdir /var/html/{www,blog,bbs} -p
[root@devops-zabbix html]# ll
总用量 0
drwxr-xr-x 2 root root 6 11月 17 14:18 bbs
drwxr-xr-x 2 root root 6 11月 17 14:18 blog
drwxr-xr-x 2 root root 6 11月 17 14:16 www
[root@devops-zabbix extra]# touch /var/html/{www,blog,bbs}/index.html
[root@devops-zabbix extra]# for name in www blog bbs;do echo "http://$name.hubo.org">/var/html/$name/index.html
;done
[root@devops-zabbix extra]# for name in www blog bbs;do cat /var/html/$name/index.html;done
http://www.hubo.org
http://blog.hubo.org
http://bbs.hubo.org
[root@devops-zabbix extra]# vim httpd-vhosts.conf
#
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/www"
ServerName www.hubo.org
ServerAlias hubo.org
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/blog"
ServerName blog.hubo.org
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/bbs"
ServerName bbs.hubo.org
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>
[root@devops-zabbix extra]# vim ../httpd.conf
[root@devops-zabbix extra]# vim httpd-vhosts.conf
[root@devops-zabbix extra]# ../../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
Syntax OK
[root@devops-zabbix extra]# ../../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
主配置文件
[root@devops-zabbix extra]# vi ../httpd.conf
<Directory "/var/html">
#
# 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.2/mod/core.html#options
# for more information.
#
Options FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
7:Apache日志轮询
1)安装日志轮询工具cronolog
cd /home/hubo/tools/
wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
tar zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
./configure
make
make install
pwd
[root@devops-zabbix cronolog-1.6.2]# ll /usr/local/sbin/cronolog
-rwxr-xr-x 1 root root 48640 11月 17 16:57 /usr/local/sbin/cronolog
[root@devops-zabbix cronolog-1.6.2]# cd -
/home/hubo/tools
[root@devops-zabbix tools]# cd /application/apache/conf/
[root@devops-zabbix conf]# cd extra/
[root@devops-zabbix extra]# cp httpd-vhosts.conf httpd-vhosts.confn.name
[root@devops-zabbix extra]# vi httpd-vhosts.conf
配置文件最后加
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/bbs"
ServerName bbs.hubo.org
ErrorLog "logs/bbs-error_log"
CustomLog "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log" combined
</VirtualHost>
[root@devops-zabbix extra]# mkdir /app/logs -p
[root@devops-zabbix extra]# ../../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
Syntax OK
[root@devops-zabbix extra]# ../../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
[root@devops-zabbix extra]# ll /app/logs/
总用量 4
-rw-r--r-- 1 root root 60 11月 17 17:11 access_bbs_20171117.log
[root@devops-zabbix extra]# cat /app/logs/access_bbs_20171117.log
查看日志
[root@devops-zabbix extra]# tail -f /application/apache/logs/www-access_log
172.16.18.1 - - [17/Nov/2017:15:47:06 +0800] "GET / HTTP/1.1" 403 202
172.16.18.1 - - [17/Nov/2017:15:47:07 +0800] "GET / HTTP/1.1" 403 202
172.16.18.1 - - [17/Nov/2017:15:49:08 +0800] "GET / HTTP/1.1" 200 20
172.16.18.1 - - [17/Nov/2017:15:49:11 +0800] "GET / HTTP/1.1" 304 -
172.16.18.1 - - [17/Nov/2017:15:49:14 +0800] "GET / HTTP/1.1" 304 -
172.16.18.1 - - [17/Nov/2017:16:01:04 +0800] "GET /zabbix/ HTTP/1.1" 404 205
172.16.18.1 - - [17/Nov/2017:16:01:04 +0800] "GET /zabbix/ HTTP/1.1" 404 205
172.16.18.1 - - [17/Nov/2017:16:01:09 +0800] "GET / HTTP/1.1" 200 20
172.16.18.1 - - [17/Nov/2017:17:11:39 +0800] "GET / HTTP/1.1" 304 -
172.16.18.1 - - [17/Nov/2017:17:11:40 +0800] "GET / HTTP/1.1" 304 -
日志切割00点:
cd /application/apache/logs
mv www-access_log www-access_$(date +%F)log
/application/apache/bin/apachectl graceful
查询日志
awk '{print $1}' www-access_log|sort|uniq -c|sort -rn -k1|head -10
功能强大,配置简单,速度快,应用广泛,性能稳定可靠,并可做代理服务器或负载均衡来使用。
2:Apache的应用场景
1)使用Apache运行静态HTML网页,图片(处理静态小文件能力不及Nginx)。
2)使用Apache结合PHP引擎运行PHP,Perl,Python,等程序,LAMP被称之为经典组合。
3)使用Apache结合Tomcat/Resin运行JSP,JAVA等程序,称为中小企业的首选。
4)使用Apache做代理,负载均衡,rewrite规则过滤等等。
3:查看Apache版本
rpm -qa httpd
卸载Apache
rpm -e httpd-2.4.6-67.el7.centos.6.x86_64
强制卸载Apache
rpm -e --nodeps httpd-2.4.6-67.el7.centos.6.x86_64
4:Apache 添加模块。
[root@devops-zabbix httpd-2.2.34]# pwd
/home/hubo/httpd-2.2.34
[root@devops-zabbix httpd-2.2.34]# cd /application/apache
[root@devops-zabbix apache]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@devops-zabbix apache]# cd conf/
[root@devops-zabbix conf]# ls
extra httpd.conf magic mime.types original
进入主配置文件
[root@devops-zabbix conf]# grep -i documentroot httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/application/apache2.2.27/htdocs"
# This should be changed to whatever you set DocumentRoot to.
# access content that does not live under the DocumentRoot.
[root@devops-zabbix conf]# cd ../
[root@devops-zabbix apache]# cd htdocs/
[root@devops-zabbix htdocs]# cat index.html
<html><body><h1>It works!</h1></body></html>
[root@devops-zabbix htdocs]# rm -f index.html
[root@devops-zabbix htdocs]# vim index.html
脚本
<html>
<head><title>hubo,s blog</title></head>
<body>
Hi,I an hubo. Myblog address is
<a href="http://blog.csdn.net/hubo2017"targe=_parent>http://blog.csdn.net/hubo2017</a>
</body>
</html>
5:平滑重启
[root@devops-zabbix conf]# ../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
Syntax OK
[root@devops-zabbix conf]# ../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
6:虚拟机主机:部署多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,不同的IP。
需要虚拟知己功能。
一句话,一个http服务要配置多个站点,就需要虚拟主机。
虚拟机分类:
1)基于域名 *****
2)基于端口
3)基于ip
域名: 站点目录
www,hubo.org /var/html/www
blog.hubo.org /var/html/blog
bbs.hubo.org /var/html/bbs
[root@devops-zabbix html]# mkdir /var/html/{www,blog,bbs} -p
[root@devops-zabbix html]# ll
总用量 0
drwxr-xr-x 2 root root 6 11月 17 14:18 bbs
drwxr-xr-x 2 root root 6 11月 17 14:18 blog
drwxr-xr-x 2 root root 6 11月 17 14:16 www
[root@devops-zabbix extra]# touch /var/html/{www,blog,bbs}/index.html
[root@devops-zabbix extra]# for name in www blog bbs;do echo "http://$name.hubo.org">/var/html/$name/index.html
;done
[root@devops-zabbix extra]# for name in www blog bbs;do cat /var/html/$name/index.html;done
http://www.hubo.org
http://blog.hubo.org
http://bbs.hubo.org
[root@devops-zabbix extra]# vim httpd-vhosts.conf
#
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/www"
ServerName www.hubo.org
ServerAlias hubo.org
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/blog"
ServerName blog.hubo.org
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/bbs"
ServerName bbs.hubo.org
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>
[root@devops-zabbix extra]# vim ../httpd.conf
[root@devops-zabbix extra]# vim httpd-vhosts.conf
[root@devops-zabbix extra]# ../../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
Syntax OK
[root@devops-zabbix extra]# ../../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
主配置文件
[root@devops-zabbix extra]# vi ../httpd.conf
<Directory "/var/html">
#
# 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.2/mod/core.html#options
# for more information.
#
Options FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
7:Apache日志轮询
1)安装日志轮询工具cronolog
cd /home/hubo/tools/
wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
tar zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
./configure
make
make install
pwd
[root@devops-zabbix cronolog-1.6.2]# ll /usr/local/sbin/cronolog
-rwxr-xr-x 1 root root 48640 11月 17 16:57 /usr/local/sbin/cronolog
[root@devops-zabbix cronolog-1.6.2]# cd -
/home/hubo/tools
[root@devops-zabbix tools]# cd /application/apache/conf/
[root@devops-zabbix conf]# cd extra/
[root@devops-zabbix extra]# cp httpd-vhosts.conf httpd-vhosts.confn.name
[root@devops-zabbix extra]# vi httpd-vhosts.conf
配置文件最后加
<VirtualHost *:80>
ServerAdmin 10416301@qq.com
DocumentRoot "/var/html/bbs"
ServerName bbs.hubo.org
ErrorLog "logs/bbs-error_log"
CustomLog "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log" combined
</VirtualHost>
[root@devops-zabbix extra]# mkdir /app/logs -p
[root@devops-zabbix extra]# ../../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
Syntax OK
[root@devops-zabbix extra]# ../../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.8.239 for ServerName
[root@devops-zabbix extra]# ll /app/logs/
总用量 4
-rw-r--r-- 1 root root 60 11月 17 17:11 access_bbs_20171117.log
[root@devops-zabbix extra]# cat /app/logs/access_bbs_20171117.log
查看日志
[root@devops-zabbix extra]# tail -f /application/apache/logs/www-access_log
172.16.18.1 - - [17/Nov/2017:15:47:06 +0800] "GET / HTTP/1.1" 403 202
172.16.18.1 - - [17/Nov/2017:15:47:07 +0800] "GET / HTTP/1.1" 403 202
172.16.18.1 - - [17/Nov/2017:15:49:08 +0800] "GET / HTTP/1.1" 200 20
172.16.18.1 - - [17/Nov/2017:15:49:11 +0800] "GET / HTTP/1.1" 304 -
172.16.18.1 - - [17/Nov/2017:15:49:14 +0800] "GET / HTTP/1.1" 304 -
172.16.18.1 - - [17/Nov/2017:16:01:04 +0800] "GET /zabbix/ HTTP/1.1" 404 205
172.16.18.1 - - [17/Nov/2017:16:01:04 +0800] "GET /zabbix/ HTTP/1.1" 404 205
172.16.18.1 - - [17/Nov/2017:16:01:09 +0800] "GET / HTTP/1.1" 200 20
172.16.18.1 - - [17/Nov/2017:17:11:39 +0800] "GET / HTTP/1.1" 304 -
172.16.18.1 - - [17/Nov/2017:17:11:40 +0800] "GET / HTTP/1.1" 304 -
日志切割00点:
cd /application/apache/logs
mv www-access_log www-access_$(date +%F)log
/application/apache/bin/apachectl graceful
查询日志
awk '{print $1}' www-access_log|sort|uniq -c|sort -rn -k1|head -10