Centos 7搭建HTTP(2)虚拟主机功能

  1. 基于IP

一台服务器有多个ip地址,而每个地址都可与每个网站对应起来

10.1.82.100

www.csb01.com

10.1.82.110

www.csb02.com

10.1.82.120

www.csb03.com

 //DNS解析以及ip设置(ip可以参考子接口)
[root@localhost 桌面]# echo 10.1.82.100 www.csb01.com >> /etc/hosts & echo 10.1.82.110 www.csb02.com >> /etc/hosts & echo 10.1.82.120 www.csb03.com >> /etc/hosts & cat /etc/hosts | grep www.csb & ip a | grep ens36
[1] 37470
[2] 37471
[3] 37472
[4] 37474
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 10.1.82.10/24 brd 10.1.82.255 scope global ens36
    inet 10.1.82.100/8 brd 10.255.255.255 scope global ens36
    inet 10.1.82.110/8 brd 10.255.255.255 scope global secondary ens36
    inet 10.1.82.120/8 brd 10.255.255.255 scope global secondary ens36
[1]   完成                  echo 10.1.82.100 www.csb01.com >> /etc/hosts
[2]   完成                  echo 10.1.82.110 www.csb02.com >> /etc/hosts
[3]-  完成                  echo 10.1.82.120 www.csb03.com >> /etc/hosts
10.1.82.120 www.csb03.com
10.1.82.100 www.csb01.com
10.1.82.110 www.csb02.com
[4]+  完成                  cat /etc/hosts | grep --color=auto www.csb
            

 //前面的实验将我的根路径改为了/www/html
[root@localhost 桌面]# cd /www/html/

[root@localhost html]# mkdir csb01 csb02 csb03  &  ls
[1] 12149
csb01  csb02  csb03
[1]+  完成                  mkdir csb01 csb02 csb03
[root@localhost html]# echo "csb 10.1.82.100" >> csb01/index.html
[root@localhost html]# echo "csb 10.1.82.110" >> csb02/index.html
[root@localhost html]# echo "csb 10.1.82.120" >> csb03/index.html

                                    //主配置文件
vim /etc/http/conf/httpd.conf
<VirtualHost 10.1.82.100>
DocumentRoot /www/html/csb01
ServerName www.csb01.com
<Directory /www/html/csb01>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 10.1.82.110>
DocumentRoot /www/html/csb02
ServerName www.csb02.com
<Directory /www/html/csb02>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 10.1.82.120>
DocumentRoot /www/html/csb03
ServerName www.csb03.com
<Directory /www/html/csb01>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
                                //更改安全上下文
[root@localhost 桌面]# cd /www/html/ 
root@localhost html]# chcon -R --reference=/var/www/html *
[root@localhost html]# ll -ldZ *
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 csb01
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 csb02
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 csb03
[root@localhost html]# curl 10.1.82.100 & curl 10.1.82.110 & curl 10.1.82.120
[1] 39471
[2] 39472
csb 10.1.82.120
csb 10.1.82.110
csb 10.1.82.100
[1]-  完成                  curl 10.1.82.100
[2]+  完成                  curl 10.1.82.110
  1. 基于域名

[root@localhost html]# cat /etc/hosts | grep www
10.1.82.120 www.csb03.com www.csb01.com www.csb02.com

vim /etc/http/conf/httpd.conf
<VirtualHost 10.1.82.120>
DocumentRoot /www/html/csb01
ServerName www.csb01.com
<Directory /www/html/csb01>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 10.1.82.120>
DocumentRoot /www/html/csb02
ServerName www.csb02.com
<Directory /www/html/csb02>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 10.1.82.120>
DocumentRoot /www/html/csb03
ServerName www.csb03.com
<Directory /www/html/csb01>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

[root@localhost 桌面]# cd /www/html/ 
root@localhost html]# chcon -R --reference=/var/www/html *
[root@localhost html]# ll -ldZ *
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 csb01
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 csb02
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 csb03
  1. 基于端口

通过指定的端口来访问指定的网站

(1)在网站根目录下创建两个目录

[root@localhost html]# cd /www/html/
[root@localhost html]# mkdir 1111 222
[root@localhost html]# echo "woshi 1111" >> 1111/index.html
[root@localhost html]# echo "woshi 2222" >> 2222/index.html 

(2)在配置文件中添加监听端口

[root@localhost html]# cat /etc/httpd/conf/httpd.conf  | grep Listen |grep -Ev "#|$&"
Listen 80
Listen 1111
Listen 2222

(3)添加主配置文件

                        vim /etc/httpd/conf/httpd.conf
<VirtualHost 10.1.82.100:1111>
DocumentRoot "/www/html/1111"
ServerName www.csb01.com
<Directory /www/html/1111>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 10.1.82.100:2222>
DocumentRoot "/www/html/2222"
ServerName www.csb02.com
<Directory /www/html/2222>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

(4)允许http服务运行1111,2222端口

SELinux允许的HTTP服务的默认端口中没有1111和2222端口,所以我们要自己加上去

[root@localhost html]# semanage port -a -t http_port_t -p tcp 1111
[root@localhost html]# semanage port -a -t http_port_t -p tcp 2222

[root@localhost html]# semanage port -l | grep http_port_t
http_port_t                    tcp      2222, 1111, 80, 81, 443, 488, 8008, 8009, 8443, 9000

(5)配置安全上下文

[root@localhost html]# chcon -R --reference=/var/www/html *
[root@localhost html]# systemctl restart httpd

(6)网站测试

[root@localhost html]# curl 10.1.82.100:1111woshi 1111[root@localhost html]# curl 10.1.82.100:2222woshi 2222
  1. 基于用户

为每个用户建立一个属于用户的网站,如果使用虚拟主机的方式来建立多个网站,那么工作量就太大了,在用户运行时也会遇到各种的权限问题,而此功能可以让系统内的用户在自己的家目录下管理个人的网站。

(1)开启个人访问功能

UserDir 参数表示网站数据在用户家目录中的保存目录名称,即 share目录

[root@localhost chinaskills]# cat /etc/httpd/conf.d/userdir.conf | grep UserDir | grep -Ev "#|$&"
    UserDir enable            //enable是所有已创建的用户都能创建个人网站
    UserDir share

(2)用户建立用于保存网站数据的目录及首页面文件

cd /home/chinaskills/

 chown chinaskills:chinaskills public_html
 chmod 777 public_html/
 ls
 echo "who am i chinaskills " >> public_html/index.html 

(3)修改安全策略

[root@localhost chinaskills]# setsebool -P httpd_enable_homedirs=on 
[root@localhost chinaskills]# getsebool -a | grep httpd_enable_homedirs
httpd_enable_homedirs --> on
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值