Apache服务的搭建与配置

一、apache安装

复制代码
systemctl stop firewalld

systemctl disable  firewalld

setenforce 0

yum -y install httpd

systemctl start httpd

netstat -ntlp | grep 80

二、认识主配置文件

# vim /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd" #定义工作目录
Listen 80 #监听端口
Listen 192.168.2.8:80 指定监听的本地网卡 可以修改
User apache # 子进程的用户,有可能被人改称www账户
Group apache # 子进程的组
ServerAdmin root@localhost # 设置管理员邮件地址
DocumentRoot "/var/www/html" # 发布网站的默认目录,想改改这里。
IncludeOptional conf.d/*.conf # 包含conf.d目录下的所有*.conf配置文件

# 设置DocumentRoot指定目录的属性
<Directory "/var/www/html"> # 网站容器开始标识
Options Indexes FollowSymLinks # 找不到主页时,链接到网站目录以外,如测试页面
AllowOverride None # 对网站设置特殊属性:none不设置特殊属性,all允许
Require all granted # granted表示允许所有人访问,denied表示拒绝所有人访问
</Directory> # 容器结束
DirectoryIndex index.html # 定义主页文件,会自动访问该文件。

三、访问控制

1.准备测试页面

复制代码
echo test1 > /var/www/html/index.html   #测试文件编写

2.访问控制测试

2.1.默认允许所有主机访问
复制代码
vim  /etc/httpd/conf/httpd.conf
复制代码
systemctl  restart httpd   #重启
2.2.只拒绝一部分客户端访问
复制代码
vim /etc/httpd/conf/httpd.conf
复制代码
curl -I http://127.0.0.1 #用另外一台机器测试访问成功得到以下内容
2.3.拒绝所有人
复制代码
vim /etc/httpd/conf/httpd.conf
2.4.修改默认网站发布目录
复制代码
vim /etc/httpd/conf/httpd.conf


127  <DocumentRoot "/www">            							# 修改网站根目录为/www
134  <Directory "/www">               							# 把这个也对应的修改为/www

四、虚拟主机

虚拟主机:将多个网站放在同一台服务器上。web服务器都可以实现

接下来以三种情况为例:基于域名、基于端口、基于ip

4.1.基于域名

复制代码
cd /etc/httpd/conf.d/
vim test.conf   #创建配置文件

<VirtualHost *:80> #指定虚拟主机端口,*代表监听本机所有ip,也可以指定ip
DocumentRoot /soso #指定发布网站目录,自己定义
ServerName www.soso666.com #指定域名,可以自己定义
<Directory "/soso/">
AllowOverride None #设置目录的特性,不设置目录的特性
Require all granted #允许所有人访问
</Directory>
</VirtualHost>

复制代码
mkdir /soso #创建发布目录
mkdir /soho
echo ceshi > /soso/index.html #创建测试页面
echo ceshi > /soho/index.html
systemctl restart httpd
配置域名解析

在windows电脑上面打开C:\Windows\System32\drivers\etc\hosts文件。可以用管理员身份打开

打开后在最后加入ip和域名

例如:

192.168.111.111 www.soso666.com

192.168.111.111 test.soso666.com

4.2.基于端口

复制代码
vim /etc/httpd/conf/httpd.conf  #添加监听端口
复制代码
vim /etc/httpd/conf.d/test.conf

<VirtualHost *:80>
DocumentRoot /soso
ServerName www.soso666.com
<Directory "/soso/">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:81> #修改端口
DocumentRoot /soho
ServerName test.soso666.com
<Directory "/soho/">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

systemctl restart httpd
注意:域名解析并没有变

4.3.基于IP

复制代码
ifconfig ens33:0 192.168.153.123  #添加一个临时ip
ip addr add 192.168.153.123/24  dev  eth0   #添加一个临时ip

#共两种添加ip方法,推荐使用第二种


vim /etc/httpd/conf.d/test.conf

<VirtualHost 192.168.153.144:80> #指定ip
DocumentRoot /soso
#ServerName www.soso666.com
<Directory "/soso/">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.153.123:80> #指定ip
DocumentRoot /soho
#ServerName test.soso666.com
<Directory "/soho/">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

复制代码
systemctl restart httpd

ifconfig ens33:0 192.168.153.123 down    #取消添加的ip地址
ip addr del 192.168.153.123/24 dev eth0   #取消添加的ip地址
相关推荐
软件派4 小时前
Apache Paimon终极教程——流批一体存储引擎深度解析(附Flink集成案例+性能调优代码)
apache·性能调优·流批一体·实时数据处理·paimon教程·flink集成·湖仓架构
三水不滴5 小时前
Apache RocketMQ的原理与实践
经验分享·apache·rocketmq
whale fall20 小时前
celery -A tool.src.main worker --loglevel=info --queues=worker1_queue & 什么意思
python·学习·apache
TracyCoder1231 天前
ElasticSearch核心引擎Apache Lucene(五):相关性算分 (Scoring)
elasticsearch·apache·lucene
码上上班1 天前
一文学会apache httpd
apache
野生技术架构师1 天前
Spring Boot 3 集成 Apache Calcite:多数据源查询的终极解决方案
spring boot·后端·apache
TracyCoder1232 天前
ElasticSearch核心引擎Apache Lucene(四):段 (Segment) 的设计与合并
elasticsearch·apache·lucene
TracyCoder1232 天前
ElasticSearch核心引擎Apache Lucene(三):数值与空间数据索引
elasticsearch·apache·lucene
Elastic 中国社区官方博客2 天前
Elasticsearch:Apache Lucene 2025 年终总结
大数据·人工智能·elasticsearch·搜索引擎·apache·lucene
TracyCoder1232 天前
ElasticSearch核心引擎Apache Lucene(二):正排索引的奥秘
elasticsearch·apache·lucene