Warning: include(/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php): failed to open stream: No such file or directory in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24

Warning: include(/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php): failed to open stream: No such file or directory in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24

Warning: include(): Failed opening '/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php' for inclusion (include_path='.:/opt/cpanel/ea-php70/root/usr/share/pear') in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24
2012 年 5 月 – unethost無限空間虛擬主機 技術分享部落格

php指定對外連線使用的IP位置

一般而言,主機商的主機,不會只有一個IP位置。通常會有超過一個,以備不時之需。然而,由於網路上的有些服務,常會鎖IP,所以透過php去存取遠端的網站時,有時候就必須考慮這個對外連線使用的IP位置。

使用ifconfig指令,通常eth0上附加的這個ip就是預設的對外連線用IP(outgoing IP address)。由於使用者登入主機時,是一般user,無法使用ifconfig。所以要用其它方法來判斷。其中一種方式是curl。下面這個指令就可以得到系統預設對外主動連線時所使用的IP了。

curl -s ip.appspot.com

而,如果對外是透過php去存取遠端的資料,又必須指定php一定要透過某個網路介面/IP address來連線的話,可以考慮使用php curl library。範例的code如下:

<?
$url = “http://www.google.com”
$curlh = curl_init($url);
curl_setopt($curlh, CURLOPT_USERAGENT, $uagent);
curl_setopt($curlh, CURLOPT_INTERFACE, “888.888.888.888“);
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlh);

echo $result;
?>

其中,”888.888.888.888″,就是要被指定用對外連線用的IP位置。

利用lftp來做資料的備分

lftp本來是linux下才能用的程式,通常我們是推荐客戶使用filezilla這種有GUI的,比較容易學習。不過,由於lftp有一些特性,例如說mirror,讓我們覺得,還是推一下它吧。

在centOS linux下安裝:

安裝lftp前要先增加CentOS官方認可的rpmforge這家的repo。
rpm –import http://apt.sw.be/RPM-GPG-KEY.dag.txt

i386系統
rpm -ihv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
x86_64系統
rpm -ihv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm

最後下yum install -y lftp就可以完成安裝。

在windows XP下安裝:

由於通常需要處理Cygwin的問題,比較麻煩。也因此,我們此處提供一個懶人包,是已經打包好Cygwin的lftp zip檔,解開資料夾就可以用了。要用的時候,滑鼠去點mirror.bat這個執行檔。而mirror.txt則是用來給lftp用的command。這個mirror.txt請用記事本修改。

Lftp的常用指令教學:

(1) 登入ftp
lftp 用戶名稱:密碼@ftp地址:通訊端口(預設值21)
或是
open -u 用戶名稱, 密碼 -p 通訊端口 ftp地址

(2) 在本機端的目錄裡移動
lcd 本機電腦的資料夾
// 註:lftp的指令命名原則是,如果命令是L開頭,指的就是local,即本機電腦的操作。所以cd就是對應ftp站的操作。

(3) 從ftp站上,把某一資料夾連同子資料完整地全部抓下來。
mirror 欲下載的目錄

(4) 從本機端,把某一資料夾連同子資料完整地全部傳上去。
mirror -R 欲上傳的目錄

(5) 從ftp站上,把某一資料夾連同子資料完整地全部抓下來,並且,若本地端有遠端沒有的檔案,則把這些多餘的檔案刪除。
mirror -e   欲下載的目錄

Lftp的完整指令,可以查linux的man page,這邊有,不過是英文的。

cpanel主機下,設定不同的帳戶可以共用資料夾

收到了客戶的要求,希望在他所租用的獨立主機下的二個account,A和B,有共用的資料夾。也就是說:

/home/A/public_html/share
/home/B/public_html/share
這兩個資料夾,是指到同一個位置。

本來我以為,只要用ln -s來建softlink,就可以把問題解決了。結果卻失敗了,softlink所在的那個帳戶,用ftp登入後,看不到share這個資料夾。這是為什麼呢?原因是在這邊,它寫說,symbolic link只能指向「正確的」的地方。使用者的ftp帳戶基本上是一個被chroot的環境,所以當然不能去看到別的使用者的檔案。

A symbolic link is a pointer to the “right” file. But if that original file is outside the jail then you can’t access it. This is the goal of a jail. Otherwise a normal user could create a symbolic link in the jail to/etc/passwd and just read it. What a security risk!

So jailed is jailed. Probably a hard link will do the job, as this is a “copy without duplicating the used size”. And for the FTP server it is like a normal file (with all the problems).

那麼,這樣的問題該如何處理呢?用 mount –bind來處理吧。這個是正確的作法。

mkdir /home/website/files/
mount –bind /home/shared/files /home/website/files/

像這樣子的作法,shared這個使用者ftp登入後,就看得到 website這個使用者的資料夾files了。因為 /home/shared/files 和 /home/website/files是同一個資料夾。

如何安裝perl的模組(module)

這幾天在debug時,發現原來bug的來源是因為有perl的模組沒有裝好,於是我們就來研究一下安裝perl module的方法吧。

(1) 先檢查,現在系統裝了那些perl的模組:
先打指令instmodsh,再打l,這樣就會列出系統中的perl modules了。

[root@proxy vhost]# instmodsh
Available commands are:
l – List all installed modules
m <module> – Select a module
q – Quit the program
cmd? l
Installed modules are:
Class::Loader
Crypt::Random
Digest::SHA
Digest::SHA::PurePerl
IPC::SysV
Math::Pari
Net::IP::Match::Regexp
Perl

(2) 來安裝模組吧:

A) 首先啟動 CPAN Shell:
# perl -MCPAN -e shell

B) 在CPAN Shell的環境下,安裝 perl module,以模組IPC::SharedMem為例子
# cpan> install IPC::SharedMem

參考資料:
(1) http://www.cyberciti.biz/faq/how-do-i-find-out-what-perl-modules-already-installed-on-my-system/
(2) http://www.cyberciti.biz/faq/how-do-i-install-a-perl-module/

Discuz的圖形驗証碼出不來

客戶反映,他們的獨立主機上,有Discuz的圖形驗証碼出不來的錯誤。

我們一查php生成的error檔,看到的訊息是:
PHP Fatal error:  Call to undefined function imagettftext()

看來是有少php的library。這個imagettftext()所對應的php library,是GD和freetype這兩個。所以解決之道就是:

  1. 執行cpanel的easyapache:WHM -> Software -> EasyApache
  2. If prompted to upgrade, do so then repeat the above step
  3. Begin customizing based on the current provile
  4. Proceed through the screens until you get to “Step 5”
  5. On “Step 5,” click on “Exhaustive Options”
  6. Under PHP, check the checkbox for GD, ttf
  7. Proceed with Build & Compile Process

一旦成功了,圖形驗証碼就出來了。

如何讓主機的防火牆csf不會誤擋到ftp

最近公司的客戶,用ftp連線時,一直被主機的防火牆誤判為攻擊。這個問題是這樣子產生的:公司的防火牆是用csf firewall這個套件,而這個套件如果沒有特別設定的話,對於ftp,很容易誤擋。

根據csf的文件,linux的iptables如果ip_conntrack和ip_conntrack_ftp沒有正常的運作時,FTP的被動模式就無法順利地運作。在這種條件下,我們必須設定ftp的被動通訊埠在防火牆上是被視為可連接的。

以pure-ftp為例子:
(1) 必須修改/etc/pure-ftpd.conf,取消
PassivePortRange 30000 35000
這一行的註解 (注意:此處的port range要和之後步驟,設定CSF的port range相符合)
(2) 重新啟動pure-ftp
(3) 修改csf的config檔 /etc/csf/csf.conf
在 TCP_IN中,加上30000:35000,例如:
# Allow incoming TCP ports
TCP_IN = “20,21,22,25,53,80,110,143,443,465,587,993,995,2077,2078,2082,2083,2086,2087,2095,2096,1891,30000:35000″
(4) 重新啟動csf

這樣子設定之後,防火牆才能和ftp完美的合作。

參考資料:http://www.configserver.com/free/csf/readme.txt

延伸閱讀:如何查看linux server連線port是否異常?

13. A note about FTP Connection Issues
######################################

It is important when using an SPI firewall to ensure FTP client applications
are configured to use Passive (PASV) mode connections to the server.

On servers running Monolithic kernels (e.g. VPS Virtuozzo/OpenVZ and custom
built kernels) ip_conntrack and ip_conntrack_ftp iptables kernel modules may
not be available or fully functional. If this happens, FTP passive mode (PASV)
won't work. In such circumstances you will have to open a hole in your firewall
and configure the FTP server to use that same hole.

For example, with pure-ftpd you could add the port range 30000:35000 to TCP_IN
and add the following line to /etc/pure-ftpd.conf and then restart pure-ftpd:
PassivePortRange	30000 35000

For example, with proftpd you could add the port range 30000:35000 to TCP_IN
and add the following line to /etc/proftpd.conf and then restart proftpd:
PassivePorts	30000 35000

FTP over SSL/TLS will usually fail when using an SPI firewall. This is because
of the way the FTP protocol established a connection between client and server.
iptables fails to establish a related connection when using FTP over SSL
because the FTP control connection is encrypted and so cannot track the
relationship between the connection and the allocation of an ephemeral port.

If you need to use FTP over SSL, you will have to open up a passive port block
in both csf and your FTP server configuration (see above).

Perversely, this makes your firewall less secure, while trying to make FTP
connections more secure.