Apache使用简介

Apache使用简介

1.全局配置配置信

1) 配置持久连接

KeepAlive <On|Off>             #是否开启持久连接功能

MaxKeepAliveRequest 100     #一次持久连接最大的请求个数

KeepAliveTimeout 15            #持久连接的超时时间

2) 配置监听IP和端口

Listen [IP:]Port                  #设置监听的IP地址以及端口,本选项可以指定多个,以支持监听多个IP及端口

3) 模块动态装卸载

LoadModule ModuleName /path/to/module

#可以在终端使用如下命令查看已装载的模块

 httpd -t -D DUMP_MODULES

Loaded Modules:

 core_module (static)

 mpm_prefork_module (static)

 ......

 dnssd_module (shared)

 php5_module (shared)

 ssl_module (shared)

Syntax OK

4) 指定站点根目录

DocumentRoot /path/to/documentroot #定义网页文件所在的目录

5) 定义默认主页信息

DirectoryIndex index.html index.php .... #各参数之间以空格分割

6) 路径别名

Alias url/ /path/ #注意,末尾的“/”要保持一致,有则都有,无则均无

7) 默认字符集设置

AddDefaultCharset 字符集

#常用中文字符集有:GB2310,GB18030,GBK

#通用字符集:UTF-8,UTF-16

8) 配置支持CGI脚本

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" #将CGI脚本执行路径定义到指定路径

 <Directory "/var/www/cgi-bin"> #设置cgi脚本路径的访问控制,下方对其中参数有详细说明

    AllowOverride None

    Options None

    Order allow,deny

    Allow from all

</Directory>

2.访问控制

a) 基于IP地址的访问控制

Order allow,deny #默认拒绝所有

Order deny,allow #默认允许所有

Allow|Deny from {all|ipaddr|NetworkAddress}

其中Networkaddress格式可以为以下几种

172.16

172.16.0.0

172.16.0.0/16

172.16.0.0 255.255.0.0

b) 基于用户认证的访问控制

#基于用户的访问认认证有basic和digest两种

#认证文件有文本文件:.htpasswd,SQL数据库,DBM:数据库引擎以及ldap

#下面我们以基于文本文件的basic认证来配置

#=========================================================

#1.确保基本认证模块已经加载

LoadModule auth_basic_module modules/mod_auth_basic.so

#2.创建认证文件,添加用户

#htpasswd FILE USERNAME

#-c:文件不存在时则创建文件

#-m:使用MD5加密密码

#-D:删除用户

[root@princessd ~]# htpasswd -c -m /etc/httpd/.passwd test1

New password:

Re-type new password:

Adding password for user test1

[root@princessd ~]# htpasswd  -m /etc/httpd/.passwd test2

New password:

Re-type new password:

Adding password for user test2

[root@princessd ~]# cat /etc/httpd/.passwd

test1:$apr1$dgaArWEk$U04MnXH2HBRT/P5fUoj2P0

test2:$apr1$N4uhZ492$1L70450o982fZPQmbd594/

#3.为目录配置用户认证

<Directory /var/www/html/auth>

    options none

    AllowOverride AuthConfig

    AuthType Basic  #定义认证类型

    AuthName "Please inout your crendential" #定义认证提示信息

    AuthUserFile /etc/httpd/.passwd  #定义认证文件

    Require valid-user  #定义可访问用户,可以为vbalid-user表示文件中所有用户,也可使用user USERNAME 定义指定用户

</Directory>

3.日志功能

#错误日志定义

ErrorLog “path/to/error/logfile”

#定义错误日志级别

LogLevel {debug|info|notice|warn|error|crit|alert|emerg}

#自定义日志

CustomLog “/path/to/customlog/file” 日志格式名

#自定义日志格式定义

LogFormat “格式” 格式名 

#格式中常用的宏

#%h:客户端地址

#%l:远程登录名

#%u:远程认证名称,没有认证为"-"

#%t:收到请求时间

#%r:请求报文的起始行

#%>s:响应状态吗

#%b:响应报文长度bits

#%{HEADER_NAME}i:记录指定首部对应的值

4.MPM参数配置

<IfModule prefork.c> #如果启用prefork模块,下列参数生效

StartServers 8#表示启动服务器时启动多少线程

MinSpareServers 5 #定义最小空闲进程数

MaxSpareServers 20 #定义最大空闲进程数

ServerLimit 256 #最大进程数

MaxClients 256 #最大客户端个数

MaxRequestsPerChild 4000 #每个进程允许在其生命周期处理多少请求

</IfModule>

#==========================

<IfModule worker.c>

StartServers 4 #表示启动服务器时启动多少线程

MaxClients 300 #最多客户端个数

MinSpareThreads 25 #最小空闲进程数

MaxSpareThreads 75 #最大空闲进程数

ThreadsPerChild 25 #每个进程生成多少线程

MaxRequestsPerChild 0 #每个进程允许在其生命收起处理多少请求

</IfModule>

5.虚拟主机

在httpd.conf文件中去掉下面行的注释符号

Include /etc/httpd24/extra/httpd-vhosts.conf

在extra/httpd-vhosts.conf文件中配置虚拟主机,以下的配置全在httpd-vhosts.conf文件中.

1) 搭建基于IP的虚拟主机

<VirtualHost 172.16.21.250:80>

    DocumentRoot "/web/172"

    ServerName www.linuxidc.com

</VirtualHost>

<Directory /web/172>

  AllowOverride None

    Options None

    Order allow,deny

    Allow from all

</Directory>

<VirtualHost 192.168.1.123:80>

    DocumentRoot "/web/192"

    ServerName www.linuxidc.com

</VirtualHost>

<Directory /web/192>

  AllowOverride None

    Options None

    Order allow,deny

    Allow from all

</Directory>

[root@princessd extra]mkdir -p /web/{192,172}

[root@princessd extra]echo "Access from 192" >/web192/index.html

[root@princessd extra]echo "Access from 172" >/web/172/index.html

#重启服务

[root@princessd extra]# service httpd24 restart

#验证

[root@princessd extra]# curl 192.168.1.123

Access from 192

[root@princessd extra]# curl 172.16.21.250

Access from 172

2) 搭建基于端口的虚拟主机

[root@princessd extra]# cat httpd-vhosts.conf

Listen 8080

<VirtualHost 172.16.21.250:80>

    DocumentRoot "/web/80"

    ServerName www.linuxidc.com

</VirtualHost>

<Directory /web/80>

    Require all granted

</Directory>

<VirtualHost 172.16.21.250:8080>

    DocumentRoot "/web/8080"

    ServerName www.linuxidc.com

</VirtualHost>

<Directory /web/8080>

    Require all granted

</Directory>

[root@princessd extra]# mkdir -v /web/{80,8080}

[root@princessd extra]# echo "Access from port8080" >/web/8080/index.html

[root@princessd extra]# echo "Access from port80" >/web/80/index.html

#重启服务

[root@princessd extra]# service httpd24 restart

#验证

[root@princessd extra]# curl 172.16.21.250:8080

Access from port8080

[root@princessd extra]# curl 172.16.21.250:80

Access from port80

3)搭建基于域名的虚拟主机

#在httpd2.2版本定义基于域名的虚拟主机时,需要加入如下现象,在2.4则不再需要,直接定义即可

#NameVirtualHost IP:PORT

[root@princessd extra]# vim httpd-vhosts.conf

<VirtualHost 172.16.21.250:80>

    DocumentRoot "/web/www1"

    ServerName www1.linuxidc.com

</VirtualHost>

<Directory /web/www1>

    Require all granted

</Directory>

<VirtualHost 172.16.21.250:80>

    DocumentRoot "/web/www2"

    ServerName www2.linuxidc.com

</VirtualHost>

<Directory /web/www2>

    Require all granted

</Directory>

[root@princessd extra]# mkdir -v /web/{www1,www2}

[root@princessd extra]# echo "www1.linuxidc.com" >/web/www1/index.html

[root@princessd extra]# echo "www2.linuxidc.com" >/web/www2/index.html

#由于没有DNS服务器,方便起见,在/etc/host中添加以下两行

172.16.21.250  www1.linuxidc.com

172.16.21.250  www2.linuxidc.com

#验证

[root@princessd extra]# curl www1.linuxidc.com

www1.linuxidc.com

[root@princessd extra]# curl www2.linuxidc.com

www2.linuxidc.com

6.Apache内嵌处理器server-status使用

[root@princessd conf.d]# cat serverstatus.conf

<Location /server-status>

options none

SetHandler server-status

Order allow,deny

allow from 192.168.1

</Location>

7.配置实用https协议实现安全连接

#安装mod_ssl

[root@princessd conf.d]# yum install mod_ssl

#为httpd生成私钥并生成证书

mkdir /etc/httpd/ssl

cd httpd/ssl

umask 077

openssl genrsa -out /etc/httpd/httpd.key 1024

openssl req -new -key httpd.key -out httpd.csr

#在CA证书服务器上为其签署证书并传回

openssl ca -in httpd.csr -out httpd.crt -days 1000

#配置ssl认证

SSLCertificateFile /etc/httpd/ssl/httpd.crt #指定证书位置

SSLCertificateKeyFile /etc/httpd/ssl/httpd.key #指定私钥位置

8.httpd自带工具使用

httpd

-t:测试

-l:列出静态模块

-D DUMP_MODULES:列出DSO模块

-M,相当于-t -DDUMP_MODUKLES

-D DUMP_VHOSTS:列出所有虚拟主机

htpasswd

-c:创建文件,第一次添加用户使用

-m:用MD5加密

-d:删除用户

apachectl: httpd服务控制工具

start

stop

restart

ab: apache基准性能测试工具

ab [options] URL

-c #:并发数

-n #:总请求数

-n的值一定大于-c的值

-c #:指定并发数

-n #:指定总的请求数

posted @ 2014-10-17 00:15  princessd8251  阅读(624)  评论(0编辑  收藏  举报