Ubuntu下用apache+perl搭建最简单的聊天室
生活随笔
收集整理的這篇文章主要介紹了
Ubuntu下用apache+perl搭建最简单的聊天室
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近學習了下perl,嘗試自己搭建一個聊天室,現(xiàn)已搭建成功,但設(shè)計方法很簡陋,誤見笑,收獲在于對apache、html、perl都有了些許認識,后面打算學習LAMP(Linux+Apache+MySQL+PHP)搭建一個在線聽歌網(wǎng)頁。
操作系統(tǒng):Ubuntu 12.04.2 LTS
linux內(nèi)核:Linux ubuntu 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linuxapache版本:Apache/2.2.22 (Ubuntu)
編程語言:HTML + perl
一、安裝apache
sudo apt-get install apache2
二、編寫HTML聊天WEB界面
1、chatroom.html文件代碼:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN"
?"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<FRAMESET ROWS = "*,65">
<FRAME SRC = message.htm>
<FRAME SRC = login.htm>
</FRAMESET>
2、message.htm文件代碼:
<html><head><META HTTP-EQUIV = "REFRESH" CONTENT = "4"></head><body>
</body></html>
3、login.htm文件代碼:
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN"
?"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html><meta charset="utf-8" /><body>
<form action = "./chatroom.pl" method = "post">輸入你的名字:
<input type = "text" name = "username">
<input type = "submit" value = "開始聊天">
<input type = "hidden" name = "message" value = "connected">
</form></body></html>
三、用perl編寫cgi程序
chatroom.pl文件代碼:
#!/usr/bin/perl -w
$buffer = "";
print "Content-type:text/html\n\n";
&get_form_data;
open(MESSAGE,"/var/www/message.htm");
@lines = <MESSAGE>;
close(MESSAGE);
$now_string = localtime;
@thetime = split(/ +/,$now_string);
print "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><body>\n";
print "<form action = \"./chatroom.pl\" method = \"post\">\n";
print "<input name = username type = hidden value = $data[1]>\n";
print "<input type = text name = message size = 40>\n";
print "<input type = submit value = \"發(fā)言\"></form>\n";
if($data[3]ne"")
{
$newmessage = "<br>$data[1]:$data[3](發(fā)送時間:$thetime[3])\n";
open(NEW,">/var/www/message.htm");
print NEW "<html><head><META HTTP-EQUIV = \"REFRESH\" CONTENT = \"4\"></head><body>\n";
#print NEW encode("utf-8",decode("utf-8",$newmessage));
print NEW $newmessage;
$limit_lines = 10;
if($#lines < 10)
{$limit_lines = $#lines;}
for($i = 1;$i < $limit_lines;$i++)
{
#print NEW encode("utf-8",decode("utf-8",$lines[$i]));
print NEW "$lines[$i]";
}
print NEW '</body></html>';
close(NEW);
}
print "</body></html>\n";
exit 0;
sub get_form_data {
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$buffer);
@data=();
foreach $pair(@pairs)
{
@a = split(/=/,$pair);
$name = $a[0];
$value = $a[1];
$value =~s/%([0-9a-fA-F][0-9a-fA-F])/pack("C",hex($1))/eg;
push (@data,$name);
push (@data,$value);
}
}
四、移動文件
將編寫好的文件chatroom.html、message.htm、login.htm和chatroom.pl統(tǒng)一移動到/var/www/目錄下。(此處可以通過配置apache自由設(shè)置目錄)
五、修改文件權(quán)限
還沒具體嘗試到最安全可靠的權(quán)限級別,目前統(tǒng)一將html文件、perl文件、/var/www/文件夾全部修改權(quán)限為777。
指令:chmod 777 /etc/www/
chmod 777 /etc/www/chatroom.html
chmod 777 /etc/www/message.htm
chmod 777 /etc/www/login.htm
chmod 777 /etc/www/chatroom.pl
六、配置apache,使其支持perl
1、進入/etc/apache2/sites-available/目錄
2、打開其中的default文件
3、修改內(nèi)容,最終內(nèi)容如下:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
allow from all
satisfy all
</Directory>
<Directory /var/www/>
Options FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
allow from all
</Directory>
ScriptAlias /cgi-bin/ /var/www/
<Directory "/var/www/">
AllowOverride all
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order deny,allow
Allow from all
AddHandler cgi-script .cgi .pl
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
? ? Alias /doc/ "/usr/share/doc/"
? ? <Directory "/usr/share/doc/">
? ? ? ? Options Indexes MultiViews FollowSymLinks
? ? ? ? AllowOverride none
? ? ? ? Order deny,allow
? ? ? ? Deny from all
? ? ? ? Allow from 127.0.0.0/255.0.0.0 ::1/128
? ? </Directory>
</VirtualHost>
七、重新開啟apache服務
指令:service apache2 restart
八、在瀏覽器輸入地址測試是否成功!
http://你服務的IP地址/chatroom.html
轉(zhuǎn)載于:https://www.cnblogs.com/fengty90/archive/2013/05/03/3768872.html
總結(jié)
以上是生活随笔為你收集整理的Ubuntu下用apache+perl搭建最简单的聊天室的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: memcpy(cv::Mat.data,
- 下一篇: android编程实现128条形码的生成