• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Ubuntu下用apache+perl搭建最简单的聊天室

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近学习了下perl,尝试自己搭建一个聊天室,现已搭建成功,但设计方法很简陋,误见笑,收获在于对apache、html、perl都有了些许认识,后面打算学习LAMP(Linux+Apache+MySQL+PHP)搭建一个在线听歌网页。


操作系统:Ubuntu 12.04.2 LTS

linux内核: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/Linux
apache版本: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 = \"发言\"></form>\n";


if($data[3]ne"")
{
$newmessage = "<br>$data[1]:$data[3](发送时间:$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统一移动到/var/www/目录下。(此处可以通过配置apache自由设置目录)


五、修改文件权限
还没具体尝试到最安全可靠的权限级别,目前统一将html文件、perl文件、/var/www/文件夹全部修改权限为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、修改内容,最终内容如下:
<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
























鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Perl爬虫--爬特定内容的网页发布时间:2022-07-22
下一篇:
Perl入门笔记3之第三章数组与列表发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap