1.執行 sudo vi /etc/apache2/httpd.conf 開始配置httpd.conf 的文件;php
//配置listen 80端口(默認配置),此處能夠修改監聽端口,例如Listen 81html
2.打開相應 LoadModulelinux
LoadModule userdir_module libexec/apache2/mod_authn_core.so //默認已經打開 LoadModule php5_module libexec/apache2/mod_authz_host.so //默認已經打開,供基於主機名、IP地址、請求特徵的訪問控制 LoadModule userdir_module libexec/apache2/mod_userdir.so //容許用戶從本身的主目錄中提供頁面(使用"/~username") LoadModule php5_module libexec/apache2/libphp5.so //php模塊
3.配置Document路徑執行你的工程目錄文件(重要) web
DocumentRoot "/Users/mac0011/Desktop/apatcl" <Directory "/Users/mac0011/Desktop/apatcl"> # # 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.4/mod/core.html#options # for more information. # Options FollowSymLinks Multiviews MultiviewsMatch Any # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride All # # Controls who can get stuff from this server. # Require all granted </Directory>
1.設置AllowOverride None 改成 AllowOverride All;
2. Apache2.2版本寫法 Order allow,deny allow from all Apache2.4版本寫法,注意只須要這一句話就能夠了 Require all granted
4.打開虛擬域名配置文件,把#Include /private/etc/apache2/extra/httpd-vhosts.conf中的#去掉 apache
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
5.配置httpd-vhosts.conf文件 執行 sudo vi /etc/apache2/extra/httpd-vhosts.confdjango
NameVirtualHost 127.0.0.1:80 <Directory "/Users/mac0011/Desktop/apatcl"> # # 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.4/mod/core.html#options # for more information. # Options FollowSymLinks Multiviews MultiviewsMatch Any # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride All # # Controls who can get stuff from this server. # # Order allow,deny Require all granted </Directory> <VirtualHost 127.0.0.1:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/Users/mac0011/Desktop/apatcl" ServerName hailong.com ErrorLog "/Users/mac0011/Desktop/apatcl/error_log" CustomLog "/Users/mac0011/Desktop/apatcl/access_log" common # <Directory "/Users/mac0011/Desktop/apatcl"> # Options Indexes FollowSymLinks Multiviews # AllowOverride All # Require all granted # </Directory> </VirtualHost>
1.NameVirtualHost 127.0.0.1:80,使用NameVirtualHost 配置具體的域名ip; 127.0.0.1指本機ip即回調地址;centos
2.若是配置的多個虛擬域名爲同一工程目錄下,能夠將<Directory></Directory>提出來,放到上面同一配置,也能夠單獨爲每個虛擬域名 單獨配置服務器
3.<Directory></Directory>中添加Require all granted.Apache 2.2版本的參考上面的寫法;app
6.配置hosts文件 執行sudo vi /etc/hostsiview
7.重啓Apache,sudo apachectl restart
8.其餘:
httpd -S 能夠定位一下,apache配置過程當中的出現的配置問題
OK!!至此,完成apache虛擬域名的配置
1.apache2.2和apache2.4配置不一樣處理問題
Apache2.2版本寫法 Order allow,deny allow from all Apache2.4版本寫法,注意只須要這一句話就能夠了 Require all granted
本次在配置時遇到的主要問題就是,樓主在apache2.4版本時單純的把allow from all替換成了 Require all granted,而沒有吧 order allow deny給屏蔽掉,致使訪問的時候一直出現403的錯誤;主要仍是太粗心了;
2.配置多個虛擬域名時,儘可能不要採用通配符 *,使用NameVirtualHost爲這個基於域名的虛擬主機指定IP地址集
也在/etc/hosts上分別爲兩個虛擬主機指向了本機IP127.0.0.1,可是第一個虛擬主機老是會覆蓋第二個甚至覆蓋localhost,效果就是無論訪問那個域名最後都會指向dummy-host1.example.com域名的應用,
最後弄了很久才發現要在同一ip上配多個基於域名的虛擬主機須要這樣作,必須用NameVirtualHost指令爲這個基於域名的虛擬主機指定IP地址集,並且每個虛擬主機不能使用*通配符,必須指定ip
3.工程目錄權限的問題(記住是每一級的目錄都要設置)
chmod 755或者chmod 777或者chmod o+x 對每個工程設置好讀寫權限;
【這個問題我沒遇到過,由於我沒這樣寫過,網上資料這麼寫,可做爲參考】
若是設置的是虛擬目錄,那麼你須要在httpd.conf中定義一個虛擬目錄,並且像極了以下的片斷:
Alias /folder "/usr/local/folder" <Directory "/usr/local/folder"> Options FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.1 192.168.1.1 </Directory>
若是是這一種狀況,並且你寫得相似我上面的代碼,三處folder都是同樣同樣的,那絕對會是403!怎麼解決呢,就是把跟在Alias後面斜槓後面的那串改了,改爲什麼,不要和虛擬目錄的文件夾同名就好,而後我就能夠用改事後的虛擬目錄訪問了,固然,改文件夾也行,只要你不怕麻煩,只要Alias後面的虛擬目錄定義字符(紅色)和實際文件夾名(黑色)不相同就OK。
若是依然是403,那就是selinux在做怪了,因而,你能夠把你的目錄進行一下selinux權限設置。
今天我遇到的就是這個問題了。
#chcon -R -t httpd_sys_content_t /usr/local/site#chcon -R -t httpd_sys_content_t /usr/local/site/test
網上資料說不過,這一步大多不會發生。但個人問題確實是,可能跟系統有關,具體原理還不是很懂。
個人虛擬主機配置爲:
<VirtualHost *:80> WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgiAlias /static/ /srv/lxyproject/collectedstatic/ServerName 10.1.101.31 #ServerName example.com#ServerAlias www.example.com <Directory /srv/lxyproject/collectedstatic> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <Directory /srv/lxyproject/wsgi/> Allow from all </Directory> ErrorLog /etc/httpd/logs/lxyproject.error.logLogLevel warn </VirtualHost>
我訪問
log報錯:
client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi
解決辦法:
修改<Directory /srv/lxyproject/wsgi/>中Allow from all
爲:Require all granted
。
這個問題是由於版本的緣由,
個人httpd版本爲:
[root@yl-web conf.d]# rpm -qa |grep httpdhttpd-devel-2.4.6-31.el7.centos.x86_64httpd-tools-2.4.6-31.el7.centos.x86_64httpd-2.4.6-31.el7.centos.x86_64
而2.3如下版本用Allow from all,2.3及以上版本爲Require all granted。
<Directory /home/aettool/aet/apache> <IfVersion < 2.3 > Order allow,deny Allow from all </IfVersion> <IfVersion >= 2.3> Require all granted </IfVersion></Directory>
參考:http://stackoverflow.com/questions/17766595/403-forbidden-error-with-django-and-mod-wsgi
7.添加多個類型的文件
修改前
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
修改後:
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
<IfModule dir_module>
三、 相關參考博客
https://wiki.apache.org/httpd/ClientDeniedByServerConfiguration
http://www.cnblogs.com/AloneSword/archive/2013/03/01/2939564.html
https://discussions.apple.com/docs/DOC-3083
http://www.th7.cn/system/lin/201507/122784.shtml //apache httpd服務器403 forbidden的問題
http://blog.csdn.net/yanzi1225627/article/details/45075265 //如何將apache的這個默認目錄更改到用戶目錄下。
http://www.cnblogs.com/wenqiangwu/archive/2013/09/12/3317030.html //linux查看及修改文件權限以及相關