httpd服务的配置

简介: httpd服务的配置

httpd服务的配置

一、环境准备

我们需要两台虚拟机,而且都需要关闭防火墙和selinux

二、认识配置文件


// 虚拟机A:构建基本Web服务   
// 我们需要先装包
]# yum -y install httpd
]# rpm  -q  httpd
// 先进行简单的测试
// /var/www/html/index.html ,其中index.html是我们默认的主页
// /var/www/html 是我们默认的网页根目录
]# echo test >    /var/www/html/index.html
]# systemctl start httpd       #启动服务
]# curl  http://10.0.0.200    #测试访问
test
// 主配置文件:/etc/httpd/conf/httpd.conf
// 我们打开主配置文件
[root@tk ~]# cat /etc/httpd/conf/httpd.conf
// .... 
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
// 该处的Listen表示httpd服务监听的端口,我们可以配置多个Listen
Listen 80
// ... 
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
// 下面是httpd服务需要使用的用户和组
User apache
Group apache
// ...
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
// 该处的<Directory /> 里面写的是我们的web访问规则
// Directory 后面写的是网页存放路径,而且该规则是会继承的
// Require all denied     #拒绝所有人访问
//  Require all granted      #允许所有人访问
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>
// ... 
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
// 该处的DocumentRoot  表示网页的根目录,我们访问web的时候,此时就会到该目录下寻找index.html
DocumentRoot "/var/www/html"

// ...
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
// 该处是我们的错误日志的目录
ErrorLog "logs/error_log"

//....

三、虚拟Web主机


//  虚拟Web主机
//  由同一台服务器提供多个不同的Web站点
//  区分方式
//  基于域名的虚拟主机
//  基于端口的虚拟主机
//  基于IP地址的虚拟主机
// 配置文件路径
//  /etc/httpd/conf/httpd.conf  #主配置文件
//  /etc/httpd/conf.d/*.conf   #调用配置文件

// 为每个虚拟站点添加配置
// <VirtualHost   IP地址:端口>
//       ServerName  此站点的DNS名称
//       DocumentRoot  此站点的网页根目录
// </VirtualHost>

// 基于域名
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
// 修改nds解析,解析到本机
[root@tk ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6q
10.0.0.200 www.test1.com
// 书写测试的html
[root@tk ~]# echo "test1" > /var/www/test1/index.html
// 重启服务
[root@tk ~]# systemctl restart httpd
// 测试访问
[root@tk ~]# curl http://www.test1.com
test1
[root@tk ~]#

// 基于端口的虚拟Web主机
// 我们只需要将端口修改即可
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
<VirtualHost *:8080>
ServerName www.test1.com
DocumentRoot /var/www/test2
</VirtualHost>

[root@tk ~]#
// 注意:此时要在我们的主配置文件中开启8080端口
[root@tk ~]# mkdir /var/www/test2
[root@tk ~]# echo "test1:8080" > /var/www/test2/index.html
[root@tk ~]# systemctl restart httpd
test1:8080

// 基于IP进行配置

[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName 10.0.0.200
DocumentRoot /var/www/test1
</VirtualHost>
<VirtualHost *:8080>
ServerName 10.0.0.201
DocumentRoot /var/www/test2
</VirtualHost>
[root@tk ~]#
[root@tk ~]# systemctl restart httpd
[root@tk ~]# curl http://10.0.0.200
test1
[root@tk ~]# curl http://10.0.0.201
test1
[root@tk ~]# curl http://10.0.0.201:8080
test1:8080

相关文章
|
Oracle 安全 关系型数据库
搭建 OpenLDAP 自助修改密码系统
让修改open ldap密码变得简单
1542 0
搭建 OpenLDAP 自助修改密码系统
|
网络协议 Android开发 Python
Android 抓包工具r0capture使用
Android 抓包工具r0capture使用
1729 1
|
弹性计算 网络协议 安全
【图文教程】阿里云服务器开放端口设置(超详细)
阿里云服务器端口怎么打开?云服务器ECS端口在安全组中开启,轻量应用服务器端口在防火墙中打开,阿里云服务器网以80端口为例,来详细说下阿里云服务器端口开放图文教程,其他的端口如8080、3306、443、1433也是同样的方法进行开启端口:
40683 2
|
程序员 iOS开发 开发者
iOS开发:报错‘Unknown class ViewController in Interface Builder file’解决方法
在iOS开发过程中,会遇到一些比较常见的错误,尤其是刚入门的初级开发者,如果不熟练的话就会出错,本篇博文就来分享一个常见的问题,即报错‘Unknown class ViewController in Interface Builder file’的解决方法。
627 1
iOS开发:报错‘Unknown class ViewController in Interface Builder file’解决方法
|
存储 安全 API
Windows Server 2022 21H2 本地域权限提升漏洞(PetitPotam)
Windows Server 2022 Standard/Datacenter 存在本地域权限提升漏洞,攻击者可通过使用PetitPotam工具进行获取服务器SYSTEM权限。
881 1
|
安全 网络安全 Windows
Kali渗透测试:Metasploit 6.0 中的Evasion模块 原创
Kali渗透测试:Metasploit 6.0 中的Evasion模块 原创
179 0
|
Java 关系型数据库 MySQL
springboot学习五:springboot整合Mybatis 连接 mysql数据库
这篇文章是关于如何使用Spring Boot整合MyBatis来连接MySQL数据库,并进行基本的增删改查操作的教程。
2633 0
springboot学习五:springboot整合Mybatis 连接 mysql数据库
|
JavaScript 前端开发 安全
JavaScript中获取随机数的方法
JavaScript中获取随机数的方法
289 1
|
网络协议 网络安全 Python
电脑中 TCP/UDP 端口是否开放的测试:令人意想不到的神奇策略等你发现!
【8月更文挑战第19天】在网络管理和维护中,常需确认TCP/UDP端口是否开放以确保服务运行顺畅。端口如同计算机对外通信的“门”,TCP提供可靠连接,UDP则快速但无连接。测试端口是否开放的方法多样:可用`telnet`测试TCP端口,如`telnet localhost 80`;UDP测试较复杂,可用`nc`工具,如`nc -u -z localhost 53`。此外,也有在线工具可供选择,但需确保其安全性。
2561 1
|
数据安全/隐私保护 Docker 容器
error: Could not get shadow information for NOUSER 问题如何处理
【6月更文挑战第15天】error: Could not get shadow information for NOUSER 问题如何处理
1646 3