Mostrando entradas con la etiqueta Raspberry Pi. Mostrar todas las entradas
Mostrando entradas con la etiqueta Raspberry Pi. Mostrar todas las entradas

jueves, 17 de abril de 2014

Raspberry Pi: Desplegando aplicación Rails,RVM,Thin en nginx con Capistrano


Configuración en el servidor:

Instalación de RVM multi-usuario [script]:

pi@raspberrypi ~ $ sudo su
root@raspberrypi:/home/pi# curl -L get.rvm.io | bash -s stable
#source /etc/profile.d/rvm.sh
root@raspberrypi:/home/pi# source /etc/profile
root@raspberrypi:/home/pi# rvm requirements;
root@raspberrypi:/home/pi# apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config
root@raspberrypi:/home/pi# rvm install 2.0.0

Tenemos que instalar Nginx. Esta explicado en la anterior entrada: Cambiando Apache por Nginx

Vamos a crear el usuario que realizara los deploy de capistrano. El aprovechamos para crear con un home diferente /var/deploy y alojar ahí las aplicaciones. Luego bloqueamos la contraseña, solo podremos logarnos y deployar a través de ssh.

root@raspberrypi:/home/pi# adduser --home /var/deploy deploy
root@raspberrypi:/home/pi# passwd -l deploy

Nos vamos ponernos como usuario deploy y lo configuramos para poder autenticarnos por ssh como el usuario deploy (explicado en entrada anterior):

root@raspberrypi:/home/pi# su - deploy
deploy@raspberrypi ~ $ pwd #comprobamos donde estamos y si es igual al HOME esperado
/var/deploy
deploy@raspberrypi ~ $ mkdir .ssh
deploy@raspberrypi ~ $ vim .ssh/authorized_keys #introducimos la llave publica a autorizar
Vamos a crear el gemset de nuestra aplicación y como instalaríamos thin:
deploy@raspberrypi ~ $ rvm use 2.0.0@showip --create
deploy@raspberrypi ~ $ rvm gemset create showip
deploy@raspberrypi ~ $ # gem install thin


Configuración en nuestra aplicación (entorno de desarrollo):

En el Gemfile añadimos capistrano y thin [ejemplo thin, ejemplo unicorn]:
# Use Capistrano for deployment
group :development do
  gem 'capistrano'
  gem "rvm-capistrano"
end

# Use Thin for production
gem 'thin', group: :production

Si no tenemos instalado ningún motor de Javascript en el servidor, tenemos que descomentar también la linea de therubyracer. Otra opción, instalar en el servidor nodejs.

Ejecutamos un bundle y comando para crear ficheros de capistrano:

$ bundle install
$ capify .
[add] writing './Capfile'
[add] writing './config/deploy.rb'
[done] capified!
Configuramos el fichero de /config/deploy.rb, (lo creamos si es necesario). En mi caso queda así:
require "rvm/capistrano"
#require "capistrano/ext/multistage"
require "bundler/capistrano"

#set :stages, %w(staging production) #Si queremos mas de un entorno

set :application, "showip"
set :repository,  "git@github.com:verdor/showip.git"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

# la versión de ruby tiene que estar instalada y el gemset existir
# antes del cap deploy:setup
set :rvm_ruby_string, 'ruby-2.0.0-p451@showip'
set :rvm_type, :system

set :scm, :git
set :git_shallow_clone, 1
server "192.168.1.10", :app, :web, :db, primary: true
#server "showip.tk", :app, :web, :db, primary: true

set :user, "deploy"
set :keep_releases, 5
set :use_sudo, false

#set :port, 22 #sustituye con el puerto que usas
# Capistrano's default location "/u/apps/#{application}"
set :deploy_to, "/var/#{user}/apps/#{application}"
set :deploy_via, :remote_cache

set :branch, "master"

after  "deploy:finalize_update" , "symlinks"
after  "deploy",                  "deploy:cleanup"

namespace :deploy do
  desc "Despues de ejecutar el setup subimos ficheros de configuracion"
  task :setup_config, roles: :app do
    #run "mkdir #{shared_path}/config"
    #run "#{try_sudo} mkdir #{shared_path}/config"
    top.upload("config/nginx.conf", "#{shared_path}/nginx.conf", via: :scp)
    top.upload("config/thin_config.yml", "#{shared_path}/thin_config.yml", via: :scp)
    top.upload("config/database.yml", "#{shared_path}/database.yml", via: :scp)
    #top.upload(".rvmrc", "#{shared_path}/.rvmrc", via: :scp)
    top.upload(".versions.conf", "#{shared_path}/.versions.conf", via: :scp)
    #sudo "mv #{shared_path}/config/nginx.conf /etc/nginx/sites-available/showip"
    #sudo "ln -nfs /etc/nginx/sites-available/showip /etc/nginx/sites-enabled/showip"
  end

  after "deploy:setup", "deploy:setup_config"
end

task :symlinks, roles: [:app] do
  run <<-CMD
    ln -s #{shared_path}/cache #{release_path}/public/;
    ln -s #{shared_path}/database.yml #{release_path}/config/;
    ln -s #{shared_path}/thin_config.yml #{release_path}/config/;
    ln -s #{shared_path}/.versions.conf #{release_path}/;
  CMD
end

namespace :deploy do
  desc "Start the Thin processes"
  task :start do
    run  <<-CMD
      cd #{current_path}; bundle exec thin start -C config/thin_config.yml
    CMD
  end

  desc "Stop the Thin processes"
  task :stop do
    run <<-CMD
      cd #{current_path}; bundle exec thin stop -C config/thin_config.yml
    CMD
  end

  desc "Restart the Thin processes"
  task :restart do
    run <<-CMD
      cd #{current_path}; bundle exec thin restart -C config/thin_config.yml
    CMD
  end
end
Un a vez hecho esto, queremos ver el listado de tareas capistrano:
verdor@enlamina ~$ cap -T
Creamos el fichero de configuración del servidor en nginx para nuestra aplicación:
upstream showip {
        server 127.0.0.1:3000;
}
server {
        listen       80;
        server_name  showip.pi showip.jeronima.tk showip.tk;

        root /var/deploy/apps/showip/current/public/;
        access_log /var/log/nginx/showip-access.log;
        error_log /var/log/nginx/showip-error.log;
        rewrite_log on;

        location ~ ^/assets/ {
                root /var/deploy/apps/showip/current/public;
                gzip_static on;
                expires 1y;
                add_header Cache-Control public;
                add_header ETag "";
                break;
        }

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                client_max_body_size 10m;
                client_body_buffer_size 128k;

                proxy_connect_timeout 90;
                proxy_send_timeout 90;
                proxy_read_timeout 90;

                proxy_buffer_size 4k;
                proxy_buffers 4 32k;
                proxy_busy_buffers_size 64k;
                proxy_temp_file_write_size 64k;

                proxy_redirect off;
                if (!-f $request_filename) {
                        proxy_pass http://showip;
                        break;
                }
        }

        if (-f $document_root/system/maintenance.html) {
                return 503;
        }
        error_page 503 @maintenance;
        location @maintenance {
                rewrite  ^(.*)$  /system/maintenance.html last;
                break;
        }

        error_page 500 502 503 504 /50x.html;
}
Escribimos nuestro fichero de configuracion del thin, pero al final no lo usamos y lo generamos con thin en el propio servidor, probar ejecutando thin -h:

---
chdir: /var/deploy/apps/showip/current
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: /var/deploy/apps/showip/current/log/thin.log
pid: /var/deploy/apps/showip/current/tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 100
require: []
wait: 30
threadpool_size: 20
servers: 1
user: deploy
group: deploy
daemonize: true
Ahora tenemos que preparar el servidor con capistrano:

verdor@enlamina ~$ cap deploy:update
verdor@enlamina ~$ cap deploy:start

* Ojo, la aplicación no tiene base de datos, ni asset.



Más configuración en el servidor. WRAPPERS. Que wrappers.

Queremos que nuestra aplicación se inicie como un servicio más (init.d):

pi@raspberrypi ~ $ sudo su
root@raspberrypi:/home/pi# source /etc/profile #cargamos rvm
root@raspberrypi:/home/pi# rvm use ruby-2.0.0-p451@showip
root@raspberrypi:/home/pi# thin install
Installing thin service at /etc/init.d/thin ...
mkdir -p /etc/init.d
writing /etc/init.d/thin
chmod +x /etc/init.d/thin
mkdir -p /etc/thin

To configure thin to start at system boot:
on RedHat like systems:
  sudo /sbin/chkconfig --level 345 thin on
on Debian-like systems (Ubuntu):
  sudo /usr/sbin/update-rc.d -f thin defaults
on Gentoo:
  sudo rc-update add thin default

Then put your config files in /etc/thin

Genera el el fichero /etc/init.d/thin, mostramos como se crea, pero luego lo renombraremos y editaremos:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          thin
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: thin initscript
# Description:       thin
### END INIT INFO

# Original author: Forrest Robertson

# Do NOT "set -e"

DAEMON=/usr/local/rvm/gems/ruby-2.0.0-p451@showip/bin/thin
SCRIPT_NAME=/etc/init.d/thin
CONFIG_PATH=/etc/thin

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

case "$1" in
  start)
        $DAEMON start --all $CONFIG_PATH
        ;;
  stop)
        $DAEMON stop --all $CONFIG_PATH
        ;;
  restart)
        $DAEMON restart --all $CONFIG_PATH
        ;;
  *)
        echo "Usage: $SCRIPT_NAME {start|stop|restart}" >&2
        exit 3
        ;;
esac

:
Si vamos a tener más de una aplicación que use thin, renombramos el fichero /etc/init.d/thin, en nuestro caso por /etc/init.d/showip y modificamos...

SCRIPT_NAME=/etc/init.d/showip
CONFIG_PATH=/etc/showip #var/deploy/showip
Lo renombramos y ponemos en el arranque:

root@raspberrypi:~# mv /etc/init.d/thin /etc/init.d/showip
root@raspberrypi:~# sudo /usr/sbin/update-rc.d -f showip defaults
Generamos la configuracion del thin para nuestra aplicacion showip

root@raspberrypi:~# thin config -C /etc/thin/showip.yml -c /var/deploy/apps/showip/current --servers 1 -p 3000
Wrote configuration to /etc/thin/showip.yml
Editamos el fichero del config /etc/thin/showip.yml para ver si le damos algun retoque:

---
chdir: /var/deploy/apps/showip/current
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: log/thin.log
pid: /var/deploy/apps/showip/current/tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 100
require: []
wait: 30
threadpool_size: 20
servers: 1
user: deploy
group: deploy
daemonize: true
Generamos el wrapper de RVM ya que la ruta /usr/local/rvm/gems/ruby-2.0.0-p451@showip/bin/thin no nos acaba de funcionar, despues editaremos de forma definitiva /etc/init.d/showip

root@raspberrypi:~# rvm wrapper ruby-2.0.0-p451@showip showip thin
Regenerating ruby-2.0.0-p451@showip wrappers.........
Comprobamos que se ha generado (es un enlace blando):

root@raspberrypi:~# ls /usr/local/rvm/bin/ | grep showip
showip_thin
Podemos probarlo y si hay algun fallo en el fichero de configuracion, nos lo dira:

root@raspberrypi:~# /usr/local/rvm/bin/showip_thin start -C /etc/thin/showip.yml
/usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/psych.rb:205:in `parse': (/etc/thin/showip.yml): could not find expected ':' while scanning a simple key at line 15 column 1 (Psych::SyntaxError)

#o funcionara

Starting server on 0.0.0.0:3000 ... 

root@raspberrypi:~# /usr/local/rvm/bin/showip_thin stop  -C /etc/thin/showip.yml 
Stopping server on 0.0.0.0:3000 ... 
Sending QUIT signal to process 3001 ... 

Exiting!

Ya generado el wrapper editamos /etc/init.d/showip dejandolo como queda:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          thin
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: thin initscript
# Description:       thin
### END INIT INFO

# Original author: Forrest Robertson

# Do NOT "set -e"

DAEMON=/usr/local/rvm/bin/showip_thin
SCRIPT_NAME=/etc/init.d/showip
CONFIG_PATH=/etc/thin/showip.yml
#chown=/bin/chown
#mkdir=/bin/mkdir

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

case "$1" in
  start)
        $DAEMON start -C $CONFIG_PATH
        ;;
  stop)
        $DAEMON stop -C $CONFIG_PATH
        ;;
  restart)
        $DAEMON restart -C $CONFIG_PATH
        ;;
  *)
        echo "Usage: $SCRIPT_NAME {start|stop|restart}" >&2
        exit 3
        ;;
esac

:
Los fichero creados los ponemos como propiedad del usuario deploy:

chown deploy:deploy /etc/init.d/showip
Hacemos una pruaba antes de reiniciar y ver que arranca la aplicación:

pi@raspberrypi ~ $ sudo service showip start
Starting server on 0.0.0.0:3000 ... 
Ya podemos ir desarrollando, subir al repositorio y hacer deploy con las ultimas versiones del código.

verdor@enlamina$ cap deploy
Tendríamos que dar algunos retoques si tuviéramos mas de un entrono (entorno de pruebas) y la aplicación necesitase de base de datos, etc... Enlaces:

miércoles, 16 de abril de 2014

Tips Linux

raspbian.org

Conocer el espacio en disco duro libre y ocupado:
df -h
Conocer espacio ocupado de cache apt:
du -sh /var/cache/apt/archives
Elimina del cache los paquetes .deb con versiones anteriores a los de los programas que tienes instalados:
sudo apt-get autoclean
sudo apt-get clean      #Elimina todos los paquetes del cache.
sudo apt-get autoremove #Borra los paquetes huérfanos
Ver RAM ocupada en megas y SWAP:
free -m -t
Lista de procesos que se están ejecutando:
top
Cuanto tiempo lleva arrancada su máquina:
uptime
Ejecuta un script/programa en el arranque:
sudo vi /etc/init.d/miscript        #creamos script
sudo chmod 755 /etc/init.d/miscript #le damos permisos de ejecución
sudo /etc/init.d/miscript start     #lo probamos
sudo /etc/init.d/miscript stop      #lo paramos
sudo update-rc.d miscript defaults  #lo metemos en el arranque
sudo update-rc.d -f miscript remove #cuando lo queramos eliminar del arranque
Instalar servidor escritorio remoto
sudo apt-get install xrdp
Instalar cliente escritorio remoto
sudo aptitude install remmina remmina-gnome

martes, 11 de marzo de 2014

Raspberry Pi: Cambiando Apache por Nginx

Paramos Apache
pi@raspberrypi ~ $ sudo service apache2 stop
Instalamos Nginx (fuente)
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade
pi@raspberrypi ~ $ sudo apt-get install nginx openssl ssl-cert php5-cli php5-sqlite php5-gd
php5-curl php5-common php5-cgi sqlite3 php-pear php-apc curl libapr1 libtool curl
libcurl4-openssl-dev php-xml-parser php5 php5-dev php5-gd php5-fpm memcached php5-memcache varnish
Configuramos Nginx con un solo procesador:
pi@raspberrypi ~ $ sudo vim /etc/nginx/nginx.conf

#/etc/nginx/nginx.conf

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;
Arrancamos Nginx:
pi@raspberrypi ~ $ sudo /etc/init.d/nginx start
Abrimos el navegador apuntando a ip de la Raspberry Pi http://192.168.1.10/ y aparecerá

Welcome to nginx!

Nginx pide certificarnos, así que creamos unos para dos años[*]:
pi@raspberrypi ~ $ sudo openssl req $@ -new -x509 -days 730 -nodes -out /etc/nginx/cert.pem -keyout /etc/nginx/cert.key
Generating a 2048 bit RSA private key
.....+++
........................+++
writing new private key to '/etc/nginx/cert.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:ES
State or Province Name (full name) [Some-State]:MADRID
Locality Name (eg, city) []:MADRID
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyRaspberryPi
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:owncloud.myraspberrypi.tk                                
Email Address []:myraspberrypi@gmail.com
Les damos permisos
pi@raspberrypi ~ $ sudo chmod 600 /etc/nginx/cert.pem
pi@raspberrypi ~ $ sudo chmod 600 /etc/nginx/cert.key
Creamos la configuración para ownCloud
pi@raspberrypi ~ $ sudo vim /etc/nginx/sites-available/owncloud
Creamos enlace blando
pi@raspberrypi ~ $ sudo ln -s /etc/nginx/sites-available/owncloud /etc/nginx/sites-enabled/
Configuramos /etc/php5/fpm/php.ini
pi@raspberrypi ~ $ sudo vim /etc/php5/fpm/php.ini

# /etc/php5/fpm/php.ini

upload_max_filesize = 1000M
post_max_size = 1000M

# Añadimos al final del fichero
extension = apc.so
apc.enabled = 1
apc.include_once_override = 0
apc.shm_size = 256
Configuramos /etc/php5/fpm/pool.d/www.conf:
pi@raspberrypi ~ $ sudo vi /etc/php5/fpm/pool.d/www.conf

listen = 127.0.0.1:9000
Reiniciamos web server y PHP
pi@raspberrypi ~ $ sudo /etc/init.d/php5-fpm restart
pi@raspberrypi ~ $ sudo /etc/init.d/nginx restart
Y como ya teníamos la instalación de ownCloud con Apache, ya debería estar funcionando. Desinstalamos Apache
pi@raspberrypi ~ $ sudo apt-get purge apache2
Vamos ha hacer que el deamon-transmission vaya por nuestro Neginx, creamos el fichero de configuración /etc/nginx/sites-available/transmission que quedara así:
# redirect http to https.
server {
        listen 80;
        server_name torrent.myraspberrypi.tk transmission.myraspberrypi.tk;
        return 301 https://$server_name$request_uri;  # enforce https
}

server {
        listen 443 ssl;
        server_name  torrent.myraspberrypi.tk transmission.myraspberrypi.tk;

        ssl_certificate /etc/nginx/cert.pem;
        ssl_certificate_key /etc/nginx/cert.key;

        # Thats the important part. Most of the tutorial on the net are not Transmission specific
        # and don't pass the Transmission-Session Header
        location / {
                proxy_read_timeout 300;
                proxy_pass_header  X-Transmission-Session-Id;
                proxy_set_header   X-Forwarded-Host $host;
                proxy_set_header   X-Forwarded-Server $host;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

                # if you changed the port number for transmission daemon, then adjust the
                # folllowing line
                proxy_pass         http://127.0.0.1:9091/transmission/web/;
        }

        # Also Transmission specific
        location /rpc {
                proxy_pass         http://127.0.0.1:9091/transmission/rpc;
        }

        location /upload {
                proxy_pass         http://127.0.0.1:9091/transmission/upload;
        }

}
Creamos un enlace blando
pi@raspberrypi ~ $ sudo ln -s /etc/nginx/sites-available/transmission /etc/nginx/sites-enabled/transmission
Ahora la configuración para amule:
# redirect http to https.
server {
        listen 80;
        server_name amule.myraspberrypi.tk;
        return 301 https://$server_name$request_uri;  # enforce https
}

server {
        listen 443 ssl;
        server_name  amule.myraspberrypi.tk;

        ssl_certificate /etc/nginx/cert.pem;
        ssl_certificate_key /etc/nginx/cert.key;

        location / {
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:4711/;
                proxy_redirect off;
                proxy_http_version 1.0;
        }
}

Creamos un enlace blandoraspberry pi
pi@raspberrypi ~ $ sudo ln -s /etc/nginx/sites-available/amule /etc/nginx/sites-enabled/amule
Para la red local de casa, crear virtual host y redirigiros desde el /etc/hosts a la ip de la red local que tiene la raspberry py.

martes, 4 de marzo de 2014

Raspberry Pi: Conocer ip publica, y mandarla por correo con tarea cron

Con curl haremos una petición a alguna web que te dice tu ip (http://myip.dnsomatic.com/, http://myexternalip.com/raw, icanhazip.com, ifconfig.me):
pi@raspberrypi ~ $ sudo apt-get install curl # si nos hace falta instalar curl
pi@raspberrypi ~ $ curl icanhazip.com
xxx.xxx.xxx.xxx
Para enviar correos tenemos que instalar [fuente], sendmail, ssmtp, mailutils:
pi@raspberrypi ~ $ sudo apt-get install postfix mailutils
Para enviar emails:
echo "texto a teclear" | mail -s "Asunto" usuario1@dominio.com
Este seria el correo que querríamos mandar:
echo "$(curl ifconfig.me) $(date)" | mail -s "RaspberryPi: $(curl icanhazip.com) $(date)" enlamina@gmail.com
Creamos un script que mande el correo [*]:
pi@raspberrypi ~ $ vi scripts/mailip.sh

#!/bin/sh
# scripts/mailip.sh
# Correo con la ip externa
echo "$(curl ifconfig.me) $(date)" | mail -s "RaspberryPi: $(curl icanhazip.com) $(date)" enlamina@gmail.com
Guardamos con :wq y cambiamos permisos de ejeccución:
pi@raspberrypi ~ $ chmod u+x scripts/mailip.sh 
Comprobamos el estatus de el demonio de cron y editamos /etc/crontab y añadimos linea:
pi@raspberrypi ~ $ sudo /etc/init.d/cron status
[ ok ] cron is running.

pi@raspberrypi ~ $ sudo vi /etc/crontab 

# /etc/crontab: system-wide crontab
# m h dom mon dow user  command
00 10   * * *   pi     /home/pi/scripts/mailip.sh
y reiniciamos cron:
pi@raspberrypi ~ $ sudo /etc/init.d/cron restart
Se pueden programar tareas para cuando se inicia la maquina, ejemplo:
@reboot pi /home/pi/scripts/mailip.sh
Ahora vamos a usar un servicio de dns dinámico, será http://www.dnsdynamic.org/ API, y actualizaremos a la ip que tiene que apuntar cada media hora con una tarea cron, una vez que nos demos de alta creamos la tarea:
pi@raspberrypi ~ $ vi scripts/update_dnsdynamic.sh

#!/bin/bash

DNSD_USERNAME=enlamina@gmail.com
DNSD_PASS=mi_clave
DNSD_SERVER=www.dnsdynamic.org
DNSD_HOSTNAME=raspberryverdor.dnsdynamic.net
MY_IP=$(curl icanhazip.com)

curl -s -u $DNSD_USERNAME:$DNSD_PASS "https://$DNSD_SERVER/api/?hostname=$DNSD_HOSTNAME&myip=$MY_IP"
sleep 3
exit 0

Damos permisos de ejecución:
pi@raspberrypi ~ $ chmod u+x update_dnsdynamic.sh
Editamos con la herramienta de crontab para el usuario pi y añadimos la tarea:
pi@raspberrypi ~ $ sudo crontab -u pi -e

# m h  dom mon dow   command
*/30 * * * * /home/pi/scripts/update_dnsdynamic.sh

domingo, 2 de marzo de 2014

Raspberry Pi: Montar clientes Torrent, aMule y servidor ownCloud

Montamos cliente Torrent con Raspberry Pi. Instalación:

pi@raspberrypi ~ $ sudo apt-get -y install transmission-daemon
Lo paramos y configuramos, usaremos disco duro externo:

pi@raspberrypi ~ $ sudo /etc/init.d/transmission-daemon stop
pi@raspberrypi ~ $ cd /media/descargas
pi@raspberrypi ~ $ mkdir torrent
pi@raspberrypi ~ $ cd torrent
pi@raspberrypi ~ $ mkdir finish
pi@raspberrypi ~ $ mkdir temp
pi@raspberrypi ~ $ chmod 777 finish
pi@raspberrypi ~ $ chmod 777 temp
ls -l
Configuración:

pi@raspberrypi ~ $ sudo vi /var/lib/transmission-daemon/info/settings.json
Campos que tenemos que modificar:

“download-dir”: “/media/descargas/torrent/finish”
“incomplete-dir-enabled”: true
“incomplete-dir”: “/media/descargas/torrent/temp”
“rpc-enabled”: true
“rpc-bind-address”: “0.0.0.0″
“rpc-username”: “transmission”
"rpc-whitelist": "127.0.0.1"
“rpc-whitelist-enabled”: false
Arrancamos el servicio:

pi@raspberrypi ~ $ sudo /etc/init.d/transmission-daemon start
Y accedemos desde nuestro ordenador al cliente torrent de la Raspberry Pi. Le pusimos ip estatica y el usuario y la clave por defecto es "transmission":

http://192.168.1.32:9091/transmission Configuración aMule Instalamos el demonio:

pi@raspberrypi ~ $ sudo aptitude install amule-daemon
Indicamos un usuario:

pi@raspberrypi ~ $ sudo vi /etc/default/amule-daemon

# Configuration for /etc/init.d/amule-daemon

# The init.d script will only run if this variable non-empty.
AMULED_USER="pi"

# You can set this variable to make the daemon use an alternative HOME.
# The daemon will use $AMULED_HOME/.aMule as the directory, so if you
# want to have $AMULED_HOME the real root (with an Incoming and Temp
# directories), you can do `ln -s . $AMULED_HOME/.aMule`.
AMULED_HOME=""
Comando para iniciar, parar... el demonio

pi@raspberrypi ~ $ sudo /etc/init.d/amule-daemon start
pi@raspberrypi ~ $ sudo /etc/init.d/amule-daemon stop
Generamos contraseña, harán falta dos, una para la aplicación, y otra para para la gestión a través de la aplicación web

pi@raspberrypi ~ $ echo -n "contraseña" | md5sum
4c882dcb24bcb1bc225391a602feca7c  -
Configuramos la aplicación:

vi ~/.aMule/amule.conf

AcceptExternalConnections=1
ECPassword=4c882dcb24bcb1bc225391a602feca7c valor md5 de la contraseña
[WebServer]
Enabled=1
Password=4c882dcb24bcb1bc225391a602feca7c 
AllocateFullFile=1 # Reserva el espacio
Generamos la configuración de la aplicación web aMule usando la configuración de la aplicación

amuleweb --create-config-from=/home/username/.aMule/amule.conf
Se puede editar. Para que funcionara tuve que poner claves con mayúsculas tanto en amule.conf como en remote.conf

pi@raspberrypi ~ $ vi .aMule/remote.conf

Locale=
[EC]
Host=localhost
Port=4712
Password=4c882dcb24bcb1bc225391a602feca7c
[Webserver]
Port=4711
UPnPWebServerEnabled=0
UPnPTCPPort=50001
Template=
UseGzip=1
AllowGuest=0
AdminPassword=4c882dcb24bcb1bc225391a602feca7c
GuestPassword=
Reiniciamos el demonio y vamos a la ip estatica de la Raspberry Pi en el puerto por defecto:
pi@raspberrypi ~ $ sudo /etc/init.d/amule-daemon restart
[ ok ] Restarting aMule daemon: amuled.
http://192.168.1.32:4711/amuleweb-main-dload.php Server Recomendados:

!! Saugstube !!
ed2k://|server|193.138.221.214|4242|/

#eMule Serverlist Nr.1#
ed2k://|server|193.138.221.213|4242|/

#eMule Serverlist Nr.2#
ed2k://|server|193.138.221.210|4242|/

..:: France Mule #1 ::..
ed2k://|server|193.42.213.30|9510|/

eDonkeyServer No1
ed2k://|server|77.247.178.244|4242|/
Instalación de servidor OwnCloud Instalamos servidor Apache:
pi@raspberrypi ~ $ sudo apt-get install apache2 php5 php5-json php5-gd php5-sqlite curl libcurl3 libcurl3-dev php5-curl php5-common php-xml-parser
Instalamos base de datos SQLite:
pi@raspberrypi ~ $ sudo apt-get install sqlite
Nos descargamos el OwnCloud:
pi@raspberrypi ~ $ cd tmp
pi@raspberrypi ~/tmp $ wget download.owncloud.org/community/owncloud-5.0.0.tar.bz2
Descomprimir el archivo y copiarlo en el directorio /var/www
pi@raspberrypi ~/tmp $ sudo tar -xjf owncloud-5.0.0.tar.bz2 -C /var/www
Cambiar propietarios de la carpeta OwnCloud.
pi@raspberrypi ~ $ sudo chown www-data:www-data -R /var/www/owncloud
pi@raspberrypi ~ $ sudo chown www-data:www-data -R /media/descargas #Disco Duro Externo puede fallar
Editar el tamaño máximo de subida de archivos en Apache. Indicamos 2G.
sudo vi /etc/php5/apache2/php.ini

# /etc/php5/apache2/php.ini
upload_max_size = 2048 M
post_max_size = 2048 M
Reiniciamos Apache:
pi@raspberrypi ~ $ sudo service apache2 restart
Para que owncloud vaya mas rápido, instalar el acelerador php cache y reiniciar el apache:
pi@raspberrypi ~ $ sudo apt-get install php-apc
pi@raspberrypi ~ $ sudo service apache2 restart
Comprobamos que esta funcionando y acabamos la instalación http://tuip/owncloud Nos muestra un error:
Los datos del directorio (carpeta /media /descargas/) es legible para otros usuarios
Por favor, cambie los permisos a 0770 Sun que el directorio no se pueden enumerar por otros usuarios.
Se soluciona editando el fichero /var/www/owncloud/lib/util.php y comentamos parte del código, quedando así:
/*if (stristr(PHP_OS, 'WIN')) {
        //TODO: permissions checks for windows hosts
} else {
        $permissionsModHint = 'Please change the permissions to 0770 so that the directory'
                .' cannot be listed by other users.';
        $prems = substr(decoct(@fileperms($dataDirectory)), -3);
        if (substr($prems, -1) != '0') {
                OC_Helper::chmodr($dataDirectory, 0770);
                clearstatcache();
                $prems = substr(decoct(@fileperms($dataDirectory)), -3);
                if (substr($prems, 2, 1) != '0') {
                        $errors[] = array('error' => 'Data directory ('.$dataDirectory.') is readable for other users',
                                'hint' => $permissionsModHint);
                }
        }
}*/
Solo falta crear usuario en ownCloud y que las descargas vayan a su carpeta files o crear enlaces blandos. PD. Hacer que transmission, amule y owncloud se lleven bien. fuente molesybits. Para evitar el parche de los permisos de owncloud hay que hacer que los usuarios www-data y debian-transmission se lleven bien haciendo que ambos usuarios compartan el mismo grupo editando /etc/group:
pi@raspberrypi ~ $ sudo vi /etc/group
Buscamos el grupo debian-transmission y añadimos a los usuarios pi y www-data, quedando la linea:
#/etc/group

debian-transmission:x:111:pi:www-data
Y damos permisos al usuario www-data a la carpeta owncloud del disco duro externo si fuera necesario.
pi@raspberrypi ~ $ cd /media/descargas/owncloud
pi@raspberrypi ~ $ sudo chown -R www-data:debian-transmission data
y reiniciamos los servicios:
pi@raspberrypi ~ $ sudo service apache2 restart
pi@raspberrypi ~ $ sudo service transmission-daemon restart

domingo, 16 de febrero de 2014

Raspberry Pi: Configurar ip fija, acceso ssh y montarle disco duro externo

Conectamos la Raspberry Pi a al router y vemos que ip tiene o le conectamos un teclado y una pantalla abrimos una consola y tecleamos ifconfig.

pi@raspberrypi ~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:e7:76:6d  
          inet addr:192.168.1.31  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:463 errors:0 dropped:3 overruns:0 frame:0
          TX packets:334 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:48748 (47.6 KiB)  TX bytes:36262 (35.4 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 80:1f:02:af:2c:ef  
          inet addr:192.168.1.32  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:507 errors:0 dropped:526 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:103617 (101.1 KiB)  TX bytes:1195 (1.1 KiB)
Nos conectamos a traves de ssh. ssh -X pi@192.168.1.13 y he introducimos la clave. Por defecto "raspberry"

verdor@enlamina ~$ ssh pi@192.168.1.31
Linux raspberrypi 3.6.11+ #474 PREEMPT Thu Jun 13 17:14:42 BST 2013 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Feb 16 21:58:15 2014 from 192.168.1.33
pi@raspberrypi ~ $ 
Asignamos una ip fija a la Raspberry Pi. Se configura en /etc/network/interfaces

pi@raspberrypi ~ $ sudo vi /etc/network/interfaces

#/etc/network/interfaces
auto lo

iface lo inet loopback
#iface eth0 inet dhcp
iface eth0 inet static

address 192.168.1.31
gateway 192.168.1.1
netmask 255.255.255.0

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp
iface default inet static
address 192.168.1.32
netmask 255.255.255.0
gateway 192.168.1.1
Reiniciamos la Raspberry Pi para ver que tome los cambios:

pi@raspberrypi ~ $ sudo reboot
Queremos autenticarnos a la Raspberry Pi a través de una llave ssh, ya tenemos una. Hacemos en el servidor, la Raspberry Pi:

pi@raspberrypi ~ $  pwd
/home/pi
pi@raspberrypi ~ $ mkdir .ssh; chmod 700 .ssh
pi@raspberrypi ~ $ cd .ssh
pi@raspberrypi ~ $ touch authorized_keys; chmod 600 authorized_keys
En el cliente:

verdor@enlamina ~$ cd ~/.ssh
verdor@enlamina ~$ cat id_dsa.pub| ssh pi@192.168.1.32 'cat - >> ~/.ssh/authorized_keys'
Ahora configuramos ssh para acceder mas fácilmente a la Raspberry Pi:

verdor@enlamina ~$ vi ~/.ssh/config

#/home/verdor/.ssh/config

Host raspberrypi
  User pi
  HostName 192.168.1.31
  IdentityFile /home/verdor/.ssh/id_rsa
Guardamos con wq y entramos en la Raspberry Pi

verdor@enlamina ~$ ssh raspberrypi 
Linux raspberrypi 3.6.11+ #474 PREEMPT Thu Jun 13 17:14:42 BST 2013 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Feb 16 22:49:09 2014 from 192.168.1.33
pi@raspberrypi ~ $ 
Montamos disco duro externo para datos. Buscamos dispositivos conectados:

pi@raspberrypi ~ $  lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 005: ID 152d:2329 JMicron Technology Corp. / JMicron USA Technology Corp. JM20329 SATA Bridge
Averiguamos como se llama el dispositivo:

pi@raspberrypi ~ $ blkid
/dev/mmcblk0p1: LABEL="RECOVERY" UUID="242D-1618" TYPE="vfat" 
/dev/mmcblk0p5: SEC_TYPE="msdos" LABEL="boot" UUID="676B-0317" TYPE="vfat" 
/dev/mmcblk0p6: UUID="0eb36e9e-40f5-47f4-a751-4e197c0dd7c8" TYPE="ext4" 
/dev/sda1: UUID="XXXX-XXXX" TYPE="vfat"
Para obtener información mas detallada de la lista de las unidades conectadas:

pi@raspberrypi ~ $ sudo fdisk -l

Disk /dev/mmcblk0: 7861 MB, 7861174272 bytes
4 heads, 16 sectors/track, 239904 cylinders, total 15353856 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ba6a2

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048     2466796     1232374+   e  W95 FAT16 (LBA)
/dev/mmcblk0p2         2473984    15353855     6439936   85  Linux extended
/dev/mmcblk0p5         2482176     2596863       57344    c  W95 FAT32 (LBA)
/dev/mmcblk0p6         2605056    15353855     6374400   83  Linux

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000683e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              63   234436544   117218241    c  W95 FAT32 (LBA)
Creamos carpeta para el disco duro:

pi@raspberrypi ~ $ sudo mkdir /media/descargas
Editamos /etc/fstab para que se monte automáticamente al arrancar, añadimos la ultima linea:

pi@raspberrypi ~ $ sudo vi /etc/fstab

# /etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p5  /boot           vfat    defaults          0       2
/dev/mmcblk0p6  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that
UUID="XXXX-XXXX" /media/descargas vfat  defaults
Damos permisos de lectura y escritura al directorio de montaje:

pi@raspberrypi ~ $ sudo chmod -Rf 777 /media/descargas
Y montamos o reiniciamos:

pi@raspberrypi ~ $ sudo mount -a
Lo próximo será montar clientes Torrent, aMule y servidor Owncloud