RHEL5配置HTTP服务
RHEL5.4配置HTTP 服务下面是关于linux 下面配置HTTP 服务的讨论,HTTP 介绍HTTP 的全名为Hyper Text Transfer Protocol(超文本传输协议)在linu
RHEL5.4配置HTTP 服务
下面是关于linux 下面配置HTTP 服务的讨论,
HTTP 介绍
HTTP 的全名为Hyper Text Transfer Protocol(超文本传输协议)
在linux 下面实现web 服务,通常使用Apache 来实现,Apache 一直是Internet 上面最流行的web 服务器,web 服务器的架设,在linux 下面有个很著名的架构叫lamp :linux apache mysql php。
下面是关于HTTP 这个服务的属性
HTTP 的相关软件包
Httpd
HTTP 的守护进程
/usr/sbin/httpd
HTTP 的脚本
/etc/init.d/httpd
HTTP 的端口
80(http) 443(https)
HTTP 的配置文件
/etc/http/* /var/www/*
下面来具体搭建HTTP 服务
现在我们主要来实现虚拟主机的搭建,
虚拟主机可以实现在一台服务器上面运行多个站点,而且之间互不影响。可以大大的节约成本。虚拟主机技术可以通过三种方式来实现:
基于域名,基于IP ,基于端口的虚拟主机。
下面主要讨论基于域名的虚拟主机的实现,
在实现基于域名的虚拟主机的之前,我们先要在DNS 服务器里面做好解析。 我已经为192.168.0.254这个IP 地址做了两个解析,一个是www1.example.com ,还有一个station1.example.com 。
第一步,安装软件包
[root@localhost ~]#
[root@localhost ~]# yum -y install httpd
Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Cluster | 1.3 kB 00:00
ClusterStorage | 1.3 kB 00:00
Server | 1.3 kB 00:00
,VT | 1.3 kB 00:00
Setting up Install Process
Resolving Dependencies
There are unfinished transactions remaining. You might consider running
yum-complete-transaction first to finish them.
The program yum-complete-transaction is found in the yum-utils package.
--> Running transaction check
---> Package httpd.i386 0:2.2.3-31.el5 set to be updated
--> Processing Dependency: libapr-1.so.0 for package: httpd
--> Processing Dependency: libaprutil-1.so.0 for package: httpd
--> Running transaction check
---> Package apr.i386 0:1.2.7-11.el5_3.1 set to be updated
---> Package apr-util.i386 0:1.2.7-7.el5_3.2 set to be updated
--> Processing Dependency: libpq.so.4 for package: apr-util
--> Running transaction check
---> Package postgresql-libs.i386 0:8.1.11-1.el5_1.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
1.2 M
Installing for dependencies:
apr i386 1.2.7-11.el5_3.1 Server 123 k
apr-util i386 1.2.7-7.el5_3.2 Server 76 k
postgresql-libs i386 8.1.11-1.el5_1.1 Server 196 k
Transaction Summary
================================================================================
Install 4 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
,Total size: 1.6 M
Total download size: 1.2 M
Downloading Packages:
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : apr 1/4
Installing : postgresql-libs 2/4
Installing : apr-util 3/4
Installing : httpd 4/4
Installed:
Dependency Installed:
apr.i386 0:1.2.7-11.el5_3.1 apr-util.i386 0:1.2.7-7.el5_3.2 postgresql-libs.i386 0:8.1.11-1.el5_1.1
Complete!
[root@localhost ~]#
HTTP 软件包就安装成功了,
第二步,编辑http 的配置文件
vim /etc/httpd/conf/httpd.conf
这个文件里面的语法很多,我们建议在定义虚拟站点的时候在这个文件的最后编辑,从这个文件的972行开始都是虚拟站点的模板文件,我们可以根据这些模板文件来定义虚拟站点。
NameVirtualHost 192.168.0.254:80
DocumentRoot /var/www/virt1
ServerName station1. example .com
ServerAlias server1.example .com
DocumentRoot /var/www/virt2
,ServerName www1. example. com
OK ,现在两个基于域名的虚拟站点基本就配置成功了。
现在,这两个基于域名的虚拟站点中根目录还没有建立,
[root@server1 ~]#
[root@server1 ~]# cd /var/www/
[root@server1 www]# mkdir virt1
[root@server1 www]# mkdir virt2
[root@server1 www]# cd virt1/
[root@server1 virt1]# vim index.html
[root@server1 virt1]# cat index.html
This is station1.example.com webstie!!!
[root@server1 virt1]#
[root@server1 virt1]# cd ../virt2/
[root@server1 virt2]#
[root@server1 virt2]# vim index.html
[root@server1 virt2]# cat index.html
This is www1.example.com website!!!
[root@server1 virt2]#
OK ,这两个虚拟站点的根目录就建立,并且还分别建立了一个测试页面。 首先,重启下http 服务,
[root@server1 ~]#
[root@server1 ~]# service httpd restart
Stopping httpd: [ OK ] Starting httpd: [ OK ]
[root@server1 ~]#
OK ,服务启动成功了,测试下。
现在我们到浏览器里面输入station1.example.com
,
OK ,页面显示正常。
然后在浏览器里面输入www1.example.com
,
OK ,显示也没有问题。
刚才我们给station1.example.conf 做了一个别名为server1.example.com 。 现在我们来测试下,
最后在浏览器里面输入server1.example.com
,
OK ,是和station1这个显示的相同的内容。
关于http 服务的自身的访问控制
我们可以通过directory 来做权限控制,
在/etc/httpd/conf/httpd.conf文件的虚拟主机之间加入directory 选项。 NameVirtualHost 192.168.0.254:80
DocumentRoot /var/www/virt1
ServerName station1. example .com
ServerAlias server1.example. com
options -Indexes -Followsymlinks
order allow,deny
allow from all
deny from 192.168.0.10
,DocumentRoot /var/www/virt2
ServerName www1. example .com
options -Indexes -Followsymlinks
order deny,allow
deny from all
allow from 192.168.0.10
关于http 服务语法参数的解释:
NameVirtualHost 192.168.0.254:80
通告虚拟主机在那个服务器上面以及那个端口,
第一个虚拟主机的站点,
DocumentRoot /var/www/virt1
虚拟主机站点的根目录,
ServerName station1.example.com
定义虚拟主机站点的域名,
ServerAlias server1.example.com
定义一个虚拟主机域名的别名,
定义虚拟主机站点的访问控制,
options -Indexes -Followsymlinks
去掉Indexes 和Followsymlinks 的功能,
order allow,deny
allow from all
deny from 192.168.0.10
允许所有,拒绝特定的主机。(这个是有顺序的)
order deny,allow
deny from all
allow from 192.168.0.10
拒绝所有,允许特定的主机。(这个是有顺序的)
这个是语法,与
这个是语法,与
station1.example.com 这个虚拟站点,拒绝192.168.0.10这台主机访问station1.example.com 这个虚拟站点。还定义了拒绝所有主机访问www1.example.com 这个虚拟站点,允许192.168.0.10这台主机访问www1.example.com 这个虚拟站点。
首先,重启下http 服务,
[root@server1 ~]#
,[root@server1 ~]# service httpd restart
Stopping httpd: [ OK ] Starting httpd: [ OK ]
[root@server1 ~]#
OK ,服务启动成功了,测试下。
现在我们到192.168.0.10这台主机上面去测试。
我们到浏览器里面输入

station1.example.com
可以看到,192.168.0.10的确不可以访问到station1.example.com 这个虚拟站点。 现在我们在到浏览器里面输入www1.example.com
,
可以看到,192.168.0.10的确是可以访问到www1.example.com 这个虚拟站点的。 现在我们在到192.168.0.254上面来测试一下,它应该可以访问
station1.example.com 这个虚拟站点,应该不可以访问www1.example.com 。 我们到浏览器里面输入station1.example.com ,