Posts

Showing posts with the label Apache

如何在 apache2 上面啟用 http/2 on CentOS7

CentOS 的 repo 某些套件更新相當的保守 例如 apache 跟 php 就是很實在的例子 目前我使用的 CentOS7 是 印象中 apache 的版本在 CentOS7 上始終停留在 2.4.6 由於 apache 支援的 http/2 功能在 2.4.17 才會出現 (不過 2.4.26 版本不安全,請升級到後續的版本) 所以要先想辦法將 apache 升級 如果不想自己 compile 的話 可以參考這篇的做法 1. 先查詢 apache 的版本 可以用 httpd -v 或是 yum info httpd 查詢 2. 先確認是否安裝 epel-release $ sudo yum install -y epel-release 3. 下載並安裝 CodeIT repo $ cd /etc/yum.repos.d && wget https://repo.codeit.guru/codeit.el`rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release)`.repo 4. 使用 yum info httpd 查詢是否可抓到目前最新版的 httpd 5. 如果還沒安裝 apache 可以輸入 $ yum install httpd 6. 如果要升級 apache 的話 $ yum update httpd-* 7. 重啟 apache $ systemctl restart httpd 接下來要設定讓 site 支援 http/2 1. 要把下面這行 Protocols h2 http/1.1 2. 加入 ssl.conf 或是 httpd.conf < VirtualHost *:443 > Protocols h2 http/1.1 ServerAdmin you@your-awesome-site.com ServerName your-awesome-site.com < /VirtualHost > 3. 重啟 apache $ systemctl restart httpd 如果想要測試可以用 ...

CentOS7: How to install Mono in the apache2

CentOS7 上面要支援 ASP / ASP.NET 有兩種方式 一種是本篇文章要介紹的 Mono 另外一種是 Microsoft 自己開發的 DotNET Core 兩種都有跨平台支援 網路上的教學安裝了太多的套件 我只想跑 ASP 而已 僅列出我需要的環境教學 我的環境是: CentOS 7.5.1804 x86_64 Apache 2.4 安裝步驟 1. 匯入 Repo $ rpm --import "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" $ su -c 'curl https://download.mono-project.com/repo/centos7-stable.repo | tee /etc/yum.repos.d/mono-centos7-stable.repo' 2. 安裝 mono 套件 $ yum install mod_mono 3. 修改 mod_mono.conf $ vi /etc/httpd/conf.d/mod_mono.conf 將這段加到最下面 MonoServerPath /usr/bin/mod-mono-server4 如果要跑 asp 請加入這行 AddType application/x-asp-net .asp 如果要讓 index.asp 變為預設首頁請加入這行 DirectoryIndex index.asp 4. 重開 apache $ systemctl restart httpd

Websocket error

Image
前端使用 React 開發 利用 restful API 來串接資料庫的部分 在本機開發的時候不會有 websocket 連接 但放到 server 上就會跑出來 而且會有 http error 400 的錯誤 妙的是 Connecting 居然是 Upgrade 網路上搜尋到分別有 Apache 跟 Nginx 的解法 我是參考 fuatsengul 說的這部分去做設定的 ServerName mysite.com ServerAlias www.mysite.com SSLEngine on SSLProxyEngine On ProxyRequests Off SSLCertificateFile /etc/apache2/ssl/mysite.com.crt SSLCertificateKeyFile /etc/apache2/ssl/mysite.com.key SSLCertificateChainFile /etc/apache2/ssl/ca.cer DocumentRoot /var/www/errorPages ErrorDocument 503 /503.html ProxyPass /503.html ! ProxyPass / http://localhost:3999/ ProxyPassReverse / http://localhost:3999/ RewriteEngine on RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC] RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC] RewriteRule .* ws://localhost:3999%{REQUEST_URI} [P] 主要是紅色區塊那部分 前面那兩行 ProxyPass & ProxyPassReverse 我本來就有加 主要是將 port 3000 導到 port 80 加上紅色區塊後(需要依照自己主機設定修改) 連線就正常了 如下圖

Apache 2.4 : How to allow a specific url ?

如果你有個網站是不允許外部 ip 連入 只永許特定的 ip 連入 這時候可以利用 SetEnv 環境變數的方式來達到目的 這個方法僅適用於 Apache 2.4 的版本 之前的版本可參考參考資源裡面的設定 以下的範例是說明 開放 https://xxx.your.domain/exceed/xxx 給外部的意思 不受限於 Require all denied 注意 SetEnv 最後面的參數跟 Require not 後面的參數要一致 <VirtualHost *:443> DocumentRoot "/var/www/html/xxx" ServerName xxx.your.domain CustomLog /var/log/httpd/xxx.your.domain-access.log combined ErrorLog /var/log/httpd/xxx.your.domain-error.log SSLEngine On SSLCertificateFile /etc/letsencrypt/live/xxx.your.domain/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/xxx.your.domain/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/xxx.your.domain/fullchain.pem <Directory "/var/www/html/xxx"> AllowOverride All Require all denied Require ip 1.2.3.4 Require ip 5.6.7.8 # allow specific url SetEnvIf Request_URI "^/exceed/$" exceed_url <RequireAny> <RequireAll> ...

手動更新 GeoIP 資料庫

Image
GeoIP 可以拿來識別 IP 的來源 可以 經由 php 檔案的載入 來判斷 也可以經由 apache 的 mod_geoip 套件來載入 利用 apache 的套件載入判斷速度會比較有效率 而 geoip 的值也會直接寫入到 $_SERVER 變數裡面 對於開發者來說會比較方便 這張圖就是裝了 mod_geoip 後多出來的 $_SERVER 變數 GeoIP 是由 maxmind  維護的 maxmind 有提供自動更新 GeoIP.dat 的 solution 雖然說 mod_geoip 是套件 但基本上更新 GeoIP database 的機率是非常低的 這邊提供一個可以手動更新的方式 可以到這邊下載最新的 GeoLite City 資料庫 http://dev.maxmind.com/geoip/legacy/geolite/ 下載完後到主機上找尋 GeoIP.dat 檔案 把檔案至換成下載的那個檔案 以 CentOS7 來說會是在 # /usr/share/GeoIP/GeoIP.dat 而經由替換新的 GeoIP 資料庫後 $_SERVER 的變數也會像上面那張圖多了經緯度跟郵遞區號等其他資訊 以我的經驗來看,準了很多喔! 原本資料庫大概 1.3M 更新後的資料庫有 15.2M 就勤勞一點手動更新吧! 參考來源: http://piwik.org/faq/how-to/faq_163/

Apache run multiple SSL Virtual Host

基本上要 run single SSL site 在 Apache 上還蠻容易的 但再加上了一個 SSL site 問題就來了 不就在 SSL zone (httpd-ssl.conf)那邊加上另外一個 Virtual Host 就好了嗎? SSL 的 Virtual Host 從 Name Based 換到 IP Based 再加上額外的 Private IP 也是不行 後來用瀏覽器連 Private IP 的 SSL 卻是正常的 才想到說應該把那 Private IP 也 mapping 到另外一個 Public IP 由於我們公司主機在防火牆下 所以除了更改 DNS 外,還有修改一下防火牆的規則 舉個實際例子說明: Public IP 200.200.200.1 (80 & 443) 原本對應到 Private IP 10.1.1.1 後來加入了一個 10.1.1.2 的 Private IP 但連 https://b.test.com 他顯示的認證還是 b.test.com 的 如果只有一個 Public IP 那就只能指定其他的 port(如 https://200.200.200.1:4433) 了 80 跟 443 以外的 port 一般人都蠻難接受的(要公開的話很不方便,自用就還好) 所以還是多個 IP 好辦事阿~ 提供 Apache 的相關設定供參考 這邊僅提供 SSL Virtual Host 的部份 我並沒有去設別設定 Port 80 (httpd-vhost.conf)那邊 Port 80 (httpd-vhost.conf)那邊還是維持 Name Based 的方式 Listen 10.1.1.1:443 Listen 10.1.1.2:443 NameVirtualHost 10.1.1.1:443 NameVirtualHost 10.1.1.2:443   DocumentRoot "/home/www/a.test.com"   ServerName a.test.com:443   ServerAdmin service@test.com   ErrorLog "/usr/local/apache2/logs/error...

裝了 Drupal6 就無法開啟 server-status & server-info 的問題

這個問題存在很久了 以前如果遇到 apache 有問題就會到 server-info & server-status 看一下 但自從使用 Druapl 後就沒看過 前幾天突然想到這應該跟 .htaccess 有關 查了一下資料只要在 drupal 下的 .htaccess 做一下修改 就可以正常看到囉! 找到 < IfModule mod_rewrite.c > 確認 RewriteEngine on (如果是 off 的話 druapl 的 clean url 會失效,所以正常都是 on 的) 在下面加入這兩段 RewriteCond %{REQUEST_URI} !=/server-status RewriteCond %{REQUEST_URI} !=/server-info 如果您的 server-info 及 server-status 還是看不到 請確認是否有開啟 apache 的 mod_status 及 mod_info mod_status 可以觀察到什麼 The number of children serving requests The number of idle children The status of each child, the number of requests that child has performed and the total number of bytes served by child A total number of accesses and byte count served The time the server was started/restarted and the time it has been running for Averages giving the number of requests per second, the number of bytes served per second and the average number of bytes per request The current percentage CPU used by each child and in total ...

Apache 的 .htaccess 失效?(續)

如果你 照著此篇做 還是發現 .htaccess 檔起不了作用 那應該是你有用 url rewrite 的功能 不知道 Apache 2 從那一版開始 預設將 mod_rewrite 給關閉 #LoadModule rewrite_module modules/mod_rewrite.so 把 # 的部份移除重開 Apache 即可

Apache 的 .htaccess 失效?

如果你的 Apache .htaccess 設定檔無效 請先檢查 httpd.conf 中的 <Directory>...</Directory> 中的 AllowOverride 設定是否為 None 改成 AllowOverrider All 就可以了 由於安全性等的考慮 Apache 可能預設會是 None 如果你只有某些地方需要支援 .htaccess 設定 可指定目錄支援即可 <Directory "/var/www/html"> AllowOverride All </Directory> 參考來源: [求助] Apache主機.htaccess強迫編碼設定

How to setup Virtual Host/Directory in Apache 2.2 ?

Image
Virtual Host/Direcroty (虛擬主機/目錄)在 Apache 中是個很常用的功能 基本上常用的 Web Server (如 IIS )都有支援 他的好處是可以用不同的 DNS 去對應不同的目錄 這樣一來,一台機器就可以架上數十個甚至上百個站 Apache 的 Virtual Host 分成兩種: IP based IP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address. name based Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless there is a specific reason to choose IP-based virtual ...