【Linux】练习---简单搭建HTTP服务器

实验一:

 要求搭建web服务器,能够访问到页面内容为“小胖,你咋这么胖呢!”

1、安装httpd包

[root@localhost ~]# yum install -y httpd
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:8:14:10 前,执行于 2020年10月28日 星期三 14时12分09秒。
Package httpd-2.4.37-21.module_el8.2.0+494+1df74eae.x86_64 is already installed.
依赖关系解决。
无需任何处理。
完毕!

2、查看是否成功安装httpd包

[root@localhost ~]# rpm -q httpd
httpd-2.4.37-21.module_el8.2.0+494+1df74eae.x86_64

3、定义基于IP访问的网站配置文件(vhost-pangpang.conf)

[root@localhost ~]# vim /etc/httpd/conf.d/vhost-pangpang.conf

<VirtualHost 192.168.74.130:80>                    //可让192.168.74.130主机访问80端口
        DocumentRoot /www/pangpang                 //网页文件主目录
        ServerName 192.168.74.130                  //主机名
</VirtualHost>                                     //结束标签

<directory /www>                                   //对/www目录的权限标签
        AllowOverride none                         //不允许被覆盖
        Require all granted                        //允许所有人访问/www
</directory>                                       //结束标签

4、创建网页文件根目录

[root@localhost ~]# mkdir /www/pangpang

5、定义网页内容

[root@localhost ~]# echo 小胖,你咋这么胖呢!> /www/pangpang/index.html

 6、关闭防火墙和selinux安全机制

[root@localhost ~]# systemctl stop firewalld                      //关闭防火墙
[root@localhost ~]# setenforce 0                                  //关闭selinux安全机制
[root@localhost ~]# systemctl status firewalld                    //查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor pres>
   Active: inactive (dead)
     Docs: man:firewalld(1)
lines 1-4/4 (END)
^C
[root@localhost ~]# getenforce                                   //查看selinux安全机制状态
Permissive

7、启动httpd服务

[root@localhost ~]# systemctl restart httpd                   //重启httpd服务
[root@localhost ~]# systemctl status httpd                    //查看http服务状态
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-10-28 21:50:30 CST; 52min ago
     Docs: man:httpd.service(8)
 Main PID: 2043 (httpd)
   Status: "Total requests: 13; Idle/Busy workers 100/0;Requests/sec: 0.00415; Bytes served/sec:   2 B/sec"
    Tasks: 213 (limit: 11367)
   Memory: 25.3M
   CGroup: /system.slice/httpd.service
           ├─2043 /usr/sbin/httpd -DFOREGROUND
           ├─2044 /usr/sbin/httpd -DFOREGROUND
           ├─2045 /usr/sbin/httpd -DFOREGROUND
           ├─2046 /usr/sbin/httpd -DFOREGROUND
           └─2047 /usr/sbin/httpd -DFOREGROUND

10月 28 21:50:30 www.openlab1.com systemd[1]: Starting The Apache HTTP Server...
10月 28 21:50:30 www.openlab1.com httpd[2043]: AH00112: Warning: DocumentRoot [/www/xiaopang] does not exist
10月 28 21:50:30 www.openlab1.com httpd[2043]: AH00112: Warning: DocumentRoot [/www/dapang] does not exist
10月 28 21:50:30 www.openlab1.com systemd[1]: Started The Apache HTTP Server.
10月 28 21:50:30 www.openlab1.com httpd[2043]: Server configured, listening on: port 80

8、测试

 

 

实验二:

  要求搭建web服务器,创建基于域名的虚拟主机,能够使用www.xiaopang.com和www.dapang.com访问各自的网站,网站存放路径分别为/xiaopang和/dapang,内容自定。

1、查看是否安装httpd包

[root@localhost ~]# rpm -q httpd
httpd-2.4.37-21.module_el8.2.0+494+1df74eae.x86_64

2、定义基于域名访问的网站配置文件(vhost-daxiaopang.conf)

[root@localhost ~]# vim /etc/httpd/conf.d/vhost-daxiaopang.conf

<VirtualHost 192.168.74.128:80>                      //允许192.168.74.128访问80端口
        DocumentRoot /www/xiaopang                   //此网站网页文件主目录
        ServerName www.xiaopang.com                  //主机域名
</VirtualHost>                                       //结束标签

<VirtualHost 192.168.74.129:80>                      //允许192.168.74.129访问80端口
        DocumentRoot /www/dapang                     //此网站网页文件主目录
        ServerName www.dapang.com                    //主机域名
</VirtualHost>                                       //结束标签

<directory /www>                                     //对/www目录的权限标签
        AllowOverride none                           //不允许覆盖
        Require all granted                          //允许所有人的访问
</directory>                                         //结束标签

3、创建网页文件根目录

[root@localhost ~]# mkdir /www/{xiaopang,dapang}

 4、定义网页内容

[root@localhost ~]# echo This is xiaopang > /www/xiaopang/index.html
[root@localhost ~]# echo This is dapang > /www/dapang/index.html

5、 关闭防火墙和selinux安全机制

[root@localhost ~]# systemctl stop firewalld                      //关闭防火墙
[root@localhost ~]# setenforce 0                                  //关闭selinux安全机制
[root@localhost ~]# systemctl status firewalld                    //查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor pres>
   Active: inactive (dead)
     Docs: man:firewalld(1)
lines 1-4/4 (END)
^C
[root@localhost ~]# getenforce                                   //查看selinux安全机制状态
Permissive

 6、启动httpd服务

[root@localhost ~]# systemctl restart httpd                   //重启httpd服务
[root@localhost ~]# systemctl status httpd                    //查看http服务状态
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-10-28 21:50:30 CST; 52min ago
     Docs: man:httpd.service(8)
 Main PID: 2043 (httpd)
   Status: "Total requests: 13; Idle/Busy workers 100/0;Requests/sec: 0.00415; Bytes served/sec:   2 B/sec"
    Tasks: 213 (limit: 11367)
   Memory: 25.3M
   CGroup: /system.slice/httpd.service
           ├─2043 /usr/sbin/httpd -DFOREGROUND
           ├─2044 /usr/sbin/httpd -DFOREGROUND
           ├─2045 /usr/sbin/httpd -DFOREGROUND
           ├─2046 /usr/sbin/httpd -DFOREGROUND
           └─2047 /usr/sbin/httpd -DFOREGROUND

10月 28 21:50:30 www.openlab1.com systemd[1]: Starting The Apache HTTP Server...
10月 28 21:50:30 www.openlab1.com httpd[2043]: AH00112: Warning: DocumentRoot [/www/xiaopang] does not exist
10月 28 21:50:30 www.openlab1.com httpd[2043]: AH00112: Warning: DocumentRoot [/www/dapang] does not exist
10月 28 21:50:30 www.openlab1.com systemd[1]: Started The Apache HTTP Server.
10月 28 21:50:30 www.openlab1.com httpd[2043]: Server configured, listening on: port 80

7、在虚拟机缓存添加域名解析信息(/etc/hosts)

[root@localhost ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.74.128 www.xiaopang.com                    //新添加
192.168.74.129 www.dapang.com                      //新添加

 8、在本地缓存添加域名解析信息(路径:C:\Windows\System32\drivers\etc\hosts)

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
192.168.74.128		www.xiaopang.com             //新添加
192.168.74.129		www.dapang.com               //新添加

 9、测试

 

实验完成!!!

猜你喜欢

转载自blog.csdn.net/trichloromethane/article/details/109346753