小工具知识网

当前位置: 首页 > 行业知识 > Apache服务的搭建与配置

行业知识

Apache服务的搭建与配置

2026-11-16 02:53:57 来源:互联网转载

一、apache安装

systemctl stop firewalldsystemctl disable  firewalldsetenforce 0yum -y install httpdsystemctl start httpdnetstat -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指定目录的属性

          # 网站容器开始标识

Options Indexes FollowSymLinks       # 找不到主页时,链接到网站目录以外,如测试页面

AllowOverride None                   # 对网站设置特殊属性:none不设置特殊属性,all允许

Require all granted                 # granted表示允许所有人访问,denied表示拒绝所有人访问

                       # 容器结束

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.conf127              # 修改网站根目录为/www134                 # 把这个也对应的修改为/www

 

 四、虚拟主机

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

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

 4.1.基于域名

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

  #指定虚拟主机端口,*代表监听本机所有ip,也可以指定ip

DocumentRoot /soso     #指定发布网站目录,自己定义

ServerName www.soso666.com  #指定域名,可以自己定义

  AllowOverride None    #设置目录的特性,不设置目录的特性

  Require all granted   #允许所有人访问

mkdir /soso #创建发布目录mkdir /sohoecho ceshi > /soso/index.html #创建测试页面echo ceshi > /soho/index.htmlsystemctl 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

  DocumentRoot /soso

  ServerName www.soso666.com

  AllowOverride None

  Require all granted

  #修改端口

  DocumentRoot /soho

  ServerName test.soso666.com

  AllowOverride None

  Require all granted


systemctl restart httpd

注意:域名解析并没有变

4.3.基于IP 

ifconfig ens33:0 192.168.153.123  #添加一个临时ipip addr add 192.168.153.123/24  dev  eth0   #添加一个临时ip#共两种添加ip方法,推荐使用第二种vim /etc/httpd/conf.d/test.conf

  #指定ip

  DocumentRoot /soso

  #ServerName www.soso666.com

  AllowOverride None

  Require all granted

  #指定ip

  DocumentRoot /soho

  #ServerName test.soso666.com

  AllowOverride None

  Require all granted

 

systemctl restart httpdifconfig ens33:0 192.168.153.123 down    #取消添加的ip地址ip addr del 192.168.153.123/24 dev eth0   #取消添加的ip地址

上一篇:骨头识变器下载攻略大解密,轻松掌握操作技巧!

下一篇:基岩版服务器怎么进去?