Simple Pi Car – Setting up HTTP Server

Introduction:

I’m a full-time software developer by day, and the primary language our company uses for our applications is PHP. The intention of this article is to describe one method for getting a Raspberry Pi to run a web-server and serve pages uses PHP.

Assumptions: 

  • Basic knowledge of how to connect to your Pi via SSH or VNC
  • You have a running Raspberry Pi with an operating system installed and an operating network device on the Pi ( Raspbian download )

To Begin:

There is a ton of information out there about how to install an HTTP server on a linux box. For the sake of ease let it be known that this is one of many options. We’re going to be installing an NGINX server with PHP5. I chose this stack because it’s what I use daily, and it’s definitely a tested and proven set of technologies.

To install NGINX simply update the OS,

sudo apt-get update

Then install NGINX

sudo apt-get install nginx  

It’s really that simple!

Now install PHP as well
sudo apt-get install php5-fpm

The apt-get program does a really good job at installing the applications and their dependencies and registering the executables with your environment’s PATH — This means that as soon as the package is installed, you can use it from the command line.

To manually start the server type
sudo /etc/init.d/nginxstart

NGINX serves files from a folder designated in the configuration. NGINX has several configuration files in their respective folders starting at /etc/nginx/. We’re only concerned with adjusting the default configuration to serve files from the /usr/share/nginx/www folder

Move your command prompt to the NGINX directory
cd /etc/nginx/

Take a look at the files in the folder if you’d like
ls -l

Now we’re going to edit the configuration file
sudo nano sites-enabled/default

Don’t be intimidated by this file, we’re only concerned with a couple of lines. Everything after a # is a comment, which is completely ignored by NGINX

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        server_name _;

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Copy and paste the lines above into the editor. CTRL+X will create a prompt near the bottom asking if you want to save, type y and enter to save and return to the prompt.

You now have a running HTTP server listening for incoming HTTP requests on port 80p. You can test it out by creating the index.php file at /usr/share/nginx/www/index.php

Here’s a simple sample page that will display some server information for you.

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);

foreach($_SERVER as $k => $v){
    print_r("$k - $v <br/>");
}

Now you can visit your Pi’s IP in any browser on your network and this page should show.

Note: you won’t be able to assign a domain name on a local network without a great deal of effort, so it’s always easiest to just visit the IP address.

Next we’ll setup a web interface for controlling the car.

 

Tags: , , ,