企业级Apache详解

安装Apache

#Apache安装
rpm -qa|grep httpd
yum install httpd

#2编译安装:          -->推荐安装
cd /root/software
yum -y install gcc+ gcc-c++
yum -y install zlib zlib-devel 
------------------------------------------------------------------------------
 wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz  
 tar -zxf apr-1.4.5.tar.gz
 cd apr-1.4.5
 ./configure --prefix=/usr/local/apr
 make && make install  
 echo $?
------------------------------------------------------------------------------
 wget http://archive.apache.org/dist/apr/apr-util-1.3.12.tar.gz  
 tar -zxf apr-util-1.3.12.tar.gz  
 cd apr-util-1.3.12
 ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config
 make && make install 
------------------------------------------------------------------------------
 wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.10/pcre-8.10.zip 
 unzip -o pcre-8.10.zip  
 cd pcre-8.10
 ./configure --prefix=/usr/local/pcre  
 make && make install 
------------------------------------------------------------------------------
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.27.tar.gz
tar xf httpd-2.4.27.tar.gz
cd httpd-2.4.27
./configure --prefix=/usr/local/apache --with-apr-util=/usr/local/apr-util --with-apr=/usr/local/apr --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker
make;make install
------------------------------------------------------------------------------
启动服务前的测试:
/usr/local/apache/bin/apachectl -t

启动服务
/usr/local/apache/bin/apachectl start
	==>/usr/local/apache/bin/httpd start
lsof -i:80 	    -->查看端口
ps -fe|grep httpd   -->查看进程

平滑启动服务:
/usr/local/apache/bin/apachectl graceful    (没有横杠)
浏览器显示:
     http://192.168.25.135/     -->It works
问题解决:关闭selinux, 关闭iptables,查看端口80,查看进程httpd,curl 192.168.25.135查看帮助:
/usr/local/apache/bin/apachectl    
查看模块列表
/usr/local/apache/bin/apachectl -l
查看首页
cd /usr/local/apache/conf
grep -i documentroot httpd.conf 
    -->DocumentRoot "/usr/local/apache/htdocs"
cat ../htdocs/index.html  
    -->It works
------------------------------------------------------------------------------
主配置文件学习  -->详情参考主配置说明
cd /usr/local/apache 
grep -Ev "#|^$" /usr/local/apache/conf/httpd.conf > /usr/local/apache/conf/httpd.conf.ori 

------------------------------------------------------------------------------

conf下重要文件:
 cd /usr/local/apache/conf/extra    
      httpd-vhosts.conf  -->基于域名的配置文件   
      httpd-mpm.conf     -->主要的工作模式    
      httpd-default.conf -->默认配置 

虚拟主机配置

虚拟主机配置: 部署多个站点,每个站点,希望用多个域名和站点目录,或者不同的端口,不同IP
    ==》一个WEB服务,配置多个网站,就需要虚拟主机
vim /usr/local/apache/conf/extra/httpd-vhosts.conf
虚拟主机分类:
    a.基于域名(最常用的)
    b.基于端口
    c.基于IP的

基于域名的配置:

需求:配置3个站点

  域名                 站点目录
www.ftl.com	         /var/html/www
www.blog.ftl.com     /var/html/blog
www.bbs.ftl.com      /var/html/bbs

创建站点目录
mkdir -p /var/html/{bbs,blog,www}
touch /var/html/{bbs,blog,www}/index.html
for name in {bbs,blog,www}; do echo "this is www.$name.ftl.com" >/var/html/$name/index.html; done; 
for name in {bbs,blog,www}; do cat /var/html/$name/index.html; done;

编辑Linux虚拟主机文件
vim /usr/local/apache/conf/extra/httpd-vhosts.conf 
   <VirtualHost *:80>
      ServerAdmin 623799533@qq.com
      DocumentRoot "/var/html/www"
      ServerName www.ftl.com
      ServerAlias ftl.com
      ErrorLog "logs/www-error_log"
      CustomLog "logs/www-access_log" common
   </VirtualHost>

启用虚拟主机(取消注释)
	使用vhost和mpm
vim /usr/local/apache/conf/httpd.conf
	# Virtual hosts
	Include conf/extra/httpd-vhosts.conf
	
	# Server-pool management (MPM specific)
	Include conf/extra/httpd-mpm.conf
查看httpd.conf开启的配置
grep "^Include"   /usr/local/apache/conf/httpd.conf
	Include conf/extra/httpd-mpm.conf
	Include conf/extra/httpd-vhosts.conf


添加新的站点配置(原配置只针对默认站点生效,不配置会有403错误)
vim /usr/local/apache/conf/httpd.conf   

      #add one and three are valid
      <Directory "/var/html">
         #not show the level of Dir
         Options -Indexes FollowSymLinks       
         AllowOverride None
         Order allow,deny
         Allow from all
      </Directory>

检查语法:
	/usr/local/apache/bin/apachectl -t
重启服务:(前面的进程结束后再重新启动下一次进程)
	/usr/local/apache/bin/apachectl graceful
放在init.dl里面:
cp  /usr/local/apache/bin/apachectl graceful /etc/init.d/httpd
service httpd graceful

Win配置host解析
	C:\Windows\System32\drivers\etc
	192.168.25.135  www.ftl.com   ftl.com
	192.168.25.135  www.blog.ftl.com  blog.ftl.com
	192.168.25.135  www.bbs.ftl.com  bbs.ftl.com
检查hosts解析是否成功
	ping www.ftl.com   -->返回190.168.25.135即为成功
页面浏览器访问
	http://www.blog.ftl.com/
	http://www.bbs.ftl.com/
	http://www.ftl.com/

基于端口的访问:

vim /usr/local/apache/conf/httpd.conf
	搜索Listen,同时复制多个Listen,用于监听不同的端口
vim /usr/local/apache/conf/extra/httpd-vhosts.conf 
	配置监听端口和虚拟IP端口
	NameVirtualHost *:8000                       -->注意此处,更改端口为8000
	<VirtualHost *:8000>                         -->注意此处,更改端口为8000
   	   ServerAdmin 623799533@qq.com
 	    DocumentRoot "/var/html/www"
	    ServerName www.hhh.com
	    ServerAlias hhh.com
	    ErrorLog "logs/hhh-error_log"
	    CustomLog "logs/hhh-access_log" common 
	</VirtualHost>
测试,请重启服务

基于IP的访问:

vim /usr/local/apache/conf/httpd.conf
	搜索Listen,增加配置
	Listen 10.0.0.3:82
vim /usr/local/apache/conf/extra/httpd-vhosts.conf 
	配置监听端口和虚拟IP端口
	NameVirtualHost *:80                       -->注意此处
	<VirtualHost 10.0.0.3:80>                  -->更改此处,更改为IP即可
   	   ServerAdmin 623799533@qq.com
 	    DocumentRoot "/var/html/www"
	    ServerName 10.0.0.3                    -->更改此处,更改为IP即可
	    ServerAlias hhh.com
	    ErrorLog "logs/hhh-error_log"
	    CustomLog "logs/hhh-access_log" common 
	</VirtualHost>
测试,请重启服务

解决FQDN问题:


vim /usr/local/apache/conf/httpd.conf
    配置:
         ServerName 127.0.0.1:80  
测试请重启服务
service httpd -t
service httpd graceful

 

Apache下重要目录

pache下重要目录
[root@lamp01 apache]# tree ../apache/ -L 2
../apache/
├── bin
│?? ├── ab                 #Apache服务器性能测试工具,简单,易用
│?? ├── apachectl          #Apache的启动命令
│?? ├── apxs               #Apache服务器编译和安装扩展模块工具,在进行DSO方式的会用到
                                 #后面编译php软件的时候会用到  --with-apxs2=/usr/local/bin/apxs
│?? ├── htcacheclean       #清理磁盘缓存,很少用
│?? ├── htdbm              #建立和更新基本认证文件,配置nagios等监控服务
│?? ├── httpd              #apache的控制命令程序,apachectl执行会调用
│?? └── rotatelogs         #apache自带的日志轮询工具,但是可用cronolog代替
├── build
│?
├── conf
│?? ├── extra              #额外的apache配置文件目录,httpd-vhosts.conf默认在此
│?? ├── httpd.conf         #主配置文件
├── error
├── htdocs                    #默认站点文件,修改httpd.conf里面默认参数  -->Directoryindex index.html
│?? └── index.html
├── icons
├── include
├── logs                      #默认的日志文件
│?? ├── access_log
│?? ├── error_log
│?? └── httpd.pid          #httpd服务器启动后会把所有的进程ID写到此
├── man
│?? ├── man1
│?? └── man8
├── manual
└── modules               #apache的模块目录,后续编译的php等模块均在此
    └── httpd.exp

 

Apache日志学习分析

1.日志分类
    通用日志(CommonLogFormat)    -->默认
    组合日志(CombinedLogFormat)  -->工作中常见

vim /usr/local/apache/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    ServerAdmin 623799533@qeq.com
    DocumentRoot "/var/html/blog"
    ServerName www.blog.ftl.com
    ServerAlias blog.ftl.com
    ErrorLog "logs/blog-error_log" 
    CustomLog "logs/blog-access_log" combined                   -->更改此处即可调用主配置里面的格式
</VirtualHost>

2.Cronolog_Apache日志轮询
 

0.0 常规下所有的日志都在一个文件里面,所以我们希望日志轮询,按天查看
  1.1 自带的rotatelogs工具:

  2.1 安装cronolog
      cronolog安装配置非常简单,下载后只需要输入几个命令即可完成。
      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
      查看cronolog安装后所在目录(验证安装是否成功)
      which cronolog
      /usr/local/sbin/cronolog
   2.2 配置轮询日志

      <VirtualHost *:80>
         ServerAdmin 623799533@qeq.com
         DocumentRoot "/var/html/blog"
         ServerName www.blog.ftl.com
         ServerAlias blog.ftl.com
         ErrorLog "logs/blog-error_log" 
         CustomLog "|/usr/local/sbin/cronolog /usr/local/apache/logs/www-access_%Y%m%d_log" combined       -->更改此处即可调用主配置里面的格式
      </VirtualHost>
    说明:按照天轮询配置:CustomLog "|/usr/local/sbin/cronolog /usr/local/apache/logs/www-access_%Y%m%d_log" combined 
              安装小时轮询:CustomLog "|/usr/local/sbin/cronolog /usr/local/apache/logs/www-access_%Y%m%d%H_log" combined 
   2.3重启服务
    service httpd -t
    service httpd graceful
------------------------------------------------------------------------------
Apache 日志分析:
    awk '{print $1}' www-access_20170905_log |sort|uniq -c|sort -rn -k1|head -1
    awk '{++S[$1]} END {for (key in S) print S[key], key }' www-access_20170905_log |sort -rn -k1

【更多参考】

企业级Apache详解2

Apache整合PHP和MYSQL 未完成  20170905

posted @ 2018-07-15 10:48  小a玖拾柒  阅读(661)  评论(0编辑  收藏  举报