Mar 22

最近在研究網站負載平衝 (Load Balance) 的技術. 在分載的部份已經獲得一定的進展, 但是如果多個網站伺服器(Apache 2) 之間的檔案同步也是個問題. 資料庫 MySQL 還可以使用一個來提供集中的資料或是用 replication 來達到資料庫同步. 但是在 Apache 下的檔案如果要同步, 就是另一個挑戰.

一般來說如果要同步檔案, 搜尋的結果都是用 rsync. 設定完 rsync 之後, 才發現 rsync 只有單方面的同步. 假設有2台 Apache servers, 只單方從一號同步到二號, 那如果用 Load Balancer 分到二號 server 上做的變更, 就不會再同步到一號上了. 再研究後發現 Unison 可以同步檔案, 再加上 cron job 的設定即可.

1. 安裝 Unison

1
 sudo apt-get install unison

2. 修改 Unison 預設值

1
 sudo nano /home/username/.unison/default.prf

* note: username 就是你 Ubuntu 登入的使用名

3. Default.prf 的內容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# skip asking for confirmations on non-conflicting changes
auto=true

# the user interface will ask no questions at all
batch=true

# Unison will request an extra confirmation if it appears that the entire replica has been deleted
confirmbigdeletes=true

# Unison will use the modification time and length of a file as a  `pseudo inode number' when scanning replicas for updates, instead of  reading the full cont$
fastcheck=true

# the group attributes of the files are synchronized
group=true

# the owner attributes of the files are synchronized
owner=true

# prefer newer version of files in case of conflicts
prefer=newer

# the textual user interface will print nothing at all, except in the case of errors.
silent=true

# file modification times (but not directory modtimes) are propagated.
times=true

4. 設定完之後, 就要設定免密碼登入到二號機, 首先先設定主機的 ssh 密碼

1
ssh-keygen -t dsa

5. 複制一號機的密碼到二號機上

1
 ssh-copy-id -i /home/username/.ssh/id_dsa.pub remoteuser@apache2.remote.com

6. 完成之後, 就可以不用再打入密碼使用 Unison 了

1
 sudo unison /var/www ssh://remoteuser@apache2.remote.com//var/www

Note: 如果在測試 unison 時發生permission 權限問題, 請確認 remoteuser 是否有權限對/var/www 目錄做更改. 如果沒有, 就用chgrp 和 chown 來變更
7. 最後再把上一步的指令加入 cron job 裡. 此例是每5分鐘同步一次

1
2
3
sudo crontab -e

*/5 * * * * sudo unison /var/www ssh://remoteuser@apache2.remote.com/var/www &> /dev/null

Post to Twitter

Tagged with:
Mar 08

Balance is a free version of load balancing created by Inlab in Germany. I am trying to setup Balance on my Ubuntu Server and hit this error Balance unable to start (“setsockopt(IPV6_V6ONLY=0): Protocol not available) when I issue the balance command. After digging around, I found the following solution which actually worked for me. So I am sharing it for everyone who encounters this issue.

This problem isn’t fixed in the latest versions, the change appears that balance now needs IPv6 addresses if you specify IP addresses (versus host names). to fix this problem for v4 addresses add ::ffff: to the beginning of your address. for example:

balance -b 192.168.1.2 631 spiffycupshost

(which gives you this error)
should now become:
balance -b ::ffff:192.168.1.2 631 spiffycupshost

and the error will go away.

Provided by Craig Sawyer

Post to Twitter

Mar 08

In a terminal console, do the following commands:

To stop apache2 for this session only:

1
 sudo /etc/init.d/apache2 stop

To remove apache2 permanently from startup scripts:

1
 sudo update-rc.d apache2 remove

To reinstate apache2 in the startup scripts:

1
 sudo update-rc.d apache2 defaults


Post to Twitter

Tagged with:
Feb 23

剛寫完 Clipbucket 2.0.91 後, 2.1測試版也推出了, 多了許多功能, 但也必須多加一些套件, 以下是要新加的.

1. cURL

<cce code=”shell”>

sudo apt-get install curl php5-curl

</code>

再重新啟動 Apache Server

<cce code=”shell”>

sudo /etc/init.d/apache2 restart

</code>

這樣就可以裝了.

另外附加的 PHPShield 現在查到的方法是以下. (待證)

Download phpshield from here

After download unzip and upload the file phpshield.5.2.lin in to /usr/lib/php5/20060613+lfs folder

Now you need to edit the php.ini file using the following command

sudo vi / etc/php5/apache2/php.ini

or

sudo nano / etc/php5/apache2/php.ini

add the following line at the end of the file

extension=phpshield.5.2.lin

Save and exit the file

Now you need to restart apache server using the following command

sudo /etc/init.d/apache2 restart

That’s it now check your phpmotion

I hope this helps

Post to Twitter

Feb 11

有天在尋找類似 Youtube 的架站系統, 比較了幾個, 但最後覺得 Clipbucket 在功能, 安裝便利性和需求都比較簡單入手. 而且它還是免費開放程式碼的PHP系統. 我就想來研究一下.雖然安裝過程很容易, 但是在環境的設定, 我可是費了很多工年才研究出正確的步驟.

Clipbucket 網站實在是很爛, 教學文件實在是什麼都沒有, 只有 blah blah blah…. 所以我就把我從頭到尾安裝到成功轉檔的步驟記錄下來.  現在最近版本是 v2.0.91 可以在這裡下載: http://sourceforge.net/projects/clipbucket/files/ClipBucket%20v2/

首先, 我不是 Linux guy, 所以我花了很多時間在整理環境和安裝需要的套件. 在開始前, 我們先看一下它的需求.

Requirements

  1. PHP 5.2.x & MySql 5.x.x
  2. PHP with Background Processing and Exec Enabled
  3. GD Library For Captchas and Other Image Manuplation
  4. FFMPEG – FLVTOOL2 – MP4Box

就這樣. :mrgreen: 很清楚吧. 整個教學就是幾行. 但是我裝了N次都少一些模組. 所以下面就是完整的教學說明.

首先, 這個說明是在 ubuntu server 10.10 32-bit 上執行的. 而且我是在 VMware ESXi 4.1 上測試的. 等寫完後再來測實機, 畢竟轉檔是很耗資源. 硬體一定要夠力.

Step I: Environmental readiness

最快最簡單的方式就是安裝 Ubuntu Server 再安裝 LAMP Server. 這樣一來基本環境 Apache 2, MySQL and PHP 5 都齊了. 剩下來就大家都覺得麻煩的 FFMPEG. 記得還有 SSH 要開啟.

第二步就是要安裝 medibuntu repository. Medibuntu 是 (Multimedia, Entertainment & Distractions In Ubuntu) 的簡稱. 由於其中包括了第三方和有專利的未開放原始碼的套始, 所以不能一起包在一般的釋出來.

加入 Medibuntu 的 repository

1
sudo wget --output-document=/etc/apt/sources.list.d/medibuntu.list http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list && sudo apt-get --quiet update && sudo apt-get --yes --quiet --allow-unauthenticated install medibuntu-keyring && sudo apt-get --quiet update

安裝 mediabuntu

1
sudo apt-get --yes install app-install-data-medibuntu apport-hooks-medibuntu

安裝libdvdcss2 (DVD 套件)

1
sudo apt-get install libdvdcss2

加入原生不支援的解碼

1
sudo apt-get install w32codecs

* 註: 如果是在64位元系統上, 打 w64codecs

安裝必需的解碼器

1
2
3
4
5
sudo apt-get update
sudo apt-get install build-essential git-core checkinstall yasm texi2html libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libvpx-dev libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev
sudo apt-get install libxvidcore-dev libmp3lame-dev libfaac-dev libfaac-dev libx264-dev
sudo apt-get install libavcodec-unstripped-52 libavdevice-unstripped-52 libavformat-unstripped-52 libavutil-unstripped-50 libpostproc-unstripped-51 libswscale-unstripped-0
sudo apt-get install liblame0 libfaad2 libfaac-dev libxvidcore-dev liba52-0.7.4 liba52-0.7.4-dev libdts-dev checkinstall

安裝 x264

1
2
3
4
5
6
sudo git clone git://git.videolan.org/x264.git
cd x264/
sudo ./configure
sudo make
sudo checkinstall --pkgname=x264 --pkgversion="2:0.$(grep X264_BUILD x264.h -m1 | cut -d' ' -f3).$(git rev-list HEAD | wc -l)+git$(git rev-list HEAD -n 1 | head -c 7)" --backup=no --deldoc=yes --fstrans=no --default
sudo make install

安裝 flvtool2

1
sudo apt-get install flvtool2

安裝mp4box

1
sudo apt-get install gpac

安裝 FFMPEG

1
sudo apt-get install ffmpeg

最後就是安裝 qt-faststart (額外選項)

1
2
make tools/qt-faststart
sudo checkinstall --pkgname=qt-faststart --pkgversion="$(./version.sh)" --backup=no --deldoc=yes --fstrans=no --default install -D -m755 tools/qt-faststart /usr/local/bin/qt-faststart

II. 下載 clipbucket 2.0.91

Sourceforge 上有他們的 source: http://sourceforge.net/projects/clipbucket/files/ClipBucket%20v2/

回到 Home directory 下

先安裝 unzip

1
sudo apt-get install unzip

下載 Clipbucket 2.0.91

1
2
3
4
5
6
7
8
9
10
11
12
cd
wget http://downloads.sourceforge.net/project/clipbucket/ClipBucket%20v2/clipbucket_2.0.91.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fclipbucket%2Ffiles%2FClipBucket%2520v2%2F&ts=1297412770&use_mirror=iweb
mv clipbucket_2.0.91.zip\?r\=http\:%2F%2Fsourceforge.net%2Fprojects%2Fclipbucket%2Ffiles%2FClipBucket%20v2%2F clipbucket2.0.91.zip
mkdir clipbucket
unzip clipbucket2.0.91.zip -d clipbucket
cd clipbucket/
cd upload/
sudo cp -r * /var/www/
sudo cp .htaccess /var/www/
cd ..
cd update_209_2091/
sudo cp -r * /var/www/

最後把 Apache 的內建測試頁幹掉

1
2
cd /var/www/
sudo rm index.html

還要記得把 Apache 的 URL-rewrite 打開

1
2
sudo a2enmod rewrite
sudo nano /etc/apache2/sites-available/default

找AllowOverride None 改成 AllowOverride All

之後再重新啟動 Apache server

1
sudo /etc/init.d/apache2 restart

III. 安裝 Clipbucket

如果是在測式環境下,簡單一點就是加一個 DNS 名在你的 host 檔案裡. 以我的例子, 我的 vm 抓到的內部 IP 是 10.175.0.144, 我就在 Windows 7 裡, 修改 C:\Windows\System32\drivers\etc 裡頭的 hosts 檔. 在下面加入10.175.0.144     cb.chaoman.com

儲存後, 去 Browser 裡打 http://cb.chaoman.com 就會看到以下的安裝畫面了:

我本身 database 也不是很上手, 所以要借助一下 phpMyAdmin 來管理一下資料庫. 所以一併安裝

安裝 phpMyAdmin

1
sudo apt-get install phpmyadmin

安裝過程中會問 web server, 選 apache2 就好了. 之後再打入自己的密碼就可以了. 安裝成功的話, 在剛加入的 host 名中打 /phpMyAdmin 就會看到了

我們建立一個叫作 clipbucket 的資料庫, 如下圖示:


回過到到剛才 Clipbucket 的安裝畫面, 按下 Fresh Install, 之後再按 I Accept 後就會遇到下面權限問題.

Chmod Issue

照下面的打就可以給適當的寫入權限了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd /var/www/
sudo chmod 777 files/
sudo chmod 777 files/conversion_queue/
sudo chmod 777 files/logs/
sudo chmod 777 files/original/
sudo chmod 777 files/temp/
sudo chmod 777 files/thumbs/
sudo chmod 777 files/videos/
sudo chmod 777 files/mass_uploads/
sudo chmod 777 files/temp/install.me
sudo chmod 777 images/
sudo chmod 777 images/avatars/
sudo chmod 777 images/backgrounds/
sudo chmod 777 images/category_thumbs
sudo chmod 777 images/groups_thumbs/
sudo chmod 777 cache/
sudo chmod 777 install/
sudo chmod 777 includes/

再按一下 Recheck, 等到全部變成綠色就可以了. 按 Continue 繼續

等到要打入資料庫設定時, 打入以下設定

Host
localhost -> 剛才設過的 DNS 名或本身網址, 本地是 localhost

Database Name
clipbucket -> 剛才用 phpmyadmin 作好了

Database User
root

Database Password
xxxxx        -> 安裝 Ubuntu 時設的

Database Prefix
cb_          -> 不用動

按下 Check Connection, 如果沒問題, 再按 Continue Installation, 最後安裝就完成了. 如圖下:

Installation Complete

IV. PHP 環境的設定

由於影片檔的 size  都滿大的, PHP 預設的上傳限制是 8 MB.  我們要把一些流量限制加大. 請打入以下指今

1
sudo nano /etc/php5/apache2/php.ini

修改以下變數. (如果想要找, 按 ctrl + w, 如果習慣用 vi 的, 就用 / )

1
2
3
4
5
max_execution_time = 3000
max_input_time = 3000
memory_limit = 512M // 請視環境而設
post_max_size = 400M
upload_max_filesize = 400M

設完後, 按 ctrl + x, 再按 y, 再按 enter 就可以儲存了. (感覺好像是電玩密技)

最後把 Apache server 重新起動一下.

1
sudo /etc/init.d/apache2 restart

V. Clipbucket 設定

預設的管理員帳號和密碼是 admin / admin , 後台網址是加上 /admin_area/

最後別忘了把 includes 權限改過

1
sudo chmod 755 includes/

在 browser 下打 http://cb.chaoman.com/admin_area/ 登入後就會看到後台介面了.

雖然以上的安裝手法不是很漂亮, 但是安裝完, 去左手邊的 Tool Box 裡的 Server Modules Info 就可以看到所有必需的模組都安裝好了. :-D

Server Modules List

最後我們要設定的, 就是 cron job 的參數. cron job 我本來只知道是排程的東西, 但說明裡完全沒說 cron job 要怎麼設, 如果沒設你的影片不會自動轉檔. 所以一定要設.  去左手邊選項裡最後一個 Clipbucket Helper 裡有 Cron Jobs 的設定. 我把它貼到這裡來.

* * * * * php -q /var/www/actions/video_convert.php >/dev/null 2>&1
* * * * * php -q /var/www/actions/verify_converted_videos.php >/dev/null 2>&1
0 0,12,13 * * * php -q /var/www/actions/update_cb_stats.php >/dev/null 2>&1

設定方法如下

1
2
3
4
5
6
sudo crontab -e
2
貼上剛才上面3行
按 ctrl + x
Y
按 enter

* 注意: 網上有人建議 cron job 的最後一行最好要換行.  所以我們把游標移去第3行最後再按 enter 跳行就可以了.

最後就可以到前端介面上傳影片了. 但後端先不要關掉, 原因是要看轉檔的進度. 影片上傳後, 去剛剛 cron jobs 裡, 會看到上次執行的時間. 如果看到是從沒執行 (never run) 就是 cron jobs 設錯了.

再來去 Tool Box 裡的 Conversion Queue Manager, 就可以看到轉檔進度, Done 就是完了. 再去前端或是 video 裡就可以看到剛才上傳的影片. 這樣子你也有自己的 youtube 網站了.

CB 前端介面

如果在安裝上有什麼問題, 我儘量回答. 最後附上一個測試的網站: http://chaoman.dyndns.tv/clipbucket/

資料來源:

  1. http://ubuntuforums.org/showthread.php?t=786095
  2. https://help.ubuntu.com/community/Medibuntu
  3. http://drupal.org/node/134439

Post to Twitter

Tagged with:
preload preload preload