A. Setting up an Apache Web Server on a Raspberry Pi
Apache is a popular web server application you can install on the Raspberry Pi to allow it to serve web pages.
On its own, Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.
Install Apache
First install the apache2 package by typing the following command in to the Terminal:
sudo apt-get install apache2 -y
Test the web server
By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://192.168.1.10 (whatever the Pi's IP address is) from another computer on the network. To find the Pi's IP address, type hostname -I at the command line (or read more about finding your IP address).
Browse to the default web page either on the Pi or from another computer on the network and you should see the following:
This means you have Apache working!
Changing the default web page
This default web page is just a HTML file on the filesystem. It is located at /var/www/html/index.html.
Note: The directory was /var/www in Raspbian Wheezy but is now /var/www/html in Raspbian Jessie
Navigate to this directory in a terminal window and have a look at what's inside:
cd /var/www/html
ls -al
This will show you:
total 12
drwxr-xr-x 2 root root 4096 Jan 8 01:29 .
drwxr-xr-x 12 root root 4096 Jan 8 01:28 ..
-rw-r--r-- 1 root root 177 Jan 8 01:29 index.html
This shows that by default there is one file in /var/www/html/ called index.htmland it is owned by the root user (as is the enclosing folder). In order to edit the file, you need to change its ownership to your own username. Change the owner of the file (the default pi user is assumed here) using sudo chown pi: index.html.
You can now try editing this file and then refreshing the browser to see the web page change.
Your own website
If you know HTML you can put your own HTML files and other assets in this directory and serve them as a website on your local network.
Additional - install PHP
To allow your Apache server to process PHP files, you'll need to install PHP5 and the PHP5 module for Apache. Type the following command to install these:
sudo apt-get install php5 libapache2-mod-php5 -y
Now remove the index.html file:
sudo rm index.html
and create the file index.php:
sudo leafpad index.php
Note: Leafpad is a graphical editor. Alternatively, use nano if you're restricted to the command line
Put some PHP content in it:
<?php echo "hello world"; ?>
Now save and refresh your browser. You should see "hello world". This is not dynamic but still served by PHP. Try something dynamic:
<?php echo date('Y-m-d H:i:s'); ?>
or show your PHP info:
<?php phpinfo(); ?>
Further - WordPress
Now you have Apache and PHP installed you can progress to setting up a WordPress site on your Pi. Continue to WordPress usage.
B. Setting up an NGINX web server on a Raspberry Pi
NGINX (pronounced engine x) is a popular lightweight web server application you can install on the Raspberry Pi to allow it to serve web pages.
Like Apache, NGINX can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.
Install NGINX
First install the nginx package by typing the following command in to the Terminal:
sudo apt-get install nginx
and start the server with:
sudo /etc/init.d/nginx start
Test the web server
By default, NGINX puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://192.168.1.10 (whatever the Pi's IP address is) from another computer on the network. To find the Pi's IP address, type hostname -I at the command line (or read more about finding your IP address).
Browse to the default web page either on the Pi or from another computer on the network and you should see the following:
Changing the default web page
NGINX defaults its web page location to /var/www/html on Raspbian. Navigate to this folder and edit or replace index.nginx-debian.html as you like. You can confirm the default page location at /etc/nginx/sites-available on the line which starts with 'root', should you need to.
Additional - Install PHP
sudo apt-get install php5-fpm
Enable PHP in NGINX
cd /etc/nginx
sudo nano sites-enabled/default
find the line
index index.html index.htm;
roughly around line 25 (Press CTRL + C in nano to see the current line number)
Add index.php after index to look like this:
index index.php index.html index.htm;
Scroll down until you find a section with the following content:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# location ~ \.php$ {
Edit by removing the # characters on the following lines:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
It should look like this:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Reload the configuration file
sudo /etc/init.d/nginx reload
Test PHP
Rename index.nginx-debian.html to index.php:
cd /var/www/html/
sudo mv index.nginx-debian.html index.php
Open index.php with a text editor:
sudo nano index.php
Add some dynamic PHP content by replacing the current content:
<?php echo phpinfo(); ?>
Save and refresh your browser. You should see a page with the PHP version, logo and current configuration settings.