Apache服务的搭建与配置

发布于:2024-03-13 ⋅ 阅读:(55) ⋅ 点赞:(0)

一、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地址

本文含有隐藏内容,请 开通VIP 后查看