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

3月 11, 2025

Google Cloud 災難復原

最後處理方式:手動重建 instance

前幾天突然想到 GCP 上跑的機台很舊了(Debian9, 2018),嘗試升級 Debian 10 後就炸了!無法遠端連線(SSH)而且資料庫主程式損毀無法動啟,幸好有留下資料庫檔(.frm/.ibd)。

1. 先到「GCP Compute Engine / 磁碟」複製一份原本的磁碟檔,避免破壞既有資料

2. 建立新的執行個體,主系統選 Debian12 並且額外連接現有磁碟(上述複製碟)

3. 開機掛載分割區,安裝與置換 MariaDB 資料夾
# 新機台安裝資料庫系統並嘗試匯出資料
# apt-get install mariadb-server
# lsblk
# mount /dev/sdb1 /mnt/rescue
# /etc/init.d/mariadb stop
# mv /var/lib/mysql /var/lib/_mysql
# cp -R /mnt/rescue/var/lib/mysql /var/lib/ -R
# chown -R mysql:mysql mysql 
# /etc/init.d/mariadb start
#
# 匯出資料庫檔案
# mysqldump -u db_user -p --default-character-set=utf8 mydb > mydb_backup.sql
4. 維持使用 Debian9 系統 --
「Compute Engine / 映像檔」顯示已淘汰的映像檔,可選擇過期映像檔(debian-9-stretch-v20220621)並建立執行個體

5. 最初掛載 Debian12 到原本 instance 時顯示錯誤訊息 UEFI setting must be the same for the instance and the boot disk. 所以最後只能停留在舊的 Debian9 映像檔;使用 GCP 好處就是 instance 與 disk 是分開的,可以隨意配接取出資料。

12月 02, 2024

開源遠端桌面閘道(Apache Guacamole)

Apache Guacamole

A clientless remote desktop gateway. It supports standard protocols like VNC, RDP, and SSH. We call it clientless because no plugins or client software are required. Guacamole is installed on a server, all you need to access your desktops is a web browser.

遠端桌面閘道(RD Gateway)支援 RDP 及 SSH 協定;使用者透過瀏覽器可直接存取遠端桌面,無需設定。

9月 18, 2024

自建 Uptime Kuma 監測平台

安裝 Node.js 與 PM2(Process Management)
# apt-get -y update
# apt-get install -y nodejs npm git
# npm install pm2 -g
# pm2 install pm2-logrotate
安裝 Uptime Kuma
# git clone https://github.com/louislam/uptime-kuma.git
# cd uptime-kuma
# npm run setup
# pm2 start server/server.js --name uptime-kuma
進行初次設定 http://localhost:3001

1月 03, 2024

在 Oracle Cloud 上安裝 Alpine Linux

1. 建立一個以 Ubuntu 為作業系統的虛擬機(當作空殼)

2. 到 Alpine Linux 官網下載 VIRTUAL 版本的映像檔。例如 alpine-virt-3.19.0-x86_64.iso

3. 刷入映像檔

# sudo dd if=alpine-virt-3.19.0-x86_64.iso of=/dev/sda

4. 重新開機

5. 使用 Oracle Cloud 主控台連線(console)操作,進行安裝前準備

# mkdir /media/setup
# cp -a /media/sda/* /media/setup
# mkdir /lib/setup
# cp -a /.modloop/* /lib/setup
# /etc/init.d/modloop stop
# umount /dev/sda
# mv /media/setup/* /media/sda/
# mv /lib/setup/* /.modloop/
6. 循原本方式進行系統安裝(setup-alpine),網路介面直接用 eth0(dhcp)。安裝後驚人的空間使用(僅150MB)

12月 31, 2023

在 Debian 下使用 libusb 開發程式

apt-get install gcc usbutils libusb-1.0-0-dev

1月 26, 2022

antiX Linux 設定教學

antiX Linux 輕量級作業系統(基於Debian發行版),映像檔選 Base 版才帶有視窗介面。實際安裝只要 5GB硬碟空間與1G RAM 就能運行系統核心與瀏覽器。預設使用 IceWM 作為視窗管理介面,運作不拖泥帶水,很棒!

安裝後的微調:

1. 更改顯示器解析度
在檔案 ~/.desktop-session/startup 內新增:
xrandr --output Virtual1 --primary --mode 1440x900

2. 安裝檔案總管,直接改成 PCManFM 比較順手
# apt-get install pcmanfm

3. 整理開始選單項目,直接修改 ~/.icewm/menu 移除用不到的軟體項。

4. 更改Hotkey Mapping
在 icewm 預設熱鍵 [Ctrl]+[Space] 常造成誤觸,修改 $HOME/.icewm/keys 註解該行設定。

5. 建立桌面捷徑圖示
應用程式鏈結為 *.desktop 設定檔,大多存在 /usr/share/applications/ 之下,最快的方式是透過 PCManFM 開啟資料夾,直接把圖示拖曳到桌面。

5. 安裝常用套件
#apt-get install 
qalculate-gtk google-chrome-stable

6. 安裝中文輸入法
# apt-get install fcitx fcitx-module-x11 fcitx-m17n fcitx-ui-classic fcitx-table-boshiamy
修改設定檔 ~/.desktop-session/desktop-session.conf

export XMODIFIERS=@im=fcitx
export DefaultIMModule=fcitx
fcitx-autostart &
7. 安裝 LibreOffice
下載 LibreOffice_[ver]_Linux_x86-64_deb.tar.gz 解壓縮,然後用 dpkg -i *.deb 整個裝完。

大概這樣能應付平日作業,算是容易調教又不吃資源的 Linux 發行版。

1月 17, 2018

使用 PHP 連接 Microsoft SQL Server 資料庫

依不同的 PHP 版本而定...

PHP 5.x

上古時代想連接 SQL Server 則要透過 FreeTDS 開源套件,說明如下:
FreeTDS is a set of libraries for Unix and Linux that allows your programs to natively talk to Microsoft SQL Server and Sybase databases.
; 安裝所需套件
# apt-get install freetds-common freetds-bin unixodbc php5-sybase

其中 php5-sybase 就包括 mssql.so 函式庫,所以基本上一行指令可以搞定所有事;之後更改 FreeTDS 相關參數(TDS連線協定版本)與連線編碼(UTF8支援中文字):

設定 /etc/freetds/freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
client charset = UTF-8

連線測試可用下列指令:
tsql -S DBserver -p 1433 -U dbadmin -P dbpass
1> SELECT  @@servername
2> GO
3> SELECT @@servicename
4> GO

在 PHP 程式中使用下列指令進行連線/查詢 (PHP7中捨棄):
  • mssql_connect()
  • mssql_query()
  • mssql_fetch_array()

 PHP 7.x

微軟針對這個PHP版本所開發延伸套件:Microsoft Drivers for PHP for SQL Server(專案網址)
; 在 /etc/apt/sources.list 加入 APT 套件庫
deb https://packages.microsoft.com/debian/8/prod jessie main

; 安裝套件
# apt-get install php-pear msodbcsql mssql-tools unixodbc-dev
# pecl install sqlsrv
# pecl install pdo_sqlsrv

; 掛載函式庫
extension=sqlsrv.so
extension=pdo_sqlsrv.so

在 PHP 程式中使用下列指令進行連線/查詢:
  • sqlsrv_connect()
  • sqlsrv_query()
  • sqlsrv_fetch_array()

12月 04, 2014

Lenovo ThinkPad X240 重開機後亮度回復最大值

新買的筆記型電腦 Lenovo ThinkPad X240 在安裝 Lubuntu 14.04 LTS 版本後一直有個問題,每次重新開機後「亮度」都會跳回最大值。 最有效的方法是在開機環境檔(rc.local)內直接指定亮度,每次開機均會維持固定值。
# vim /etc/rc.local
echo 30 > /sys/class/backlight/acpi_video0/brightness
exit 0 
都什麼時候了...還在這樣調亮度。

6月 25, 2014

LXDE Ubuntu chinese fonts

最近找到一個非常棒的字體給 Ubuntu 使用,字型名稱:文泉驛微米黑(wqy-microhei.ttc),其實就是黑體系列的字型。

用起來的效果像這樣: wiki維基百科Mobile01網站

安裝方式:
  1. 把字型檔丟到 /usr/share/fonts/truetype/wqy/ 目錄下 (手動建立)
  2. 更新字型庫 fc-cache -v -f
[ 2014/07/16 UPDATE ]
思源黑體:字型系列具有七種粗細,可為日文、韓文、繁體中文和簡體中文提供完整支援,這些全都包含在一種字體之中

5月 15, 2014

在 Linux 環境下使用無蝦米輸入法 (gcin 與 noseeing.gtab)

試用 Ubuntu 14.04 內建的輸入法系統 IBus (Intelligent Input Bus) 後第一個感想是:難用到爆炸!尤其對無蝦米用戶來說,有些字莫名其妙就是送不出來。像是「表」「系」,明明都在字根表內,但不論鍵盤怎麼敲,輸入法就是無動於衷,只好忍痛跟 IBus 說再見,改用 gcin 當作輸入法框架。

1. 安裝 gcin 輸入法框架
# apt-get install gcin

2. 在 gcin 裡面預設,無蝦米的字根表叫:noseeing.gtab,所以只要把字根表丟到 ~/.gcin 目錄下就可以了,這個字根檔網路上有一堆。
$ mv noseeing.gtab ~/.gcin

3. 記得重新切換預設輸入法
$ im-config

1月 02, 2014

[MySQL] Can't create database 'performance_schema'

在執行完 MySQL upgrade 後就炸掉了...
/etc/mysql/debian-start[4802]: ERROR 1007 (HY000) at line 160: Can't create database 'performance_schema'; database exists
修正方式 :
# rm -rf /var/lib/mysql/performance_schema
# mysql_upgrade -u root -p

12月 13, 2013

Debian 第一次設定 rtorrent 就上手

零、概念:
User / Browser -->Web Server-->rutorrent--scgi:5000--> rtorrent
前端叫:rutorrent (這是 php 寫成的網頁)
後端叫:rtorrent (兩者差一個"u"字 別搞錯了)

使用者用瀏覽器操作 UI 介面,訊號透過 SCGI 傳到後端的 rtorrent 程式。
一、安裝 web server、php、rtorrent、screen
 # apt-get install lighttpd rtorrent php5-cgi screen
Reading package lists... Done
  Building dependency tree
  Reading state information... Done
  The following NEW packages will be installed:
    lighttpd php5-cgi php5-common rtorrent spawn-fcgi
  After this operation, 23.4 MB of additional disk space will be used.

二、啟用 php 模組
# vim /etc/lighttpd/conf-enabled/15-fastcgi-php.conf
server.modules += ( "mod_fastcgi" )
fastcgi.server += ( ".php" =>
  ((
      "bin-path" => "/usr/bin/php-cgi",
      "socket" => "/var/run/lighttpd/php.socket",
      "max-procs" => 1,
      "bin-environment" => (
          "PHP_FCGI_CHILDREN" => "0",
          "PHP_FCGI_MAX_REQUESTS" => "10000"
      ),
      "bin-copy-environment" => (
              "PATH", "SHELL", "USER"
      ),
      "broken-scriptfilename" => "enable"
   ))
)

三、開啟 Web Server 上的 SCGI 通道 (使用 Port 5000,連線名稱 RPC2)
# vim /etc/lighttpd/conf-enabled/20-bt.conf
server.modules += ( "mod_scgi" )
scgi.server = (
"/RPC2"=>
 (
 "127.0.0.1" =>
  (
   "host" => "127.0.0.1",
   "port" => 5000,
   "check-local" => "disable"
  )
 )
)

四、建立「家目錄」網頁鏈結(假設用戶 jack) 並重新啟動 Web Server
# ln -s /home/jack /var/www/jack
# /etc/init.d/lighttpd restart
* Stopping web server lighttpd             [ OK ]
* Starting web server lighttpd             [ OK ]

五、接下來請用一般用戶(jack)權限,我們要開始設定 Web UI 部份。
$ cd ~
$ wget http://dl.bintray.com/novik65/generic/rutorrent-3.6.tar.gz
$ tar -zxvf rutorrent-3.6.tar.gz
$ chmod -R 777 ./rutorrent/share/
$ mkdir /tmp /bt  /bt/session

六、設定使用者 rtorrent 環境檔案:.rtorrent.rc
$ vim ~/.rtorrent.rc
# Maximum and minimum number of peers to connect to per torrent.
min_peers = 20
max_peers = 60

# Same as above but for seeding completed torrents
min_peers_seed = 1
max_peers_seed = 1

# Maximum number of simultanious uploads per torrent.
max_uploads = 1

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 1500
upload_rate = 1

# Default directory to save the downloaded torrents.
directory = ./

# Default session directory.
session = ./session

# For Web UI (Rutorrent @ SCGI port 5000)
scgi_port = localhost:5000
encoding_list = UTF-8

# Watch a directory for new torrents
schedule = watch_directory,5,5, "load_start=./*.torrent, d.set_custom1=./../tmp"
system.method.set_key = event.download.finished,move_complete,"execute=mv,-u,$d.get_base_path=,$d.get_custom1=;"

# Enable DHT.
dht = on
dht_port = 6778

# Enable peer exchange
peer_exchange = yes

七、執行 rtorrent 並用 screen 丟在背景執行 (Ctrl+a d)
$ screen
$ cd bt
~/bt$ rtorrent

八、設定結束了。上傳種子有兩種方式:
(a) 透過網頁 http://x.x.x.x/jack/rutorrent/ 進行種子上傳。
(b) 透過 FTP 將種子上傳到 ~/bt/ 目錄下。
九、請記得:rtorrent 一定要在 screen 裡面執行,才能確保 torrent 運作不中斷。

12月 02, 2012

我的 rtorrent.rc

2021 (rtorrent 0.9.7)

# rtorrent (version 0.9.X)
# directory tree [~/bt/session] && [~/bt/unfinished] && [~/done]

# Maximum and minimum number of peers to connect to per torrent.
throttle.min_peers.normal.set = 20
throttle.max_peers.normal.set = 70

# Same as above but for seeding completed torrents (-1 = same as downloading)
throttle.min_peers.seed.set = 1
throttle.max_peers.seed.set = 1

# Maximum number of simultanious uploads per torrent.
throttle.max_uploads.set = 1

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 2048
upload_rate = 1

# Default directory to save the downloaded torrents.
directory.default.set = ~/bt/unfinished

# Default session directory. Make sure you don't run multiple instance
session.path.set = ~/bt/session
schedule2 = watch_directory,5,5,load.start=~/bt/*.torrent

# Move finished file to [done] directory
method.insert = d.get_finished_dir, simple, "cat=~/done/,$d.custom1="
method.insert = d.data_path, simple, "if=(d.is_multi_file), (cat,(d.directory),/), (cat,(d.directory),/,(d.name))"
method.insert = d.move_to_complete, simple, "d.directory.set=$argument.1=; execute=mkdir,-p,$argument.1=; execute=mv,-u,$argument.0=,$argument.1=; d.save_full_session="
method.set_key = event.download.finished,move_complete,"d.move_to_complete=$d.data_path=,$d.get_finished_dir="

# Close torrents when disk-space less than 1000M.
schedule2 = low_diskspace,5,60,close_low_diskspace=1000M

# For web UI -- rutorrent
encoding_list = UTF-8
scgi_port = 127.0.0.1:5000

2012

# Maximum and minimum number of peers to connect to per torrent.
min_peers = 20
max_peers = 70

# Same as above but for seeding completed torrents
min_peers_seed = 1
max_peers_seed = 1

# Maximum number of simultanious uploads per torrent.
max_uploads = 1

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 700
upload_rate = 2

# Default directory to save the downloaded torrents.
directory = ./

# Default session directory.
session = ./session

# Watch a directory for new torrents.
schedule = 
  watch_directory,5,5, 
  "load_start=./*.torrent, d.set_custom1=./../Download"

# Move completed files to Directory [custom1]
system.method.set_key = 
  event.download.finished,move_complete,
  "execute=mv,-u,$d.get_base_path=,$d.get_custom1=;"

# Enable DHT support for trackerless torrents.
dht = on
dht_port = 6778

# Enable peer exchange (for torrents not marked private)
peer_exchange = yes

8月 19, 2012

Screen: No more PTY's. Sorry could not find a PTY

今天在設定一台 VPS 時碰到的狀況,一般用戶無法正常執行 screen 套件,錯誤訊息是:
No more PTY's. Sorry could not find a PTY.
解法:
(1) 先確認只有一般用戶會碰到這個狀況,而 root 沒這個問題。
(2) 檢查一下 pty* 檔案的權限
$ ls -l /dev/ptmx
crw-rw-rw- 1 root tty 5, 2 Jul 31 07:35 /dev/ptmx
$ ls -l /dev/pty*
crw-rw-rw- 1 root tty 2, 0 Jul 31 01:19 /dev/ptyp0
一般用戶應都能寫入,所以並非 device 檔的毛病。
(3) 這邊跟 Setuid 的運作原理有關
# chmod u=rwxs,g=rx,o=rx /usr/lib/pt_chown
# chown root:root /usr/lib/pt_chown

7月 27, 2012

Nginx 與 spawn-fcgi 的設定

設定 .php 的解析方式

這邊記得要用 Unix domain socket 的模式,比較不會有 "502 Bad Gateway" 錯誤產生。

# vim /sites-enabled/default
server {
 charset utf-8;
 location ~ \.php$ 
 {
  try_files $uri $uri/ =404;
  fastcgi_index index.php;
  include /etc/nginx/fastcgi_params;
  fastcgi_pass unix:/tmp/php-cgi.socket;
 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
 }
}

使用 spawn-fcgi 來管理 php-cgi 數目

spawn-fcgi 已經成為獨立套件,在 Debian 中可以透過 apt 來安裝。
# sudo apt-get install spawn-fcgi

丟個 script 到 /etc/init.d/ 目錄下,讓 php-fcgi 開機時就能自己啟動。
# vim /etc/init.d/php-fastcgi
# chmod +x php-fastcgi
# update-rc.d -f php-fastcgi defaults 2

內容如下:
#!/bin/bash
### BEGIN INIT INFO
# Provides:          php-fastcgi
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      1
# Short-Description: Run the PHP FastCGI
# Description:       PHP FastCGI environment for Nginx web server
#
### END INIT INFO

CHILDREN=1
PHP5=/usr/bin/php5-cgi
SOCKET=/tmp/php-cgi.socket
PIDFILE=/var/run/php-cgi.pid

USER=www-data
GROUP=www-data
SPAWN=/usr/bin/spawn-fcgi

OPT="-s $SOCKET -P $PIDFILE -f $PHP5  \ 
     -u $USER -g $GROUP -C $CHILDREN"

do_start() {
  start-stop-daemon --start --quiet --exec $SPAWN -- $OPT
}
do_stop() {
  start-stop-daemon --stop  --quiet --pidfile $PIDFILE
}
do_restart() {
  start-stop-daemon --stop  --quiet --pidfile $PIDFILE
  sleep 1
  start-stop-daemon --start --quiet --exec $SPAWN -- $OPT
}

case "$1" in
  start)
    do_start;;
  stop)
    do_stop;;
  restart)
    do_restart;;
  *)
    echo "Usage: php-fastcgi {start|stop|restart}" ;;
esac

5月 18, 2012

不用資料庫也能跑的 PHP 軟體

使用 PHP 語言,搭配 SQLite 或者跟本不需要資料庫:

1. 論壇:FluxBB
2. 網誌:FlatPressBliteTypechoChyrp
3. Wiki:DokuWiki

3月 18, 2012

Exim4 與 SquirrelMail 設定


設定 Exim4


​# apt-get install dovecot-common dovecot-imapd dovecot-pop3d

# vim /etc/dovecot/dovecot.conf 加入
protocols = imap imaps pop3 pop3s
# vim /etc/exim4/update-exim4.conf.conf
dc_eximconfig_configtype='internet'
# 接收 user@mail.server.net 的信件
dc_other_hostnames='mail.server.net'
# 接受各方連線
dc_local_interfaces='0.0.0.0'
dc_readhost=''
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost=''
CFILEMODE='644'
dc_use_split_config='false'
dc_hide_mailname=''
dc_mailname_in_oh='true'
dc_localdelivery='mail_spool'
# exim4 -bt user (檢測郵件派送路徑)
R: system_aliases for user@mail.server.net
R: userforward for user@mail.server.net
R: procmail for user@mail.server.net
R: maildrop for user@mail.server.net
R: lowuid_aliases for user@mail.server.net (UID 1000)
R: local_user for user@mail.server.net
user@mail.server.net
router = local_user, transport = mail_spool


設定 SquirrelMail


1. 至 SquirrelMail 網站下載最新版

2. 在 SquirrelMail Configuration - Languages 下面(控制信件內容)
Default Language : zh_TW
Default Charset : UTF-8
3. 在 functions/i18n.php 下面改成 (控制信件列表)
$languages['zh_TW']['NAME'] = 'Chinese Trad';
$languages['zh_TW']['CHARSET'] = 'big5';
$languages['zh_TW']['LOCALE'] = array('zh_TW.UTF-8', 'zh_TW.big5');
$languages['tw']['ALIAS'] = 'zh_TW';

3月 15, 2012

Lighttpd SSL 加密連線設定

使用 OpenSSL 製作憑證


1. 產生 Private Key
# openssl genrsa -out private.key 1024

2. 使用私鑰產出 Certificate Signing Request(CSR)
# openssl req -new -key private.key -out server.csr

3. 產生 SSL Certificate(CRT)
# openssl x509 -req -days 365 -in server.csr -signkey private.key -out server.crt

4. 把 Private Key 與 CRT 倒在一起,成為 PEM 檔案
# cat private.key server.crt > server.pem

修改 Lighttpd 連線設定


1. 把檔案都丟到 /etc/lighttpd/ssl/ 下面
# cp * /etc/lighttpd/ssl/

2. 更改檔案擁有者
# chown -R www-data:www-data /etc/lighttpd/ssl/*

3. 設定 lighttpd 的 SSL 模組
# vim /etc/lighttpd/conf-enabled10-ssl.conf
$SERVER["socket"] == "0.0.0.0:443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/ssl/server.pem"
}
4. 重新啟動 lighttpd
憑證只有在 lighttpd 啟動時會載入一次,所以當有新憑證產生,記得重新啟動。
# /etc/init.d/lighttpd restart

12月 25, 2010

lighttpd rewrite rule for wordpress 3

新版的 Wordpress 3 架構,讓原有的 rewrite rule 無法正常運作。所以要改成下列:
$HTTP["host"] == "www.site.com" {
url.rewrite-final = (

# Exclude some directories from rewriting
"^/(wp-admin|wp-includes|wp-content|gallery2)/(.*)" => "$0",

# Exclude .php files at root from rewriting
"^/(.*.php)" => "$0",
"^/sitemap.xml" => "$0" ,

# Handle permalinks and feeds
"^/(.*)$" => "/index.php/$1"
)
}

9月 17, 2010

rTorrentWeb: Web UI of rTorrent

Web directory [/var/www/rtorrentweb/]?
rTorrentWeb directory [/usr/local/share/rtorrentweb/]?
Data directory [/var/local/rtorrentweb/]?
URL to WWW dir [http://x.x.x.x/rtorrentweb/]?
Web server username [www-data]?
rTorrent username [root]?