httpd是著名的开源项目,有ASF组织,是世界上用的最多的web服务器程序,今天我们以最新的apache 2.4.6为例,解释httpd的安装与应用
1.http 源码安装
2.端口监听
3.配置选用的MPM
4.Keep-alived支持
5.访问控制
6.日志
7.虚拟主机
8.https
1.httpd源码安装
[root@stu11 ~]# yum groupinstall"Development tools" [root@stu11 ~]# yum groupinstall"Server Platform Development"
对于centos 6.4 安装新版本的http2.4.6还需要三个包,一个是APR,APR-utils,pcre-devel,前面两个对于新版本的http版本不够,所以要下载新的版本去安装,而对于后面的只需要yum安装即可。
[root@stu11 apr-1.4.6]# ./configure--prefix=/usr/local/apr [root@stu11 apr-1.4.6]# make &&make install [root@stu11 apr-util-1.5.2]# ./configure--prefix=/usr/local/apr-utils --with-apr=/usr/local/apr [root@stu11 apr-util-1.5.2]# make&& make install [root@stu11 httpd-2.4.6]# yum installpcre-devel
OK,正式的去安装
[root@stu11 ~]# tar xf httpd-2.4.6.tar.bz2 [root@stu11 ~]# cd httpd-2.4.6 [root@stu11 httpd-2.4.6]# ./configure--prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl--enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-utils --enable-modules=most --enable-mpms-shared=all--with-mpm=event
--enable-so |
支持动态链接库 DSO |
--enable-cgi |
支持通用网关界面 |
--enable-rewrite |
支持重写 |
--enable-zlib |
支持zlib压缩 |
--enable-modules=most |
把大部模块编译进httpd |
--enable-mpms-shared=all |
支持所有的MPM |
--with-mpm=event |
默认启用事件驱动 |
[root@stu11 httpd-2.4.6]# make &&make install
修改环境变量使直接运行apachectl 就可运行,并且把所依赖的库文件链接过去
[root@stu11 httpd-2.4.6]# echo "exportPATH=/usr/local/apache/bin:$PATH" > /etc/profile.d/apache.sh [root@stu11 httpd-2.4.6]# ./etc/profile.d/apache.sh [root@stu11 httpd-2.4.6]# ln -s/usr/local/apache/include/ /usr/include/
复制以前的启动文件,并且修改文件
[root@stu11 ~]# cd /etc/rc.d/init.d/ [root@stu11 init.d]# cp httpd httpd24 apachectl=/usr/local/apache/bin/apachectl//修改apachectl的位置 httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}修改pid文件的位置
2.配置监听端口
先备份一遍
配置文件在 /etc/httpd24/httpd.conf中配置文件
Listen [IP:]port Listen 80 说明监听所有IP的的80端口 Listen 172.16.11.1:8080 监听172.16.11.1的8080端口
3.配置选用的MPM
MPM即多道处理模块,Apachehttpd2.4.6支持3中MPM,即prefork,worker还有event.
在配置文件中去掉前面的注释符
Include /etc/httpd24/extra/httpd-mpm.conf
下面的一句中选一个,然后注释掉其他
LoadModule mpm_event_module modules/mod_mpm_event.so LoadModule mpm_worker_module modules/mod_mpm_worker.so LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
定义MPM配置文件的路径在/etc/httpd24/extra/httpd-mpm.conf中
<IfModule mpm_prefork_module> //如果启用的模块式prefork StartServers 5 //启动时候建立的子进程的个数 MinSpareServers 5 //空闲子进程的最大数量 MaxSpareServers 10 //空闲子进程的最小个数 MaxRequestWorkers 250 //同时接入的最大请数量,求相当httpd2.2中的MaxClients MaxConnectionsPerChild 0 </IfModule> <IfModule mpm_worker_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule> <IfModule mpm_event_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule>
4.Keep-alived支持
我们首先说明为什么要使用KeepAlive(长连接),一般一个页面会有很多个资源,比如说有70个,我们如果没有,如果每次请求资源都要建立连接,发送请求,等待响应,接受数据,那用户的体验是非常差的,为了解决这个问题,就有了长连接,我们的KeepAlived的配置文件在/extra/httpd-default.conf
首先在httpd.conf中启用Include /etc/httpd24/extra/httpd-default.conf
KeepAlive On //打开长连接 MaxKeepAliveRequests 100 //每次请求最大的资源数目 KeepAliveTimeout 5 //每次最多响应时长
5. 配置日志
我们看怎么定义日志存放,日志一般分为访问访问日志(一般需要自定义格式的)还有错误日志
启用日志功能,去掉LoadModule log_config_module modules/mod_log_config.so前的注释符号,果启用了log_config_module则启用一下配置,其中有一句为
LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined Combined 是一种日志格式 <VirtualHost 172.16.11.1:80> ServerAdmin cyb@a.com DocumentRoot "/web/a.com" ServerName www.a.com CustomLog /var/log/access.log combined//定义访问日志在/var/log/access.log中,格式为combined ErrorLog/var/log/err.log//定义错误日志的/var/log/err.log <Directory "/web/a.com"> Options None AllowOverride None <RequireALL> Require all granted Require not ip172.16.254.83 </RequireALL> </Directory> </VirtualHost>
6,访问控制
访问控制可以分为两类一类是基于IP的访问控制,另外一类是基于用户的访问控制
我们先看基于用户的访问控制
我们在Direcotry容器中增加新的内容
AuthTypeBasic//认证方式有两种,一种是BASIC认证另外一种digest数据库认证,我们使用basic认证 AuthUserFile/etc/httpd24/.htpasswd//认证文件所在的位置,事先我们要新建一个叫.htpasswd的文件 Requirevalid-user//要求有效的用户去验证
我们使用htpasswd命令去建立用户名和密码
[root@stu11extra]# htpasswd -c -m /etc/httpd24/.htpasswd test New password: Re-type newpassword: Adding passwordfor user test
添加第二个用户名的时候去掉-c(创建),会覆盖以前的用户的
基于IP的访问控制在httpd2.2和httpd2.4中略有不同
我们看httpd2.4中的基于IP的访问控制
我们只允许192.168.0.0/16的主机访问,我们的客户机是172.16.251.83,所以就会出现如下字样。
Forbidden
You don't havepermission to access / on this server.
允许除了172.16.254.83访问,其他主机都可以访问,我们在虚拟主机了设定容器RequireAll
<RequireALL>
Require all granted
Require not ip 172.16.254.83
</RequireALL>
这样我们同样看到了上面的Forbidden的字样。
7.启用虚拟主机
我们新建4个虚拟主机分别为,分别基于端口,IP地址,和FQDN的不同
FQDN |
IP |
端口 |
www.a.com. |
172.16.11.1 |
80 |
www.b.com. |
172.16.11.1 |
8080 |
www.c.com |
172.16.11.11 |
80 |
首先注释掉配置文件的DocumentRoot
#DocumentRoot"/usr/local/apache/htdocs"
去掉Vitrual Host #启用配置文件
Include /etc/httpd24/extra/httpd-vhosts.conf
配置虚拟主机文件
[root@stu11htdocs]# vim /etc/httpd24/extra/httpd-vhosts.conf
新建Virtual Host
<VirtualHost 172.16.11.1:80>//指定主机所监听的套接字 ServerAdmin cyb@a.com//出错时联系的管理员邮箱 DocumentRoot "/web/a.com"//网站的根目录 ServerName www.a.com//网站的名字 <Directory "/web/a.com">// Options None AllowOverride None Require all granted//允许所有主机访问 </Directory> </VirtualHost> 使用让虚拟主机监听在172.16.11.1的8080端口上 <VirtualHost 172.16.11.1:8080> ServerAdmin cyb@b.com DocumentRoot "/web/b.com" ServerName www.b.com <Directory "/web/b.com"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 172.16.11.11:80> ServerAdmin cyb@c.com DocumentRoot "/web/c.com" ServerName www.c.com <Directory "/web/c.com"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
上面的配置就是分别实现事先了不同的FQDN,不同的端口和IP地址之上配置虚拟主机。在生产环境中不同的FQDN去实现虚拟主机是最常见的。
8,https的实现
我们要实现https,首先,我们事先有一个CA,很明显,我们有一个,我们只需要给我们的http颁发一个证书就可以了(我修改了这台主机的名字)
首先我们生成一个私钥
[root@www CA]# (umask 077;openssl genrsa-out /root/www.pri 2048) 生成请求文件 [root@www CA]# openssl req -new -key/root/www.pri -out /root/www.csr You are about to be asked to enterinformation that will be incorporated into your certificate request. What you are about to enter is what iscalled a Distinguished Name or a DN. There are quite a few fields but you canleave some blank For some fields there will be a defaultvalue, If you enter '.', the field will be leftblank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name)[]:shanxi Locality Name (eg, city) [DefaultCity]:houma Organization Name (eg, company) [DefaultCompany Ltd]:a.com Organizational Unit Name (eg, section)[]:tec Common Name (eg, your name or your server'shostname) []:www.a.com Email Address []: Please enter the following 'extra'attributes to be sent with your certificate request A challenge password []: An optional company name []:
由CA去签署证书
[root@www CA]# openssl ca -in /root/www.csr-out /root/www.crt Listen 443//监听443端口 <VirtualHost 172.16.11.1:443> //新建一个虚拟主机 DocumentRoot "/web/a.com/" //根目录 ServerName www.a.com:443 //主机名 ServerAdmin you@example.com ErrorLog"/usr/local/apache/logs/error_log" TransferLog"/usr/local/apache/logs/access_log" SSLEngine on//开启SSL引擎 SSLCertificateFile"/etc/httpd24/www.crt" //SSL的证书文件 SSLCertificateKeyFile"/etc/httpd24/www.pri" //SSL的私钥文件 <Directory "/web/a.com/">//这里别忘了修改 SSLOptions +StdEnvVars AllowOverride None Require all granted//授权所有用户访问 </Directory> </VirtualHost>
客户端主机下载,CA的证书,去验证。