centos伺服器下更改網站根目錄
作為一個web前端的新手,花了幾天買了伺服器註冊了域名,然後幾天的焦頭爛額終於網站可以運作了。滿心歡喜的把自己做的網頁放到了伺服器上,可是還是出現了不少問題,在配置Apache的時候花了不少時間。好了,講一下今天的主題,如何更改Apache訪問網站的根目錄呢?
找到目錄/etc/httpd/conf/下的httpd.conf,這是Apache的配置檔案,我們要對Apache進行配置:
提醒一下,httpd.conf下,文字前面有“#”的都是無效的,這些內容是用來進行一些必要的解釋,其實仔細讀一下這些內容你就大體知道某些配置是用來做什麼的了。
如果想要增加埠號,因為apache預設是80埠,找到 Listen 80,把想要增加新的埠直接新增到Listen 80後就可以了, 如
Listen 80
Listen 8081
然後在你的IP地址後面加上:80或者8081就可以訪問你的網站了,同時如果沒有為不同的埠指定目錄,它們都訪問DocumentRoot,預設在/var/www/html下,修改目錄:
apache預設訪問根目錄由DocumentRoot決定,在配置檔案中找到:
DocumentRoot "/var/www/html"
將它改成你想要設定的目錄,比如
DocumentRoot "/home/www/html"
但 僅僅修改目錄是不行的,你還要對目錄進行授權,如果不給許可權還是不能訪問的。配置中的這一段內容就是告訴你要給目錄許可權才能訪問。當然,預設的許可權已經是所有人都可以訪問,我們只需在換一下目錄就可以了:
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/home/www/html"> //這裡的目錄對應網站的根目錄,只改這一句。授權開始
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all //預設所有人都可以訪問
</Directory> //授權結束
如果你對Apache做了一下配置,可是還是無權訪問,出現了“you don’t have permission to access / on this server”,為什麼呢?
既然配置檔案沒有問題,那就需要考慮http.conf檔案中指定的使用者和組的訪問許可權。
在配置檔案中找到以下:
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# . On SCO (ODT 3) use "User nouser" and "Group nogroup".
# . On HPUX you may not be able to use shared memory as nobody, and the
# suggested workaround is to create a user www and use that user.
# NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
# when the value of (unsigned)Group is above 60000;
# don't use Group #-1 on these systems!
#
User apache
Group apache
有道翻譯:如果您希望httpd為不同的使用者或組執行,你必須首先執行httpd的根源,然後切換。
使用者/組:執行使用者/組的名稱。它通常是良好的實踐為執行httpd建立一個專用的使用者和組,與大多數系統服務。
好了,有用的就這兩句,User apache Group apache
改成Apache有訪問許可權的group或者是user,這個就要根據root許可權使用者的操作來看了。
比如有使用者組:www和使用者ccc,就改成 User ccc Group www
這樣,網站就能正常訪問啦!