Linux搭建web服务器

一、开启服务

1.安装软件包

#yum install -y httpd

2.关闭防火墙SELINUX,开启服务。

#systemctl stop firewalld
#setenforce 0
#systemctl start httpd

3.配置文件

http的配置文件都在/etc/httpd/

主配置文件:/etc/httpd/conf/

辅助(子)配置文件:/etc/httpd/conf.d/

模块组件配置文件:/etc/httpd/conf.modules.d

编辑主配置文件,只需将第98行取消注释,ServerName对应值修改为自己的IP。

#vim /etc/httpd/conf/httpd.conf

请添加图片描述

修改完配置文件后,要重启httpd

#systemctl restart httpd

二、实例一:通过多IP搭建多网站访问

1.创建多个IP

#nmcli c modify ens160 ipv4.addresses 192.168.220.129 ipv4.gateway 192.168.220.2 ipv4.dns 8.8.8.8 ipv4.method manual connection.autoconnect yes
#nmcli c modify ens160 +ipv4.addresses 192.168.220.131 +ipv4.gateway 192.168.220.2 +ipv4.dns 8.8.8.8 ipv4.method manual connection.autoconnect yes
#nmcli c modify ens160 +ipv4.addresses 192.168.220.132 +ipv4.gateway 192.168.220.2 +ipv4.dns 8.8.8.8 ipv4.method manual connection.autoconnect yes
#nmcli c up ens160

2.建立一个虚拟用户配置文件并编辑:

#vim /etc/httpd/conf.d/vhosts.conf

编辑内容如下:

###给目录/net的访问权限
<Directory /net>
        AllowOverride none
        Require all granted
</Directory>
###192.168.220.129
<VirtualHost 192.168.220.129:80>
        DocumentRoot /net/129
        ServerName 192.168.220.129
</VirtualHost>
###192.168.220.131
<VirtualHost 192.168.220.131:80>
        DocumentRoot /net/131
        ServerName 192.168.220.131
</VirtualHost>
###192.168.220.132
<VirtualHost 192.168.220.132:80>
        DocumentRoot /net/132
        ServerName 192.168.220.132
</VirtualHost>

重启服务

#systemctl restart httpd

创建对应文件夹和html文件

#mkdir -r /net/{129,131,133}
#echo this is 129 > /net/129/index.html
#echo this is 131 > /net/131/index.html
#echo this is 133 > /net/133/index.html

请添加图片描述
在这里插入图片描述
在这里插入图片描述

三、实例二:通过多端口搭建多网站访问

1.编辑虚拟用户配置文件:

#vim /etc/httpd/conf.d/vhosts.conf

编辑内容如下:

<Directory /net>
        AllowOverride none
        Require all granted
</Directory>
###129:8096
<VirtualHost 192.168.220.129:9999>
        DocumentRoot /net/9999
        ServerName 192.168.220.129
</VirtualHost>
<VirtualHost 192.168.220.129:8096>
        DocumentRoot /net/8096
        ServerName 192.168.220.129
</VirtualHost>
listen 9999
listen 8096

2.重启服务

#systemctl restart httpd

3.创建对应文件夹和html文件

#mkdir -r /net/{8096,9999}
#echo this is 8096 > /net/8096/index.html
#echo this is 9999 > /net/9999/index.html

在这里插入图片描述
在这里插入图片描述

四、实例三:通过域名搭建网站访问

1.编辑虚拟用户配置文件:

#vim /etc/httpd/conf.d/vhosts.conf

编辑内容如下:

<Directory /net>
        AllowOverride none
        Require all granted
</Directory>
<VirtualHost 192.168.220.129:80>
        DocumentRoot /net/xixi
        ServerName www.xixi.com
</VirtualHost>
<VirtualHost 192.168.220.129:80>
        DocumentRoot /net/haha
        ServerName www.haha.com
</VirtualHost>

2.重启服务

#systemctl restart httpd

3.创建对应文件夹和html文件

#mkdir -r /net/{xixi,haha}
#echo this is xixi > /net/xixi/index.html
#echo this is haha > /net/haha/index.html

4.编辑Windows下的hosts域名解析文件(C:\Windows\System32\drivers\etc\hosts),指定xixi和haha对应自己的IP,如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 7
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值