Linux Apache web服务器 配置详细教程
原创ExpiresDefault A9030400
1 星期
ExpiresDefault A604800
3 小时
ExpiresDefault M10800
htm,html,txt类的文件缓存一个小时
header set cache-control "max-age=3600"
css, js, swf类的文件缓存一个星期
header set cache-control "max-age=604800"
Axxxx - Access seconds ,表示访问之后经过多少秒
Mxxxx - Modifyed seconds ,表示修改之后经过多少秒(推荐)
ExpiresActive On
ExpiresDefault A3600 #表示一小时后更新
ExpiresByType image/x-icon A86400#表示1天后更新
#脚本文件和css样式,我们最好使用 Modification
ExpiresByType application/javascript M604800 #表示修改完之后,更新之后的1周以后更新
ExpiresByType text/css M2592000
#表示修改后如果没有再次修改,那么经过一个星期才允许更新缓存
ExpiresByType image/gif M604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg M604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A900
3.11 apache 防盗链
资源盗链:是不法网站未经过同意通过链接合法网站的图片,视频等资源在自身的网站上展示来达到宣传自已的目的,侵害了合法网站的权益,同时也占用了合法网站的流量和资源
防盗链手段:利用referer和rewirte实现apache防盗链调用
防盗链环境说明:
[*]搭建A,B二个网站,A中放置一个正常图片mm.jpg,同时放置一张版本警告图片nolink.jpg,
[*]B网站目录下建立一个html,添加图片链接
[*]正常情况下,www.b.com打开html网页的时候,可以正常显示从a盗过来的mm.jpg图片
[*]在A的httpd.conf的配置文件中,添加如下防盗链代码:重启服务,再测试html打开是否正常
防盗链代码:
RewrtieEngine on
RewriteCond %{HTTP_REFERER} !^www.a.com/.$
RewriteCond %{HTTP_REFERER} !^www.a.com$
RewriteCond %{HTTP_REFERER} !^www.c.com/.$
RewriteCond %{HTTP_REFERER} !^www.c.com$
RewriteRule .*.(gif|jpg|swf)$ http://www.a.com/img/nolink.jpg
3.12 Apache 常用目录学习
/usr/local/apache/
├── bin
│ ├── ab
│ ├── apachectl
│ ├── apxs
│ ├── checkgid
│ ├── dbmmanage
│ ├── envvars
│ ├── envvars-std
│ ├── htcacheclean
│ ├── htdbm
│ ├── htdigest
│ ├── htpasswd
│ ├── httpd
│ ├── httxt2dbm
│ ├── logresolve
│ └── rotatelogs
├── conf
│ ├── extra
│ │ ├── httpd-autoindex.conf
│ │ ├── httpd-dav.conf
│ │ ├── httpd-default.conf
│ │ ├── httpd-info.conf
│ │ ├── httpd-languages.conf
│ │ ├── httpd-manual.conf
│ │ ├── httpd-mpm.conf
│ │ ├── httpd-multilang-errordoc.conf
│ │ ├── httpd-ssl.conf
│ │ ├── httpd-userdir.conf
│ │ └── httpd-vhosts.conf
│ ├── httpd.conf
├── htdocs
│ └── index.html
/usr/local/apache/conf/extra/
├── httpd-autoindex.conf
├── httpd-dav.conf
├── httpd-default.conf
├── httpd-info.conf
├── httpd-languages.conf
├── httpd-manual.conf
├── httpd-mpm.conf
├── httpd-multilang-errordoc.conf
├── httpd-ssl.conf
├── httpd-userdir.conf
└── httpd-vhosts.conf
1)|- /etc/httpd/conf/httpd.conf(Apache的主配文件)
2)|- /etc/httpd/conf.d/*.conf((include文件)
此目录下放置着.conf的配置文件,这里面的文件在httpd服务运行时被全部加载,所以在维护或修改参数时,为了不修改原配置文件,可以自已定义一个独立的conf文件,方便管理。
include conf.d/welcome.cnf # 代码格式
3)|- /usr/lib64/httpd/modules/
|- /etc/httpd/modules/
Apache支持很多的外挂模块,例如PHP以及SSL都是Apache外挂的一种。所有你想要使用的模块文件默认是放置在这个目录当中的。
4)|- /var/www/html/((网站根目录)
5)|- /var/www/error/
当因为服务器设置错误,或是浏览器要求的数据错误时,在浏览器上出现的错误信息就以这个目录的默认信息为主
|- /var/www/icons/
这个目录提供Apache默认给予的一些小图示,可以随意使用。
|- /var/www/cgi-bin/
默认给一些可执行的CGI(网页程序)程序放置的目录。
6)|- /var/log/httpd/
默认的Apache日志文件都放在这里,对于流量比较大的网站来说,这个目录要格外注意,这里的数据文件可能会非常大。
7)|- /usr/sbin/apachectl
这个就是Apache的主要执行文件,这个执行文件其实就是一个Shell Script而已,他可以主动地侦测系统上面的一些设置值,好让你启动Apache时更简单一些。
8)|- /usr/sbin/httpd
这个是主要的Apache二进制执行文件。
9)|- /usr/bin/htpasswd((Apache密码保护)
在当你想要登入某些网页时你需要输入帐号与密码,那Apache本身就提供一个最基本的密码保护方式,该密码的产生就是通过这个命令来实现的。
3.13 Apache 配置文件权限操作
3.6.1 参考:京峰Linux高级运维视频资料(2016版)
3.14 正确设置站点目录文件及目录权限
为了保证网站不遭受×××***上传及修改文件
安全的权限
[*]站点所有目录的用户和组都应该为root
[*]站点所有目录权限是默认的755不能往目录放文件
[*]站点所有文件权限默认是644 不能改文件
注意:网站服务的用户不能用root
此权限主要是针对nginx进程用户的访问权限,因为×××主要是用此用户进行×××的
但是以上设置会对有些网站需要上传文件而没有取限,如何解决呢?
3.15 Apache 管理
Httpd –V # 查看系统本版等详细信息,也包括apache工作模式
Httpd -l # 查看工作模式
/usr/local/apache/bin/apachectl –M# 查询当前加载的模块
curl -x192.168.2.135:80 www.a.com -I
3.16 apache的web认证
[*]修改配置文件httpd.conf
Options Indexes FollowSymLinks
allowoverride authconfig
order allow,deny
allow from al
[*] 在根目录下添加.htaccess文件
vim /var/www/html/.htaccess
AuthName "auth"
AuthType basic
AuthUserFile /etc/httpd/conf/htpasswd
require valid-user
[*] 使用htpasswd配置用户名和密码
htpasswd -bc /etc/httpd/conf/htpasswd user1 1234
[*]重启apache apachectl -k graceful
3.17 Apaceh ab压力测试
ab -n 100 -c 10 http://test.com/
ab常用参数的介绍:
-n:总共的请求执行数,缺省是1;
-c: 并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件
-w: 以HTML表的格式输出结果
# ab -n 3000 -c 10 https://121.42.183.93/index.php
This is ApacheBench, Version 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 121.42.183.93 (be patient)
Server Software:
Server Hostname: 121.42.183.93
Server Port: 443
SSL/TLS Protocol: TLSv1/SSLv3,RC4-MD5,2048,112
Document Path: /index.php
Document Length: 274 bytes
Concurrency Level: 10 # 并发请求
Time taken for tests: 471.724 seconds # 整个测试持续时间
Complete requests: 259 # 完成请求数
Failed requests: 0 # 失败请求数
Write errors: 0
Non-2xx responses: 269
Total transferred: 123471 bytes # 整个场景中的网络传输量
HTML transferred: 73706 bytes # 整个场景中HTML内容传输量
Requests per second: 0.55 [#/sec] (mean) # 吞吐率
Time per request: 18213.293 (mean) # 用户平均请求等待时间(mean)平均值
Time per request: 1821.329 (mean, across all concurrent requests) # 服务器平均请求处理时间
Transfer rate: 0.26 received # 平均每秒网络上的流量可以判断是否存在延时
Connection Times (ms) 网络消耗时间
minmean[+/-sd] median max
Connect: 130212 991.5 145 16070
Processing: 17368 1737415.717373 17622
Waiting: 41 45 2.8 46 53
Total: 17499 17586 991.317520 33438
Percentage of the requests served within a certain time (ms) 百分比处理时间(毫秒)
50%17520 处理到50%时所花费的时间
66%17524
75%17526
80%17528
90%17533
95%17539
98%17556
99%18267
100%33438 (longest request)
3.18 Curl命令
见nginx章节
3.19 apache 常见故障
错误日志
tail -fn 100 /var/log/httpd/error_log 故障排错
3.19.1 提示configure: error: APR not found .Please read the documentation
./configure --prefix……检查编辑环境时出现:
checking for APR... no
configure: error: APR not found .Please read the documentation
解决办法:
[*]事先在编译apache时需要安装aprarp-utilpcre三个组件
[*]在编译的时候,指定aprapr-utilpcre路径
3.19.2 提示Address already in use: make_sock: could not bind to address [::]:80
# /usr/local/apache/bin/apachectl -k start
(98)Address already in use: make_sock: could not bind to address [::]:80
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
解决方法:有服务占用此端口,用netstat-ntupl找到程序停止它
3.19.3 提示apache/bin/apxs:/usr/local/perl: bad interpreter: No such file or directory
apache/bin/apxs:/usr/local/perl: bad interpreter: No such file or directory
configure: error: no - APXSrefers to an old version of Apache
./confiure报错:这是因为apsx文件没有指定perl执行程序位置
解决:# vi /usr/local/apache/bin/apxs
#!/replace/with/path/to/perl/interpreter–w#将第一行修改为#!/usr/bin/perl –w即可
#添加环境变量
# echo "PATH=$PATH/:/usr/local/subversion/bin" >> /etc/profile
# source /etc/profile
# svnserve –version#显示版本信息表示正常
3.19.4 提示You don't have permission to access / on this server.
修改根目录权限
AllowOverride none
Require all granted(denied)
3.19.5 httpd: Could not> (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
原因:找不到完整的FQDN域名
解决方法: 修改httpd.conf配置
#ServerName 127.0.0.1:80
页:
[1]