天天看点

用apache搭建web服务器

 一。基于域名的虚拟主机

1,

[root@localhost ~]# uname -r 

2.6.32-71.el6.i686 

[root@localhost ~]# cat /etc/redhat-release 

Red Hat Enterprise Linux Server release 6.0 (Santiago) 

2,安装httpd

yum install httpd -y 

vim /etc/httpd/conf/httpd.conf 

NameVirtualHost *:80将这一行的#去掉 

<VirtualHost *:80> 复制这一行下面的7行 粘贴至最后(Y7Y P) 

修改成为 

<VirtualHost *:80> 

    ServerAdmin  [email protected] 管理员的邮箱 

    DocumentRoot /var/www/html 发布目录 

    ServerName www.test1.com 一个虚拟主机的域名 

    (其余均为日志,可以不要) 

</VirtualHost> 

    ServerAdmin  [email protected] 

    DocumentRoot /www/virtual 另一个虚拟主机的发布目录 

    ServerName www.test2.com 另一个虚拟主机的域名 

[root@localhost virtual]# mkdir /www/virtual -p 

[root@localhost virtual]# vi /www/virtual/index.html 

www.test2.com 

[root@localhost virtual]# vi /var/www/html/index.html 

www.test1.com 

[root@localhost virtual]# ll -Z /var/www/html/ 查看这个目录的安全上下文

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 \ 

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html 

[root@localhost virtual]# chcon index.html -R -t httpd_sys_content_t /www  修改安全上下文

[root@localhost virtual]# /etc/init.d/httpd restart 重启服务 

WINDOW下

C:\Windows\System32\drivers\etc\hosts用记事本打开,添加

192.168.1.12 www.test1.com

192.168.1.12 www.test2.com

LINUX下 vi /etc/hosts

测试:在浏览器重输入www.test1.com会显示虚拟机1的发布目录里的东西

      在浏览器重输入www.test2.com会显示虚拟机2的发布目录里的东西

二,用户认证

在/www/virtual目录下建secret目录,在secret目录下建index.html

[root@localhost ~]# vi /www/virtual/index.html 

This  is a secret! 

在发布目录为、www/virtal的块内的后面加上

<Directory /www/virtual/secret> 

   authuserfile   /etc/httpd/conf/passwd 

   authname  "secret" 

   authtype    basic 

   require   valid-user 

   require  user 123 

 </Directory> 

这样这个块就变成了

    DocumentRoot /www/virtual 

    ServerName www.test2.com 

   <Directory /www/virtual/secret> 

    authuserfile   /etc/httpd/conf/passwd 

    authname  "secret" 

    authtype    basic 

    require   valid-user 

    require  user 123 用户名

  </Directory> 

[root@localhost ~]# htpasswd -cm /etc/httpd/conf/passwd 123 

给用户设置密码 

[root@localhost ~]# /etc/init.d/httpd restart 

输入用户,密码 访问,若没有权限,修改安全上下文

三。基于端口号的虚拟主机

因为在/etc/httpd/conf/httpd.conf 里包含 Include conf.d/*.conf,故在 /etc/httpd/conf.d目录下

vim port.conf

[root@vm1 www]# vim /etc/httpd/conf.d/port.conf 

Listen 81 开启端口81

Listen 82  开启82

<VirtualHost 192.168.122.11:81> 

ServerName 81.qq.com   可以不要

ServerAdmin  [email protected]  省略日志文件

DocumentRoot /www/virtual 

<VirtualHost 192.168.122.11:82 > 

ServerName 81.qq.com 

ServerAdmin  [email protected] 

DocumentRoot /var/www/html 

[root@vm1 ~]# /etc/init.d/httpd restart 

Stopping httpd:                                            [FAILED] 

Starting httpd: Warning: DocumentRoot [/www/virtual] does not exist 

(13)Permission denied: make_sock: could not bind to address [::]:81 

(13)Permission denied: make_sock: could not bind to address 0.0.0.0:81 

no listening sockets available, shutting down 

Unable to open logs 

                                                           [FAILED] 

[root@vm1 www]# ll -Z 

drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin 

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 error 

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html 

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 icons 

[root@vm1 www]# chcon -R -t httpd_sys_content_t /www 

[root@vm1 www]# /etc/init.d/httpd restart 

Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:81 

[root@vm1 www]# semanage port -a -t http_port_t  -p tcp 81 

-bash: semanage: command not found 找不到这个命令 

[root@vm1 www]# yum install policycoreutils-python -y 安装命令 

[root@vm1 www]# semanage port -l|grep http 

http_cache_port_t              tcp      3128, 8080, 8118, 10001-10010 

http_cache_port_t              udp      3130 

http_port_t                    tcp      80, 443, 488, 8008, 8009, 8443 

pegasus_http_port_t            tcp      5988 

pegasus_https_port_t           tcp      5989 

[root@vm1 www]# semanage port -a -t http_port_t  -p tcp 82 

Starting httpd:                                            [  OK  ] 好了 

在两个主机的目录里写上东西,浏览器访问 

四。基于IP地址的虚拟主机

[root@vm1 www]# ifconfig eth0:1 192.168.122.111 netmask 255.255.255.0 

[root@vm1 www]# ifconfig eth0:1 up  临时添加子接口IP 

[root@vm1 www]# vim /etc/httpd/conf/httpd.conf  

<VirtualHost 192.168.122.11:80> 

    ServerAdmin [email protected] 

    ServerName 11.example.com 

    ErrorLog logs/dummy-host.example.com-error_log 

    CustomLog logs/dummy-host.example.com-access_log common 

<VirtualHost 192.168.122.111:80> 

    DocumentRoot /var/www/html 

    ServerName 111.example.com 

重启HTTP OK 

cp ifconfig-eth0  ifconfig-eth0:1

vi ifconfig-eth0:1

DEVICE=eth0:1

IPADDR=192.168.122.111

重启网卡即可  这是永久添加网卡

本文转自 369蓝宝 51CTO博客,原文链接:http://blog.51cto.com/3739387/1154476,如需转载请自行联系原作者