Saturday, June 28, 2014

Streaming USB webcam on Raspberry PI

I had a USB webcam lying around and a Raspberry Pi. And, I wanted to stream it over my local WiFi network. To do this, I picked up MJPEG-Streamer. Connecting the webcam and issuing 'lsmod' showed this:
$ lsmod
.
.
Bus 001 Device 004: ID 0ac8:3420 Z-Star Microelectronics Corp. Venus USB2.0 Camera
The device is supported by UVC driver.
ls /dev/vid*
/dev/video0
So, we have '/dev/video0' as our webcam.
Let's install some required packages.
On a RPi, I have faced multiple issues trying to install packages without first upgrading the system to the latest state. So, let's upgrade the system first.
$ sudo apt-get update && sudo apt-get upgrade
And, some requirements for MJPEG-Streamer
$ sudo apt-get install subversion imagemagick libjpeg8-dev
Now, let's download the latest source
$ svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
$ make
When it finishes, it leaves "mjpeg-streamer" binary to the folder. This may be added to path variable. Now, let's stream. 15fps with hardware MJPEG Compression
$ ./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -f 15 " -o "./output_http.so -w ./www"
If you get the error like this:
The input device does not supports MJPEG mode
You may also try the YUV mode (-yuv option), but it requires a much more CPU power
 Init v4L2 failed !! exit fatal 
 i: init_VideoIn failed
Then we need to add '-y' argument to fallback to software mode.
$ ./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -y -f 15" -o "./output_http.so -w ./www"
You can access the web interface at http://<ip>:8080/ where 'ip' is the IP of the Raspberry Pi server. The frame rate can also be changed by specifying the custom value instead of 15. To stream at different resolution with 30fps:
$ ./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -f 15 -r 960x720" -o "./output_http.so -w ./www"
The best part of MJPEG-Streamer is that you can also use VLC or any other streaming player to watch the stream and you are not limited to a web interface.
Happy streaming.

Saturday, January 5, 2013

WordPress SEO by Yoast and Nginx

Wordpress-SEO by Yoast is a great plugin for search engine optimization. It can generate XML sitemaps on the fly. While on Nginx, I found that I was getting 404 errors when trying to load the sitemap.

The solution was simple. We just need to pass the "sitemap name" as a URI to "index.php" which can be done by simply adding this to "server block":

rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
If you are already using Nginx with Wordpress friendly URLs, these steps are not necessary and redundant.

Wednesday, August 22, 2012

Allow only SCP/SFTP transfers with 'scponly'

scponly restricts users to SCP and SFTP logins by acting itself as a shell and this disables SSH logins. This allows using Filezilla or 'scp' tool to transfer files but restrict SSH logins via SSH clients. Debian has this package as 'scponly'. TO install it:
# apt-get install scponly
Since, we will not be chrooting users, suid binary is not required. So, select no to: Install the chrooted binary /usr/sbin/scponlyc SUID root? <-- No For existing users, do:
# usermod -s /usr/bin/scponly username
To create a new user with scponly:
# useradd -m -s /usr/bin/scponly username
# passwd username

Wednesday, August 1, 2012

Optimized LEMP Installer for Debian/Ubuntu with Virtual Hosts Automator.

LAMP stacks are used widely but they are notorious for being less resource friendly and in the same time, incredibly attracting to script kiddies and part time crackers.

There's a tradition to play with most popular toys rather than one which was designed to be small and less expensive. As a kid, what would you like to steal in a kindergarten: a Spider-Man toy or a small spider miniature?
Leaving aside these arguments, let's move on to the real-valued part. I wrote this script which I think, has the following featured jobs:

  • Configures Nginx for performance and static file caching as well as gzipping the output.
  • Installs and PHP-FPM as well as PHP-APC for Opcode Caching
  • Optimizes MySQL for caching queries, while being light on resources.
  • Facilitates interactive virtual hosts creation with dedicated PHP-FPM pool for each user so that each PHP process runs as the owner. This eliminates the need to install FTP server for updating Wordpress or installing plugins. Since, PHP runs as the user owning the script, "wp-config" can be set to very strict permissions like 700.

There's even more...

  • Virtual hosts generation script also has the ability to create MySQL user and database as well as allowing user to import SQL file onto the newly created database for easy migration.
  • PHPMyAdmin installer is also built-in which allows PHPMyAdmin to be installed on specified domain or sub-domain. 
  • This script uses unix sockets instead of TCP which promises some more improvement and lowers TCP overhead within the system. 
  • I have implemented some security practices like "cgi.fixpath_info=0". 
Download the script here and run it as root:
wget https://raw.github.com/aatishnn/lempstack/master/lemp-debian.sh
chmod +x lemp-debian.sh
./lemp-debian.sh
This script works for me but I cannot guarantee that it will work for you too. I am not a Bash expert. Please add some regexp to your brain to ignore my scripting laziness. This is designed to work with Debian and Ubuntu. You may freely use it to suit your needs under the condition that you will thank me. I would be glad if you can share something about this script, test it, debug it or just scold, rebuke or criticize.