1. 程式人生 > >ubuntu設定apache部署多個站點

ubuntu設定apache部署多個站點

需求描述:

在本機上之前已經部署了一個專案,可以通過localhost/pro1訪問,現在要新開發一個專案,如何通過localhost/pro2訪問?

本機環境:

ubuntu1604
apache2: 在/etc/apache2/目錄下,通過sudo apt-get apache2自動安裝。
若在安裝好後啟動apache2時提示各種directory未配置時,可以先source /etc/apache2/envvars,讓apache2的配置檔案生效。

解決方法

首先需要新增新埠。在site-available資料夾下複製000-default.conf檔案,並重命名,可隨意,假設為8081.conf。開啟檔案進行修改,下面是我修改好後的內容。

其中8081是我設定的新埠,可根據自己需求進行修改;
ServerName與ServerAlias也需要根據需求修改,ServerName即在瀏覽器端訪問的路徑,ServerAlias即路徑別名,
在此處我省略掉了埠號,並將專案名稱新增進來,這樣有多個專案時,即可通過專案名稱來加以區分,而不用考慮其埠區別,對外界訪問更友好;
Directory節點是專案所在路徑,要保證目錄下存在index.html檔案,這樣在訪問ServerAlias或ServerName時會自動載入改頁面;
其他內容不變。
<VirtualHost *:8081>
    # 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 set it for any further virtual host explicitly. ServerName 127.0.0.1:8081 ServerAlias 127.0.0.1/two ServerAdmin [email protected] DocumentRoot /var/www/html <Directory /home/mamq/work/two/> AllowOverride All </Directory> # 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> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet

輸入以下命令使埠生效:sudo a2ensite 8081.conf
然後,修改/etc/apache2/apache2.conf檔案,新增如下內容:

ServerName 127.0.0.1:80

Alias /VES "/home/mamq/work/one"
<Directory "/home/mamq/work/one">
  Options Indexes MultiViews FollowSymLinks
  AllowOverride None
  Order deny,allow
  Allow from all
</Directory>

Alias /ARCTIC "/home/mamq/work/two"
<Directory "/home/mamq/work/two">
  Options Indexes MultiViews FollowSymLinks
  AllowOverride None
  Order deny,allow
  Allow from all
</Directory>

重啟apache2. service apache2 restart
通過以上設定,在瀏覽器端輸入localhost/one與localhost/two即可分別訪問兩個專案,兩個專案分別對應80和8081埠。若需要在區域網內其他機器訪問,需要在防火牆中開啟埠。
在centos伺服器上apache是以httpd的形式存在,要達到上述目的要稍微複雜點,方法略有不同,等之後有時間再更新上來。

以上,歡迎交流。