Skip to content

env ​

  • ubuntu 14.04
  • apache2
  • PHP 7

Install ​

Apache 套件 ​
bash
itachi@ubuntu:~$ sudo apt install apache2 -y
Apache 版本 ​
bash
itachi@ubuntu:~$ apache2 -v
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Sep 18 2017 16:37:54
PHP7.0 套件 ​
  • apache 為 php7.0
  • nginx 為 php7.0-fpm
bash
itachi@ubuntu:~$ sudo add-apt-repository ppa:ondrej/php #`Debian` 及其衍生產品(如 `Ubuntu`)維護的 `PPA`
itachi@ubuntu:~$ sudo apt update
itachi@ubuntu:~$ sudo apt install php7.0 -y
PHP 版本 ​
  • php -v
bash
itachi@ubuntu:~$ php -v
PHP **7.0.24**-1+ubuntu14.04.1+deb.sury.org+1 (cli) (built: Sep 28 2017 16:33:02) ( NTS )
...
  • phpinfo 查看
bash
itachi@ubuntu:~$ itachi@ubuntu:~$ sudo vi /var/www/html/message.php
<?php
  phpinfo();
?>

http://server_IP-address/message.php

做完之後此檔案必須刪除,有安全性問題。

檢驗 ​

http://your_server_IP_address

Apache 服務啟動與關閉 ​
bash
itachi@ubuntu:~$ sudo service apache2 stop #停止  
itachi@ubuntu:~$ sudo service apache2 start #啟動  
itachi@ubuntu:~$ sudo service apache2 restart #重啟

配置文件 ​

Apache 預設配置檔文件 ​
bash
itachi@ubuntu:~$ ls /etc/apache2/
apache2.conf    conf-enabled  magic           mods-enabled  sites-available
conf-available  envvars       mods-available  ports.conf    sites-enabled
bash
$ tree /etc/apache2/
/etc/apache2/
├── apache2.conf
├── conf-available
│   ├── charset.conf
│   ├── localized-error-pages.conf
│   ├── other-vhosts-access-log.conf
│   ├── security.conf
│   └── serve-cgi-bin.conf
├── conf-enabled
│   ├── charset.conf -> ../conf-available/charset.conf
│   ├── localized-error-pages.conf -> ../conf-available/localized-error-pages.conf
│   ├── other-vhosts-access-log.conf -> ../conf-available/other-vhosts-access-log.conf
│   ├── security.conf -> ../conf-available/security.conf
│   └── serve-cgi-bin.conf -> ../conf-available/serve-cgi-bin.conf
├── envvars
├── magic
├── mods-available
│   ├── access_compat.load
│   ├── actions.conf
......
│   ├── userdir.conf
│   ├── userdir.load
│   ├── usertrack.load
│   ├── vhost_alias.load
│   └── xml2enc.load
├── mods-enabled
│   ├── access_compat.load -> ../mods-available/access_compat.load
│   ├── alias.conf -> ../mods-available/alias.conf
│   ├── alias.load -> ../mods-available/alias.load
│   ├── auth_basic.load -> ../mods-available/auth_basic.load
│   ├── authn_core.load -> ../mods-available/authn_core.load
│   ├── authn_file.load -> ../mods-available/authn_file.load
│   ├── authz_core.load -> ../mods-available/authz_core.load
│   ├── authz_host.load -> ../mods-available/authz_host.load
│   ├── authz_user.load -> ../mods-available/authz_user.load
│   ├── autoindex.conf -> ../mods-available/autoindex.conf
│   ├── autoindex.load -> ../mods-available/autoindex.load
│   ├── deflate.conf -> ../mods-available/deflate.conf
│   ├── deflate.load -> ../mods-available/deflate.load
│   ├── dir.conf -> ../mods-available/dir.conf
│   ├── dir.load -> ../mods-available/dir.load
│   ├── env.load -> ../mods-available/env.load
│   ├── filter.load -> ../mods-available/filter.load
│   ├── mime.conf -> ../mods-available/mime.conf
│   ├── mime.load -> ../mods-available/mime.load
│   ├── mpm_event.conf -> ../mods-available/mpm_event.conf
│   ├── mpm_event.load -> ../mods-available/mpm_event.load
│   ├── negotiation.conf -> ../mods-available/negotiation.conf
│   ├── negotiation.load -> ../mods-available/negotiation.load
│   ├── setenvif.conf -> ../mods-available/setenvif.conf
│   ├── setenvif.load -> ../mods-available/setenvif.load
│   ├── status.conf -> ../mods-available/status.conf
│   └── status.load -> ../mods-available/status.load
├── ports.conf
├── sites-available
│   ├── 000-default.conf
│   └── default-ssl.conf
└── sites-enabled
    └── 000-default.conf -> ../sites-available/000-default.conf

6 directories, 175 files
服務配置 ​
  • /etc/apache2:Apache 配置目錄。所有的Apache 配置文件都在這。
  • /etc/apache2/apache2.conf:主要的Apache配置文件。可以修改 Apache 全域配置。該文件負責加載配置目錄中的許多其他文件。
  • /etc/apache2/ports.conf:該文件指定了 Apache 監聽的端口。默認情況下,Apache 監聽端口 80,當啟用提供 SSL 功能的模塊時,在端口 443 上監聽。
  • /etc/apache2/sites-available/:可以存儲每個站點 虛擬主機 的目錄。除非鏈接到目錄,否則 Apache 不會使用此目錄中的配置文件 sites-enabled。通常,所有服務配置都在此目錄中完成,然後通過使用該 a2ensite 命令鏈接到其他目錄來啟用。
  • /etc/apache2/sites-enabled/:存儲啟用每個站點 虛擬主機 的目錄。通常,這些是通過鏈接到 sites-available 目錄中的配置文件創建的a2ensite。Apache 在啟動或重新加載以編譯完整配置時讀取此目錄中的配置文件和鏈接。
  • /etc/apache2/conf-available/ 和 /etc/apache2/conf-enabled/:這些目錄與 sites-available 和目錄具有相同的關係 sites-enabled,但用於存儲不屬於虛擬主機的配置區塊。conf-available 目錄中的文件可以使用該 a2enconf 命令啟用,並使用該 a2disconf 命令禁用。
  • /etc/apache2/mods-available/ 和 /etc/apache2/mods-enabled/:這些目錄分別包含可用和啟用的模組結尾的文件 .load 包含片段以加載特定的模組,而結尾的文件 .conf 包含這些模塊的配置。使用 a2enmod 和 a2dismod 命令可以啟用和禁用模塊。
預設 DocumentRoot ​
bash
itachi@ubuntu:~$ ls /var/www/html/
index.html
Apache 的配置文件中描述 ​
bash
itachi@ubuntu:~$ cat /etc/apache2/sites-enabled/000-default.conf | grep -n "/var/www/html"
12:     DocumentRoot /var/www/html #第12行

rewrite redirect ​

rewrite mod ​
bash
itachi@ubuntu:~$ sudo a2enmod rewrite #啟動
Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart
檢查載入 ​
bash
itachi@ubuntu:~$ ls /etc/apache2/mods-enabled/ | grep "rewrite"
rewrite.load
DocumentRoot ​
  • /var/www/ 下新增目錄
bash
itachi@ubuntu:~$ sudo mkdir /var/www/mysite
  • 修改 apache2.conf

新增下面描述

bash
itachi@ubuntu:~$ sudo vi /etc/apache2/apache2.conf
<Directory /var/www/mysite>
        Options Indexes FollowSymLinks
        AllowOverride All # None 更改 All .htaccess 才能執行
        Require all granted
</Directory>
  • 修改 DocumentRoot
bash
itachi@ubuntu:~$ sudo vi /etc/apache2/sites-enabled/000-default.conf
DocumentRoot /var/www/mysite #原本 /var/www/html
redirect ​
  • 新增 .htaccess

RewriteCond:定義一條複寫測試規則。
RewriteRule:取得複寫字串,並套入重寫規則。

1.完全導向

bash
itachi@ubuntu:~$ vi /var/www/mysite/.htaccess
RewriteEngine on #啟用 `mod_rewrite`
RewriteBase / #設定比對基本目錄
RewriteRule ^(.*) http://www.google.com$1 [R=301,L] #重新導向到 google
ErrorDocument 404 /404.php #使用者輸入的檔案在此目錄下找不到,則導向到 404.php

<Files .htaccess> #.htaccess 檔案
        Require all granted #允許所有存取  
</Files>

2.針對檔案

THE_REQUEST:類似 GET / HTTP/1.1。

bash
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*test\.php\ HTTP/ #針對 test.php 檔,* 表示開頭任意但結尾要是 test.php
RewriteRule .*test\.php$ https://www.google.com [R=301,L]
ErrorDocument 404 /404.php

<Files .htaccess>
        Require all granted
</Files>
  • Apache Service 重啟
bash
itachi@ubuntu:~$ sudo service apache2 restart #重啟

基本 proxypass ​

啟動 mod ​

mod_proxy:Apache 的代理模組,用於管理連接並重新導向它們。

bash
itachi@ubuntu:~$ sudo a2enmod proxy #非 mod_proxy,它會顯示不存在。
Enabling module proxy.
To activate the new configuration, you need to run:
  service apache2 restart
檢查載入 ​
bash
itachi@ubuntu:~$ ls /etc/apache2/mods-enabled/ | grep "proxy"
proxy.conf
proxy.load
實驗 ​

新增目錄 wp 與 itachi.html 檔案

bash
itachi@ubuntu:~$ sudo mkdir /var/www/mysite/wp
itachi@ubuntu:~$ sudo touch /var/www/mysite/wp/itachi.html
itachi@ubuntu:~$ sudo vi /var/www/mysite/wp/itachi.html
<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

  <h1>This is a Heading</h1>
  <p>This is a paragraph.</p>

</body>
</html>
語法 ​

ProxyPass:遠端服務的 mirror
ProxyPassReverse:類似重新導向

bash
itachi@ubuntu:~$ sudo vim /etc/apache2/sites-enabled/000-default.conf
#新增下兩行
ProxyPass /wp/ http://192.168.7.200/404.php
ProxyPassReverse /wp/ http://192.168.7.200/404.php

基本安全 ​

1.當你輸入的 URL 出現 404 ,也沒做 ErrorDocument 404 設定。
出現敏感資訊 Apache/2.4.7 (Ubuntu) Server at 192.168.7.200 Port 80 解決

bash
itachi@ubuntu:~$ sudo vi /etc/apache2/conf-enabled/security.conf #檔案位置  
#參數 Full | OS | Minimal | Minor | Major | Prod ,顯示敏感資訊的多寡。
ServerTokens Prod # 預設 OS
#參數 On | Off | EMail
ServerSignature Off # 預設 On,敏感資訊全部顯示

2.目錄清單 把當前目錄檔案全列出 解決

bash
itachi@ubuntu:~$ sudo cat /var/www/mysite/.htaccess
RewriteEngine on
RewriteBase /
...
Options -Indexes #新增此行 - 為關閉目錄清單;+ 為開啟目錄清單
...

結果:出現 403

3.Apache更新
4.Log 檔的分析

Built with VitePress