您的位置:首页 > 理论基础 > 计算机网络

httpd服务配置(未完待续)

2015-09-19 13:16 441 查看
httpd服务的详细配置

程序环境
主配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
服务脚本:
/etc/rc.d/init.d/httpd
配置文件:/etc/sysconfig/httpd
主程序文件:
/usr/sbin/httpd #默认为prefork,后边介绍
/usr/sbin/httpd.event
/usr/sbin/httpd.worker
日志文件目录:
/var/log/httpd
access_log: 访问日志
error_log:错误日志

模块文件路径:
/usr/lib64/httpd/modules

socket:
http: tcp:80
https: udp: 443

网页文件目录(DocumentRoot);
静态页面: /var/www/html
动态页面(CGI): /var/www/cgi-bin/
默认主页面:index.html

CentOS6.6默认已经安装了httpd的2.2版本。如果没有请用yum安装httpd.
[root@localhost ~]# rpm -qa httpd*
httpd-tools-2.2.15-39.el6.centos.x86_64
httpd-2.2.15-39.el6.centos.x86_64   #默认已经安装httpd2.2


查看一下是否开机启动
[root@localhost ~]# chkconfig --list httpd  #看到服务没有启动
httpd          0:关闭1:关闭2:关闭3:关闭4:关闭5:关闭6:关闭
[root@localhost ~]# chkconfig httpd on  #设置开机自启动
[root@localhost ~]# chkconfig --list httpd#已经启用开机自启动
httpd          0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭


通过ss命令我们查看一下tcp/80端口是否处于监听状态:
[root@localhost ~]# service httpd start
正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[确定]
[root@localhost ~]# ss -tnl | grep 80
LISTEN     0      128                      :::80                      :::*


我们看到80端口以及处于监听状态,说明httpd服务已经启动,我们可以通过浏览器输入服务器ip地址来验证一下:





我们已经看到上面apache HTTP server test 测试页面,说明httpd能正常工作了,ip地址根据自己实际情况来定。

接下来自己定义一个主页来替换当前默认主页,目录查看开始介绍位于/var/www/html/目录下,
新建一个名为index.html的文件,编辑其内容如下:
[root@localhost ~]# cd /var/www/html/ #到默认主页目录下
[root@localhost html]# vim index.html #简单测试编辑如下:






现在我们刷新一下浏览器就会看到已经改为我们自己定义的主页信息:





配置文件的组成:
[root@localhost ~]# grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment  #全局环境配置
### Section 2: 'Main' server configuration # 主服务器
### Section 3: Virtual Hosts #虚拟主机
###全局配置:对主服务器和虚拟主机都有效,且有些功能为服务器自身属性
###主服务器:主站属性,用于仅提供一个站点时
###虚拟主机:虚拟主机属性,用于多个站点时使用

注: 介意主服务器和虚拟机主机不要同时使用

常用配置:

1.修改监听的IP和Port
格式为: Listen [ip:]PORT

注:
#ip可以省略,表示监听主机上的所有地址
#listen可出现多次,用于指定多个端口或套接字

进入配置文件找到Listen行进行配置:

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf






然后使用service restart httpd 重新加载

注:修改完端口需要restart重启服务器,实际过程中服务器不能关闭,所以要需提前规划设定。

查看8080端口已经出于监听状态:





现在通过浏览器用8080端口访问已经没有问题:





2.配置所选用的MPM的属性:

MPM:(Multipath Process Module)多道处理模块,共有三个工作方式:

prefork: 一个主进程产生多个子进程,一个子进程响应一个请求
worker:一个进程生成多个线程,一个线程响应一个请求
event: 基于事件驱动

注:httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个;rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持;确认方法:
# ps aux | grep httpd

默认为/usr/sbin/httpd, 使用为:prefork

通过httpd命令即可查看MPM模块信息:
[root@localhost ~]# httpd -help
Usage: httpd [-D name] [-d directory] [-f file]
[-C "directive"] [-c "directive"]
[-k start|restart|graceful|graceful-stop|stop]
[-v] [-V] [-h] [-l] [-L] [-t] [-S]
Options:
-D name            : define a name for use in <IfDefine name> directives
-d directory       : specify an alternate initial ServerRoot
-f file            : specify an alternate ServerConfigFile
-C "directive"     : process directive before reading config files
-c "directive"     : process directive after reading config files
-e level           : show startup errors of level (see LogLevel)
-E file            : log startup errors to file
-v                 : show version number
-V                 : show compile settings
-h                 : list available command line options (this page)
-l                 : list compiled in modules
-L                 : list available configuration directives
-t -D DUMP_VHOSTS  : show parsed settings (currently only vhost settings)
-S                 : a synonym for -t -D DUMP_VHOSTS
-t -D DUMP_MODULES : show all loaded modules
-M                 : a synonym for -t -D DUMP_MODULES
-t                 : run syntax check for config files
[root@localhost ~]# httpd -l #查看静态编译的模块
Compiled in modules:
core.c #核心模块
prefork.c   #默认为prefork模块
http_core.c  #http功能核心模块
mod_so.c  #支持模块动态装卸载接口
[root@localhost ~]# httpd -M#查看静态编译及动态装载的模块
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
authn_anon_module (shared)
authn_dbm_module (shared)
authn_default_module (shared)
authz_host_module (shared)
authz_user_module (shared)
authz_owner_module (shared)
authz_groupfile_module (shared)
authz_dbm_module (shared)
authz_default_module (shared)


更换使用的http程序:

编辑脚本程序/etc/sysconfig/httpd即可更换:
[root@localhost ~]# vim /etc/sysconfig/httpd




重新启动服务: service httpd restart

ps 查看:
[root@localhost ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd.worker: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[确定]
[root@localhost ~]# ps aux | grep httpd
root  7310  0.0  0.8 184528  4056 ?   Ss   01:10   0:00 /usr/sbin/httpd.worker
apache 7313  0.0  0.6 528788  3292 ?   Sl   01:10   0:00 /usr/sbin/httpd.worker
apache 7314  0.0  0.6 528788  3296 ?   Sl   01:10   0:00 /usr/sbin/httpd.worker
apache 7315  0.0  0.6 528788  3292 ?   Sl   01:10   0:00 /usr/sbin/httpd.worker
apache 7316  0.0  0.6 528788  3300 ?   Sl   01:10   0:00 /usr/sbin/httpd.worker
root  7426  0.0  0.1 103256   840 pts/0  S+   01:10   0:00 grep httpd
我们看到后边就已经切换为worker模块,event模块同样操作,这里就不演示了,这就是更换httpd模块的方式,只需要直接编辑/etc/sysconfig/httpd即可

配置http的几种模型:

Prefork:

http服务默认就是以Prefork模式启动,无须更改启动模式,下面来演示如何更改其模型参数
编辑主配置文件:vim/etc/httpd/conf/httpd.onf找到与模块相关的参数:IfModule为指令,意为判断模块是否存在,如果存在那么参数则生效,反之如果不存在此模块,参数将不会生效
<IfModule prefork.c>
StartServers       8    #启动服务时启动多少个进程
MinSpareServers    5    #最少空闲进程数
MaxSpareServers   20    #最大空闲进程数
ServerLimit      256    #限定最多允许并发进来的活动用户连接个数
MaxClients       256    #最多允许启动多少个进程
MaxRequestsPerChild  4000 #一个服务器进程最多响应多少个请求
</IfModule>
Worker:

当第一个用户请求到达的时候线程发现需要复制文件,通过内核将文件复制给其进程,所以第二个用户请求同一个文
件,对于第二个线程来说这个文件已存在,因为线程是共享同一个进程空间,所以速度得到提升;
缺点:共享意味着争用,共享资源被称作临界资源,线程共享进程空间就可能产生资源争用,并不是全状态并行。所
以一个进程里不能启用太多线程;可启用多个进程然后由每个进程再启用多个线程,但无论如何每个线程也都是一个
独立的执行实体(线程要能运行必须给其cpu资源)。
<IfModule worker.c>
StartServers         4  #启动服务启动多少线程
MaxClients         300  #最多允许启动多少个线程
MinSpareThreads     25  #最小的空闲线程数
MaxSpareThreads     75  #最大的空闲线程数
ThreadsPerChild     25  #允许每个进程最多生成多少个线程
MaxRequestsPerChild  0  #设置一个独立的子进程将能处理的请求数量
</IfModule>


event: 由于现在不是很稳定,用的很少,这里不在做介绍。

网站衡量指标:

PV: Page View
#即页面浏览量,或点击量;用户每1次对网站中的每个网页访问均被记录1次。用户对同一页面的多次访问,访问量累计。
UV: User View
#根据用户数量计数,也叫独立IP量,即有多少个IP访问网站

配置服务器支持keep-alived:
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive (On|Off)#启用之后支持一次连接可以发多个请求(繁忙时建议Off)
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100 # 一次长连接之内最多允许50个请求
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15  #15秒之后超时断开连接


配置加载模块:
#LoadModule指令  模块名称   模块所在的路径(相对于ServerRoot的路径)
# LoadModule foo_module modules/mod_foo.so
#
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_alias_module modules/mod_authn_alias.so
LoadModule authn_anon_module modules/mod_authn_anon.so
LoadModule authn_dbm_module modules/mod_authn_dbm.so


主服务器配置:

配置站点根目录(网页存放目录)
DocumentRoot:#指向的路径为URL路径的起始位置
#DOcumentRoot " /var/www/html " ;
<Directory ""> </Direcotry>#文件系统路径
<File ""> </File>
<FileMatch ""> </FileMatch>
<Location ""> </Location>   # URL路径
#两种路径定义方法
/var/www/html/images/logo.jpg => <Directory "/var/www/html" ></Directory> http://www.magedu.com/images/logo.jpg => <Location "/"></Location> #这里的“/”==/var/www/html

<Directory "FS_PATH">

Options
Indexes: #是否允许索引页面文件(不安全,建议关闭);
FollowSynLinks: #是否跟随软链接文件(不安全,建议关闭);
SymLinksifOwnerMatch:#相对安全的跟随软链接指令(如果软链接文件的属主与网页访问用户匹配则允许)
ExecCGI:#是否允许执行CGI脚本;
All
None
</Directory>

Indexes: 索引;
FollowSymlinks:允许跟踪符号链接文件;

删除或者备份欢迎页面和默认主页面文件,重新加载:
[root@localhost conf]# cd /etc/httpd/
[root@localhost httpd]# ls
conf  conf.d  logs  modules  run
[root@localhost httpd]# cd conf.d/
[root@localhost conf.d]# ls
mod_dnssd.conf  README  welcome.conf
[root@localhost conf.d]# mv welcome.conf welcome.conf.bak
[root@localhost conf.d]# cd /var/www/html/
[root@localhost html]# mv index.html index.html.bak
[root@localhost html]# service httpd reload






网页以索引的方式呈现

本文出自 “开源盛世” 博客,请务必保留此出处http://zhaohongfei.blog.51cto.com/6258685/1696267
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: