这里说的是Apache基金会下的httpd web服务器组件,官网地址:http://httpd.apache.org/
Centos下操作Apache httpd_配置文件

【1】安装httpd

① 检测是否安装

Centos6下通常默认安装,Centos7未安装。

[root@janus www]# rpm -qa|grep httpd
httpd-tools-2.2.15-53.el6.centos.x86_64
httpd-2.2.15-53.el6.centos.x86_64

② yum命令安装

如果没有安装,则可以使用yum命令直接安装:

yum install httpd -y

当然也可以自行下载二进制包手动编译安装。

③ 操作命令

service httpd status    #查看服务状态
service httpd start #启动服务
service httpd stop #停止服务
service httpd restart #重启服务
systemctl enable httpd #Centos7下设置服务开机启动

另外,httpd默认监听80端口,所以需要保证80端口没有被占用,且被防火墙放开。或者,换个监听端口也可以。关于防火墙的操作可以参考博文CentOS7下操作iptables防火墙和firewalld防火墙 。


④ 关闭selinux

如果不关闭selinux,就要做针对性配置,否则会出现诸如403 Forbidden,或者you have no privileges…之类错误提示。

参考博文:Centos7下查看并关闭selinux


⑤ 浏览器访问默认页面

Centos下操作Apache httpd_centos_02


【2】创建用户家目录站点

Centos6下直接修改httpd.conf:

[root@janus www]# vim /etc/httpd/conf/httpd.conf
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
#UserDir disabled #这里注释掉

#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
#
UserDir public_html #这里放开注释
</IfModule>

设置每个用户 web 站点目录的访问权限:

<Directory "/home/*/public_html">
Options Indexes FollowSymLinks
AllowOverride None
Allow from all
</Directory>

创建用户并设置密码:

useradd jane
passwd jane

在用户家目录创建文件夹​​public_html​​并添加index.html即可访问!

su jane
mkdir public_html
cat > ./public_html/index.html
this is jane virtual host!

需要注意的是,若是提示404或者403,不妨查看下httpd的错误日志根据错误进行解决。如果遇到权限问题,可以使用如下命令添加执行权限:

chmod -R a+x /home/*

Centos7下主要步骤一致,区别是你用yum安装的httpd默认的httpd.conf中没有​​<IfModule mod_userdir.c>​​,但是有配置文件userdir.conf,在这里面修改即可!

浏览器访问实例如下:
Centos下操作Apache httpd_httpd_03


【3】配置基于同一IP不同端口的站点目录

需求:访问不同端口,展示不同站点目录的文件。

① 创建两个目录并分别在目录下创建文件index.html

mkdir /var/www/vhost-ip8000
mkdir /var/www/vhost-ip8888

将编辑好的index.html放到对应目录下。

② 修改httpd.conf

追加配置如下:

Listen 8000
Listen 8888

<VirtualHost 192.168.18.130:8000>
DocumentRoot /var/www/vhost-ip8000
</VirtualHost>

<VirtualHost 192.168.18.130:8888>
DocumentRoot /var/www/vhost-ip8888
</VirtualHost>

重启服务,即可访问测试!


【4】配置基于不同IP地址的虚拟主机

① 在/var/www下分别创建vhost-ip30 、vhost-ip40目录

Centos下操作Apache httpd_apache_04

② 分别在两个目录中创建对应的index.html

[root@janus www]# cat > vhost-ip30/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>virtual host 192.168.18.30</p>
</body>
</html>

[root@janus www]# cat > vhost-ip40/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>virtual host 192.168.18.40</p>
</body>
</html>
^C
[root@janus www]#

③ 设置网卡的别名,并为其分别设置ip地址

[root@janus www]#  ifconfig eth0:0 192.168.18.30 netmask 255.255.255.0 up
[root@janus www]# ifconfig eth0:1 192.168.18.40 netmask 255.255.255.0 up
[root@janus www]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:F7:4F:28
inet addr:192.168.18.130 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef7:4f28/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2780 errors:0 dropped:0 overruns:0 frame:0
TX packets:777 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:221194 (216.0 KiB) TX bytes:111819 (109.1 KiB)

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:F7:4F:28
inet addr:192.168.18.30 Bcast:192.168.18.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth0:1 Link encap:Ethernet HWaddr 00:0C:29:F7:4F:28
inet addr:192.168.18.40 Bcast:192.168.18.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:226 errors:0 dropped:0 overruns:0 frame:0
TX packets:226 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:11340 (11.0 KiB) TX bytes:11340 (11.0 KiB)

需要注意的是,这种方法设置别名,重启即会实效,有两种方案可以解决:

  • 1.将增加ip别名的命令填写到/etc/rc.local文件中
  • 2.手动编写ip别名的网卡配置文件
    所有网卡的配置文件都保存在/etc/sysconfig/network-scripts目录中,一个网卡对应一个配置文件,文件ifcfg-eth0就是本机网卡的配置文件,我们可以通过仿照这个文件的格式来编写其他ip别名的配置文件。

清除ip别名:

ifconfg eth0:0 down
ifconfg eth0:1 down

④ 编辑​​/etc/httpd/conf/httpd.conf​​配置文件

追加配置如下:

<VirtualHost 192.168.18.30>
DocumentRoot /var/www/vhost-ip30
</VirtualHost>

<VirtualHost 192.168.18.40>
DocumentRoot /var/www/vhost-ip40
</VirtualHost>

重启httpd服务,使用不同ip地址访问!
Centos下操作Apache httpd_centos_05
Centos下操作Apache httpd_html_06