Install MongoDB and Node.js on a Raspberry Pi
2016-07-06 17:25
916 查看
April 24 by
Yannick Loriot
.entry-header
This tutorial is the second part of the series on
raspberry pi. In this part we will focus on the installation of MongoDB and Node.js that will run at startup. MongoDB is a popular NOSQL database that is often used with Node.js which is a JavaScript runtime mainly used to build
server-side applications.
MongoDB
Let’s get started with the MongoDB installation which is pretty simple:
$ sudo
apt-get update
$ sudo
apt-get upgrade
$ sudo
apt-get install mongodb-server
And to start it as a service when the raspberry pi starts just have to enter this command:
$ sudo
service mongod start
The binaries are stored in the /usr/bin/ folder while the datas are in the
/var/lib/mongodb/ folder. You can check everything is ok by using the mongo shell:
$ mongo
Node.js
Now we are going to install the Node.js server on our raspberry pi and put it as a service. Firstly download the latest version:
$ wget https://nodejs.org/dist/latest-v5.x/node-v5.11.0-linux-armv7l.tar.gz You can download another version here but be careful to take the linux-armv7l distribution.
Once the archive downloaded, extract the package, move it in the
/opt/node folder and create the symbolic links:
$ tar -xvzf node-v5.11.0-linux-armv7l.tar.gz
$ sudo
mv node-v5.11.0-linux-armv7l /opt/node
$ sudo mkdir /opt/bin
$ sudo
ln -s /opt/node/bin/* /opt/bin/
To finish the installation, let’s add the binaries in the
PATH:
$ sudo
nano /etc/profile
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin"
Press CTRL+O then
CTRL+X to save the file. Voilà! The installation is done, check that everything is ok by taping these commands:
$ npm --version
3.7.3
$ node -v
v5.9.1
Run your web server as a service
Now that you have node and npm installed on your raspberry pi 2 you can work with your web server. Imagine you have a really simple on the
/home/pi/dev/node folder named
app.js with this code:
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type',
'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`); });
Here is just a hello world to illustrate the tutorial that you can run using this command
node /home/pi/dev/node/app.js
To run it as a service, we are firstly going to create a file in the
/etc/init.d/ folder to have our own service. Let’s create a
node file:
$ sudo
nano /etc/init.d/node
And put the following content:
#!/bin/sh
# /etc/init.d/node
if [ true !=
"$INIT_D_SCRIPT_SOURCED" ] ;
then
set
"$0"
"$@"; INIT_D_SCRIPT_SOURCED=true
. /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides: node
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts the DAEMON_PATH/DAEMONOPTS server
# Description: Starts the DAEMON_PATH/DAEMONOPTS server
### END INIT INFO
export PATH=$PATH:/opt/node/bin
DAEMON_PATH="/home/pi/dev/node"
DAEMON=node
DAEMONOPTS="app.js”
NAME=node
DESC="myprogram"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 &
echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ];
then
printf
"%s\n" "Fail"
else
echo $PID > $PIDFILE
printf
"%s\n" "Ok"
fi
;;
status)
printf
"%-50s" "Checking
$NAME..."
if [ -f $PIDFILE ];
then
PID=`cat $PIDFILE`
if [ -z
"`ps axf |
grep ${PID} | grep -v
grep`" ];
then
printf
"%s\n" "Process dead but pidfile exists"
else
echo
"Running"
fi
else
printf
"%s\n" "Service not running"
fi
;;
stop)
printf
"%-50s" "Stopping
$NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ];
then
kill -HUP $PID
printf
"%s\n" "Ok"
rm -f $PIDFILE
else
printf
"%s\n" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo
"Usage: $0 {status|start|stop|restart}"
exit 1
esac
exit 0
This file simply describes how to service should start or stop. Then you can start your custom service using this command:
$ sudo
service node start
You just have to restart your raspberry pi 2 and everything should be ok.
Conclusion
The installation of MongoDB and Node.js is pretty simple. If you have some troubles or questions don’t hesitate to leave a comment.
References
Installer MongoDB sur sa Raspberry Pi (in french)
Install MongoDB on Linux
Tuto Raspberry Pi : Configurer le wifi et installer node.js
(in french)
Installing Node.js v4.0.0 on a Raspberry Pi
Yannick Loriot
.entry-header
This tutorial is the second part of the series on
raspberry pi. In this part we will focus on the installation of MongoDB and Node.js that will run at startup. MongoDB is a popular NOSQL database that is often used with Node.js which is a JavaScript runtime mainly used to build
server-side applications.
MongoDB
Let’s get started with the MongoDB installation which is pretty simple:
$ sudo
apt-get update
$ sudo
apt-get upgrade
$ sudo
apt-get install mongodb-server
And to start it as a service when the raspberry pi starts just have to enter this command:
$ sudo
service mongod start
The binaries are stored in the /usr/bin/ folder while the datas are in the
/var/lib/mongodb/ folder. You can check everything is ok by using the mongo shell:
$ mongo
Node.js
Now we are going to install the Node.js server on our raspberry pi and put it as a service. Firstly download the latest version:
$ wget https://nodejs.org/dist/latest-v5.x/node-v5.11.0-linux-armv7l.tar.gz You can download another version here but be careful to take the linux-armv7l distribution.
Once the archive downloaded, extract the package, move it in the
/opt/node folder and create the symbolic links:
$ tar -xvzf node-v5.11.0-linux-armv7l.tar.gz
$ sudo
mv node-v5.11.0-linux-armv7l /opt/node
$ sudo mkdir /opt/bin
$ sudo
ln -s /opt/node/bin/* /opt/bin/
To finish the installation, let’s add the binaries in the
PATH:
$ sudo
nano /etc/profile
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin"
Press CTRL+O then
CTRL+X to save the file. Voilà! The installation is done, check that everything is ok by taping these commands:
$ npm --version
3.7.3
$ node -v
v5.9.1
Run your web server as a service
Now that you have node and npm installed on your raspberry pi 2 you can work with your web server. Imagine you have a really simple on the
/home/pi/dev/node folder named
app.js with this code:
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type',
'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`); });
Here is just a hello world to illustrate the tutorial that you can run using this command
node /home/pi/dev/node/app.js
To run it as a service, we are firstly going to create a file in the
/etc/init.d/ folder to have our own service. Let’s create a
node file:
$ sudo
nano /etc/init.d/node
And put the following content:
#!/bin/sh
# /etc/init.d/node
if [ true !=
"$INIT_D_SCRIPT_SOURCED" ] ;
then
set
"$0"
"$@"; INIT_D_SCRIPT_SOURCED=true
. /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides: node
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts the DAEMON_PATH/DAEMONOPTS server
# Description: Starts the DAEMON_PATH/DAEMONOPTS server
### END INIT INFO
export PATH=$PATH:/opt/node/bin
DAEMON_PATH="/home/pi/dev/node"
DAEMON=node
DAEMONOPTS="app.js”
NAME=node
DESC="myprogram"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 &
echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ];
then
printf
"%s\n" "Fail"
else
echo $PID > $PIDFILE
printf
"%s\n" "Ok"
fi
;;
status)
printf
"%-50s" "Checking
$NAME..."
if [ -f $PIDFILE ];
then
PID=`cat $PIDFILE`
if [ -z
"`ps axf |
grep ${PID} | grep -v
grep`" ];
then
printf
"%s\n" "Process dead but pidfile exists"
else
echo
"Running"
fi
else
printf
"%s\n" "Service not running"
fi
;;
stop)
printf
"%-50s" "Stopping
$NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ];
then
kill -HUP $PID
printf
"%s\n" "Ok"
rm -f $PIDFILE
else
printf
"%s\n" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo
"Usage: $0 {status|start|stop|restart}"
exit 1
esac
exit 0
This file simply describes how to service should start or stop. Then you can start your custom service using this command:
$ sudo
service node start
You just have to restart your raspberry pi 2 and everything should be ok.
Conclusion
The installation of MongoDB and Node.js is pretty simple. If you have some troubles or questions don’t hesitate to leave a comment.
References
Installer MongoDB sur sa Raspberry Pi (in french)
Install MongoDB on Linux
Tuto Raspberry Pi : Configurer le wifi et installer node.js
(in french)
Installing Node.js v4.0.0 on a Raspberry Pi
相关文章推荐
- mongodb数据库的备份
- mongoskin 是让 Node.js 支持 MongoDB 的内嵌访问层。
- MongoDB初步认识
- MongoDB增删改查
- MongoDB启动和停止
- mongodb入门学习小记
- centos6.5安装MongoDB3.0及安装对应php mongo扩展
- centos6.5安装MongoDB2.4.9
- [置顶] centos安装mongodb3.0及php mongo扩展和代码测试
- 如何在MongoDB中建立新数据库和集合
- 利用JAVA操作MongoDB
- 安装mongodb,创建数据库、用户、创建表、导出导入数据库
- php mongodb操作类,适合mongodb2.x和mongodb3.x
- Windows下mongoDb 安装、启动和初识
- 应用node.js+mongodb数据库实现一个mvc架构的web项目
- 安装mongodb数据库
- MongoDB的基础查询和索引操作方法总结
- MongoDB的mongo shell常用操作方法及操作脚本笔记
- Mongodb介绍
- MongoDB的部署及java访问