日常生活中,我们经常浏览的web服务器,一般有两种方法搭建。其一就是微软的IIS,另一种就是APACHE,由于IIS只局限用于微软的产品,那么在linux操作系统中是怎样搭建web服务器呢?这就需要借助于apache来实现服务器的搭建。而且,apache还属于开源软件,我们可以进行自己的设置,满足更多的需求。
那么现在让我们来安装软件吧,实现服务器的搭建。
httpd软件的安装,可以采用linux系统自带的rpm包安装,也可以采用较为麻烦但拥有更多功能的源码安装,所以还是让我们源码安装吧。
实验准备:
操作平台为硬件i686,内核版本2.6.18-164.el5的linux操作系统 
源码为分别为: apr-1.4.6.tar.gz apr-util-1.5.1.tar.gz  httpd-2.4.4.tar.bz2
步骤一:将所需要的软件包传到/root目录下我们建立的apache下
[root@localhost ~]# mkdir apache 091840742.png
步骤二:将软件包解压到 (/usr/local/src)下
[root@localhost apache]#  tar zxvf apr-1.4.6.tar.gz -C /usr/local/src/
[root@localhost apache]#  tar zxvf apr-util-1.5.1.tar.gz -C /usr/local/src/
[root@localhost apache]#  tar jxvf httpd-2.4.4.tar.bz2 -C /usr/local/src/
092020177.png
步骤三:编译
由于apache的编译需要apr和apr-util的支持,所以我们先编译它们。
说明:( apr是apache的可移植运行库,主要为上层的应用程序提供跨操作平台的底层支持接口库,包括apr apr- util等开发包)
--prefix // 指定服务程序的安装路径 
--enable-so:启用动态加载模块支持 
--enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。

--enable-charset-lite:启动字符集支持,以便支持使用各种字符集编码的网页。

--enable-cgi:启用CGI脚本程序支持,便于扩展网站的应用访问能力。

--sysconfdir: 指定服务配置文件目录
1)编译apr
cd apr-1.4.6/
./configure --prefix=/usr/local/apr //指定apr的安装目录
make&&make install //编译配置安装
2)编译apr-util-1.5.1
cd apr-util-1.5.1/ 
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config //指定apr-util的安装目录和配置文件的  路径 
make&&make install //编译配置安装
3)编译httpd
cd httpd-2.4.4/
./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache 
--with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr-util/ --enable-so --enable-rewrite --enable-charset-list --enable-cgi  //指定httpd的安装目录和配置文件的路径以及一些功能
make&&make install //编译配置安装
4)编辑man手册
由于是源码安装我们需要把apache的man路径加入 /etc/man.config
092733451.png
5)为源码安装的apache的头文件设置连接,方便系统识别
ln -s /usr/local/apache/include/ /usr/include/apache
6)编写脚本来控制httpd的启动,停止,重启等 并且受到chkconfig管理
vim httpd
mv httpd /etc/init.d/ // 移动脚本到init.d目录
chmod a+x /etc/init.d/httpd //给予可执行的权利
脚本内容如下:
#!/bin/bash
#chkconfig:26 80 30
#description: Apache Server
prog=/usr/local/apache/bin/httpd
lockfile=/var/lock/subsys/httpd
pidfile=/var/run/httpd.pid
. /etc/init.d/functions
start() {
if [ -e $lockfile ] ;then
echo "the programe `basename $prog` is started..."
else
echo -e -n "the programe `basename $prog` is starting....."
sleep 2
$prog && echo [ok] && touch $lockfile || echo "fail"
fi
}
stop() {
if [ -e $lockfile ] ;then
echo -e -n "the programe `basename $prog` is stoping......"
sleep 2
killproc `basename $prog` && rm -rf $lockfile && echo "ok" || echo "fail" 
else
echo -e "the programe `basename $prog` is stoped"
fi
}
status() {
if [ -e $pidfile ];then
echo "the prog `basename $prog` `cat $pidfile` is running"
else
echo "the prog `basename $prog` is stop"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "usage: start|stop|status"
esac
测试一下脚本是否可行:
093017256.png
7)了解http的配置文件
apache安装完成了,接下来我们了解一下它的配置文件,(配置文件安装在了 /etc/apache
主配置文件为 httpd.conf,大部分配置都包括在区域中规则如下:

<Directory />

Options FollowSymLinks

AllowOverride None // 代理相关

Order deny,allow // 规则 拒绝 允许 (注意二者顺序不一样,规则也不一样)

(allow,deny 表示先允许后拒绝,执行的时候,先满足允许的规则,再看拒绝的规则

deny,allow 表示先拒绝后允许,执行的时候,先满足拒绝的规则,在看允许的规则)

Allow from all // 允许所有

</Directory>

在安装apache完成后,我们可以在/etc/apache/http.conf中修改配置文件,来部署web服务器

vim /etc/apache/httpd.conf编辑其中的内容

ServerAdmin //管理员的邮箱 ServerName // 服务器的域名

ServerName 192.168.1.190

启动apache service httpd start

093636134.png

8)客户端的访问控制
为了保证网络资源,和服务器的稳定运行,我们 可以在apache的配置文件中对访问者做出一些控制和授权
包括:用户验证,加密防护等。这里我们仅仅来实施简单的用户验证。
一般都在目录安全性下面(directroy 安全参数规定 /directory)来实施
a.基于客户端地址的限制
093753627.png
我们亦可以控制一段ip地址段或某个ip
allow from 192.168.2.2 192.168.2.30/24 允许这个网段的客户机访问
deny from 某个ip地址或地址段 拒绝该网段的客户机访问
b。基于客户身份的验证

httpd的基本认证通过校验用户名,密码组合来判断是否允许用户访问。使用htpasswd工具程序,可以创建授权用户数据文件,并维护其中的用户账户。

 

在站点的主目录下( /var/www/html/)编辑 说明文件  .htaccess
添加:
authuserfile /var/www/ .htpasswd
authname "please input your name and password"
authtype basic
require valid-user
产生用户:
[root@localhost html]#  cd /var/www/
[root@localhost www]#  htpasswd -c  .htpasswd  zhangsan (-c 增加账户,增加一个后就不用 了)
New password: 
Re-type new password: 
Adding password for user zhangsan
[root@localhost www]# htpasswd  .htpasswd lisi 
New password: 
Re-type new password: 
Adding password for user lisi
这时候我们在访问就需要输入账户名字了
094010116.png