debian配置apache2.4配置虛擬機器主機遇到的問題

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   for   

0x01 Ubuntu配置apache2的前置知識

使用apt-get安裝的apache2與直接編譯安裝版本略有不同,其設定檔不在是httpd.conf,而是/etc/apache2/apache2.conf。

而其虛擬機器主機的位置的設定檔也不在是vhost之類的,而是sites-avilable/xxx.conf。預設的localhost為000-default.conf,按照這個設定檔,配置一份demo.conf,就完成了虛擬機器主機的配置工作。

配置參考http://wiki.ubuntu.org.cn/Apache虛擬機器主機指南,這是Ubuntu的中文維基百科。推薦配置如下:

 

<VirtualHost *:80>ServerName edunuke.example.comServerAdmin [email protected]DocumentRoot "/var/www/edunuke/"ErrorLog "/var/log/apache2/edunuke_errors.log"CustomLog "/var/log/apache2/edunuke_accesses.log" common    </VirtualHost>

可以看到推薦的配置是/var/www/的子目錄。

按照000-default.conf的配置,填寫完整的配置如下:

<VirtualHost *:80>        ServerName demo          ServerAdmin [email protected]163.com        DocumentRoot /home/user/webroot        <Directory /home/user/webroot>                Options Indexes FollowSymLinks MultiViews                AllowOverride All                Order allow,deny                Allow from all        </Directory>          ErrorLog ${APACHE_LOG_DIR}/demo_error.log        CustomLog ${APACHE_LOG_DIR}/demo_access.log combined</VirtualHost>

很多時候的403錯誤都是<Directory>標籤沒有配置Allow from all,因為預設是Deny from all。

(注意,這裡的Allow from all的設定項目屬於2.2.x及以下版本才有,2.4.x已經不需要這個配置了)

然後使用命令a2ensite demo來使demo啟用,再使用service apache2 reload來使配置生效。

別忘了修改/etc/hosts檔案,使demo指向127.0.0.1。

原理上說到這裡已經配置完畢,應該可以直接存取了,但是,一大波403正在靠近。。。。。

0x02問題來了

如果按照維基百科的配置,將目錄選擇在/var/www/目錄或者子目錄的話,完全無壓力可以跑。但是換到/home/user/webroot之類的地方就出現403錯誤。

看錯誤記錄檔,具體資訊為:

AH00035: access to / denied (filesystem path ‘/home/user/webroot‘) because search permissions are missing on a component of the path

這說明在/home/user/webroot的路徑上,存在一個目錄使用權限設定有問題,導致拒絕訪問。

1、修改/home/user/許可權。

因為預設情況下該目錄是700,apache2是無法訪問的。

修改為711即可。

2、修改/home/user/webroot許可權。

chmod 755 -R /home/user/webroot/

讓webroot及其子目錄擁有755許可權,將一些特殊目錄設定777,如assets目錄,runtime目錄等,這個根據程式修改。

許可權修改完畢,請求依然是403。

H01630: client denied by server configuration: /home/user/webroot/

看來是伺服器配置問題啊。可是在網上搜了不少文章,都沒有提到這個事情。

悲劇折騰了兩天,重裝,換目錄也不行,哪怕換到/var/webroot/也還是不行,一一對比了/var/www和/home/user/webroot的許可權設定,也沒有問題啊,很糾結。

這時候叫來了Scrat同學,經過一陣瞎折騰,在修改了/etc/apache2/apache2.conf檔案後的一個配置後居然可以了

<Directory />        #Options FollowSymLinks        #AllowOverride None        #Require all denied        #我也忘記當時他改了什麼,大概是把demo.conf中的<Directory>複製過來了</Directory>

(PS這不是正確的方法,正確的方法見後續)

注釋掉了這裡的東西,寫入了不知道從哪裡找來的幾行配置,居然訪問通過了。

說實話,我覺得apache通過a2ensite/a2dissite這類命令來控制虛擬機器主機,是架構上先進的體現,沒想到配置虛擬機器主機還得修改apache2檔案。。。。鬱悶。。

晚上回去我又仔細看了看這個設定檔,發現是自己傻逼了。

# Sets the default security model of the Apache2 HTTPD server. It does# not allow access to the root filesystem outside of /usr/share and /var/www.# The former is used by web applications packaged in Debian,# the latter may be used for local directories served by the web server. If# your system is serving content from a sub-directory in /srv you must allow# access here, or in any related virtual host.<Directory />        Options FollowSymLinks        AllowOverride None        Require all denied</Directory>  <Directory /usr/share>        AllowOverride None        Require all granted</Directory>

注釋已經說了,這是防止訪問根目錄以外的目錄做的特殊安全設定,預設情況下除了/usr/share和/var/www外,其他目錄都apache伺服器都不能訪問。要使/home/user/webroot可以訪問,就要增加一個類似的訪問請求項目:

<Directory /home/user/webroot>        AllowOverride None        Require all granted</Directory>

OK,這下達到原先的目標了。

但是這還沒完,難道增加一個虛擬機器主機就得修改一次apache2.conf檔案?真不能通過a2enssite無縫增加一個虛擬機器主機?

答案是肯定的,其實還是demo.conf配置問題,需要增加一個設定項目:Require all granted。

完整的配置如下:

<VirtualHost *:80>        ServerName demo          ServerAdmin xbzbing#163.com        DocumentRoot /home/user/webroot        <Directory /home/user/webroot>                Options Indexes FollowSymLinks MultiViews                AllowOverride All                Require all granted        </Directory>          ErrorLog ${APACHE_LOG_DIR}/demo_error.log        CustomLog ${APACHE_LOG_DIR}/demo_access.log combined</VirtualHost>

 

debian配置apache2.4配置虛擬機器主機遇到的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.