參考文獻:
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創建為目錄,在目錄里上傳文件。