Raspberry Pi + Nginx + PHP

This post is a basic step-by-step of how to setup a Raspberry Pi as a web server. There is quite a few of those post out there but I wanted to put down what worked best for me.

Bear in mind that most commands have to be ran prefixed by sudo.

Basic Setup

First we need to go through the basic setup on the Pi, run raspi-config
– Choose option 8.5 to update raspi-config utility itself.
– Choose option 8.3 to change the memory split, only need 16MB for graphics.
– Change the user pi password with option 2.
– Make sure the time zone is correct with option 4.2.
– If you need to, you can use option 1 to resize the Pi partition (you’ll need to reboot for this to take effect).

Clean Up

Before you install anything I suggest you make some space by removing anything related to server X.
– Run apt-get remove –auto-remove –purge libx11-.*
– And apt-get autoremove, to remove packages that were automatically installed to satisfy dependencies.
– Run apt-get autoclean, to clear repository cache of packages that can no longer be downloaded.

You might need to reboot after this.

You can check the disk space left by typing df –h; partitions usage should be around 50% without having resized them in step 1. Also you can display the list of installed packages by running dpkg –get-selections.

Update Firmware and Software

– Run rpi-upgrade to upgrade the Raspberry Pi firmware, unlike most hardware the firmware actually resides on the SD card and not in the hardware itself.
– Run apt-get update to download the latest package lists from the repositories.
– Finally run apt-get dist-upgrade to fetch new versions of packages installed on the Pi.

Now you have a fully updated and cleaned Pi.

Install Nginx & PHP

Download and install Nginx
apt-get install nginx

Download and install PHP 5
apt-get install php5-cgi php5-fpm php5-cli

Download and install any other PHP packages
apt-get install php5-curl

Config Nginx & PHP

The php config file should be in /etc/php/fpm/pool.d/php.conf but you don’t actually need to change it at this stage.

With regards to Nginx server the document root is in /usr/shared/nginx/www, the standard is /var/www but again for simplicity we won’t change it. What we need to make sure however is that the root folder is owned by www-data which is the user running nginx and PHP services by default.
chown –R www-data:www-data /usr/shared/nginx/www

Then we need to enable the default “site” by copying the default file into the sites-enabled folder.
cp /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

And edit the default file as per below
nano /etc/nginx/sites-enabled/default

The default file should have this section un-commented.

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
        }
#       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#       # With php5-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
#       # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}
 
# deny access to .htaccess files, if Apache's document root concurs with nginx's one
location ~ /\.ht {
        deny all;
}

Then we make www-data user the owner of the root folder and give it read/write access.
chown www-data:www-data /etc/share/nginx/www
chmod 644 /etc/share/nginx/www

Now you can restart both services
service nginx restart
service php-fpm restart

Install & Setup an FTP Server

For the purpose of this we’ll install vsftpd but any other server would do.
apt-get install vsftpd

Edit the config file.
nano /etc/vsftpd.conf
There change anonymous_enable to No, local_enable to Yes and write_enable to Yes.

We can then restart the ftp service.
service vsftpd restart

Because for all intent and purpose we will connect to the ftp server using the www-data user, there are a couple more things we need to do:

1) give www-data a password.
passwd www-data

2) Change the www-data user home folder to be the web server root folder. Now, in order to do that we need to stop any processes ran by www-data.
service nginx stop
service php-fpm stop
usermod –d /usr/share/nginx/www/ www-data
service nginx start
service php-fpm start

We now have a basic web server up and running, on a reasonably clean install.

Useful Commands

df -h Display the amount of available disk space.
ps aux List all processes and associated users.
htop Interactive process viewer for Linux including memory usage.

chown user:group /path/to/folder/to/own Change ownership of folder and files.
chmod 664 Change access rights; Read is 4, write is 2 and execute 1. Add to combine. First number is for owning user, second for owning group and third for others.

cat /etc/passwd List all users by displaying the passwd file content
groups List all processes and associated users.

passwd user Add or change user password
usermod -aG user group Append group to the current list of user groups, do not use without option “a”.
usermod –d /usr/share/nginx/www/ user Change user default folder.

service “service name” start|stop|restart Start, stop or restart a service.

Raspberry Pi + Nginx + PHP

3 thoughts on “Raspberry Pi + Nginx + PHP

  1. DrDoom says:

    – Run rpi-upgrade to upgrade the Raspberry Pi firmware, unlike most hardware the firmware

    is supposed to be

    rpi-update to upgrade the Raspberry Pi firmware, unlike most hardware the firmware

Leave a comment