用apache搭建web服务器
一。基于域名的虚拟主机1,
[*]# uname -r
[*]2.6.32-71.el6.i686
[*]# cat /etc/redhat-release
[*]Red Hat Enterprise Linux Server release 6.0 (Santiago)
2,安装httpd
[*]yum install httpd -y
3,编辑配置文件
[*]vim /etc/httpd/conf/httpd.conf
[*]NameVirtualHost *:80将这一行的#去掉
[*] 复制这一行下面的7行 粘贴至最后(Y7Y P)
[*]修改成为
[*]
[*] ServerAdmin1@qq.com 管理员的邮箱
[*] DocumentRoot /var/www/html 发布目录
[*] ServerName www.test1.com 一个虚拟主机的域名
[*] (其余均为日志,可以不要)
[*]
[*]
[*] ServerAdmin1@qq.com
[*] DocumentRoot /www/virtual 另一个虚拟主机的发布目录
[*] ServerName www.test2.com 另一个虚拟主机的域名
[*]
[*]
[*]# mkdir /www/virtual -p
[*]# vi /www/virtual/index.html
[*]www.test2.com
[*]# vi /var/www/html/index.html
[*]www.test1.com
[*]# 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
[*]# chcon index.html -R -t httpd_sys_content_t /www修改安全上下文
[*]# /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
192.168.1.12 www.test1.com
192.168.1.12 www.test2.com
测试:在浏览器重输入www.test1.com会显示虚拟机1的发布目录里的东西
在浏览器重输入www.test2.com会显示虚拟机2的发布目录里的东西
二,用户认证
在/www/virtual目录下建secret目录,在secret目录下建index.html
[*]# vi /www/virtual/index.html
[*]Thisis a secret!
在发布目录为、www/virtal的块内的后面加上
[*]
[*] authuserfile /etc/httpd/conf/passwd
[*] authname"secret"
[*] authtype basic
[*] require valid-user
[*] requireuser 123
[*]
这样这个块就变成了
[*]
[*] ServerAdmin1@qq.com
[*] DocumentRoot /www/virtual
[*] ServerName www.test2.com
[*]
[*] authuserfile /etc/httpd/conf/passwd
[*] authname"secret"
[*] authtype basic
[*] require valid-user
[*] requireuser 123 用户名
[*]
[*]
[*]# htpasswd -cm /etc/httpd/conf/passwd 123
[*]给用户设置密码
[*]# /etc/init.d/httpd restart
在浏览器输入http://www.test2.com/secret/
输入用户,密码 访问,若没有权限,修改安全上下文
三。基于端口号的虚拟主机
因为在/etc/httpd/conf/httpd.conf 里包含 Include conf.d/*.conf,故在 /etc/httpd/conf.d目录下
vim port.conf
[*]# vim /etc/httpd/conf.d/port.conf
[*]Listen 81 开启端口81
[*]Listen 82开启82
[*]
[*]ServerName 81.qq.com 可以不要
[*]ServerAdmincc@qq.com省略日志文件
[*]DocumentRoot /www/virtual
[*]
[*]
[*]ServerName 81.qq.com
[*]ServerAdmincc@qq.com
[*]DocumentRoot /var/www/html
[*]
[*]# /etc/init.d/httpd restart
[*]Stopping httpd:
[*]Starting httpd: Warning: DocumentRoot 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
[*]
[*]# 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
[*]# chcon -R -t httpd_sys_content_t /www
[*]# /etc/init.d/httpd restart
[*]Stopping httpd:
[*]Starting httpd: (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
[*]
[*]# semanage port -a -t http_port_t-p tcp 81
[*]-bash: semanage: command not found 找不到这个命令
[*]# yum install policycoreutils-python -y 安装命令
[*]# 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
[*]# semanage port -a -t http_port_t-p tcp 81
[*]# semanage port -a -t http_port_t-p tcp 82
[*]# /etc/init.d/httpd restart
[*]Stopping httpd:
[*]Starting httpd: 好了
[*]在两个主机的目录里写上东西,浏览器访问
四。基于IP地址的虚拟主机
[*]# ifconfig eth0:1 192.168.122.111 netmask 255.255.255.0
[*]# ifconfig eth0:1 up临时添加子接口IP
[*]# vim /etc/httpd/conf/httpd.conf
[*]
[*] ServerAdmin cc@qq.com
[*] DocumentRoot /www/virtual
[*] ServerName 11.example.com
[*] ErrorLog logs/dummy-host.example.com-error_log
[*] CustomLog logs/dummy-host.example.com-access_log common
[*]
[*]
[*] ServerAdmin cc@qq.com
[*] DocumentRoot /var/www/html
[*] ServerName 111.example.com
[*] ErrorLog logs/dummy-host.example.com-error_log
[*] CustomLog logs/dummy-host.example.com-access_log common
[*]
[*]重启HTTP OK
cp ifconfig-eth0ifconfig-eth0:1
vi ifconfig-eth0:1
DEVICE=eth0:1
IPADDR=192.168.122.111
重启网卡即可这是永久添加网卡
页:
[1]