刚到手的树莓派

前面我用小米路由搭建起了从外网可以访问的网站,并和域名绑定在了一起,但还是不完美。因为我发现无论怎么搞,从外网也无法ssh到路由器上去。猜测可能是小米出于安全的考虑,做了屏蔽。所以即使加了防火墙例外,也还是登不进来。如果我想随时随地的修改服务器文件,就没法做到了。于是买了个树莓派,把http服务架设在树莓派上,通过在小米路由上设置端口转发,这样外网的http和ssh请求就被转发到树莓派了。小米路由的usb足以给低功耗的树莓派供电,无需占用多余的电源插座。

先来秀一下我的树莓派,全部自己动手组装,还算是挺简单的。开着风扇太吵,所以我把风扇的线给拔了。回头写个脚本定时控制开启,可以在不扰民的时段自动开启。

第一步、在树莓派上搭建lamp,先运行 sudo apt-get update 更新软件源,然后运行如下命令:

sudo apt-get install apache2         # 安装Apache
sudo apt-get install mysql-server    # 安装Mysql
sudo apt-get install php5            # 安装PHP
sudo apt-get install php5-mysql

访问树莓派IP,可以看到如下页面,表示http服务搭建完成:

http服务搭建成功

第二步、在小米路由上做一个端口转发,使得外网访问被转发到树莓派上去。打开小米路由的后台设置 - 高级设置 - 端口转发 - 添加规则:

外部端口:80
内部IP地址:树莓派的IP
内部端口:80

点击“保存并生效”,再从外网访问www.binglen.com,我发现居然能够返回上面的页面。由此可见,前面在玩路由的时候得出的结论说运营商屏蔽了80端口是不科学的,问题应该还是处在小米路由上。因为路由器对内的80端口已经被用来做路由后台服务了,所以在小米路由上假设的llmp无法再使用80端口。

第三步、在小米路由上再添加一条端口转发规则,转发到树莓派的22端口,使得外网可以ssh登录。方法同上。

第四步、需要确定树莓派的conf文件以及DocumentRoot的位置。打开/etc/apache2/apache2.conf文件,在里面找不到DocumentRoot的配置,执行如下命令:

find /etc/apache2 -type f|xargs grep "DocumentRoot"

查找/etc/apache2下包含"DocumentRoot"字符的文件,得到如下结果:

/etc/apache2/sites-available/default:    DocumentRoot /var/www
/etc/apache2/sites-available/default-ssl:    DocumentRoot /var/www

说明在这两个文件中定义了DocumentRoot 的位置在/var/www,在该文件夹下放index.php文件编辑如下:

<?php

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: text/plain");

echo ""
."Hello World!\r\n";
?>

通过http://树莓派地址/index.php,得到如下内容:

 第五步、把Apach的默认页面指向该index.php。现在默认指向的是index.html,按照同样的方法查找关键字:

find /etc/apache2 -type f|xargs grep "index"

得到如下内容:

/etc/apache2/mods-available/dir.conf:          DirectoryIndex index.html index.cgi index.plindex.php index.xhtml index.htm /etc/apache2/mods-available/autoindex.load:LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so /etc/apache2/mods-available/autoindex.conf:<IfModule mod_autoindex.c> /etc/apache2/mods-available/autoindex.conf:# server-generated indexes. These are only displayed for FancyIndexed /etc/apache2/mods-available/autoindex.conf:# directory indexes. /etc/apache2/mods-available/autoindex.conf:# IndexIgnore is a set of filenames which directory indexing should ignore

看起来像是在/etc/apache2/mods-available/dir.conf,修改该行为:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

然后重启http服务:

sudo service apache2 restart

搞定!

posted @ 2016-01-01 12:10  palance  阅读(1188)  评论(0编辑  收藏  举报