2024年7月11日 星期四

 在ubuntu中安裝gsutil,把log 轉到google cloud storge

安装 gsutil

  1. 安装 Google Cloud SDK

首先,下载并安装 Google Cloud SDK:


sudo apt-get update && sudo apt-get install -y curl apt-transport-https ca-certificates gnupg

添加 Google Cloud 的公钥:


echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -

更新并安装 Google Cloud SDK:


sudo apt-get update && sudo apt-get install google-cloud-sdk
  1. 初始化 Google Cloud SDK

安装完成后,初始化 Google Cloud SDK:


gcloud init

按照提示登录 Google 帐户并设置项目。

  1. 安装 gsutil

gsutil 是 Google Cloud SDK 的一部分,安装 SDK 后,gsutil 就已经包含在内了。您可以通过以下命令验证安装:


gsutil version

配置并使用 gsutil

  1. 创建上传脚本

创建一个脚本来将日志文件上传到 GCS。例如,在 /usr/local/bin/upload_logs_to_gcs.sh 创建脚本:


sudo nano /usr/local/bin/upload_logs_to_gcs.sh

添加以下内容:


#!/bin/bash # 定义日志文件和GCS路径 LOG_DIR="/var/log/apache2" GCS_BUCKET="gs://niea-log-data/IP60apache-logs" # 使用 gsutil 将日志文件复制到 GCS gsutil -m cp ${LOG_DIR}/*.log ${GCS_BUCKET}/

保存并关闭文件,然后使脚本可执行:


sudo chmod +x /usr/local/bin/upload_logs_to_gcs.sh
  1. 设置定时任务

编辑 cron 任务计划:


sudo crontab -e

添加以下行来每小时运行一次脚本:


58 * * * * /usr/local/bin/upload_logs_to_gcs.sh

测试配置

您可以手动运行脚本来验证其工作是否正常:


/usr/local/bin/upload_logs_to_gcs.sh

检查 GCS 存储桶中的目标目录,确认日志文件已成功上传。

通过这些步骤,您的日志文件将每小时自动上传到 GCS,确保日志的备份和存档。

2023年3月21日 星期二

網頁安全

 

HTTP Host Header Injection


在 .htaccess 中
<IfModule mod_headers.c>
Header unset X-Forwarded-Host
</IfModule>

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.aaa.com [NC]
    #RewriteCond %{HTTP_HOST} !^(www.aaa.com|abcdef.com)$ [NC]
    RewriteCond %{REQUEST_URI} !^/error [NC]
    RewriteRule ^.(.*) - [L,F]

2021年10月29日 星期五

Ubuntu 一些小事項

 1.防火牆可以加開DNS

To open ports 53 & 43 on the UFW firewall run the commands below:

$ sudo ufw allow 53/tcp
$ sudo ufw allow 43/tcp
$ sudo ufw reload

Command Line 測試localhost 

        lynx "http://localhost"


Start Apache Web Server

        sudo systemctl start apache2

Check status 

        sudo systemctl status apache2


檢查埠號是否有開啟

        nc -zvw3 192.168.1.10 80

檢查網路狀態

        sudo netstat -nlp


解決啟動 Apache 網站伺服器時找不到 ServerName 的問題

hostname www.yourdomain.com

https://blog.miniasp.com/post/2012/06/23/apache2-Could-not-reliably-determine-the-server-fully-qualified-domain-name-using-for-ServerName


Apache 不紀錄某些log

SetEnvIf Remote_Addr "::1" dontlog

#不紀錄來自127.0.0.1的連線
SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog
CustomLog logs/access_log common env=!dontlog

#不紀錄存取robot.txt的連線
SetEnvIf Request_URI "^/robots\.txt$" dontlog
CustomLog logs/access_log common env=!dontlog











2021年9月4日 星期六

產生CSR檔及建立伺服器SSL

 啟用 ssl 模組

參閱2018 SSL安裝

PHP 業餘玩家: SSL 安裝 (beclass.blogspot.com)

a2enmod ssl
啟用 ssl 虛擬站台
a2ensite default-ssl
重啟 apache
sudo service apache2 restart
修改憑證路徑(private.key,public.crt/pem)
sudo nano /etc/apache2/sites-enabled/default-ssl.conf

**記得防火牆443要開
-------------------------
 SSLCertificateFile        /etc/ssl/6123456.crt
 SSLCertificateKeyFile     /etc/ssl/your_own.key
 SSLCertificateChainFile   /etc/ssl/gd_bundle-g2-g1.crt
--------------------------------------------------------
The .crt file is sent to everything that connects; it is public.
 (chown root:root and chmod 644)
基本上 crt:

sudo chmod 755 /etc/ssl/ur_own_define_ssl_folder
sudo chown -R root:root /etc/ssl/ur_own_define_ssl_folder/
sudo chmod 644 /etc/ssl/ur_own_define_ssl_folder/*.crt

To add to the private key location; make sure you secure it properly as well as having it in there. (chown root:ssl-cert and chmod 640)

sudo chmod 710 /etc/ssl/private
sudo chown -R root:ssl-cert /etc/ssl/private/
sudo chmod 640 /etc/ssl/private/*.key

----------------------------------------------------------------------
apache2ctl configtest
Restart Apache2 service
sudo service apache2 restart

or

sudo systemctl restart apache2.service
-----------------------------------------------------------

產生CSR檔

使用root登入後,執行:

openssl req -new -newkey rsa:2048 -nodes -keyout  您的網域.key -out 您的網域.csr

說明:

你將需要提供以下資訊給你的CSR檔:
Country Name (2 letter code) [AU]: TW
State or Province Name (full name) [Some-State]: Taiwan
Locality Name (eg, city) []: Taipei 
Organization Name (eg, company) [Internet Widgits Pty Ltd]: Hello Company
Organizational Unit Name (eg, section) []: IT
Common Name (eg, YOUR name) []: www.website.com (網站網址)
Email Address []: service@websit.com

Please enter the following ‘extra’ attributes to be sent with your certificate request

A challenge password []: 直接Enter
An optional company name []: 直接Enter -

在文字編輯器內開啟 CSR,並複製全部文字內容。
將全部 CSR 貼至您帳戶中的 SSL 註冊表。