# 3.Apache伺服與後端資料庫的架設 ###### tags: `虛擬機流程` ## 第一步 - 建立一台Linux Ubuntu 20的機器,搭載Apache 參考[網頁](https://ui-code.com/archives/271) 本台機器用來放置網頁檔,提供連線入口(本機器亦為Wazuh agent) 安裝 Apache ```=bash sudo apt update && sudo apt install apache2 ``` 測試 Apache ```=bash sudo service apache2 status ``` 如果它已啟動並正在運行,您應該會看到綠色的活動(Active)狀態 ```=bash apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-09-07 11:34:02 UTC; 1min 9s ago Docs: https://httpd.apache.org/docs/2.4/ Main PID: 21513 (apache2) Tasks: 55 (limit: 4705) Memory: 5.1M CGroup: /system.slice/apache2.service ├─21513 /usr/sbin/apache2 -k start ├─21515 /usr/sbin/apache2 -k start └─21516 /usr/sbin/apache2 -k start Sep 07 11:34:02 ubuntu-20 systemd[1]: Starting The Apache HTTP Server... Sep 07 11:34:02 ubuntu-20 systemd[1]: Started The Apache HTTP Server. ``` 好了之後到瀏覽器輸入IP(如果用虛擬機架設記得使用NAT轉換) 虛擬機的話執行下列指令修改IP ```=bash sudo vim /etc/hosts ``` 之後會看到如下圖片,表示安裝成功  該機器也要進行Wazuh的agent註冊 步驟與其他Linux的agent註冊相同 可參考[此篇](https://hackmd.io/wK9gqEZPQwKC-Co1bjLXug) 然後進行SQL injection的前置準備,參考[這篇](https://documentation.wazuh.com/current/proof-of-concept-guide/detect-web-attack-sql-injection.html) 這個部分的Configuration都只在Apache安裝的機器上更動,無須更改Wazuh-manager-master 第一步驟的 Add the following lines to /var/ossec/etc/ossec.conf at the Wazuh Ubuntu 20 host. This sets the Linux agent to monitor the access logs of your Apache server. 應該已經自動弄好,我們無須更動 我們要執行的是第三步驟 增加apache的部分如圖:  Modify the FilesMatch directive at /etc/apache2/apache2.conf as follow: ```=bash <FilesMatch ".ht*"> Require all denied </FilesMatch> ``` 好了之後可以重啟agent ```=bash systemctl restart wazuh-agent ``` ## 第二步 - 在windows上安裝MySQL (可跳過) https://www.mysql.com/ 安裝流程可參考此部[影片](https://www.youtube.com/watch?v=3zzszKQ8Kk4) ## 第三步 - 在windows上安裝XAMPP (測試用,可跳過) 用來啟動Apache與本地資料庫(windows上的MySQL)的連線 安裝好後按下Apache和MySQL的start會變成如下圖  如有顯示端口占用,可嘗試[此篇](https://www.796t.com/content/1548616691.html) 按下Mysql的Admin會開啟本地的資料庫如下圖  可以注意到,我採用的資料庫為test 資料表為students 這裡的資料庫名稱務必與conn_mysql.php中一樣,conn_mysql.php的code如下 該檔案可在第四步中獲得  ## 第四步 - 測試網頁與後端資料庫的連結是否正常 (可跳過) 可先[到此]下載檔案,本步驟都在windows上執行 下載好後裡面有三個檔案,分別是conn_mysql.php、index.html、logic_php.php 把上述三個檔案放到XAMPP中,路徑如下 C:\xampp\htdocs 完成後應如圖所示  到瀏覽器中輸入localhost應出現下圖,表示index.html檔案運作正常  一樣在瀏覽器輸入localhost/conn_mysql.php 如果正確與資料庫連接將出現下圖  ## 第五步 - 架設網頁伺服器 本步驟使用的檔案為上一步驟完成測試之檔案,但因為本步驟要把檔案放到一開始架設的Apache上,所以需要使用clone指令獲取檔案 ```=bash git clone https://github.com/Titus181/websitetest.git git clone https://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@github.com/Titus181/websitetest.git ``` 放置到apache伺服器上的下列路徑 ```=bash /var/www/html ``` 會變成圖片這樣  抓取成功後透過ls指令查詢放置位置是否正確,應該如下圖  放置完成後,到瀏覽器上輸入Apache架設的機器的IP,就可以看到和上一步驟一模一樣的網頁 ## 第六步 - 架設資料庫 [參考網頁1](https://ithelp.ithome.com.tw/articles/10216445) ``` sudo apt install mysql-server ``` 安裝過程系統會詢問你是否要繼續,輸入y就行了 安裝好後,輸入以下指令,啟動互動式shell script: ``` sudo mysql_secure_installation ``` 接著系統會詢問你是否要配置VALIDATE PASSWORD PLUGIN ``` VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: ``` 輸入y的話便會看到以下文字: ``` There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 ``` 若輸入了上面的驗證規範(validation policy),設定MySQL密碼時就必須遵從你所設定的強度規範,否則MySQL會拒絕你的密碼並出現錯誤。 ``` Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : n ``` 接下來系統問你的每個問題只要按y和enter就可以了。 不要刪除匿名用戶和測試資料庫,也不要禁止root遠端登入。 安裝好後,打開terminal,輸入 mysql -uroot -p 然後輸入你的MySQL密碼,看到這個畫面就表示安裝成功:  ## 第七步 - 安裝PHP [參考網頁1](https://ithelp.ithome.com.tw/articles/10217007) ``` sudo apt update sudo apt install php libapache2-mod-php php-mysql ``` 除了install php之外,後面兩個package是幫助PHP程式碼能在Apache server下運行,並與MySQL連結。 接著我們要修改當client從server發出路徑的請求時,Apache首先會去找的檔案: 輸入下列指令,以root權限的文字編輯器開啟dir.conf: ``` sudo nano /etc/apache2/mods-enabled/dir.conf ``` 然後你會看到這個畫面: ``` <IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule> ``` 把index.php移到最前面,讓server知道第一個要找的是index.php這個檔案。如下: ``` <IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule> ``` 接著重新啟動Apache,讓server辨識你對dir.conf所作的更改。 ``` sudo systemctl restart apache2 ``` 最後要來在server上測試安裝好的PHP。 建立一個叫info.php的script,而為了讓server找到文件,我們要建立在/var/www/html/這個路徑下。 ``` sudo nano /var/www/html/info.php ``` 在info.php中加入這段: ``` <?php phpinfo(); ?> ``` 接下來打開瀏覽器,輸入: ``` http://你的server_ip/info.php ``` 以我們實驗室的環境來看的話會是 ``` http://140.122.184.28/info.php ``` 能看到這個畫面的話就表示PHP安裝成功囉  ## 第八步 - 常見的資料庫操作指令 [參考網頁1](https://ithelp.ithome.com.tw/articles/10221541) ## 第九步 - PHP連接資料庫 [參考網頁1](https://ithelp.ithome.com.tw/articles/10222798) 在這個步驟會使用到我之前在github上的websitetest這個專案的檔案 在瀏覽器輸入 140.122.184.28/conn_mysql.php 如果成功應該會看到 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up