保姆级讲解 Apache服务器的配置与管理

保姆级讲解 Apache服务器的配置与管理Apache 或 httpd 服务 是 Internet 上使用最多的 Web 服务器技术之一 通俗来讲就是一个用于搭建网站的服务

大家好,欢迎来到IT知识分享网。

一、什么是Apache?

  • http:超文本传输协议,通过线路以明文形式发送,默认使用80端口/TCP
  • https:经TLS/SSL安全加密的超文本传输协议,默认使用443端口/TCP

二、Apache的配置文件

1、配置文件的位置

 配置文件 存放位置 服务目录 /etc/httpd 主配置文件 /etc/httpd/conf/httpd.conf 虚拟主机的配置文件目录 配置文件 存放位置 服务目录 /etc/httpd 主配置文件 /etc/httpd/conf/httpd.conf 虚拟主机的配置文件目录 /etc/httpd/conf.d 基于用户的配置文件 /etc/httpd/conf.d/userdir.conf 日志文件目录 /etc/httpd/logs 默认的网站数据目录 /var/www/html

2、主配置文件的重要参数

主配置文件:/etc/httpd/conf/httpd.conf 参数 作用 参数 作用 serverRoot 服务目录 Servername 网站服务器的域名 Listen 监听的IP地址端口号 DocumentRoot 默认网站数据目录 User 运行服务的用户 Directory 文件目录权限 Group 运行服务的用户组 DirectoryIndex 默认的索引页面 Serveradmin 管理员邮箱 ErrorLog 错误日志文件 

三、如何搭建Apache服务器

基本环境:主机名、网卡网络、yum源

1、更改主机名

2、配置网络

3、配置yum源

1、搭建简单的httpd服务 1.1、安装Apache服务 [root@ayaka ~]# yum install -y http 1.2、关闭防火墙 [root@ayaka ~]# systemctl stop firewalld 1.3启动Apache服务 [root@ayaka ~]# systemctl restart httpd 访问Apche网站 [root@ayaka ~]# curl 192.168.123.101 

2、搭建基于用户的个人网站

·首先确定已经安装了httpd服务、

2.1.新建用户(用于基于该用户)

[root@localhost ~]# useradd ayaka

2.2.创建个人的网页文件

2.3.修改用户网页文件的访问权限

[root@localhost ~]# chmod -R 705 /home/ayaka

2.4.修改基于用户的配置文件.

2.5.关闭防火墙修改selinux权限

2.6.重启服务

[root@localhost public_html]# systemctl restart httpd

2.7.访问网页

3、搭建基于域名访问的虚拟主机

以“www.toto.com”为域名来创建一个虚拟网站
1.网站数据存放在/www/toto/下
2.网站主页内容为:“welcome to toto’s website”
3.网站对所有客户端开放


#老样子 首先还是确认安装了httpd服务

3.1、创建虚拟主机的网页文件

[root@localhost public_html]# mkdir /www/toto -p

3.2、修改文件的访问权限(使其它用户具有可执行权力)

3.3、配置虚拟主机的网页文件

[root@localhost toto]# cd /etc/httpd/conf.d [root@localhost conf.d]# vim toto.conf <Virtualhost 192.168.123.101> ServerName www.toto.com //定义域名 DocumentRoot /www/toto //网站主页文件的目录 <Directory /www/toto> require all granted //所有客户端都可以访问 </Directory> </Virtualhost> 

~

3.4、做域名解析文件

3.5、配置防火墙和selinux

3.6、重启服务

4、搭建基于端口访问的虚拟主机

4.1、新建虚拟主机的网页文件

[root@localhost conf.d]# mkdir /www/8088 -p [root@localhost conf.d]# mkdir /www/8089 -p [root@localhost conf.d]# cd /www/8088 [root@localhost 8088]# echo "this is a new port 8088 for www.toto.com" >>index.html [root@localhost 8088]# cd /www/8089 [root@localhost 8089]# echo "this is a new port 8089 for www.toto.com" >>index.html 

4.2、修改文件的访问权限

4.3、配置虚拟主机的文件

[root@localhost conf.d]# vim 8088.conf <Directory /www/8088/> Require all granted </Directory> <VirtualHost 192.168.123.101:8088> DocumentRoot /www/8088 Servername www.toto.com </VirtualHost> <VirtualHost 192.168.123.101:8089> DocumentRoot /www/8089 ServerName www.toto.com </VirtualHost> 

4.4、添加监听端口

4.5、添加新的端口到防火墙(前面只是添加了服务,并没有添加新的端口)

4.6、重启服务

systemctl restart httpd

5、搭建网站并完成认证

5.1、新建虚拟机的网页文件

5.2、修改文件的访问权限

5.3、修改主文件

需要修改的参数

119 DocumentRoot "/www" 124 <Directory "/www"> 131 <Directory "/www"> 在服务目录的最后添加认证信息 355 <VirtualHost 192.168.123.101:80> 356 ServerName www.basic.com 357 DocumentRoot /www/basic 358 <Directory /www/basic> 359 AuthType basic 360 Authname passwd 361 AuthUserfile /etc/httpd/webpasswd 362 require user webuser1 363 </Directory> 364 365 </VirtualHost 

5.4 创建用户和认证文件

[root@www ~]# htpasswd -c /etc/httpd/webpasswd webuser1 //创建认证用户webuser1 New password: //输入密码 Re-type new password: //输入密码 Adding password for user webuser1 [root@www ~]# htpasswd /etc/httpd/webpasswd webuser2 New password: //输入密码 Re-type new password: //输入密码 Adding password for user webuser2 

5.4 关闭防护墙

[root@www ~]# systemctl stop firewalld [root@www ~]# setenforce 0 

5.5、重启服务

[root@www ~]# systemctl restart httpd 

5.6、测试:

字符界面

[root@www ~]# curl www.basic.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> 
  • 输入用户密码
    在这里插入图片描述

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/133295.html

(0)
上一篇 2025-07-24 18:20
下一篇 2025-07-24 18:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信