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

来源:互联网 发布:js获取标签所有属性 编辑:程序博客网 时间:2024/05/21 01:05

最近学习了下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























原创粉丝点击