顯示具有 apache 標籤的文章。 顯示所有文章
顯示具有 apache 標籤的文章。 顯示所有文章

2017年6月19日 星期一

Let's Encrypt 免費 SSL 憑證設定(Nginx,Apache,Postfix,Dovecot)

Let's Encrypt:

Let's Encrypt 每張免費憑證期限是90天,但廠商提供了自動更新 script,可排程檢查 SSL 期限並自動更新 SSL 憑證。

須停用 WEB 服務:
systemctl stop nginx.service (Let's Encrypt 程式會模擬  web 給 SSL 發行的網站確認用,80 port 不能被使用,所以要停 web)

下載、安裝  Let's Encrypt 套件:
CentOS:
# yum install epel-release
# yum install certbot

Ubuntu:
$ sudo apt install -y git bc wget
$ sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt (安裝 Let's Encrypt 程式到 /opt/letsencrypt)


# /opt/letsencrypt/certbot-auto certonly --standalone --email xxx@example.com --agree-tos -d example.com (可利用 -d 加多個Domain)


憑證檔案 :
安裝完的憑證會依照申請的 domain 當作資料夾名稱放到 /etc/letsencrypt/live/ 目錄下
如果同時申請了 example.com 與 www.example.com,那麼憑證檔案就會分別放在 /etc/letsencrypt/live/example.com/ 及 /etc/letsencrypt/live/www.example.com/ 目錄下

憑證檔案分別會有 4 個
檔案名稱說明
cert.pem申請網域的憑證
chain.pemLet's Encrypt 的憑證
fullchain.pemcert.pem 及 chain.pem 合併檔案
privkey.pem申請網域的憑證密鑰

設定 nginx 使用 SSL 憑證:
vi /etc/nginx/templates/ssl.tmpl
內容:
server {
    # 設定憑證檔案
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    .......
}

設定 nginx 80 port 自動轉址導向 SSL 443 port
# vi /etc/nginx/sites-enabled/00-default.conf
內容:
server {

     location ~ /\.well-known\/acme-challenge {
      allow all;
    }

    if ($request_uri !~ /\.well-known) {
      return 301 https://$host$request_uri;
    }
}

設定 Postfix 使用 SSL 憑證:
# vi /etc/postfix/main.cf
修改:
# Force TLS-encrypted SASL authentication.
smtpd_tls_auth_only = yes
smtpd_tls_security_level = may
smtpd_tls_loglevel = 0
smtpd_tls_key_file = /etc/letsencrypt/live/example.com/privkey.pem
smtpd_tls_cert_file = /etc/letsencrypt/live/example.com/cert.pem
smtpd_tls_CAfile = /etc/letsencrypt/live/example.com/fullchain.pem
tls_random_source = dev:/dev/urandom

設定 dovecot 使用 SSL 憑證:
# vi /etc/dovecot/dovecot.conf
修改:
ssl_cert = </etc/letsencrypt/live/example.com/cert.pem
ssl_key = </etc/letsencrypt/live/example.com/privkey.pem
ssl_ca = </etc/letsencrypt/live/example.com/fullchain.pem


重啟相關 Service:
# systemctl restart nginx.service postfix.service dovecot.service

測試自動更新憑證:


/opt/letsencrypt/certbot-auto renew --dry-run

加入 Crontab 排程執行憑證更新 :
crontab -e
設定每個禮拜一的凌晨 2:30 (排程自訂)進行一次憑證的檢查及更新
30 2 * * Mon /opt/letencrypt/certbot-auto renew >> /var/log/le-renewal.log; systemctl restart nginx.service postfix.service dovecot.service


這樣就有了一個半永久的 SSL 憑證


建立 Let's Encrypt 設定檔 :
複製在原本 letsencrypt 目錄下的範例設定檔 /opt/letsencrypt/examples/cli.ini
cp /opt/letsencrypt/examples/cli.ini /usr/local/etc/le-renew-webroot.ini

編輯自訂設定檔:
vi /usr/local/etc/le-renew-webroot.ini
修改:
rsa-key-size = 4096
email = xxxxx@example.com
domains = example.com, example1.com
webroot-path = /var/www/html/    # www預設目錄


使用 Script 自動更新憑證:
下載憑證更新 shell script,並將 Script 設定為可執行檔案 :
apt-get install -y curl

curl -L -o /usr/local/sbin/le-renew-webroot https://gist.githubusercontent.com/thisismitch/e1b603165523df66d5cc/raw/fbffbf358e96110d5566f13677d9bd5f4f65794c/le-renew-webroot

chmod +x /usr/local/sbin/le-renew-webroot

le-renew-webroot Script 讀取 /usr/local/etc/le-renew-webroot.ini 設定資料並進行憑證更新,若憑證還有 30 天以上才過期,則不更新憑證。

手動執行,測試 shell script 更新憑證 :
/usr/local/sbin/le-renew-webroot

Checking expiration date for example.com...
The certificate is up to date, no need for renewal (89 days left).
...........................................

如有需要可強制更新憑證:
/opt/letsencrypt/certbot-auto renew --force-renew 



設定 Apache 使用 SSL 憑證:
1.確認 Apache httpd.conf 有載入 mod_ssl 模組,如果沒有安裝 SSL 模組:
yum install -y mod_ssl openssl

2.修改要套件 SSL 的網頁 conf 設定檔:
Apache 設定使用 SSL 憑證:
<VirtualHost *:443>
  ServerName example.com
  DocumentRoot /var/www/html/example

  SSLEngine on
  SSLCertificateFile    /etc/letsencrypt/live/example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.com/fullchain.pem

  <Directory "/var/www/html/example">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

3.設定 80 port 轉址 443 port:
<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/html/example
  Redirect permanent / https://example.com/
  <Directory "/var/www/html/example">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>




2017年4月19日 星期三

GoAccess 分析 Apache、Nginx Log,監控 Server 狀態


系統平台: CentOS 7

所需的 compile 套件(環境差異有所不同):
# yum install -y gcc GeoIP-devel php-pear php-pecl-geoip ncurses-devel

下載 GoAccess 套件:
# wget http://tar.goaccess.io/goaccess-1.2.tar.gz
# tar -xzvf goaccess-1.2.tar.gz
# cd goaccess-1.2/

編譯安裝:
# ./configure --enable-utf8 --enable-geoip=legacy
# make
# make install

修改 goaccess.conf 設定檔:
vi /usr/local/etc/goaccess.conf

調整時間格式:
# The following time format works with any of the
# Apache/NGINX's log formats below.
#
time-format %H:%M:%S

調整日期格式:
# The following date format works with any of the
# Apache/NGINX's log formats below.
#
date-format %d/%b/%Y

Log 檔內容格式:
# NCSA Combined Log Format
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

更改 UI 配色(SSH 連線 Console UI):
# Choose among color schemes
# 1 : Monochrome
# 2 : Green
# 3 : Monokai (if 256-colors supported)
#
color-scheme 2

排除 IP 位址:
# Exclude an IPv4 or IPv6 from being counted.
# Ranges can be included as well using a dash in between
# the IPs (start-end).
#
#exclude-ip 127.0.0.1
#exclude-ip 192.168.0.1-192.168.0.100
#exclude-ip ::1
#exclude-ip 0:0:0:0:0:ffff:808:804-0:0:0:0:0:ffff:808:808

調整 靜態檔案(static-file) 類型:
.................................等等


即時查看(沒有特別修改 Apache、Nginx 的 Log 格式沒有修改過 goaccess.conf 設定檔):
# /usr/local/bin/goaccess -f /var/log/nginx/access.log --log-format=COMBINED

沒修改 goaccess.conf、沒加 --log-format=COMBINED 啟動即時查看時,會先彈出要選擇 "Log Format Configuration"

Dashboard 欄位說明:
1.Unique visitors per day – Including spiders(分析每天多少不同的IP造訪)
2.Requested files (Pages-URL)(瀏覽的動態網頁)
3.Requested static files – (Static content: png,js,etc)(瀏覽的靜態檔案)
4.HTTP 404 Not Found response code (HTTP 404 錯誤的頁面)
5.Hosts (拜訪最多次數的IP,DDos常常看這裡)
6.Operating Systems (分析來客的系統,但這個參數可以偽裝)
7.Browsers (分析來客的瀏覽器,同樣可以偽裝)
8.Referrers URLs (最多請求的 URL)
9.Referring Sites (從哪裡URL來源鏈結過來的)
10.Top Keyphrases used on Google’s search engine (分析Google熱門關鍵字,似乎沒有辦法分析)
11.Geo Location (來源的地區)
12.HTTP Status Codes (HTTP狀態,可以拿來看網站的穩定度)
(Dashboard 欄位說明來源: Mr. 沙先生 https://shazi.info/)


動態更新 goaccess report.html 網頁(Console 下執行,執行後顯示持續執行中狀態):
# goaccess /var/log/nginx/access.log -o /var/www/html/report.html --real-time-html

WEB UI 重新整理(F5) 資訊即時更新。

crontab 排程產生網頁(每五分鐘更新一次):
*/5 * * * * /usr/local/bin/goaccess -f /var/log/nginx/access.log -a -o /var/www/html/web-report.html

瀏覽器連線:
http://host/web-report.html


其他用法:
goaccess 也附加了 GeoIP,用來分析 IP 的位置 :
# geoiplookup www.hinet.net

GeoIP Country Edition: TW, Taiwan


備註,以下設定都可在 goaccess.conf 裡設定:
排除 IP:
# /usr/local/bin/goaccess -f /var/log/nginx/access.log -e 192.168.1.1
# /usr/local/bin/goaccess -f /var/log/nginx/access.log -e 192.168.1.1-192.168.1.20

-e 的參數可以排除指定的 IP,也可以用範圍,但不支援 netmask

更改配色(SSH 連線 Console UI):
# /usr/local/bin/goaccess -f /var/log/nginx/access.log --color-scheme=(預設 1 灰色, 2 綠色)




2017年1月17日 星期二

.htaccess 網頁認證的方式

1.建立 .htaccess
在要限制的網頁目錄裡(例: /var/www/html/xxxxx),建立一個 .htaccess 的檔案
.htaccess 內容:
AuthName "User Account Login"    #網頁上顯示的文字
AuthUserFile /var/www/.htpasswd    #.htaccess 密碼檔存放位置,儘量不要放在 www 目錄下
AuthType Basic
require valid-user

2. 產生帳號密碼檔
利用 htpasswd 指令來產生帳號密碼檔

1.新建一個登入帳號 為 test,
如 /var/www/.htpasswd 帳號密碼檔還不存在,則需要輸入下列指令:

       htpasswd -c /var/www/.htpasswd test

(需特別注意的是,如果 /var/www/.htpasswd 已經存在的話,不要再使用 -c 參數,因為它會覆蓋掉原本/var/www/.htpasswd 的內容)

2.再新建一個登入帳號 為 admin,而這個時候 /var/www/.htpasswd 已經存在,則只需要輸入下列指令,

       htpasswd  /var/www/.htpasswd admin

輸入此使用者的密碼,輸入完畢後,在 /var/www/.htpasswd 就會看到剛剛新建的使用者帳號及密碼的資料了。


CentOS 6 Apache 設定方式 .htaccess:
apache 預設應開是開啟此功能的,在 http.conf 裡
確認 .htaccess 是可生效的
AccessFileName .htaccess
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>
新增保護的網頁:
<Directory "/var/www/html/xxxxx">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>


CentOS 7 Apache 設定方式 .htaccess:
新增保護的網頁 /etc/httpd/conf.d/xxxx.conf:
<Directory "/var/www/html/xxxxx">
    AllowOverride All
    Require all granted
</Directory>





CubeCOS 虛擬機刪除網路介面,且主機內沒有 virtio 驅動程式成為孤島主機時,進行 virtio iso 掛載流程

 # 當 CubeCOS 虛擬機刪除網路介面,且主機內沒有 virtio 驅動程式成為孤島主機時,進行 virtio iso 掛載流程。 # 步驟一:確認目標虛擬機的底層名稱 # 在 Compute Node(計算節點)上列出運行中的 instance,並反查對應的 OpenSt...