前言
在日常的網(wǎng)站發(fā)布中很多情況下都無法做到一個ip對應一個站點,在ip4的情況下ip的資源是相對有限的。然而作為最流行的apache自然也考慮到這種情況,下面來一起看看詳細的介紹吧。
配置方法
首先apache的版本是2.4.7,然后系統(tǒng)是ubuntu 14.04.1 lts。(因為好像配置文件和目錄有差異)
首先進到apache2目錄下,
我們要探討的主要是sites-available和sites-enabled根據(jù)字面意思,前一個是網(wǎng)站可用的,后一個是網(wǎng)站可用的,然后我們還知道了,sites-enabled里面的文件是sites-available里面文件的軟鏈接,所以我們主要改site-available的文件,打開site-available有兩個文件,但我們只需要000-default.conf文件,打開cat文件
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<virtualhost *:80> # the servername directive sets the request scheme, hostname and port that # the server uses to identify itself. this is used when creating # redirection urls. in the context of virtual hosts, the servername # specifies what hostname must appear in the request's host: header to # match this virtual host. for the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # however, you must seothert it for any further virtual host explicitly. #servername www.example.com serveradmin webmaster@localhost documentroot /var/www/ # available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # it is also possible to configure the loglevel for particular # modules, e.g. #loglevel info ssl:warn errorlog ${apache_log_dir}/error.log customlog ${apache_log_dir}/access.log combined # for most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. for example the # following line enables the cgi configuration for this host only # after it has been globally disabled with "a2disconf". #include conf-available/serve-cgi-bin.conf </virtualhost> |
這就是網(wǎng)址配置文件了,而我們要修改的只有被注釋掉的servername 域名,documentroot 路徑這兩個部分,去掉注釋剩下。
1
2
3
4
5
6
7
|
<virtualhost *:80> servername #這里是域名地址 serveradmin webmaster@localhost documentroot /var/www/ #這里是路徑 errorlog ${apache_log_dir}/error.log customlog ${apache_log_dir}/access.log combined </virtualhost> |
可以直接添加在下面,重啟apache就成了。但是上面的優(yōu)先級要更高,訪問自己的域名會跳轉到你設置的路徑,直接訪問ip還是會到第一個設置的路徑,你也可以選擇刪除。
還有另外一種改法就是新建一個文件,xxx.conf然后內容一樣,
1
2
3
4
5
6
7
|
<virtualhost *:80> servername #這里是域名地址 serveradmin webmaster@localhost documentroot /var/www/ #這里是路徑 errorlog ${apache_log_dir}/error.log customlog ${apache_log_dir}/access.log combined </virtualhost> |
在創(chuàng)建軟鏈接 ln -s ../sites-avaiable/xxx.conf ../sites-enable/xxx.conf
這樣子也可以實現(xiàn),但是優(yōu)先級還是000-default.conf高。
好了,就是這么簡單。
附:配置apache2.4.7反向代理的方法
1.設置httpd.conf
打開apache24/conf文件夾下的httpd.conf設置文件,找到一下幾行把前面的注釋‘#'刪除
1
2
3
4
|
loadmodule proxy_module modules/mod_proxy.so loadmodule proxy_connect_modulemodules/mod_proxy_connect.so loadmodule proxy_ftp_modulemodules/mod_proxy_ftp.so loadmodule proxy_http_modulemodules/mod_proxy_http.so |
(ps:很多人都會注釋loadmoduleproxy_balancer_modulemodules/mod_proxy_balancer.so
,然而這個是做負載均衡用的一個功能,單純做反向代理的話,不需要用這個,而且取消了這里的注釋不進行相應的設置的話,會導致apache服務無法開啟)
然后找到include conf/extra/httpd-vhosts.conf
這一行前面的注釋‘#'也刪除,引入這個文件
2.設置httpd-vhosts.conf
打開apache24/conf/extra文件夾下的httpd-vhosts.conf.conf找到
1
2
3
4
|
<virtualhost _default_:80> #servername www.example.com:80 documentroot "${srvroot}/htdocs" </virtualhost> |
在后面添加
1
2
3
|
proxyrequests off proxypass /***(你想要訪問的地址) http://*******(想要代理的地址) proxypassreverse /***(你想要訪問的地址) http://*******(想要代理的地址) |
比如說我想在瀏覽器中輸入localhost,但實際獲取的內容是www.baidu.com的話就可以設置為proxypass /***(你想要訪問的地址) http://*******(想要代理的地址),第二個proxypassreverse是做域名重定向使用的,如果你代理的那個地址重定向的跳到另一個地方,有了proxypassreverse的設置就可以相應的跳轉過去 沒有的話可能就會報錯
如果想讓別的電腦訪問自己電腦的外網(wǎng)地址就可以訪問自己服務器可以設置一下httpd.conf中的<directory "${srvroot}/htdocs">
把 require all denied
改為require all granted
允許所有的請求和訪問
然后就可以使用了~
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.csdn.net/cltomoya/article/details/78564453