httpd

web服务器

web服务器也称为www服务器,http服务器,主要提供网页浏览功能。

unix和linux平台下常用web服务器有Apache,nginx,lighttp,tomcat,IBM Websphere等,其中最广泛的是Apache,而Windows服务器平台下最常用的是微软公司的IIS。

HTTP响应状态码

100        请求已接收,客户端可以继续发送请求
101        swithching  protocals服务器根据客户端的请求切换协议
200        一切正常
201       服务器已经创建了文档
202       已经接收了请求,但处理尚未完成
203       文档正常返回,但一些头部信息可能不正确
300       客户端请求的资源可以在多个位置找到
301       客户端请求的资源可以在其他位置找到
305       请用代理服务
400       请求语法错误
401       访问别拒绝
401.1    登陆失败
403       资源不可用
403.6    ip地址被拒绝
403.9    用户过多
404       无法找到指定资源
406      指定资源已找到,但MIME类型与客户端要求不兼容
407      要求进行代理身份验证
500      服务器内部错误
500.13 服务器太忙
501      服务器不支持客户端的请求功能
502      网关错误
503      服务不可用
504      网关超时,服务器处于维护或负载过高无法响应
505      服务器不支持客户端请求的http版本 
505 

http  server 之 Apache

apache http server 是一款跨平台(windwos,linux,unix)开源的http服务器软件。apche http server采用模块化设计模型,apche模块分为静态模块和动态模块,静态模块式是apache最基本模块,是无法随时添加与卸载的模块,静态模块在编译软件时设定。动态模块是可以随时添加和删除的模块,在部署时获得最大的灵活性,每个企业都可以根据自己的平台与需求,安装使用不同的模块。 apache模块被编译为动态共享对象(DSO),这些动态共享对象独立于http程序,DSO模块可以编译apache时添加,也可以后期随时通过apache扩展工具编译添加。   

使用http -M命令查看模块加载清单

源码安装apache(需手动解决依赖包   make  gcc  autoconf  automake  pcre  pcre-devel  openssl  openssl-devel)

可以手动指定安装路径

[root@centos ~]# yum install -y gcc autoconf automake make pcre pcre-devel openssl openssl-devel
[root@centos ~]# tar -zxvf  apr-1.4.6.tar.gz  -C /usr/src
[root@centos ~]# cd /usr/src/apr-1.5.0/
[root@centos apr-1.5.0]# ./configure
[root@centos apr-1.5.0]#make 
[root@centos apr-1.5.0]#make install
[root@centos ~]# tar -zxvf apr-util-1.5.3.tar.gz  -C /usr/src/
[root@centos ~]# cd /usr/src/apr-util-1.5.3/
[root@centos apr-util-1.5.3]# ./config  --with=/usr/local/apr
[root@centos apr-util-1.5.3]#make 
[root@centos apr-util-1.5.3]#make  install
[root@centos ~]# tar -zxvf httpd-2.4.4.tar.gz -C /usr/src
[root@centos ~]# cd /usr/src/httpd-2.4.4/
[root@centos httpd-2.4.4]#./configure --prefix=/usr/src/local/apache2 --enables-so --enable-ssl --enable-rewrite --with-mpm=worker --with-suexec-bin --with-apr=/usr/local/apr/
[root@centos httpd-2.4.4]#make && make install

源码软件包中的configure脚本用来检查系统环境,查找依赖文件,设置安装路径等操作,以下是configure支持的选项

--prefix                    指定Apache httpd程序的安装目录
--enable-so             开启模块化功能,支持DSO(动态共享对象)
--enable--ssl           支持ssl加密
--enable-rewrite     支持地址重写
--with-mpm             设置Apache httpd工作模式
--with suexec-bin    支持SUID SGID
--with-apr               指定apr程序绝对路径

yum 安装

yum  install  -y  httpd     安装(centos中默认httpd服务的使用的是Apache http软件)
yum remove httpd 卸载

启动httpd服务

通过源码安装的的Apache
/usr/local/apache2/bin/apachectl   start     启动httpd程序
/usr/local/apache2/bin/apachectl   stop     停止httpd程序
/usr/local/apache2/bin/apachectl   restart  重启httpd程序
/usr/local/apache2/bin/apachectl   status  查看httpd 状态
除了 start stop  restart status 选项外,还是使用以下选项
graceful-stop    停止httpd,不中断现有的http连接请求
graceful            启动httpd,不中断现有的http连接请求
configtest         检查httpd主配置文件语法

通过yum方式安装的Apache
service   httpd   start     启动httpd服务
service   httpd   stop      停止httpd服务
service   httpd   restart   重启httpd服务
service   httpd   status    查看httpd服务状态

tips :httpd服务启动的时候可能会报 "could not reliably detemine  the server's fully qualified domain name"  可以通过修改主配置文件的servername选项来解决,也可以忽略。

验证服务

1.通过netstat 命令查看

netstat -nutlp |grep  httpd

2.客户端验证

通过客户端浏览器访问服务器,不出意外,会看 到页面显示“it  works”

tips: 有时候服务器已经启动成功,但是客户端还是访问不了服务器,可能是因为防火墙阻止了,我们需要设置一下防火墙

iptables -I INPUT -P tcp   --dport  80  -j   ACCEPT    开放tcp 80端口
service iptables  save   保存防火墙设置

配置文件解析

1.通过源码安装的Apache配置文件默认保存在/usr/local/apache2/conf 目录下,最主要的配置文件为httpd.conf ,还有以下在extra 目录下的附加配置文件。 此外original 目录中存有这些配置文件的所有副本。 

2.Apache  http  server主配置文件主要有指令以及容器组成,容器使用<容器 名称> 开始,</容器名称>结尾,容器的指令一般尽在容器内有效。

3.通过yum安装的Apache  http  server 配置文件保存在  /etc/httpd/conf目录下

主配置文件中的重点指令

 指令

ServerRoot 设置Apache软件的安装主目录,如果采用源码安装,则默认路径为/usr/local/apache2

Listen 设置服务器的监听的ip以及端口号, 默认监听本机的80端口,格式为 Listen [ip地址:]端口[协议],ip地址和协议为可选项,默认监听所有ip,使用TCP协议。 一个配置文件可以多次使用Listen 指令来开启多个端口。

LoadModule 用于加载模块,格式为 loadmodule 模块 模块文件名称,模块文件一般位于serverroot目录下的modules目录中。 如果需要Apache动态加载模块,需要在编译Apache时通过--enable-so将mod-so以静态方式编译到Apache核心模块中。

Loadfile 与loadmoudle类似,区别在于 loadfile通过绝对路径加载moudles目录下的模块文件。

serveradmin 为提供客户一个可以帮助其解决问题的邮件地址

servername 设置服务器本机名称以及端口,对URL地址的重定向很重要

DocumentRoot 设置客户端访问网站的根路径。默认为/usr/local/apche2/htdocs

errorlog 定位服务器的错误日志的位置,默认使用相对路径,为serveroot目录下的“logs/error_log”

errorlogformat 设置错误日志的格式,Apache http server 预先定义了 很多格式字串可以直接引用。

customlog 设置客户端的访问日志文件名以及日志格式,默认为"logs/access_log",语法格式为 customlog 文件名 格式

logformat 描述日志文件格式,可以直接使用Apache预先设置的格式字串,一般我们会为logformat指令设置的日志格式创建别名,然后通过customlog 指令调用该日志格式别名。

include 允许Apache在主配置文件加载其他配置文件,指令语法就是在include指令后直接跟上其他附加的配置文件的路径即可

options 为特定目录设置选项, 语法格式 options [+/-]选项 [[+/—] 选项]
选项
none 不启用任何额外的功能

all 开启出multiviews 之外的所有选项

execCGI 允许执行options指定目录下的所有CGI脚本

followsymlinks 允许指定目录下的文件链接到目录外的文件或目录

indexes 如果与url对应的options目录下找不到directoryindex 指定的首页文档,则Apache将会把当前目录的所有文件索引出来

order 控制默认访问状态以及allow与deny的次序,( 如果使用order deny,allow 则先检查拒绝,在检查允许,当拒绝与允许有冲突时,允许优先,默认规则为允许。) (如果使用order allow,deny, 则先检查允许,再检查拒绝,当允许与拒绝有冲突时,拒绝优先,默认规矩为拒绝。)
1.order allow,deny
allow from 192.168.0.1
deny from all
结果为deny所有
2.order deny,allow
deny from all
allow from 192.168.0.1
结果为允许192.168.0.1的访问

容器

IfDefine
IfDefine容器封装的指令仅在启动Apache时测试条件为真才会被处理,测试条件需要在启动Apache时通过http-D定义,语法格式 <IfDefine>指令</IfDefine>
例子 <IfDefine MemCache>
loadmodule mem_cache_module modules/mod_mem_cache.so
</IfDefine>
这样使得可以采用多种配置方式启动Apache,启动Apache时,如果使用了httpd-D useCache -D MemCache,则Apache将加载mod_mem_cache 与mod_cache模块,如果没有使用-D指定任何参数,则Apache则将不加载这些模块。
ifmodule容器

使用ifmodule容器可以封装仅在条件满足时才会处理的指令,根据指定的模块是否加载决定条件是否满足, 语法 <ifmodule 模块> 指令</ifmodule>
<ifmodule unixd_module>
user daemon
group daemon
</ifmodule>
仅在Apache加载了unix_module模块后,user daemon与group daemon才会被Apache处理。

Directory 容器
该容器的指令仅应用与特定的文件系统目录,子目录以及目录下的内容,路径可以使用~匹配正则表达式 语法格式 <Directory directory-path>指令</Directory>

<DirectoryMatch>
与Directory类似,但可以直接使用正则表达式匹配,无须Directory一样使用~符号才可以匹配。

files容器
与Directory容器类似,但Files容器内的指令应用与特定的文件, 语法格式 <Files 文件名>指令</File>。与Directory类似,可以使用~匹配正则表达式。
filesmatch容器
仅使用正则表达式匹配需要的文件,容器内的指令应用于匹配成功的特定文件。filesmatch等同于使用了~符号的files。
location 容器
location容器定义的指令仅对特定的URL有效,语法格式 <location URL-path|URL> 指令</Location>. 如果需要使用正则表示匹配url,可以使用~符号。
locationmathch容器
locationmatch仅使用正则表达式匹配url,等同于使用~符号匹配的locating。



















 

虚拟主机类型

Apache  http  server支持基于域名以及基于ip虚拟主机类型。
1.在Apache配置文件中,虚拟主机指令需要使用virtualhost容器封装。

2.基于ip的虚拟主机可以根据不同的ip地址以及端口定位不同的网站的请求,但基于ip的虚拟主机需要独立的ip地址定位连接的网站。

3.基于域名的虚拟主机,服务器可以根据客户端访问http的头部信息来实现网站的分离解析,客户端可以使用不同的域名访问位于同一ip地址的服务器资源。
 

虚拟主机实力之基于域名

1.首先需要将主配置文件中的include conf/extra/http-vhosts.conf语句开启(即去掉#,因为默认该行是被注释掉的),其掉之后include指令将读取httpd-vhost.conf文件的内容作为主配置文件的一部分。

2.修改http-vhosts.conf,内容大概如下
<virtualhost *:80>
serveradmin  lee@lee.com
DocumentRoot "/usr/local/apache2/htdocs/lee"
servername www.lee.com 
errorlog "logs/www.lee.com-error_log"
custlog "logs/www.lee.com-access_log" common
</virtualhost>

<virtualhost *:80>
serveradmin  kele@kele.com
DocumentRoot "/usr/local/apache2/htdocs/kele"
servername www.kele.com 
errorlog "logs/www.kele.com-error_log"
custlog "logs/www.kele.com-access_log" common
</virtualhost>

3.创建不同主机的根目录
mkdir -p /usr/local/apache2/htdocs/{lee,kele}
echo "lee.com">/usr/local/apche2/htdocs/lee/index.html
echo "kele.com">/usr/local/apche2/htdocs/kele/index.html

4.设置防火墙开放tcp协议的80端口
iptables  -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save 

5.开启或重启服务器
/usr/local/apache2/bin/apachectl   start/restart

6.测试
在客户端浏览器输入不同的域名访问网站,由于通过域名访问需要用到dns服务,所以需要有dns服务器,如果没有,可以通过修改hosts文件的方式进行域名解析。

虚拟主机实力之基于ip

1.首先需要将主配置文件中的include conf/extra/http-vhosts.conf语句开启(即去掉#,因为默认该行是被注释掉的),其掉之后include指令将读取httpd-vhost.conf文件的内容作为主配置文件的一部分。

2.修改http-vhosts.conf,内容大概如下
<virtualhost 192.168.1.100:80>
serveradmin  lee@lee.com
DocumentRoot "/usr/local/apache2/htdocs/lee"
servername www.lee.com 
errorlog "logs/www.lee.com-error_log"
custlog "logs/www.lee.com-access_log" common
</virtualhost>

<virtualhost 192.168.1.200:80>
serveradmin  kele@kele.com
DocumentRoot "/usr/local/apache2/htdocs/kele"
servername www.kele.com 
errorlog "logs/www.kele.com-error_log"
custlog "logs/www.kele.com-access_log" common
</virtualhost>

3.创建不同主机的根目录
mkdir -p /usr/local/apache2/htdocs/{lee,kele}
echo "lee.com">/usr/local/apche2/htdocs/lee/index.html
echo "kele.com">/usr/local/apche2/htdocs/kele/index.html

4.设置防火墙开放tcp协议的80端口
iptables  -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save 

5.开启或重启服务器
/usr/local/apache2/bin/apachectl   start/restart

6.测试
在客户端浏览器中输入不同的ip访问网站.

http server 之 nginx

Nginx是一款开源的高性能http服务器和反向代理服务器,同时支持IMAP/POP3代理服务。有俄罗斯人Igor Ysyoev在2002年开始开发,于2004年底发布第一个公开版本。Nginx以高性能,高可用,丰富的功能模块,简单明了的配置文档以及低资源占用而著称。Nginx采用最新的网络I/O模型,支持高达50000个并发连接。

Nginx与Apache一样采用的是模块化设计,Nginx模块分为内置模块和第三方模块,其中,内置模块中包括主模块与事件模块。

默认自动编译的模块可以使用 --with参数禁用,内置模块中的附加模块需要在编译时通过--with参数手动开启

编译安装时还可以通过--with-module=/path/module的方式编译第三方模块

 安装

源码安装,安装后主程序在/usr/local/nginx下,该目录下还有conf(主文件配置),html(网站根目录),logs(日志文件目录),sbin(主程序目录)。

[root@centos ~]# wget http://nginx.org/download/nginx-1.4.0.tar.gz
[root@centos nginx-1.4.0]# yum install -y install gcc pcre pcre-devel openssl openss-devel gd gd-devel perl perl-ExtUtils-Embed
[root@centos ~]# tar -zxvf nginx-1.4.0.tar.gz -C /usr/src
[root@centos ~]# cd /usr/src/nginx-1.4.0/
root@centos nginx-1.4.0]# ./configure  --prefix=/usr/local/nginx --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-mail --with-mail_ssl_module  
[root@centos nginx-1.4.0]# make && make install

rpm包安装

rpm -ivh  nginx-release-centos-6-0.el6.ngx.noarch.rpm

启动Nginx

[root@centos sbin]# /usr/local/nginx/sbin/nginx   启动主程序
[root@centos sbin]# /usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/nginx.conf
[root@centos logs]# /usr/local/nginx/sbin/nginx -s  stop
[root@centos logs]# /usr/local/nginx/sbin/nginx -s  reload

tips:Nginx会将进程号保存在/usr/local/nginx/logs/nginx.pid 文件中,我可以使用kill指令发送信号给该进程号

[root@centos logs]# kill -QUIT 'cat /usr/local/nginx/logs/nginx.pid'
[root@centos logs]# kill -HUP 'cat /usr/local/nginx/logs/nginx.pid'

常用参数:
TERM,INT    快速关闭
HUP              重启应用新的配置    
USER2          升级程序
QUIT             关闭进程,但保持现有的客户端链接
USR1            重新打开日志文件
WINCH         关闭工作进程

配置文件解析

Nginx默认的配置文件为/usr/local/nginx/conf/nginx.conf,配置文件主要包括全局,event,http,server配置。
event   主要用来定义Nginx工作模式
http      提供web功能
server   用来设置虚拟主机,server必须位于http内部,一个配置文件可以有多个server

#user nobody; 设置用户和组
worker_processes 1; 启动子进程数,可以通过ps aux |grep nginx
#error_log logs/error.log; 错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid; 进程号保存文件

events {
worker_connections 1024; 每个进程可以处理的连接数,受系统文件句柄的限制
}

http { 为文件类型定义文件
include mime.types;
default_type application/octet-stream; 默认文件类型


#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; 日志格式


#access_log logs/access.log main; 创建访问日志,格式采用mail定义的格式


sendfile on; 是否使用sendfile进行数据复制,sendfile复制数据是在内核级别完成的,会比一般的read,write更高效
#tcp_nopush on; 开启后 服务器的响应头部信息产生独立的数据包发送,即一个响应头信息一个包


#keepalive_timeout 0; 保持链接的超时时间
keepalive_timeout 65;


#gzip on; 页面压缩


server {
listen 80;
server_name localhost; 使用server定义虚拟主机


#charset koi8-r; 编码格式,如果网页编码与此设置不同,则将自动转码


#access_log logs/host.access.log main; 设置虚拟主机的访问日志


location / { url匹配
root html; 设置网页根路径,使用的是相对路径,html指的是处于nginx安装路径下
index index.html index.htm; 首页,先找index。html ,如果没有则找index.htm
}


#error_page 404 /404.html; 设置错误代码对应错误页面


# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}


# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; 若用户访问的url是以.php结尾,则自动将请求转到127.0.0.1服务器,通过proxy_pass 可以实现代理功能
#}


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}


# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht { 拒绝所有人访问 .ht页面
# deny all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration

#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;


# location / {
# root html;
# index index.html index.htm;
# }
#}



# HTTPS server
#
#server {
# listen 443; 监听tls使用443端口
# server_name localhost;


# ssl on; 开启ssl
# ssl_certificate cert.pem; 指定证书文件,使用相对路径证书需要存放在与nginx.conf同目录下
# ssl_certificate_key cert.key; 制定私钥文件,使用相对路径私钥需要存放在与nginx.conf同目录下


# ssl_session_timeout 5m;


# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;


# location / {
# root html;
# index index.html index.htm;
# }
#}

}
 

配置nginx虚拟主机

        配置四台虚拟主机

user  nobody;
worker_processes  1;

error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  www.domain.com  *.domain.com;

        #charset koi8-r;

        access_log  logs/www.domain.com.access.log  main;

        location / {
            root   html/domain;
            index  index.html index.htm;
        }
   error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ /\.ht {
            deny  all;
        }
   }

server {
listen 80;
server_name web.domain.com;
location / {
root     html/web;
index index.html index.htm;
}
}

server {
listen 80;
server_name www.example.com;
location / {
root     html/example;
index index.html index.htm;
}
}

server {
listen 80  default_server;
location / {
root     html/default;
index index.html index.htm;
}
}
}     

 网站安全

1.一般情况下,网站使用明文传输模式,但未了安全一般比较重要的网站一般会采用SSL加密数据。
2.由于有SSL专注数据加密,所以TCP/IP协议就可以专注于数据传输
3.Apache http server 通过mod_ssl模块来实现对数据的SSL/TLS的支持。
4.TSL是对SSL的扩展与优化。
5.SSL,TLS都是基于非对称加密算法实现的网络数据安全,非对称加密算法使用公钥与私钥两把不同钥匙,,公钥与私钥是不可逆的。采用公钥加密私钥解密机制,必须要同时拥有公钥和私钥才能加密数据解开,
6.个人生成公钥与私钥是不被信任的,必须经过CA认证后的才被认为是可信任的密钥。如果被CA中心认证签名过,则是合法的数字证书。有CA认证过程复杂,所以有时我们需要自己生成数据证书,即自签名证书。
7.openssl工具可以生成证书

在网站上部署TLS/SSL 1.生成私钥与证书 [root@centos conf]# openssl genras -out server.key 2048 生成私钥 [root@centos conf]# openssl req -new -x509 -key server.key -out server.crt 根据私钥生成根证书 按要求填写选项 [root@centos conf]# cp {server.key,server.crt} /usr/local/apache2/conf/ 复制证书 2.修改Apache配置文件,主配置文件中需要加载mod_ssl 以及 mod_socache_shmcb两个模块,使用include指令读取conf/extra/目录下的httpd_ssl.conf vi /usr/local/apache2/conf/httpd.conf #LoadModule ssl_module modules/mod_ssl.so前面的#去掉 #LoadModule socache_shmcb_module modules/mod_socache_shmcb.so 前面的#去掉 vi /usr/local/apache2/conf/extra/http_ssl.conf #Include conf/extra/httpd-ssl.conf 前面的#去掉 3.修改http_ssl.conf Listen 443 监听端口 SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
<VirtualHost _default_:443> 设置虚拟主机 DocumentRoot "/usr/local/apache2/htdocs/lee" 网站根目录 ServerName www.example.com:443 域名以及端口 ServerAdmin you@example.com ErrorLog "/usr/local/apache2/logs/error_log" 错误日志 TransferLog "/usr/local/apache2/logs/access_log" 传输日志 SSLEngine on 开启ssl引擎 SSLCertificateFile "/usr/local/apache2/conf/server.crt" 设置证书文件 SSLCertificateKeyFile "/usr/local/apache2/conf/server.key" 设置私钥文件

验证
在客户端浏览器中输入https://域名或ip,没有dns服务器可以通过修改hosts文件进行域名解析

 

 



 

 

 

 

 

posted @ 2014-03-24 14:56  DylanLee  阅读(395)  评论(0)    收藏  举报