搭建服务
1、 首先下载httpd服务
[root@redhat ~]# dnf install 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.
Repository epel is listed more than once in the configuration
Last metadata expiration check: 1:35:46 ago on Tue 17 Aug 2021 12:28:59 PM CST.
Dependencies resolved.
================================================================================================
Package Arch Version Repository Size
================================================================================================
Installing:
httpd x86_64 2.4.37-39.module_el8.4.0+778+c970deab AppStream 1.4 M
Installing dependencies:
apr x86_64 1.6.3-11.el8 AppStream 125 k
apr-util x86_64 1.6.1-6.el8 AppStream 105 k
centos-logos-httpd noarch 85.8-1.el8 baseOS 75 k
httpd-filesystem noarch 2.4.37-39.module_el8.4.0+778+c970deab AppStream 38 k
httpd-tools x86_64 2.4.37-39.module_el8.4.0+778+c970deab AppStream 106 k
mod_http2 x86_64 1.15.7-3.module_el8.4.0+778+c970deab AppStream 154 k
Installing weak dependencies:
apr-util-bdb x86_64 1.6.1-6.el8 AppStream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 AppStream 27 k
Transaction Summary
================================================================================================
Install 9 Packages
Total download size: 2.0 M
Installed size: 5.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/9): apr-util-1.6.1-6.el8.x86_64.rpm 241 kB/s | 105 kB 00:00
(2/9): centos-logos-httpd-85.8-1.el8.noarch.rpm 167 kB/s | 75 kB 00:00
(3/9): apr-1.6.3-11.el8.x86_64.rpm 261 kB/s | 125 kB 00:00
(4/9): apr-util-openssl-1.6.1-6.el8.x86_64.rpm 401 kB/s | 27 kB 00:00
(5/9): apr-util-bdb-1.6.1-6.el8.x86_64.rpm 164 kB/s | 25 kB 00:00
(6/9): httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.n 446 kB/s | 38 kB 00:00
(7/9): httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 884 kB/s | 106 kB 00:00
(8/9): mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64.rp 931 kB/s | 154 kB 00:00
(9/9): httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64.rpm 3.7 MB/s | 1.4 MB 00:00
------------------------------------------------------------------------------------------------
Total 2.0 MB/s | 2.0 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : apr-1.6.3-11.el8.x86_64 1/9
Running scriptlet: apr-1.6.3-11.el8.x86_64 1/9
Installing : apr-util-bdb-1.6.1-6.el8.x86_64 2/9
Installing : apr-util-openssl-1.6.1-6.el8.x86_64 3/9
Installing : apr-util-1.6.1-6.el8.x86_64 4/9
Running scriptlet: apr-util-1.6.1-6.el8.x86_64 4/9
Installing : httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 5/9
Running scriptlet: httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch 6/9
Installing : httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch 6/9
Installing : centos-logos-httpd-85.8-1.el8.noarch 7/9
Installing : mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64 8/9
Installing : httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 9/9
Running scriptlet: httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 9/9
Verifying : centos-logos-httpd-85.8-1.el8.noarch 1/9
Verifying : apr-1.6.3-11.el8.x86_64 2/9
Verifying : apr-util-1.6.1-6.el8.x86_64 3/9
Verifying : apr-util-bdb-1.6.1-6.el8.x86_64 4/9
Verifying : apr-util-openssl-1.6.1-6.el8.x86_64 5/9
Verifying : httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 6/9
Verifying : httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch 7/9
Verifying : httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 8/9
Verifying : mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64 9/9
Installed products updated.
Installed:
apr-1.6.3-11.el8.x86_64
apr-util-1.6.1-6.el8.x86_64
apr-util-bdb-1.6.1-6.el8.x86_64
apr-util-openssl-1.6.1-6.el8.x86_64
centos-logos-httpd-85.8-1.el8.noarch
httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64
httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch
httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64
mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64
Complete!
2、关闭防火墙
[root@redhat ~]# systemctl stop firewalld
3、启动服务
查看httpd状态(目前已经成功启动)
[root@redhat ~]# systemctl start httpd //启动
[root@redhat ~]# systemctl status httpd //查看状态
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-08-17 14:09:08 CST; 20s ago //running正在运行
Docs: man:httpd.service(8)
Main PID: 17932 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11076)
Memory: 42.9M
CGroup: /system.slice/httpd.service
├─17932 /usr/sbin/httpd -DFOREGROUND
├─17933 /usr/sbin/httpd -DFOREGROUND
├─17934 /usr/sbin/httpd -DFOREGROUND
├─17935 /usr/sbin/httpd -DFOREGROUND
└─17936 /usr/sbin/httpd -DFOREGROUND
Aug 17 14:09:03 redhat systemd[1]: Starting The Apache HTTP Server...
Aug 17 14:09:08 redhat httpd[17932]: AH00558: httpd: Could not reliably determine the server's>
Aug 17 14:09:08 redhat systemd[1]: Started The Apache HTTP Server.
Aug 17 14:09:14 redh
案例一:
要求搭建web服务器,能够访问到网页内容为“小胖,你咋这么胖呢!”
更改配置实现访问资源
[root@redhat ~]# vim /etc/httpd/conf.d/vhost.conf
1、vhost.conf配置文件内容
<VirtualHost 192.168.12.130:80>
DocumentRoot /www/130
ServerName 192.168.12.130
</VirtualHost>
<Directory /www>
AllowOverride none
Require all granted
</Directory>
2、根据配置文件创建对应的资源(文件)
[root@redhat ~]# mkdir -p /www/130
[root@redhat ~]# echo ”小胖你,你咋这么胖呢!“ > /www/130/index.html
3、重启httpd服务
[root@redhat ~]# systemctl start httpd
4、浏览器查看配置的资源
案例二:
创建基于域名的虚拟主机,能够使用www.xiaopang.com和www.dapang.com访问各自的网站网站存放路径分别为/xiaopang和/dapang,内容自定。
1、配置文件
[root@redhat ~]# vim /etc/httpd/conf.d/vhost.conf
2、vhost.conf配置文件内容
#<VirtualHost 192.168.12.130:80>
# DocumentRoot /www/130
#ServerName 192.168.12.130
#</VirtualHost>
<VirtualHost 192.168.12.130:80>
DocumentRoot /www/xiaopang
ServerName www.xiaopang.com
</VirtualHost>
<VirtualHost 192.168.12.130:80>
DocumentRoot /www/dapang
ServerName www.dapang.com
</VirtualHost>
<Directory /www>
AllowOverride none
Require all granted
</Directory>
3、根据配置文件创建对应的资源(文件)
[root@redhat ~]# mkdir -p /www/{xiaopang,dapang}
[root@redhat ~]# echo this is xiaopang > /www/xiaopang/index.html
[root@redhat ~]# echo this is dapang > /www/dapang/index.html
4、重启httpd服务
[root@redhat ~]# systemctl restart httpd
5、临时关闭SELinux
设置SELinux 成为permissive模式 临时关闭selinux的
[root@redhat ~]# setenforce 0
6、进入我们的主 机配置
[root@redhat ~]# vim /etc/hosts
7、在里面添加我们的ip与域名
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.12.130 www.xiaopang.com
192.168.12.130 www.dapang.com
8、使用curl命令查看网页资源
[root@redhat ~]# curl www.xiaopang.com
this is xiaopang
[root@redhat ~]# curl www.dapang.com
this is dapang
如果要想在Windows的网页实现访问
则需要用nodepad++打开我们C:\Windows\System32\drivers\etc下的hosts文件,因为我们在对此文件进行操作时会需要权限而nodepad++刚好解决了这个问题,添加我们的IP 然后是对应的域名
网页输入域名结果如下