108 lines
2.7 KiB
Markdown
108 lines
2.7 KiB
Markdown
## Install
|
||
```
|
||
sudo apt update && sudo apt install apache2
|
||
```
|
||
|
||
## 測試Apache
|
||
```
|
||
sudo service apache2 status
|
||
```
|
||
|
||
## 設置虛擬主機(Virtual Hosts)
|
||
假設要建立2個網站*test1.ui-code.com*與*test2.ui-code.com*
|
||
|
||
### 建立目錄並設置權限(Permissions)
|
||
```
|
||
sudo mkdir -p /var/www/test1.ui-code.com/public_html
|
||
sudo mkdir -p /var/www/test2.ui-code.com/public_html
|
||
sudo chmod -R 755 /var/www
|
||
```
|
||
|
||
### 建立測試頁面
|
||
#### 建立test1.ui-code.com的測試頁面
|
||
```
|
||
sudo nano /var/www/test1.ui-code.com/public_html/index.html
|
||
```
|
||
填入以下內容:
|
||
```html
|
||
<html>
|
||
<head>
|
||
<title>Welcome to test1.ui-code.com</title>
|
||
</head>
|
||
<body>
|
||
<h1>Welcome to test1.ui-code.com</h2>
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
#### 建立test2.ui-code.com的測試頁面
|
||
```
|
||
sudo nano /var/www/test2.ui-code.com/public_html/index.html
|
||
```
|
||
填入以下內容:
|
||
```html
|
||
<html>
|
||
<head>
|
||
<title>Welcome to test2.ui-code.com</title>
|
||
</head>
|
||
<body>
|
||
<h1>Welcome to test2.ui-code.com</h2>
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
### 建立虛擬主機文件(Virtual Host Files)
|
||
虛擬主機文件位於 /etc/apache2/sites-available/ 中,其用於告訴 Apache 網頁伺服器如何響應(Respond )各種網域請求(Request)。
|
||
讓我們為test1.ui-code.com 網域創建一個新的虛擬主機文件。
|
||
```
|
||
sudo nano /etc/apache2/sites-available/test1.ui-code.com.conf
|
||
```
|
||
|
||
將以下內容貼上:
|
||
```
|
||
<VirtualHost *:80>
|
||
ServerAdmin webmaster@test1.ui-code.com
|
||
ServerName test1.ui-code.com
|
||
ServerAlias www.test1.ui-code.com
|
||
DocumentRoot /var/www/test1.ui-code.com/public_html
|
||
ErrorLog ${APACHE_LOG_DIR}/error.log
|
||
CustomLog ${APACHE_LOG_DIR}/access.log combined
|
||
</VirtualHost>
|
||
```
|
||
|
||
再來為test2.ui-code.com 網域創建一個新的虛擬主機文件。
|
||
```
|
||
sudo nano /etc/apache2/sites-available/test2.ui-code.com.conf
|
||
```
|
||
|
||
將以下內容貼上:
|
||
```
|
||
<VirtualHost *:80>
|
||
ServerAdmin webmaster@test2.ui-code.com
|
||
ServerName test2.ui-code.com
|
||
ServerAlias www.test2.ui-code.com
|
||
DocumentRoot /var/www/test2.ui-code.com/public_html
|
||
ErrorLog ${APACHE_LOG_DIR}/error.log
|
||
CustomLog ${APACHE_LOG_DIR}/access.log combined
|
||
</VirtualHost>
|
||
```
|
||
|
||
### 啟用新的虛擬主機文件(Virtual Host Files)
|
||
現在我們有兩個虛擬主機文件,我們需要使用 a2ensite 工具來啟用它們。
|
||
```
|
||
sudo a2ensite test1.ui-code.com
|
||
sudo a2ensite test2.ui-code.com
|
||
```
|
||
|
||
測試配置語法是否有錯誤。
|
||
```
|
||
apachectl configtest
|
||
```
|
||
|
||
如果「Syntax OK」,重啟 Apache。
|
||
```
|
||
sudo systemctl reload apache2
|
||
```
|
||
|
||
## 參考
|
||
- [[教學][Ubuntu 架站] 在 Ubuntu 20.04 安裝 Apache 網頁伺服器,並架設多個網站(多網域) | 優程式](https://ui-code.com/archives/271) |