溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

apache如何實現(xiàn)部署多個網(wǎng)站

發(fā)布時間:2021-08-21 10:07:26 來源:億速云 閱讀:107 作者:小新 欄目:服務(wù)器

這篇文章主要介紹了apache如何實現(xiàn)部署多個網(wǎng)站,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

配置方法

首先apache的版本是2.4.7,然后系統(tǒng)是Ubuntu 14.04.1 LTS。(因為好像配置文件和目錄有差異)

首先進(jìn)到apache2目錄下,

apache如何實現(xiàn)部署多個網(wǎng)站

我們要探討的主要是sites-available和sites-enabled根據(jù)字面意思,前一個是網(wǎng)站可用的,后一個是網(wǎng)站可用的,然后我們還知道了,sites-enabled里面的文件是sites-available里面文件的軟鏈接,所以我們主要改site-available的文件,打開site-available有兩個文件,但我們只需要000-default.conf文件,打開cat文件

代碼如下:

<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 路徑這兩個部分,去掉注釋剩下。

<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)先級要更高,訪問自己的域名會跳轉(zhuǎn)到你設(shè)置的路徑,直接訪問ip還是會到第一個設(shè)置的路徑,你也可以選擇刪除。

還有另外一種改法就是新建一個文件,xxx.conf然后內(nèi)容一樣,

<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.設(shè)置httpd.conf

打開Apache24/conf文件夾下的httpd.conf設(shè)置文件,找到一下幾行把前面的注釋‘#'刪除

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,然而這個是做負(fù)載均衡用的一個功能,單純做反向代理的話,不需要用這個,而且取消了這里的注釋不進(jìn)行相應(yīng)的設(shè)置的話,會導(dǎo)致apache服務(wù)無法開啟)

然后找到Include conf/extra/httpd-vhosts.conf

這一行前面的注釋‘#'也刪除,引入這個文件

2.設(shè)置httpd-vhosts.conf

打開Apache24/conf/extra文件夾下的httpd-vhosts.conf.conf找到

<VirtualHost _default_:80>
#ServerName www.example.com:80
DocumentRoot "${SRVROOT}/htdocs"
</VirtualHost>

在后面添加

ProxyRequests Off
ProxyPass /***(你想要訪問的地址) http://*******(想要代理的地址)
ProxyPassReverse /***(你想要訪問的地址) http://*******(想要代理的地址)

比如說我想在瀏覽器中輸入localhost,但實際獲取的內(nèi)容是www.baidu.com的話就可以設(shè)置為ProxyPass /***(你想要訪問的地址) http://*******(想要代理的地址),第二個ProxyPassReverse是做域名重定向使用的,如果你代理的那個地址重定向的跳到另一個地方,有了ProxyPassReverse的設(shè)置就可以相應(yīng)的跳轉(zhuǎn)過去 沒有的話可能就會報錯

如果想讓別的電腦訪問自己電腦的外網(wǎng)地址就可以訪問自己服務(wù)器可以設(shè)置一下httpd.conf中的<Directory "${SRVROOT}/htdocs">

Require all denied改為Require all granted允許所有的請求和訪問

然后就可以使用了~

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“apache如何實現(xiàn)部署多個網(wǎng)站”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI