码迷,mamicode.com
首页 > Web开发 > 详细

CentOS6中http服务搭建以及部分配置(1)

时间:2017-10-24 01:32:33      阅读:3744      评论:0      收藏:0      [点我收藏+]

标签:服务   http   centos6   

开心,博主收获到了第一个评论,谢谢懒小米的认真阅读,以后会继续努力

近日博主学习了有关http服务的搭建,此次来介绍http服务的搭建以及部分相关配置

实验环境:CentOS6

CentOS6中默认http版本为2.2

http服务的安装以及启动什么的

[root@centos6 ~]# rpm -ivh /misc/cd/Packages/httpd-2.2.15-59.el6.centos.x86_64.rpm     ##rpm安装方式:从光盘中安装,确保了http的稳定性
warning: /misc/cd/Packages/httpd-2.2.15-59.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...                ########################################### [100%]
   1:httpd                  ########################################### [100%]
[root@centos6 ~]# ###成功安装

安装后生成的文件:

[root@centos6 ~]# ls /etc/rc.d/init.d/httpd     #服务脚本文件,启动服务就是启动它
/etc/rc.d/init.d/httpd
[root@centos6 ~]# ls /etc/sysconfig/httpd     #服务脚本文件的配置文件,可以修改http服务的工作模式
/etc/sysconfig/httpd
[root@centos6 ~]# ls /etc/httpd/conf/httpd.conf     #服务的主配置文件,可以进行服务配置,一般不在主配置文件进行修改
/etc/httpd/conf/httpd.conf
[root@centos6 ~]# ls /etc/httpd/conf.d/*.conf     #服务的子配置文件,通常在这里来进行服务的配置,不修改主配置文件,便于修改以及排错,注意!!!必须是以.conf结尾才为配置文件
/etc/httpd/conf.d/welcome.conf
[root@centos6 ~]# ls /var/www/html/     #站点网页页面的根目录,默认的客户端访问站点目录
a.jpg  http_group_file        image.html  secret
bbs    http_user_passwd_file  index.html
[root@centos6 ~]# ls /etc/httpd/modules/     #服务模块文件,实际上是/usr/lib64/httpd/modules的一个软连接,可以ll命令查看:ll /etc/httpd/
mod_actions.so          mod_cgi.so           mod_mime.so
...........
mod_cgid.so             mod_mime_magic.so
[root@centos6 ~]# ls /etc/httpd/logs/     服务日志文件,实际上是/var/log/httpd的一个软连接,可以ll命令查看:ll /etc/httpd/
access_log #访问日志      error_log #错误日志

http服务的控制和启动:

[root@centos6 ~]# chkconfig httpd on/off     #on:http服务开机启动,off:http服务开机不启动
[root@centos6 ~]# service httpd {start/stop/restart/status/configtest/reload}   
#http服务的控制:start:启动服务,restart:重启服务,stop:停止服务,status:服务状态,configtest:配置语法检查,reload:重载配置文件,服务不用重启

简单小栗子保你大彻大悟:

[root@centos6 ~]# service httpd start     #开启服务
Starting httpd: httpd: apr_sockaddr_info_get() failed for centos6.9magedu.com
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]     #OK代表成功开启

接下来你就可以去用浏览器(推荐使用火狐浏览器)去查看站点网页:地址栏输入你虚拟机本机IP即可访问,默认如图界面,是由子配置文件/etc/httpd/conf.d/welcome.conf决定的

技术分享

我们进入站点页面根目录做点事情:

[root@centos6 ~]# cd /var/www/html/     #进入根目录
[root@centos6 html]# vim index.html     #编辑一个名为index.html文件,内容如下:
<h1>centos6<h1>
<h2>centos6<h2>
<h3>centos6<h3>

刷新浏览器页面,是不是很熟悉那个界面呢(http服务支持html格式文件),懂了什么是http服务了吧,哈哈哈

好了,老鼠拉木锨——大头在后面,安装很简单,主要还是配置滴

http服务常用配置

既然介绍配置,首先我们来了解下主配置文件的组成以及配置格式:

在主配置文件中,分为三类配置:

### Section 1: Global Environment:全局环境的配置

### Section 2: ‘Main‘ server configuration:主服务的配置

### Section 3: Virtual Hosts:虚拟主机相关的配置

配置格式:directive value

directive:指令,不区分大小写

value:值,可以理解为某指令的参数

(1)修改显示的服务器版本信息的格式

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
ServerTokens OS     #负责显示的服务器版本信息的格式,建议改为 ServerTokens Prod 较为安全
.......
Include conf.d/*.conf     #解释了子配置文件的存在,子配置文件在此处生效
.......
[root@centos6 conf.d]# service httpd reload     #重载配置文件                  
Reloading httpd:

附图:来自官方的格式解释以及示例

技术分享

那么如何查看服务器版本信息呢?

法1:可以开启一台虚拟机充当客户客户端,利用命令curl

[root@centos7 ~]#curl -I 192.168.136.131     #选项-I,显示响应报文首部信息
HTTP/1.1 200 OK
Date: Sat, 21 Oct 2017 06:08:47 GMT
Server: Apache/2.2.15 (CentOS)     #服务器版本信息
Last-Modified: Sat, 21 Oct 2017 02:56:51 GMT
ETag: "60429-30-55c05bdb9fa40"
Accept-Ranges: bytes
Content-Length: 48
Connection: close
Content-Type: text/html; charset=UTF-8     #显示字符集,我们也可以修改字符集,后文会介绍

法2:通过浏览器工具查看:

技术分享

(2)修改监听的IP和port

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
#Listen 12.34.56.78:80
Listen 80     #不写IP,默认监听本机所有IP的端口,可以修改为 Listen 192.168.136.131:8000 指定IP以及端口,可以小小实现访问控制
.......
[root@centos6 conf.d]# service httpd reload     #重载配置文件                  
Reloading httpd:

利用客户端或者浏览器测试:

[root@centos7 ~]#curl 192.168.136.131     #默认80端口
curl: (56) Recv failure: Connection reset by peer
[root@centos7 ~]#curl 192.168.136.131:8000     #指定8000端口
<h1>centos6<h1>
<h2>centos6<h2>
<h3>centos6<h3>

技术分享

(3)定义站点页面根路径:

[root@centos6 conf.d]# mkdir /app/site     #创建新的根目录
[root@centos6 conf.d]# mv /var/www/html/index.html /app/site/     #准备页面文件
[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
DocumentRoot "/var/www/html"     #默认根路径,可以修改为 DocumentRoot "/app/site" 修改根路径,访问时找寻此路径.
.......

(4)定义站点主页面:

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
# same purpose, but it is much slower.
#
DirectoryIndex index.html index.html.var     #默认访问站点时访问的主页面,可以修改为 DirectoryIndex index.txt index.html.var 等
.......

(5)设定默认字符集

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
AddDefaultCharset UTF-8     #默认UTF-8字符集,可以修改字符集
.......

(6)定义路径别名

[root@centos6 ~]# vim /etc/httpd/conf.d/mozart.conf     #创建子配置文件,以.conf结尾
alias "/bbs/" "/app/site/bbs/"     #定义别名,格式为:alias "URL" "/PATH/" 注意!!!URL为默认根目录下的子目录,等同于路径 /var/www/html/bbs/ 但不可写为路径,PATH为路径
[root@centos6 conf.d]# mkdir /var/www/html/bbs/
[root@centos6 conf.d]# echo /var/www/html/bbs > /var/www/html/bbs/index.html
[root@centos6 conf.d]# mkdir /app/site/bbs
[root@centos6 conf.d]# echo /app/site/index.html > /app/site/bbs/index.html
[root@centos6 conf.d]# service httpd reload     #重载配置文件
Reloading httpd:

(7)持久连接 Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭持久连接

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
KeepAlive Off     #默认关闭持久连接,可以修改为 on ,开启持久连接
.......
MaxKeepAliveRequests 100     #持久连接的最大连接资源数,连接数到一定值时断开连接
.......
KeepAliveTimeout 15     #持久连接的最大持久时间,默认单位为秒

持久连接的测试方法:在客户端模拟连接

[root@centos7 ~]#telnet 192.168.136.131 80
Trying 192.168.136.131...
Connected to 192.168.136.131.
Escape character is ‘^]‘.
GET /index.html HTTP/1.1     #模拟连接
HOST:192.168.136.131     #Web服务器

HTTP/1.1 200 OK     #回复的响应报文
Date: Sat, 21 Oct 2017 07:47:53 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sat, 21 Oct 2017 07:23:35 GMT
ETag: "60429-30-55c0977a92555"
Accept-Ranges: bytes
Content-Length: 48
Connection: close
Content-Type: text/html; charset=UTF-8

<h1>centos6<h1>
<h2>centos6<h2>
<h3>centos6<h3>
Connection closed by foreign host
##此处不会断开。继续等待其他请求,直到时间到或者资源数到才断开连接

(8)日志设定

日志分为:访问日志和错误日志

错误日志:

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
ErrorLog logs/error_log     #错误日志路径,可以修改默认路径

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn     #错误日志的级别,默认为warn
.......

附图:来自官网日志级别

技术分享

emerg 紧急 - 系统无法使用。 "Child cannot open lock file. Exiting"  

alert 必须立即采取措施。 "getpwuid: couldn‘t determine user name from uid"  

crit 致命情况。 "socket: Failed to get a socket, exiting child"  

error 错误情况。 "Premature end of script headers"  

warn 警告情况。 "child process 1234 did not exit, sending another SIGHUP"  

notice 一般重要情况。 "httpd: caught SIGBUS, attempting to dump core in ..."  

info 普通信息。 "Server seems busy, (you may need to increase StartServers, or Min/MaxSpareServers)..."  

debug 出错级别信息 "Opening config file ..."  

定义日志格式:

[root@centos6 conf.d]# vim /etc/httpd/conf/httpd.conf     #编辑主配置文件
.......
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined     #定义日志格式,常用定义:LogFormat "%h %l %u %t \"%r\" %>s %b\"%{Referer}i\" \"%{User-Agent}i\"" combined
.......
CustomLog logs/access_log combined     #日志格式的使用
.......

具体参数定义:

%h 客户端IP地址
%l 远程用户, 启用mod_ident 才有效,通常为减号“-” ”
%u 验证(basic ,digest )远程用户, 非登录访问时,为一个减号“-” ”
%t 服务器收到请求时的时间
%r First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL” 以及协议版本
%>s 响应状态码
%b 响应报文的大小,单位是字节;不包括响应报文http 首部
%{Referer}i 请求报文中首部“referer” 的值;即从哪个页面中的超链接跳转至当前页面的
%{User-Agent}i 请求报文中首部“User-Agent” 的值;即发出请求的应用程序

以上步骤均已实验成功,(可累死了我了),好了,部分配置先介绍到此,下次再介绍几个重要的配置

对了!差点忘记了检查配置语法命令:

[root@centos6 ~]# httpd -t     #常用命令,CentOS7上http2.4版本同样适用
httpd: apr_sockaddr_info_get() failed for centos6.9magedu.com
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK     #OK,配置语法正确
[root@centos6 ~]# service httpd configtest     #适用于CentOS6上的配置语法检查命令
httpd: apr_sockaddr_info_get() failed for centos6.9magedu.com
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK     #OK,配置语法正确


本文出自 “Mozart” 博客,请务必保留此出处http://mozart.blog.51cto.com/13286673/1975317

CentOS6中http服务搭建以及部分配置(1)

标签:服务   http   centos6   

原文地址:http://mozart.blog.51cto.com/13286673/1975317

(0)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!