httpd2.2安装
yum安装
# yum install httpd-2.2.15-26.el6.centos.x86_64
配置文档:编辑之前复制一份
vim /etc/httpd/conf/httpd.conf
1、ServerRoot "/etc/httpd" 服务器运行目录
配置文件、错误文档、日志的保持目录
2、端口
Listen [IP] PORT
Listen 80
Listen 8080
这样,http://192.168.1.31:8080 就可以访问主页面了
3、是否开启MPM模块,当MPM模块存在的情况下,就开启
prefork: 一个进程响应一个请求;
<IfModule prefork.c>
StartServers 8 最少启动空闲子进程数
MinSpareServers 5 最小空闲进程
MaxSpareServers 20 最大空闲进程
ServerLimit 256 服务器并发最大连接数
MaxClients 256 最多客户端链接数,大于ServerLimit,否则无意义
MaxRequestsPerChild 4000 服务器进程最大处理数,到了这个数值就重新连接
</IfModule>
worker:一个进程生成多个线程,一个线程响应一个请求;
启用worker
vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker
# ps aux | grep httpd 查看
root 9055 0.0 0.7 173692 3900 ? Ss 18:22 0:00 /usr/sbin/httpd.worker
apache 9057 0.0 0.6 517952 3240 ? Sl 18:22 0:00 /usr/sbin/httpd.worker
apache 9059 0.0 0.6 517952 3232 ? Sl 18:22 0:00 /usr/sbin/httpd.worker
apache 9060 0.0 0.6 517952 3240 ? Sl 18:22 0:00 /usr/sbin/httpd.worker
root 9233 0.0 0.1 103244 832 pts/0 S+ 18:22 0:00 grep httpd
<IfModule worker.c>
StartServers 4 启动线程数量
MaxClients 300 最大链接客户端数量
MinSpareThreads 25 最少空闲线程数
MaxSpareThreads 75
ThreadsPerChild 25 每个进程允许生成线程数
MaxRequestsPerChild 0
</IfModule>
httpd -l 显示使用模块有哪些
-h 显示帮助
-D DUMP_MODULES 显示所有加载模块
4、配置服务器支持keep-alived
KeepAlive {On|Off}
KeepAliveTimeout 2 最大超时时间
MaxKeepAliveRequests 50 每次请求资源数量
5、配置加载的模块
LoadModule foo_module modules/mod_foo.so
各种模块有很多
6、Include conf.d/*.conf 包括其他
7、ServerAdmin root@localhost 网站管理员的邮箱
8、ServerName www.magedu.com:80 如果不启用会做反向解析,很耗时间
9、根目录
DocumentRoot "/var/www/html"
<Directory "FS_PATH"> 这个路径对于上面的路径而言只有当为其子路径才有效。
</Directory>
10、配置页面文件访问属性
<Directory "FS_PATH">
Options
Indexes: 是否允许索引页面文件,建议关闭;
FollowSynLinks: 是否跟随软链接文件,当有软链接的时候,可以显示其本来的文件。不安全;
SymLinksifOwnerMatch: 比上面一个选项安全
ExecCGI:是否允许执行CGI脚本;
All
None
</Directory>
11、访问控制法则:allow和deny哪个在前面哪个先生效,如果不指定前一个的范围,默认就不生效,然后后一个生效。如果前一个指定范围,all、IP,后一个就继承默认法则。
Order allow,deny
allow 0.0.0.0 仅允许本地主机
12、userdir
让每个都拥有个人站点:http://HOST/~username/ UserDir public_html 开启配置选项
访问这个目录:http://192.168.1.31/~tom/
[root@www public_html]# pwd
/home/tom/public_html
[root@www public_html]# cat index.html
user:tom
# setfacl -m u:apache:x /home 修改这个文件的目录的apache访问权限。
13、定义默认主页面:
DirectoryIndex index.php index.jsp index.html
14、模式匹配
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
15、配置日志功能
日志有两类:访问日志(格式需自定义)、错误日志
错误日志:
ErrorLog "/path/to/error_log_file"
访问日志:
CustomLog /PATH/TO/CUSTEOM_LOG_FILE LOGFORMAT, 例如:
CustomLog logs/access_log combined
LogFormat
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
environment variable FOOBAR | |||||||
%f |
Filename | ||||||
%h |
Remote host | ||||||
%H |
The request protocol | ||||||
%{Foobar}i |
The contents of Foobar: header line(s) in the request sent to the server. Changes made by other modules (e.g. mod_headers ) affect this. |
||||||
%k |
Number of keepalive requests handled on this connection. Interesting if KeepAlive is being used, so that, for example, a '1' means the first keepalive request after the initial one, '2' the second, etc...; otherwise this is always 0 (indicating the initial request). |
||||||
%l |
Remote logname (from identd, if supplied). This will return a dash unless mod_ident is present and IdentityCheck is set On . |
||||||
%m |
The request method | ||||||
%{Foobar}n |
The contents of note Foobar from another module. | ||||||
%{Foobar}o |
The contents of Foobar: header line(s) in the reply. |
||||||
%p |
The canonical port of the server serving the request | ||||||
%{format}p |
The canonical port of the server serving the request or the server's actual port or the client's actual port. Valid formats are canonical , local , or remote . |
||||||
%P |
The process ID of the child that serviced the request. | ||||||
%{format}P |
The process ID or thread id of the child that serviced the request. Valid formats are pid , tid , and hextid . hextid requires APR 1.2.0 or higher. |
||||||
%q |
The query string (prepended with a ? if a query string exists, otherwise an empty string) |
||||||
%r |
First line of request | ||||||
%R |
The handler generating the response (if any). | ||||||
%s |
Status. For requests that got internally redirected, this is the status of the *original* request --- %>s for the last. |
||||||
%t |
Time the request was received (standard english format) | ||||||
%{format}t |
The time, in the form given by format, which should be in strftime(3) format. (potentially localized) |
||||||
%T |
The time taken to serve the request, in seconds. | ||||||
%u |
Remote user (from auth; may be bogus if return status (%s ) is 401) |
||||||
%U |
The URL path requested, not including any query string. | ||||||
%v |
The canonical ServerName of the server serving the request. |
||||||
%V |
The server name according to the UseCanonicalName setting. |
||||||
%X |
Connection status when response is completed:
(This directive was |
||||||
%I |
Bytes received, including request and headers, cannot be zero. You need to enable mod_logio to use this. |
||||||
%O |
Bytes sent, including headers, cannot be zero. You need to enable mod_logio to use this.来源: <http://192.168.1.31/manual/mod/mod_log_config.html#formats> |
16、路径别名
Alias 访问路径 转移的路径 //定义的选项
17、脚本路径别名:
CGI:协议
ScriptAlias /PATH/ /PATH/TO/SOMFEDIR/
例如:ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 然后在/var/www/cgi-bin/编写一个脚本,用http://IP/cgi-bin/Script_Name来运行这个脚本
CGI测试脚本示例:
#!/bin/bash
#
cat << EOF
Content-Type: text/html
<pre>
The hostname is: `/bin/hostname`.
The time is: `date`.
</pre>
EOF
# end of cgi script file
18、基于用户访问控制
DocumentRoot "/var/www/"
<Directory "/var/www/download">
Options Indexes
AllowOverride AuthConfig
AuthName "Only for employer" 名字随便写
AuthType Basic
AuthUserFile /etc/httpd/conf/.htpass用户的帐号存放文件
Require valid-user 提供用户和密码,这里不写用户
</Directory>
(1) 建立用户帐号文件
htpasswd -c -m /path/to/password_file USERNAME
19、虚拟主机
虚拟主机类型:
基于端口的虚拟主机:
基于IP的虚拟主机:
基于主机名的虚拟主机:
www.a.com 192.168.1.31
www.b.org 192.168.1.31
虚拟主机和主服务器不能同时使用:关闭主服务器,注释主服务器的DocumentRoot即可;
每个虚拟主机的定义:
<VirtualHost IP:PORT>
ServerName
DocumentRoot ""
</VirutalHost>
(1) 基于端口的虚拟主机
192.168.1.31:80 , /web/host1/
192.168.1.31:8080, /web/host2/
(2)基于IP的虚拟主机
192.168.1.31:80 , /web/host1/
192.168.1.131:80, /web/host2/
(3)基于主机名的虚拟主机
httpd-2.2: NameVirtualHost
192.168.1.31:80 , /web/host1/, www.a.com
192.168.1.31:80 , /web/host2/, www.b.org
测试,本机访问网页
elinks http://www.a.com
elinks -dump http://www.a.com
Windows下 C:\Windows\System32\drivers\etc\hosts
例如:
<VirtualHost *:80>
ServerName www.a.com
DocumentRoot "/web/host1"
#<Directory>
# Options Indexes
#</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerName www.b.com
DocumentRoot "/web/host2"
#<Directory>
# Options Indexes
#</Directory>
</VirtualHost>
<VirtualHost 192.168.1.31:80>
ServerName www.c.com
DocumentRoot "/web/host3"
#<Directory>
# Options Indexes
#</Directory>
</VirtualHost>
<VirtualHost 192.168.1.131:80>
ServerName www.c.com
DocumentRoot "/web/host4"
#<Directory>
# Options Indexes
#</Directory>
</VirtualHost>
20、httpd status
LoadModule status_module modules/mod_status.so
ExtendedStatus On
<Location /status>
SetHandler server-status
AuthName "Status"
AuthType Basic
AuthUserFile /etc/httpd/conf/.statuspass
Require valid-user
</Location>
21、https: SSL/TLS会话的建立仅能基于IP地址进行
443/tcp:
生成证书
# yum install mod_ssl
# vim /etc/httpd/conf.d/ssl.conf
LoadModule ssl_module modules/mod_ssl.so
<VirtualHost 192.168.1.31:443>
DocumentRoot "/web/host3"
ServerName www.magedu.com
SSLEngine on
SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key
测试命令:
#openssl s_client -connect www.magedu.com:443 -CAfile /etc/pki/CA/cacert.pem