apache的安装与配置

安装

01)下载httpd-2.2.27.tar.gz

02)解压:tar xf httpd-2.2.27.tar.gz

然后进入解压的文件查看INSTALLRAEDME

03)直接make会出错,然后进行编译(也会有问题,这时候yum -y install zlib zlib-devel即可解决),然后进行编译:

1
2
3
4
5
6
7
8
9
. /configure  \
--prefix= /application/apache2 .2.27 \   安装路径
-- enable -deflate \    压缩(为了发送给客户端的时候更快一点,但是消耗CPU)
-- enable -expires \    过期
-- enable -headers \   提供对http请求头的控制
-- enable -modules=most \    激活大多数模块
-- enable -so \    
--with-mpm=worker \    选择work模式(进程下的线程提供服务)还有profork模式(直接用进程提供服务,更安全稳定,但是消耗内存)
-- enable -rewrite   伪静态

04make&makeinstall

05ln -s /application/apache2.2.27/ /application/apache

06/application/apache/bin/apachectl -t

/application/apache/bin/apachectl start

    lsof -i :80

Netstat -ntlup|grep 80

Ps -ef |grep httpd

 

07curl 本机ip  如果不能访问进行下面的检查与操作:

/etc/init.d/iptables stop

setenforce 0

然后看看端口起来没

08)查看配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@qbPC ~] # cd /application/apache
[root@qbPC apache] # ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs   man   manual  modules
[root@qbPC apache] # cd conf/
[root@qbPC conf] # ls
extra  httpd.conf  magic  mime.types  original
[root@qbPC conf] # grep -i documentroot httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot  "/application/apache2.2.27/htdocs"
查看主配文件的存放数据的目录(index.html)
# This should be changed to whatever you set DocumentRoot to.
# access content that does not live under the DocumentRoot.
  
去掉注释查看主配文件
[root@qbPC conf] # grep -Ev "#|^$" httpd.conf > httpd.conf.ori
[root@qbPC conf] # cd extra/
[root@qbPC extra] # ls
httpd-autoindex.conf  httpd-info.conf       httpd-mpm.conf                 httpd-userdir.conf
httpd-dav.conf        httpd-languages.conf  httpd-multilang-errordoc.conf  httpd-vhosts.conf
httpd-default.conf    httpd-manual.conf     httpd-ssl.conf

深入解析主配文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[root@qbPC conf] # cat httpd.conf.ori
ServerRoot  "/application/apache2.2.27" 软件安装路径,也是服务器的根目录
#监听的端口
Listen 80
<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
User daemon
Group daemon
< /IfModule >
< /IfModule >
ServerAdmin you@example.com 管理员邮箱
DocumentRoot  "/application/apache2.2.27/htdocs" 默认站点目录
<Directory />
     Options FollowSymLinks
     AllowOverride None
     Order deny,allow
     Deny from all
< /Directory >
<Directory  "/application/apache2.2.27/htdocs" >
这个配置只对默认站点生效,如果有虚拟主机,需要复制这一段到主配文件最后,进行配置,对于option里面的indexes(没有首页展示的目录,一般去掉,不对外展示目录结构)
  Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
< /Directory >
<IfModule dir_module>指定访问首页
     DirectoryIndex index.html
< /IfModule >
<FilesMatch  "^\.ht" >文件匹配
     Order allow,deny
     Deny from all
     Satisfy All
< /FilesMatch >
ErrorLog  "logs/error_log" 设置错误日志
LogLevel warn 设置日志级别
<IfModule log_config_module>
     LogFormat  "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""  combined
     LogFormat  "%h %l %u %t \"%r\" %>s %b"  common
     <IfModule logio_module>
       LogFormat  "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"  combinedio
     < /IfModule >
     CustomLog  "logs/access_log"  common
< /IfModule >
<IfModule alias_module>
     ScriptAlias  /cgi-bin/  "/application/apache2.2.27/cgi-bin/"
< /IfModule >
<IfModule cgid_module>
< /IfModule >
<Directory  "/application/apache2.2.27/cgi-bin" >
     AllowOverride None
     Options None
     Order allow,deny
     Allow from all
< /Directory >
DefaultType text /plain
<IfModule mime_module>
     TypesConfig conf /mime .types
     AddType application /x-compress  .Z
     AddType application /x-gzip  .gz .tgz
< /IfModule >
Include conf /extra/httpd-mpm .conf
Include conf /extra/httpd-vhosts .conf
<IfModule ssl_module>
SSLRandomSeed startup  builtin
SSLRandomSeed connect  builtin
< /IfModule >

01)建立目录配置apache基于域名虚拟主机

1
2
3
4
5
6
7
8
9
[root@qbPC var] # mkdir -p /var/html/{www,blog,bbs}
[root@qbPC var] # touch /var/html/{www,blog,bbs}/index.html
[root@qbPC var] # for name in www blog bbs;do echo "http://$name.my.org" > /var/html/$name/index.html;done
[root@qbPC html] # cat bbs/index.html
http: //bbs .my.org
[root@qbPC html] # cat blog/index.html
http: //blog .my.org
[root@qbPC html] # cat www/index.html
http: //www .my.org


02)在http-vhosts.conf里面配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@qbPC conf] # ls
extra  httpd.conf  httpd.conf.ori  magic  mime.types  original
[root@qbPC conf] # cd extra/
[root@qbPC extra] # ls
httpd-autoindex.conf  httpd-info.conf       httpd-mpm.conf                 httpd-userdir.conf
httpd-dav.conf        httpd-languages.conf  httpd-multilang-errordoc.conf  httpd-vhosts.conf
httpd-default.conf    httpd-manual.conf     httpd-ssl.conf                 httpd-vhosts-name.conf
[root@qbPC extra] # cat httpd-vhosts.conf
#
# Virtual Hosts
#
NameVirtualHost *:80
<VirtualHost *:80>
     ServerAdmin 15581737164@163.com
     DocumentRoot  "/var/html/www"
     ServerName www.my.org
     ServerAlias my.org
     ErrorLog  "logs/www-error_log"
     CustomLog  "logs/www-access_log"  common
< /VirtualHost >
  
<VirtualHost *:80>
     ServerAdmin 15581737164@163.com
     DocumentRoot  "/var/html/blog"
     ServerName blog.my.org
     ErrorLog  "logs/blog-error_log"
     CustomLog  "logs/blog-access_log"  common
< /VirtualHost >
  
<VirtualHost *:80>
     ServerAdmin 15581737164@163.com
     DocumentRoot  "/var/html/bbs"
     ServerName bbs.my.org
     ErrorLog  "logs/bbs-error_log"
     CustomLog  "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log"  combined
< /VirtualHost >
[root@qbPC extra] #


03)在主配文件中打开虚拟主机

1
2
Include conf /extra/httpd-mpm .conf
Include conf /extra/httpd-vhosts .conf


04)检查语法和发布

1
2
3
4
5
6
7
8
9
10
11
12
/application/apache/bin/apachectl  -t
/application/apache/bin/apachectl  graceful
 
注:做以上操作的时候可能会有延迟和报错(fully qualified domian name FQDN),解决如下:
[root@qbPC ~] # cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 httpd
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@qbPC ~] #
 
[root@qbPC conf] # vim httpd.conf
     #ServerName www.example.com:80
     ServerName 127.0.0.1:80

 


05)当访问的时候会报403错误,因为没有dns解析,所以可以进行本地解析

 

wKiom1k9-N-yd_lMAACPBQn3YUI628.png-wh_50

wKioL1k9-OChog0jAABanrckjMA047.png-wh_50


06之后还是不能访问,因为没有配置虚拟主机访问站点

解决:在主配文件最后加

1
2
3
4
5
6
<Directory  "/var/html" >
     Options  FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
< /Directory >


07)
到此就可以访问了

wKiom1k9-RDyKuzQAAAntnmACG0868.png-wh_50 

 

08)基于IP的虚机主机、基于不同端口的虚拟主机

配置apache轮询日志

01)安装轮询软件

1
2
3
4
5
tar  zxvf cronolog-1.6.2. tar .gz
cd  cronolog-1.6.2
. /configure
make & make  install
pwd


02)建立日志目录

1
mkdir  -p  /app/logs

03)在子配文件中配置

1
2
3
4
5
6
7
<VirtualHost *:80>
     ServerAdmin 15581737164@163.com
     DocumentRoot  "/var/html/bbs"
     ServerName bbs.my.org
     ErrorLog  "logs/bbs-error_log"
     CustomLog  "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log"  combined
< /VirtualHost >


 

04)检查语法和发布

1
2
/application/apache/bin/apachectl  -t
/application/apache/bin/apachectl  graceful


 

05)此时访问网站就会生成日志

wKioL1k9-U-jmiJiAAAyO4bbUgQ224.png-wh_50 

 

06)其他日志轮询工具rotatelog

07)不记录图片的日志

在主配文件中

<FilesMatch "\.(css|js|gif|jpg|ico|swf)">

    SetEvn IMAG 1

</FilesMatch>

在虚拟主机配置文件http-vhosts.conf的日志配置中

CustomLog "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log" combinedevn=!IMAG

 

08)RS服务器不记录负载均衡健康检查日志

http-vhosts.conf

SetEvnIf Request_URI ^/check\.txt$ dontlog

CustomLog "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log" combinedevn=!dontlog

 

 

09)通过脚本分析日志

统计访问IP

awk '{print $1}' access_bbs_20170608.log |sort |uniq -c |sort -rn -k1|head -10

 

awk '{++S[$1]} END {for (key in S) print S[key],key}' access_bbs_20170608.log | sort -rn -k1|head -10

 

010)安装PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
=================php=====================
  5.3=====>
=============================
. /configure  \
--prefix= /application/php5 .3.27 \
--with-apxs2= /application/apache/bin/apxs  \
--with-mysql= /application/mysql  \
--with-xmlrpc \
--with-openssl \
--with-zlib \
--with-freetype- dir  \
--with-gd \
--with-jpeg- dir  \
--with-png- dir  \
--with-iconv= /usr/local/libiconv  \
-- enable -short-tags \
-- enable -sockets \
-- enable -zend-multibyte \
-- enable -soap \
-- enable -mbstring \
-- enable -static \
-- enable -gd-native-ttf \
--with-curl \
--with-xsl \
-- enable - ftp  \
--with-libxml- dir
  
==========php==========
安装准备
yum  install  zlib libxml libjpeg freetype libpng gd  curl libiconv  zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel -y
tar  zxf libiconv-1.14. tar .gz
cd  libiconv-1.14
. /configure  --prefix= /usr/local/libiconv
make
make  install
cd  ../