Centos7 - 上搭建http服務器以及設置目錄訪問

1. 安裝httpd服務

sudo yum install httpd

Apache 的所有配置文件都位於 /etc/httpd/conf/etc/httpd/conf.d 網站的數據默認位於 /var/www,但如果你願意,你可以改變它。

2. 配置
Apache 主要的配置文件是 /etc/httpd/conf/httpd.conf 。 它包含許多在基本安裝中不需要更改的配置。 實際上,只需對此文件進行一些更改即可啓動並運行一個簡單的網站。

2.1 監聽端口
第一個要修改的是 Listen 配置項,它定義了 Apache 要監聽頁面請求的 IP 地址和端口。 現在,你只需要使這個網站可以從本地訪問,所以使用 localhost 地址。 完成後,該行應該看起來像這樣:

Listen 127.0.0.1:80

通過將此配置項設置爲 localhost 的 IP 地址,Apache 將只偵聽來自本地主機的連接。 如果您希望 Web 服務器偵聽來自遠程主機的連接,則可以使用主機的外部 IP 地址。

2.2 網站頁面HTML文件位置
DocumentRoot 配置項指定組成網站頁面的 HTML 文件的位置。 該配置項不需要更改,因爲它已經指向標準位置。 該行應該看起來像這樣:

DocumentRoot"/var/www/html"

Apache 安裝包會創建 /var/www 目錄。 如果您想更改存儲網站文件的位置,則使用此配置項來完成此操作。 例如,您可能想要爲 www 目錄使用不同的名稱,以更明確地識別網站。 這可以是這樣的:

DocumentRoot"/var/mywebsite/html"

這些是創建一個簡單網站需要唯一修改的 Apache 配置項。

2.3 防火牆端口設置:打開端口 80
(1)查詢TCP/UDP的80端口占用情況:

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

如果返回結果爲“no”,則表示該端口尚未開放,需要作以下設置纔可以;否則,跳過步驟2.3。
(2)永久開放TCP/UDP的80端口

sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --permanent --zone=public --add-port=80/udp

(3)重啓防火牆

sudo firewall-cmd --reload

3.創建index.html文件
index.html 文件是你使用域名訪問網站而不是訪問特定網頁時的默認文件。在 /var/www/html 中,創建一個名字爲 index.html 的文件,在其中添加字符串 Hello World 。你不需要添加任何的 HTML 標誌去完成這項工作。web 服務器的唯一任務是提供文本數據流,服務器不知道數據是什麼,也不知道如何呈現它。它只是將數據流傳輸給請求主機。
保存文件後,將所有權設置爲 apache.apache 。

chown apache.apache index.html

4. 啓動 Apache

$ sudo systemctl start httpd

$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2018-04-16 11:01:59 CST; 5h 50min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 41464 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 41473 (httpd)
   Status: "Total requests: 6; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─41473 /usr/sbin/httpd -DFOREGROUND
           ├─41474 /usr/sbin/httpd -DFOREGROUND
           ├─41475 /usr/sbin/httpd -DFOREGROUND
           ├─41476 /usr/sbin/httpd -DFOREGROUND
           ├─41477 /usr/sbin/httpd -DFOREGROUND
           ├─41478 /usr/sbin/httpd -DFOREGROUND
           └─43670 /usr/sbin/httpd -DFOREGROUND

416 11:01:58 Sun systemd[1]: Starting The Apache HTTP Server...
416 11:01:59 Sun httpd[41473]: AH00557: httpd: apr_sockaddr_info_get() failed for Sun
416 11:01:59 Sun httpd[41473]: AH00558: httpd: Could not reliably determine the s...age
416 11:01:59 Sun systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

5. 訪問web服務器

在Chrome或firefox瀏覽器中,輸入本機的url地址: http://localhost ,即可訪問到本機。
試試看,屏幕上顯示的內容是不是和文件 /var/www/html/index.html 中的內容一致呢?

6. 開啓目錄結構

6.1 修改配置文件 welcome.conf
將配置文件 /etc/httpd/conf.d/welcome.conf 的-號改爲+號:
原文: Options -Indexes
修改後: Options +Indexes
備註:Indexes 其實就是Apache中的索引服務

6.2 重啓http服務
在終端執行命令 systemctl restart httpd ,重啓服務就可以看到目錄服務器下的目錄了

6.3 瞭解配置文件welcome.conf
如果6.2一步之後得到了形如這樣的目錄結構,則可以跳過這一小節。
否則,還是花一分鐘來了解下這個配置文件吧!
這裏是它的全文:

[User@Host ~]$ cat  /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

解釋一下,"default index page“ 指的是位於http文件服務器下載目錄的文檔 index.html。
在本例中,這個文件的全稱是 /var/www/html/index.html
在這裏,爲了使welcome.conf文檔中的目錄結構設置生效,我們必須刪除index.html!
刪除index.html之後,再瀏覽器中打開本機的網址 http://localhost ,看看結果是不是變成目錄結構了呢?

6.4 更改http服務器的默認目錄
在配置文件 /etc/httpd/conf/httpd.conf 中一共有三個地方需要修改,這裏以目標目錄 /pub/meetings/test 爲例。
(1)修改參數 “DocumentRoot”:
關於這個參數的一部分原文長這樣:

[User@Host ~]$ cat /etc/httpd/conf/httpd.conf | grep "DocumentRoot"
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
# access content that does not live under the DocumentRoot.

可以看到,它默認的目錄位於 /var/www/html
接下來,我們註釋掉原文,把它改成我們需要的 /pub/meetings/test 目錄。

[User@Host ~]$ sudo vi /etc/httpd/conf/httpd.conf
...
# DocumentRoot: The directory out of which you will serve your
# DocumentRoot "/var/www/html"
DocumentRoot "/pub/meetings/test"
    # access content that does not live under the DocumentRoot.
...

(2)修改目錄參數

[User@Host ~]$ sudo vi /etc/httpd/conf/httpd.conf
...
#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
<Directory "/pub/meetings">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
...

(3)再次修改目錄參數

[User@Host ~]$ sudo vi /etc/httpd/conf/httpd.conf
...
# Further relax access to the default document root:
#<Directory "/var/www/html">
<Directory "/pub/meetings/test">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
...

(4)同樣地,再次重啓http服務,確保我們的更改立即生效:

sudo systemctl restart httpd

經過這樣一番設置,在瀏覽器(http://localhost)看到的,應該就是 /pub/meetings/test 的目錄結構了。
(5)如果第(4)步不能正常訪問到目標目錄,那麼,通常是由於Apache用戶關於該文件夾的權限太低(apache的用戶:apache 運行apache的組:apache,默認權限爲750)[有關內容可以從這裏瞭解]。
我們需要給它賦予作以下設置

# 755或者777均可 (二選一:最好是777,因爲它具有寫權限)
# 755: rwxr-xr-x
sudo chmod -R 755 /pub/meetings/
# 777: rwxrwxrwx
sudo chmod -R 777 /pub/meetings/

再重啓http服務,就可以搞定了。

(6)其它一些細節設置
默認的設置有一些地方需要修改:不支持中文、顯示優化等等。
具體執行的操作是將默認的設置參數按照如下方式修改,增加4個參數:

sudo vi /etc/httpd/conf.d/autoindex.conf 
...
#IndexOptions FancyIndexing HTMLTable VersionSort
 IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst Charset=UTF-8 NameWidth=* XHTML
...

其中,FoldersFirst 保證顯示結果中的文件夾名稱居於前面,UTF-8字符集有效地解決了中文顯示的問題,“NameWidth=*”的作用不詳。

(7) 加載 NTFS 格式的分區時遇到的問題
按以上方法對 NTFS 格式的分區所在的目錄進行設置,並不能瀏覽每一個分區內的內容,只能看到分區的根目錄下的內容。
根據系統報告可以進行修復:

[User@Host ~]$ journalctl -xe
...
418 16:33:23 localhost.localdomain dbus[733]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
418 16:33:24 localhost.localdomain setroubleshoot[32222]: failed to retrieve rpm info for /mnt/Disk2T/L
418 16:33:24 localhost.localdomain setroubleshoot[32222]: SELinux is preventing /usr/sbin/httpd from read access on the directory
                                                             /mnt/Disk2T/L. For complete SELinux messages. run se
418 16:33:24 localhost.localdomain python[32222]: SELinux is preventing /usr/sbin/httpd from read access on the directory /mnt/Disk2T/L.
                                                      
                                                      *****  Plugin catchall_boolean (89.3 confidence) suggests   ******************
                                                      
                                                      If you want to allow httpd to use fusefs
                                                      Then you must tell SELinux about this by enabling the 'httpd_use_fusefs' boolean.
                                                      You can read 'None' man page for more details.
                                                      Do
                                                      setsebool -P httpd_use_fusefs 1
                                                      
                                                      *****  Plugin catchall (11.6 confidence) suggests   **************************
                                                      
                                                      If you believe that httpd should be allowed read access on the L directory by default.
                                                      Then you should report this as a bug.
                                                      You can generate a local policy module to allow this access.
                                                      Do
                                                      allow this access for now by executing:
                                                      # ausearch -c 'httpd' --raw | audit2allow -M my-httpd
                                                      # semodule -i my-httpd.pp
                                                      
418 16:33:32 localhost.localdomain fprintd[32183]: No devices in use, exit
[User@Host ~]$ sudo setsebool -P httpd_use_fusefs 1
[User@Host ~]$ sudo systemctl restart httpd

7. 創建局域網內的機器互訪
這部分的內容不屬於Web服務器搭建的範疇,有關的內容可以參考路由器DMZ端口映射的配置。
主要原理就是,將提供Web服務的主機的80端口映射出去,使上一級局域網中的用戶也能訪問到它。
設置完成之後,在瀏覽器中採用主機加端口的方式來訪問,比如:
首頁:http://172.28.21.201:11080/index.html
或者,目錄結構: http://172.28.21.201:11080/

8. 重啓後無法訪問http服務器
首先查詢http服務的狀態:

 sudo systemctl status httpd 

如果http服務的狀態正常,顯示它“active”,則問題不出在httpd服務上。
最有可能出問題的是SELINUX的設置,因爲重啓之後,之前的設置都不生效了。
關閉Centos 7 的方法很簡單:
(0)查詢SELINUX的狀態

getenforce

(1)臨時關閉SELINUX

#設置SELinux 成爲permissive模式
##setenforce 1 設置SELinux 成爲enforcing模式
setenforce 0

(2)永久關閉SELINUX

vi /etc/selinux/config

SELINUX=enforcing 改爲 SELINUX=disabled ,設置後需要重啓才能生效。

全文完。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章