---
title: Apache 實現部署多個網站(一個ip部署多域名)
tags: Django, python, deployed, apache2, multiple Domain Name
---
# Apache 實現部署多個網站(一個ip部署多域名)
> [TOC]
>
> Reference Website:
> 1. [How to configure multiple websites with Apache web server](https://opensource.com/article/18/3/configuring-multiple-web-sites-apache)
> 2. [Apache Virtual Host 多網域網站放置在同一台主機上](https://blog.xuite.net/tolarku/blog/485166953-Apache+Virtual+Host+%E5%A4%9A%E7%B6%B2%E5%9F%9F%E7%B6%B2%E7%AB%99%E6%94%BE%E7%BD%AE%E5%9C%A8%E5%90%8C%E4%B8%80%E5%8F%B0%E4%B8%BB%E6%A9%9F%E4%B8%8A)
> 3. [apache實現部署多個網站(一個ip部署多域名)的方法詳解](https://codertw.com/%E4%BC%BA%E6%9C%8D%E5%99%A8/376301/)
---
## 環境要求與基礎確認 (USE terminal)
* OS:Linux - ubuntu 18.04
* 伺服軟體 - Apache
* IP address - 192.168.0.102 (IP)
```
$ ifconfig
```
* 測試網站(兩種)
* HTML
1. `/var/www/html/index.html`
2. `/var/www/test1/index.html`
* Framework - Django
1. `/site_test/tutorial`
```
/site_test
├── access.log
├── error.log
├── tutorial
│ ├── db.sqlite3
│ ├── home
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ ├── models.py
│ │ ├── __pycache__
│ │ ├── templates
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── manage.py
│ └── tutorial
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── venv
```
2. `/site_test2/stat01`
```
/site_test2
├── access.log
├── error.log
├── stat01
│ ├── db.sqlite3
│ ├── app2
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ ├── models.py
│ │ ├── __pycache__
│ │ ├── templates
│ │ ├── tests.py
│ │ └── views.py
│ ├── manage.py
│ └── stat01
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── venv
```
:::info
* [tree 命令:](https://wangchujiang.com/linux-command/c/tree.html)以樹狀圖列出目錄的内容。
```
-L level # 限制目錄顯示層級。
$ tree /site_test2 -L 3
```
:::
---
> * 先準備一台電腦,透過 `ifconfig` 指令取得ip。
> * 接著,將兩個以上網站置於該台電腦上。
> * 開始撰寫Apache2設定檔,並向[No-IP 網站](https://www.noip.com/login?ref_url=console)申請Domain Name。
> * Note:
> 1. No-IP 所申請之免費 Domain Name,每個僅能維持30天。
> * http://fengchia.ddns.net
> * http://chia.ddnsking.com
> 2. 一個網站,撰寫一個Apache2設定檔。兩個網站,便有兩個Apache2設定檔。
> * 完成以上設定後,於瀏覽器上輸入 Domain Name,便能顯示已部署的兩個網站。
---
## Domain Name 與 IP 的關係
* 以筆記中實作的內容為例:
* http://fengchia.ddns.net 與 http://chia.ddnsking.com 兩者對應之IP皆為 `192.168.0.102`
* 搭配個別的Apache2設定檔,即可實現 ==個別Domain Name 導向 個別網站==。
---
## (p) 網站測試 (HTML版)
### **Step 1: 將兩個以上網站置於該台電腦上**
1. 網站 1:`/var/www/html/index.html`
```htmlmixed=
<html>
<head>
<title>Welcome to 192.168.0.102</title>
</head>
<body>
<h1>Site 1 Success! The 192.168.0.102 host is working!</h1>
<h2>You can also input follows to browser: </h2>
<h2>http://localhost </h2>
<h2>http://chia.ddnsking.com/ </h2>
</body>
</html>
```
2. 網站 2:`/var/www/test2/index.html`
```htmlmixed=
<html>
<head>
<title>Welcome to http://fengchia.ddns.net/</title>
</head>
<body>
<h1>Site 2 Success! </h1>
<h1>The http://fengchia.ddns.net/ host is working!</h1>
</body>
</html>
```
### **Step 2: 向 [No-IP 網站](https://www.noip.com/login?ref_url=console) 申請Domain Name**
![](https://i.imgur.com/E64FcCp.png)
* 如何取得?
1. 先註冊,後登入。登入後點選 Dynamic DNS -> Create Hostname
* 把透過 `ifconfig` 指令取得的ip 填入 IPv4 Address
2. 依截圖操作完後,便告一段落。
![](https://i.imgur.com/PiHjwOF.png)
![](https://i.imgur.com/YPabIwS.png)
### **Step 3: 撰寫個別網站的Apache2設定檔**
* 安裝Apache2
```
sudo apt install apache2
```
1. `/var/www/html/index.html` 對應 /etc/apache2/sites-available/==000-default.conf==
```htmlmixed=
<VirtualHost *:80>
ServerName chia.ddnsking.com
#ServerName 192.168.0.102
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```
2. `/var/www/test1/index.html` 對應 /etc/apache2/sites-available/==test2.conf==
```htmlmixed=
<VirtualHost *:80>
ServerName fengchia.ddns.net
ServerAdmin webmaster@localhost
DocumentRoot /var/www/test2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```
* 開啟 `000-default.conf`、`stat01.conf` Apache2設定檔
```
a2ensite 000-default.conf
a2ensite test2.conf
```
* 載入設定 + 重新啟動Apache2
```
systemctl reload apache2
systemctl restart apache2
```
* 查看Apache2狀態
```
systemctl status apache2.service
```
* 完成!
---
## (p) 網站測試 (Django版)
### **Step 1: 將兩個以上網站置於該台電腦上**
1. 網站 1:`/site_test/tutorial`
* /site_test/tutorial/tutorial/==settings.py==
```python=
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
```
* /site_test/tutorial/tutorial/==urls.py==
```python=
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include('home.urls')),
]
```
* /site_test/tutorial/==home/urls.py==
```python=
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
```
* /site_test/tutorial/==home/views.py==
```python=
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render,HttpResponse
def home(request):
return render(request, 'home/home.html')
```
* /site_test/tutorial/==home/templates/home/home.html==
```htmlmixed=
<html>
<head>
<title>Welcome to 192.168.0.102</title>
</head>
<body>
<h1>Site 1 Success! The 192.168.0.102 host is working!</h1>
<h2>You can also input follows to browser: </h2>
<h2>http://localhost </h2>
<h2>http://chia.ddnsking.com/ </h2>
</body>
</html>
```
2. 網站 2:`/site_test2/stat01`
* /site_test2/stat01/stat01/==settings.py==
```python=
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app2',
]
```
* /site_test2/stat01/stat01/==urls.py==
```python=
from django.contrib import admin
from django.urls import path
from app2.views import hello_world, index
urlpatterns = [
path('admin/', admin.site.urls),
path('s1/', hello_world),
path('s2/', index),
]
```
* /site_test2/stat01/app2/==views.py==
```python=
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from datetime import datetime
def hello_world(request):
return HttpResponse("Hello World!")
def index(request):
return render(request, 'index.html', {
'current_time': str(datetime.now()),
})
```
* /site_test2/stat01/==app2/templates/index.html==
```htmlmixed=
<!DOCTYPE html>
<html>
<head>
<title>I come from template!!</title>
<style>
body { background-color: lightyellow; }
em { color: LightSeaGreen; }
</style>
</head>
<body>
<h1>http://fengchia.ddns.net/s2</h1>
<em>{{ current_time }}</em>
</body>
</html>
```
![](https://i.imgur.com/5hHkLD5.png)
### **Step 2: 向 [No-IP 網站](https://www.noip.com/login?ref_url=console) 申請Domain Name**
![](https://i.imgur.com/E64FcCp.png)
* 如何取得?
1. 先註冊,後登入。登入後點選 Dynamic DNS -> Create Hostname
* 把透過 `ifconfig` 指令取得的ip 填入 IPv4 Address
2. 依截圖操作完後,便告一段落。
![](https://i.imgur.com/PiHjwOF.png)
![](https://i.imgur.com/YPabIwS.png)
### **Step 3: 撰寫個別網站的Apache2設定檔**
* 安裝Apache2
```
sudo apt install apache2 libapache2-mod-wsgi-py3
```
1. `/site_test/tutorial` 對應 /etc/apache2/sites-available/==000-default.conf==
```htmlmixed=
<VirtualHost *:80>
ServerName chia.ddnsking.com
#ServerName 192.168.0.102
ServerAdmin webmaster@localhost
DocumentRoot /site_test
ErrorLog /site_test/error.log
CustomLog /site_test/access.log combined
<Directory /site_test/tutorial/tutorial>
<Files wsgi.py>
Require all granted
Require ip 127.0.0.1
</Files>
</Directory>
WSGIDaemonProcess site_test python-path=/site_test/tutorial python-home=/site_test/venv
WSGIProcessGroup site_test
WSGIScriptAlias / /site_test/tutorial/tutorial/wsgi.py
</VirtualHost>
```
2. `/site_test2/stat01` 對應 /etc/apache2/sites-available/==test2.conf==
```htmlmixed=
<VirtualHost *:80>
ServerName fengchia.ddns.net
ServerAdmin webmaster@localhost
DocumentRoot /site_test2
ErrorLog /site_test2/error.log
CustomLog /site_test2/access.log combined
<Directory /site_test2/stat01/stat01>
<Files wsgi.py>
Require all granted
Require ip 127.0.0.1
</Files>
</Directory>
WSGIDaemonProcess site_test2 python-path=/site_test2/stat01 python-home=/site_test2/venv
WSGIProcessGroup site_test2
WSGIScriptAlias / /site_test2/stat01/stat01/wsgi.py
</VirtualHost>
```
* 開啟 `000-default.conf`、`stat01.conf` Apache2設定檔
```
a2ensite 000-default.conf
a2ensite test2.conf
```
* 載入設定 + 重新啟動Apache2
```
systemctl reload apache2
systemctl restart apache2
```
* 查看Apache2狀態
```
systemctl status apache2.service
```
* 完成!
* Site 1: chia.ddnsking.com
![](https://i.imgur.com/cq2OgGq.png)
![](https://i.imgur.com/U63NOjp.png)
* Site 2: fengchia.ddns.net
![](https://i.imgur.com/0y2tS71.png)
![](https://i.imgur.com/uQqlTcX.png)
![](https://i.imgur.com/TZl1Gxa.png)
---