Setting up a web server
What is a web server?
A web server is just a computer that serves content by using standard internet protocols (HTTP, IP, TCP, ARP).
If your device can join a local area network and has a web browser it can be a web server.
To set up the Pi as a web server, you need to install (Linux, Apache, MySQL, PHP).
- Open terminal and type the following line to install Apache, PHP and the library that connects them together:
sudo apt-get install apache2 php5 libapache2-mod-php5
- After that process completes, you need to restart the Apache service or daemon:
sudo service apache2 restart
- The next line will ensure that the default home page was created:
- Open the LXDE desktop
- Open the MIdori browser and navigate to http://localhost
- If you see a page that says "It works" everything went well.
- Now you must verify the PHP configuration. Navigate to /var/www
- Create a new text file named phpinformation.php:
sudo nano phpinformation.php
- In nano add the following line:
- Press CTRL+X and then Y and Enter to save file and exit nano.
- In the LXDE Open Midori and type:
http://localhost/phpinformation.php
You should see the phpinfo() function.
- Now you need to install the M in LAMP stack, MySQL.Type:
sudo apt-get install mysql-server mysql-client php5-mysql
When prompted, set a decent password and write it down somewhere.
- From terminal type:
sudo /etc/init.d/mysql status
You should be able to see a screen that indicates MySQL is installed.
Transfering Files
You can use SFTP to transfer content to the Pi.
Download
FileZilla onto your computer as your SFTP client.
- Open the application
- Click File > Site Manager
- In the General tab fill out the relevant connection details:
- Host:ip of your pi
- Protocol: select SFTP-SSH File Transfer Protocol
- Logon Type: set to normal
- User: specify the user (pi)
- Password: Specify the current password for the chosen account
- Click connect
- Navigate to /var/www
- Drag and drop any ontent into the target pi directory in FileZilla
- Click Disconnect on the toolbar to end the session
Setting up phpMyAdmin
phpMyAdmin is an open source PHP web application that provides you with a graphical front end interface
to MySQL. This is a browser-based tool, but there is still a bit of installation.
- In terminal type:
sudo apt-get install phpmyadmin
- In the configuring screen press Spacebar to place the selection asterisk next to apache2. Then press Tab and Enter
- When the next screen appears, select Yes to install the database.
- Type in the MySQL root user password and press Enter to continue. Then confirm the phpMyAdmin admin password
- Now you need to open the Apache 2 configuration file and link Apache to phpMyAdmin:
sudo nano /etc/apache2/apache2.conf
- In the conf file press CTRL+V repeatedly to scroll to the bottom of the file. At the bottom of the file add this line:
Include /etc/phpmyadmin/apache.conf
- Save changes and exit nano
- Restart apache by typing:
sudo /etc/init.d/apache2 restart
- To test , start LXDE, open Midori and navigate to http://localhost/phpmyadmin
- Log in with the username root and your MySQL admin password.
Joomla
Joomla is a content management system.
- From the LXDE open Midori and download the latest version of Joomla.
- Open Terminal and unpack the contents of Jooma to the apache content directory.
cd into the content directory and then type Replace Joomla.zip, with the downloaded Joomla name
sudo unzip joomla.zip -d /var
- Open the php configuration file:
sudo nano /etc/php5/apache2/php.ini
- In nano press CTRL+W to search for output_buffering
- Set the Development Value and Production Value to 0
- Press CRTL+O, Enter and then CTRL+X to save and exit
- Reboot the pi
- After reboot, open the LXDE and open another terminal seesion so that you can creare the Joomla configuration file and ensure that
it is writable:
cd /var/www
sudo touch configuration.php
sudo chmod 777 configuration.php
- Open Midori and navigate to the apache content page:
- If you see the default apache page instead of the Joomla page, then delete the old index page:
- If you cannot delete the installation page, in terminal:
cd /var/www
sudo rm -rf installation/
On the Internet
By putting your pi on the internet you can take advantage of services like Dropbox and Pandora, you can communicate with other users and you can use internet-dependent applications.
If you are going to do this, you must consider viruses and exposing private data.
Some precautions you can take involve configuring the pi with a static ip and using a DNS service.
Using No-ip
- Navigate to www.no-ip.com and create a free account
- Navigate to manage hosts and set up your ip.
>
- Create a subdirectory inside your home directory so that you have a place to store the No-IP client software
- Download No-IP's dynamic update client from www.no-ip.com
wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz
- Unpack and navigate to the directory:
ar vzxf noip-duc-linux.tar.gz
ls no*
cd noip-2.1.9.1
- You now have uncompiled source files that you will need to compile
sudo make
sudo make install
- Enter your no-ip membership credentials
- Run the client:
sudo /usr/local/bin/noip2
- Log into your router and set up port forwarding to allow traffic on TCP port 80 (HTTP)
- Open a web browser on another computer and load your Joomla site. Follow this tutorial
to get the client to run automatically at startup.
A web Cam
Your Pi has a Camera Serial Interface 2 camera connector, which you can find between the Ethernet and HDMI port.
Never plug or unplug hardware from the pi while it is running, but you will need to enable the use of the camera before you can use it.
- While connected to your computer update the software
sudo apt-get update && sudo apt get upgrade
- Now you want to navigate to the raspi-config menu:
- Scroll down to camera and press ENTER
- Choose Enable suport for the Raspberry Pi and press ENTER
- Exit the config menu and reboot your pi
- Now shutdown your pi
- WARNING: The interface is very delicate
Make sure that your Pi is not powered
- Gently grasp the edges of the CSI-2 connector and carefully lift it off (it will remain attached, just lift it about a millimeter).
- Insert the camera board ribbon cable into the interface with the copper traces facing away from the Ethernet port
- When the cable is connected to the interface, gentlt press the retaining clip down to lock the cable in place.
- To tell your Pi to take a picture:
raspistill -o myImage.jpg -q 50 -t 10000
Which creates a still image with the name myImage.jpg, a quality of 50% (0 being the lowest and 100 being the highest) and a 10 second shutter delay.
raspistill -v -w 1024 -h 768 -o myImage.jpg -q 50 -t 10000
the -v turns on the verbose command and is helpful for debugging
-w is the width and -h is the height of the image.
- To see more raspistill commands type:
- To record a 5 second video:
raspivid -o fiveseconds.h264 -t 5000
By default the size is 1080p HD at 1920x1080 pixels
-
raspivid -o smallvid.h264 -t 15000 -w 1024 -h 768
This is a 15 second video with a size of 1024x769
- To atually see your raw video you have to wrap the stream in an MPEG layer 4 container. To do this, download and install MP4BOX:
sudo apt-get install -y gpac
- Then:
MP4Box -fps 30 -add smallvid.h264 smallvid.mp4
- To view the movie:
For more video info see elinux.org/Rpi_Camera_Module#RaspiVid
Setting Up a Time-Lapse Webcam
- Install fswebcam
sudo apt-get install fswebcam
fswebcam -r 1024 x 768 -d /dev/video0 imagename.jpg
The r switch specifies the dimensions the d switch the output directory
- Create a configuration file for fswebcam to save your defaults
cd ...
sudo nano .fswebcam.conf
- Add these lines and customize:
device /dev/video0
input 0
loop 15
skip 20
background
resolution 320x240
set brightness=60%
set contrast=13%
top-banner
title "webcam"
timestamp "%d-%m-%Y %H:%M;%S (%Z)"
jpeg 95
save /home/pi/webcam/viewcam.jpg
palette MJPEG
Save file
-
To start the fswebcam using the config file
fswebcam -c ~/.fswebcam.conf
- To stop the process:
- To create a repeating operating system level job that snaps a picture every minute:
- Enter the following:
#!/bin/sh
filename=$(date +"%m-%d-%y||%H%M%S")
fswebcam -r 356x292 -d /dev/video0 $filename.jpg
cp $filename.jpg /home/pi/webcam
Save
- Make your shell script executable
sudo chmod +x camscript.sh
- Start the crontab editor for the pi user:
- When the file opens, move the curso r to the bottom. By entering ths line the pi will run the camscript.sh script every minute:
*/1 * * * * /home/pi/campscript.sh