? ? ? ?首先說(shuō)明一下,這里提到的Ubuntu server是安裝在Windows 10上運(yùn)行的VirtualBox上面的。Ubuntu server上host的網(wǎng)站,其訪問(wèn)可以分為在host machine上(也就是這里提到的Windows 10)以及在外網(wǎng)的訪問(wèn)。
web server 安裝和基本設(shè)置:
? ? ? ? 這里的Ubuntu server是20.04 LTS (Focal Fossa): 64-bit PC (AMD64) Server,具體的安裝請(qǐng)參考我之前的介紹“Windows上安裝VirtualBox運(yùn)行Ubuntu Server及SSH登錄”。
? ? ? web server的選擇和安裝有不少組合,這里選擇比較流行的LAMP。LAMP不是一個(gè)軟件,而是一個(gè)組合:Linux, Apache HTTP Server, MySQL/MariaDB, and PHP。這里已經(jīng)有了Ubuntu server,接下來(lái)介紹一下其他的軟件安裝,這里選的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.基本設(shè)置—MySQL
?? ? a.使用mysql_secure_installation,根據(jù)提示,進(jìn)行選擇設(shè)置,建議選擇是。
? ? ? ? ? sudo mysql_secure_installation
?? ? b.啟動(dòng)database server
? ? ? ? ? sudo /etc/init.d/mysql start
?? ? c.編輯基本配置
? ? ? ? ? sudo vi /etc/mysql/my.cnf
?? ? ? ? 可以添加類似如下基本設(shè)置:
[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. 基本設(shè)置— Apache
?? ? ? a.調(diào)整防火墻,確保Port打開(kāi)(這里使用的是默認(rèn)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.啟動(dòng)web server
????????????sudo systemctl status apache2
????????????sudo systemctl start apache2
????????????sudo systemctl status apache2
?? 5.本地host machine連接
? ? ? ? 完成上面的步驟后,就可以在host machine(這里是Windows 10)做測(cè)試。打開(kāi)瀏覽器,輸入ubuntu server的IP地址,比如,http://192.168.68.108?(請(qǐng)?zhí)鎿QIP)就可以看到Apache2 Ubuntu Default page:?

? ? 6.? 在其他PC或者手機(jī)上連接
?? ? ? ? 為了使網(wǎng)頁(yè)可以被外部設(shè)備訪問(wèn),需要設(shè)置Ubuntu server的network和路由器的Port Forwarding。 這里可參考“Windows上安裝VirtualBox運(yùn)行Ubuntu Server及SSH登錄”?;痉椒ㄈ缦拢?/p>
????????PC(outside) —> Router Port —> Windows 10 Port —> Ubuntu server Port
這里對(duì)應(yīng)的Ubuntu server Port,默認(rèn)的為80,亦可以更改。這里簡(jiǎn)單其間,我們就使用80。在VM VirtualBox Manager窗口,打開(kāi) Ubuntu server的network設(shè)置:Setting—>Network->Adapter 1->Advanced-> Port Forwarding,來(lái)設(shè)置Virtual machine的Port Forwarding, 添加一個(gè)新的Port Forwarding Rule, 比如
? ? ?????Name:html? Protocol:TCP? Host Port:2024? Guest Port: 80
這里Host IP和Guest IP可以為空。完成這一步,可以實(shí)現(xiàn)在host machine上訪問(wèn)。如果只是在host machine訪問(wèn),這一步也可以省略,因?yàn)閔ost machine會(huì)通過(guò)ubuntu server上默認(rèn)的port 80來(lái)訪問(wèn)。
?? ? ? 路由器的Port Forwarding設(shè)置,不同的路由器各有不同,基本原則是一致的。比如路由器的ip是1.2.3.4,設(shè)置的port為2024,host machine (windows 10) IP為5.6.7.8,設(shè)置的port是2024,那么就可以通過(guò)http://1.2.3.4:2024 來(lái)訪問(wèn)網(wǎng)頁(yè)。這時(shí)候就可以看到看到Apache2 Ubuntu Default page。
設(shè)計(jì)自己的網(wǎng)站
? ? ?有了網(wǎng)站的訪問(wèn)之后,接下來(lái)我們看如何添加一個(gè)自己的網(wǎng)站。
1. 可選擇關(guān)閉默認(rèn)的Apache的虛擬host
? ? ? sudo a2dissite *default
?? -----------------------
? ? Site 000-default disabled.
? ? To activate the new configuration, you need to run:
? ? systemctl reload apache2
? ? ------------------------
2. 編輯網(wǎng)頁(yè)
? ? ? 默認(rèn)的Ubuntu 允許訪問(wèn)的文件在/var/www,及public_html
cd /var/www/html
?? ? 創(chuàng)建網(wǎng)頁(yè)文件
sudo mkdir -p mywebsite.com/{public_html,log}
? ? 添加網(wǎng)頁(yè)文件,比如index.html
————————————————-———
<!DOCTYPE html>
<html>
<head>
? ? Hellow World!
</head>
<body>
<p>Hello World!</p>
</body>
</html>
——————————————————————-
? ? 創(chuàng)建虛擬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 webmaster@mywebsite.com
? ? ? 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. 打開(kāi)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