首先說明一下,這裏提到的Ubuntu server是安裝在Windows 10上運行的VirtualBox上面的。Ubuntu server上host的網站,其訪問可以分爲在host machine上(也就是這裏提到的Windows 10)以及在外網的訪問。
web server 安裝和基本設置:
這裏的Ubuntu server是20.04 LTS (Focal Fossa): 64-bit PC (AMD64) Server,具體的安裝請參考我之前的介紹“Windows上安裝VirtualBox運行Ubuntu Server及SSH登錄”。
web server的選擇和安裝有不少組合,這裏選擇比較流行的LAMP。LAMP不是一個軟件,而是一個組合:Linux, Apache HTTP Server, MySQL/MariaDB, and PHP。這裏已經有了Ubuntu server,接下來介紹一下其他的軟件安裝,這裏選的database是MySQL。
1.登錄到Ubuntu server,檢查和安裝更新
sudo apt update
sudo apt upgrade
2.安裝Apache, MySQL, PHP
sudo apt-get install apache2
sudo apt-get install mysql-server mysql-client
sudo apt-get install php php-common php-mysql php-gd php-cli php-pear
sudo apt install libapache2-mod-php
3.基本設置—MySQL
a.使用mysql_secure_installation,根據提示,進行選擇設置,建議選擇是。
sudo mysql_secure_installation
b.啓動database server
sudo /etc/init.d/mysql start
c.編輯基本配置
sudo vi /etc/mysql/my.cnf
可以添加類似如下基本設置:
[mysqld]
max_allowed_packet = 1M
thread_stack = 128K
max_connections = 75
table_open_cache = 32M
key_buffer_size = 32M
d.重啓mysql
sudo systemctl restart mysql
4. 基本設置— Apache
a.調整防火牆,確保Port打開(這裏使用的是默認port)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
sudo ufw app list
sudo ufw app allow ‘Apache’
sudo ufw status
b.啓動web server
sudo systemctl status apache2
sudo systemctl start apache2
sudo systemctl status apache2
5.本地host machine連接
完成上面的步驟後,就可以在host machine(這裏是Windows 10)做測試。打開瀏覽器,輸入ubuntu server的IP地址,比如,http://192.168.68.108 (請替換IP)就可以看到Apache2 Ubuntu Default page:
6. 在其他PC或者手機上連接
爲了使網頁可以被外部設備訪問,需要設置Ubuntu server的network和路由器的Port Forwarding。 這裏可參考“Windows上安裝VirtualBox運行Ubuntu Server及SSH登錄”。基本方法如下:
PC(outside) —> Router Port —> Windows 10 Port —> Ubuntu server Port
這裏對應的Ubuntu server Port,默認的爲80,亦可以更改。這裏簡單其間,我們就使用80。在VM VirtualBox Manager窗口,打開 Ubuntu server的network設置:Setting—>Network->Adapter 1->Advanced-> Port Forwarding,來設置Virtual machine的Port Forwarding, 添加一個新的Port Forwarding Rule, 比如
Name:html Protocol:TCP Host Port:2024 Guest Port: 80
這裏Host IP和Guest IP可以爲空。完成這一步,可以實現在host machine上訪問。如果只是在host machine訪問,這一步也可以省略,因爲host machine會通過ubuntu server上默認的port 80來訪問。
路由器的Port Forwarding設置,不同的路由器各有不同,基本原則是一致的。比如路由器的ip是1.2.3.4,設置的port爲2024,host machine (windows 10) IP爲5.6.7.8,設置的port是2024,那麼就可以通過http://1.2.3.4:2024 來訪問網頁。這時候就可以看到看到Apache2 Ubuntu Default page。
設計自己的網站
有了網站的訪問之後,接下來我們看如何添加一個自己的網站。
1. 可選擇關閉默認的Apache的虛擬host
sudo a2dissite *default
-----------------------
Site 000-default disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
------------------------
2. 編輯網頁
默認的Ubuntu 允許訪問的文件在/var/www,及public_html
cd /var/www/html
創建網頁文件
sudo mkdir -p mywebsite.com/{public_html,log}
添加網頁文件,比如index.html
————————————————-———
<!DOCTYPE html>
<html>
<head>
Hellow World!
</head>
<body>
<p>Hello World!</p>
</body>
</html>
——————————————————————-
創建虛擬host文件
sudo vi /etc/apache2/sites-available/mywebsite.com.conf
# domain: mywebsite.com
# public: /var/www/html/mywebsite.com/public_html/
# Admin email, Server Name (domain name), and any aliases
ServerAdmin [email protected]
ServerName mywebsite.com
ServerAlias www.mywebsite.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/mywebsite.com/public_html
# Log file locations
LogLevel warn
ErrorLog /var/www/html/mywebsite.com/log/error.log
CustomLog /var/www/html/mywebsite.com/log/access.log combined
3. 打開mywebsite
sudo a2ensite mywebsite.com.conf
------------
Enabling site mywebsite.com.
To activate the new configuration, you need to run:
systemctl reload apache2
-------------
sudo systemctl reload apache2