基于Apache服务在centos7上搭建文件列表

参考文献:

https://www.cnblogs.com/snake553/p/8856729.html

https://blog.csdn.net/yejinxiong001/article/details/77527189


步骤:

1、安装Apache,也就是httpd服务

[root@localhost html]# yum install httpd

2、配置

  配置httpd.conf。

  首先配置Listen。这个配置项是Apache的监听位置,Apache默认监听80端口,将配置项更改为localhost的IP只监听来自本地主机的连接。

Listen 127.0.0.1:80

  或者使用外网IP监听来自远程主机的连接。

 

  配置DocumentRoot

  这个配置项是网站默认目录的位置,默认位置是/var/www/html,如下

DocumentRoot "/var/www/html"

  如果需要更改可以将引号中的路径更换。

3、80端口

  因为Apache占用80端口,所以要确保80端口畅通。

  查看80端口是否开启:

firewall-cmd --query-port=80/tcp

  返回的是yes说明已经开启,返回no则是没有开启。

  开启80端口:

firewall-cmd --add-port=80/tcp --permanent   # --permanent 永久生效,没有此参数重启后失效

  关闭80端口:

firewall-cmd --remove-port=80/tcp --permanent   # --permanent 永久生效,没有此参数重启后失效

  重启防火墙:

firewall-cmd --reload

4、index.html

  index.html是使用域名访问时的默认页面,在/var/www/html下创建index.html

touch /var/www/html/index.html

  然后编辑一下,输入一下hello world

5、启动Apache

systemctl start httpd.service

  检查Apache运行状态:

systemctl status httpd

  Apache启动后就可以在浏览器中输入 localhost 进行访问,显示的内容和/var/www/html/index.html的内容是一致的。

6、开启目录结构

在查询资料的时候发现文件列表是由mod_autoindex.so模块控制的,按照教程中在/etc/httpd/conf/httpd.conf配置文件中找不到这个模块

但是在配置文件中看到如下代码:

……

# Example:
# LoadModule foo_module modules/mod_foo.so
Include conf.modules.d/*.conf
……

  大概的意思就是装载的模块包含在conf.modules.d目录下后缀名为.conf的文件中

  于是就把conf.modules.d下的所有.conf文件都查看了一遍

  在/etc/httpd/conf.modules.d/00-base.conf中找到了mod_autoindex.so模块

LoadModule autoindex_module modules/mod_autoindex.so

  这个模块是默认已经装载的,其实完全没有必要找到模块具体位置。

  配置welcome.conf

  welcome.conf位置在/etc/httpd/conf.d/下

[root@localhost html]# vim /etc/httpd/conf.d/welcome.conf

# 
# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL.  To disable the
# Welcome page, comment out all the lines below. 
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
<LocationMatch "^/+$">
    Options +Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>

<Directory /usr/share/httpd/noindex>
    AllowOverride None
    Require all granted
</Directory>

Alias /.noindex.html /usr/share/httpd/noindex/index.html
Alias /noindex/css/bootstrap.min.css /usr/share/httpd/noindex/css/bootstrap.min.css
Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png

  将Options -Indexes中的‘-’改为‘+’即可,更改后是上面的样子。

  然后重启一下httpd服务

systemctl restart httpd

  文件目录列表就成功开启了

  但是这时候需要回过头来将前面创建的index.html删除掉,不然在使用域名访问时会默认访问index.html,或者将index.html创建为目录,在目录里上传文件。

 

posted @ 2019-07-09 15:00  Simon丿  阅读(708)  评论(0编辑  收藏  举报