HTTP服务器搭建

Posted 坏坏-5

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP服务器搭建相关的知识,希望对你有一定的参考价值。


实验环境说明

主机系统系统版本IP地址
服务端LinuxRHEL 7.0192.168.43.128/24
客户端LinuxRHEL 7.4192.168.43.15/24
客户端Windows 11Windows 11192.168.43.1/24

服务器配置

下载安装HTTP服务

  • 下载安装Apache
[root@Server ~]# yum -y install httpd
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Base                                                  | 4.1 kB     00:00     
(1/2): Base/group_gz                                    | 137 kB   00:00     
(2/2): Base/primary_db                                  | 4.0 MB   00:00     
Resolving Dependencies

...

Installed:
  httpd.x86_64 0:2.4.6-67.el7                                                

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                apr-util.x86_64 0:1.5.2-6.el7      
  httpd-tools.x86_64 0:2.4.6-67.el7       mailcap.noarch 0:2.1.41-2.el7      

Complete!
[root@Server ~]# 
  • 防火墙放行HTTP服务
[root@Server conf]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcp dhcpv6-client samba ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@Server conf]# firewall-cmd --permanent --add-service="http"  //防火墙放行服务
success
[root@Server conf]# firewall-cmd --reload   //重新加载防火墙规则
success
[root@Server conf]# firewall-cmd --list-all  //列出防火墙的放行列表
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcp dhcpv6-client http samba ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@Server conf]# 
  • 设置HTTP服务开机自启并启动HTTP服务
[root@Server conf]# systemctl enable httpd  //设置开机自启动
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[root@Server conf]# systemctl restart httpd  //重新启动HTTP服务
[root@Server conf]# systemctl status httpd  //查看HTTP服务状态
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Fri 2021-12-24 10:31:41 CST; 10s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 5230 ExecStop=/bin/kill -WINCH $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 5274 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─5274 /usr/sbin/httpd -DFOREGROUND
           ├─5275 /usr/sbin/httpd -DFOREGROUND
           ├─5276 /usr/sbin/httpd -DFOREGROUND
           ├─5277 /usr/sbin/httpd -DFOREGROUND
           ├─5278 /usr/sbin/httpd -DFOREGROUND
           └─5279 /usr/sbin/httpd -DFOREGROUND

Dec 24 10:31:21 Server systemd[1]: Starting The Apache HTTP Server...
Dec 24 10:31:31 Server httpd[5274]: AH00557: httpd: apr_sockaddr_info_ge...er
Dec 24 10:31:31 Server httpd[5274]: AH00558: httpd: Could not reliably d...ge
Dec 24 10:31:41 Server systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@Server conf]# 
  • 在客户端进行访问测试
    • 如果没有安装图形化界面,可以使用命令curl 192.168.43.128来进行访问
    • 如果安装了图形化界面,则可以直接通过服务器的IP地址进行访问
[root@Server conf]# curl 192.168.43.128
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<title>Test Page for the Apache HTTP Server on Red Hat Enterprise Linux</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<style type="text/css">
			/*<![CDATA[*/

....

			<div class="content-columns">
				<div class="content-column-left">
					<h2>If you are a member of the general public:</h2>

					<p>The fact that you are seeing this page indicates that the website you just visited is either experiencing problems, or is undergoing routine maintenance.</p>

					<p>If you would like to let the administrators of this website know that you've seen this page instead of the page you expected, you should send them e-mail. In general, mail sent to the name "webmaster" and directed to the website's domain should reach the appropriate person.</p>

					<p>For example, if you experienced problems while visiting www.example.com, you should send e-mail to "webmaster@example.com".</p>

					<p>For information on Red Hat Enterprise Linux, please visit the <a href="http://www.redhat.com/">Red Hat, Inc. website</a>. The documentation for Red Hat Enterprise Linux is <a href="http://www.redhat.com/docs/manuals/enterprise/">available on the Red Hat, Inc. website</a>.</p>
					<hr />
				</div>

				<div class="content-column-right">
					<h2>If you are the website administrator:</h2>

					<p>You may now add content to the directory <tt>/var/www/html/</tt>. Note that until you do so, people visiting your website will see this page, and not your content. To prevent this page from ever being used, follow the instructions in the file <tt>/etc/httpd/conf.d/welcome.conf</tt>.</p>

                                        <p>You are free to use the image below on web sites powered by the Apache HTTP Server:</p>
					
                                        <p align="center"><a href="http://httpd.apache.org/"><img src="/icons/apache_pb2.gif" alt="[ Powered by Apache ]"/></a></p>

				</div>
			</div>
		</div>
	</body>
</html>

  • 可以看到以上界面(默认界面),则说明HTTP服务已经安装成功,接下来需要修改配置文件

修改配置文件

  • 修改网页头文件路径
[root@Server ~]# cd /etc/httpd/conf/
[root@Server conf]# mv httpd.conf httpd.conf.bak   //将配置文件做备份
[root@Server conf]# cat httpd.conf.bak | grep -v "#" | grep -v "^$" > httpd.conf
[root@Server conf]# vim httpd.conf
ServerRoot "/etc/httpd"

Listen 80  //HTTP服务端口

Include conf.modules.d/*.conf

User apache
Group apache


ServerAdmin root@localhost


<Directory />
    AllowOverride none
    Require all denied
    
#DocumentRoot "/var/www/html"
DocumentRoot "/var/www/Bad"  //网页头文件位置

...

[root@Server conf]# cd /var/www
[root@Server www]# mkdir Bad
  • 运行虚拟主机配置
[root@Server www]# rpm -ql httpd | grep vhosts  //查看虚拟主机软件包
/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
[root@Server www]# cp /usr/share//doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf
[root@Server www]# cd /etc/httpd/conf
[root@Server conf]# ls
httpd.conf  httpd.conf.bak  httpd-vhosts.conf  magic
[root@Server conf]# mv httpd-vhosts.conf httpd-vhosts.conf.bak
[root@Server conf]# cat httpd-vhosts.conf.bak | grep -v "#" > httpd-vhosts.conf
[root@Server conf]# vim httpd-vhosts.conf
[root@Server conf]# vim httpd-vhosts.conf
#<VirtualHost *:@@Port@@>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
#    ServerName dummy-host.example.com
#    ServerAlias www.dummy-host.example.com
#    ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
#    CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
#</VirtualHost>

<VirtualHost www.bad.boy.com>      //配置虚拟域名,也可以配置为访问端口
    DocumentRoot /var/www/Bad      //网页头文件的目录
    ServerName www.bad.boy.com     //域名地址
</VirtualHost>
[root@Server conf]#
  • 将网页文件通过Xftp或其他远程软件上传至服务器的/var/www/Bad目录下
  • 重新启动HTTP服务
[root@Server ~]# systemctl restart httpd
[root@Server ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Fri 2021-12-24 12:21:50 CST; 29s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 8406 ExecStop=/bin/kill -WINCH $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 8410 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─8410 /usr/sbin/httpd -DFOREGROUND
           ├─8411 /usr/sbin/httpd -DFOREGROUND
           ├─8412 /usr/sbin/httpd -DFOREGROUND
           ├─8413 /usr/sbin/httpd -DFOREGROUND
           ├─8414 /usr/sbin/httpd -DFOREGROUND
           └─8415 /usr/sbin/httpd -DFOREGROUND

Dec 24 12:21:30 Server systemd[1]: Starting The Apache HTTP Server...
Dec 24 12:21:40 Server httpd[8410]: AH00557: httpd: apr_sockaddr_info_ge...er
Dec 24 12:21:40 Server httpd[8410]: AH00558: httpd: Could not reliably d...ge
Dec 24 12:21:50 Server systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@Server ~]#

客户端测试

Linux客户端测试

  • 使用IP地址进行访问测试
  • 使用域名进行访问测试
  • 会出现无法访问的提示,这是因为客户端无法解析www.bad.boy.com这个域名,因为还没有搭建【DNS服务器】解析域名,所以需要做一个本地映射,将域名映射为服务器的IP地址
  • Linux客户端配置本地映射
[root@Client ~]# vim /etc/hosts
[root@Client ~]# tail -1 /etc/hosts
192.168.43.128 www.bad.boy.com
[root@Client ~]#
  • 重新使用域名在Linux客户端进行访问测试

Windows客户端测试

  • 使用IP进行访问测试
  • 直接使用域名访问,也会出现无法访问的错误
  • 在Windows客户端同样也需要修改IP与域名的映射关系才能正常使用域名进行访问
    • 在主机的C:\\Windows\\System32\\drivers\\etc目录下,将hosts移动到桌面上,进行修改,添加192.168.43.128 www.bad.boy.com保存
  • 重新使用域名在Windows客户端进行访问测试
  • HTTP基本配置完成,另外也可以修改HTTP的访问端口,可以使用IP/域名+端口号进行访问。其他详细的配置,可以参考帮助手册。

以上内容均属原创,如有不详或错误,敬请指出。

以上是关于HTTP服务器搭建的主要内容,如果未能解决你的问题,请参考以下文章

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

HTTP客户端代码片段

代码适用于与单个 html 文件不同的堆栈片段

Node.js学习4~搭建web服务的2种常见方式,附上代码和运行效果

在PaddlePaddle中的Notebook代码片段

XAMPP本地服务器搭建好了,就是不会访问