How to install NGINX + PHP-FPM on Raspberry Pi

Next we have prepared a simple tutorial on how to install the NGINX web server with PHP-FPM in Raspbian. It takes a few minutes to prepare Raspberry Pi to launch your website.

1. Update/upgrade and a good habit is to install the time synchronization and postfix

root@raspberrypi:~# apt-get update && apt-get -y upgrade
root@raspberrypi:~# apt-get install postfix ntp ntpdate

2. Install NGINX, PHP-FPM and APC cache package

root@raspberrypi:~# apt-get install nginx php5-fpm php-apc

3. Install MySQL server and PHPMyAdmin if needed

root@raspberrypi:~# apt-get install mysql-client mysql-server php5-mysql phpmyadmin

4. Add group and user for your domain. First uid and gid is 10000 and home user (domain) directory is in /home/

root@raspberrypi:~# addgroup --gid 10000 group001
root@raspberrypi:~# adduser --home /home/ --shell /dev/null --uid 10000 --gid 10000 --disabled-password --disabled-login --gecos '' user001

5. Create structure logs and web directory

root@raspberrypi:~# mkdir /home/       # for Nginx logs
root@raspberrypi:~# mkdir /home/  # for PHP logs
root@raspberrypi:~# mkdir /home/        # for your web page
root@raspberrypi:~# mkdir /home/        # for temp
root@raspberrypi:~# mkdir /home/   # for sessions
root@raspberrypi:~# chown -R user001:group001 /home/

6. Add to pool for your domain and edit it

root@raspberrypi:~# cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/
root@raspberrypi:~# nano /etc/php5/fpm/pool.d/
; pool name ('www' here)
; Unix user/group of processes
user = user001
group = group001
; The address on which to accept FastCGI requests.
listen = /var/run/
; Set permissions for unix socket, if one is used. In Linux, read/write
listen.owner = user001 = group001
listen.mode = 0666
; Default Value: nothing is defined by default except the values in php.ini ...
; add php.ini admin values
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f
php_flag[display_errors] = off
php_admin_value[error_log] = /home/
php_admin_flag[log_errors] = on
php_admin_value[upload_tmp_dir] = /home/
php_admin_value[session.save_path] = /home/
php_admin_value[open_basedir] = /home/
php_admin_value[mail.log] = /home/
php_admin_value[memory_limit] = 64M
php_admin_value[post_max_size] = 18M
php_admin_value[max_execution_time] = 60
php_admin_value[allow_url_fopen] = Off
php_admin_value[upload_max_filesize] = 18M
php_admin_value[date.timezone] = Europe/Prague


Do you need more power than RPi?

7. Stop NGINX and PHP-FPM services. Delete symlink for default vhost.

root@raspberrypi:~# service nginx stop && service php5-fpm stop
root@raspberrypi:~# unlink /etc/nginx/sites-enabled/default

8. Create new NGINX conf for your domain.

root@raspberrypi:~# nano /etc/nginx/sites-available/
# redirect www
server {
        listen 80;
        return 301 $scheme://$request_uri;
server {
        listen   80; ## listen for ipv4
        # root dir of your pages
        root /home/;
        index index.php index.html index.htm;
        location / {
                try_files $uri $uri/ /index.html;
        # pass the PHP scripts to FastCGI
        location ~ \.php$ {
                try_files $uri = 404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # php5-fpm :
                fastcgi_pass unix:/var/run/;
                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;
        # error and access logs
        error_log /home/ crit;
        access_log /home/;
        # other converting rewrite rules search on:

9. Create index.php file in your web home directory and insert phpinfo function into this file

root@raspberrypi:~# nano /home/
<?php phpinfo(); ?>

10. Create symlink from sites-available to sites-enabled directory and restart NGINX and php5-fpm

root@raspberrypi:~# ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
root@raspberrypi:~# service nginx restart && service php5-fpm restart
Restarting nginx: nginx.
[ ok ] Restarting PHP5 FastCGI Process Manager: php5-fpm.

11. Enjoy!

Rasberry Pi Raspbian NGINX PHP5-FPM



I am using VPS 512MB and articles help me get better performance with Nginx & PHP-FPM
Thank you!

Super !!!

I had to


include fastcgi_params;


include fastcgi.conf;

in the nginx-config to make PHP work...

Hi Steffen,

replacing fastcgi_params; with fastcgi.conf;

solved my issue too. Thanks !

Unit php5-fpm.service has begun starting up.
Jan 09 15:33:25 BriDiPi php5-fpm[7144]: [09-Jan-2017 15:33:25] ERROR: [pool www] unable to set listen address
Jan 09 15:33:25 BriDiPi php5-fpm[7144]: [09-Jan-2017 15:33:25] ERROR: failed to post process the configuration
Jan 09 15:33:25 BriDiPi php5-fpm[7144]: [09-Jan-2017 15:33:25] ERROR: FPM initialization failed
Jan 09 15:33:25 BriDiPi systemd[1]: php5-fpm.service: main process exited, code=exited, status=78/n/a
Jan 09 15:33:25 BriDiPi systemd[1]: Failed to start The PHP FastCGI Process Manager.
-- Subject: Unit php5-fpm.service has failed
-- Defined-By: systemd
-- Support:
-- Unit php5-fpm.service has failed.
-- The result is failed.
Jan 09 15:33:25 BriDiPi systemd[1]: Unit php5-fpm.service entered failed state.
Yes I am a Newb
On 2 seperate occassions I have had failures related to phpfastcgi. Any help would be appreciated.

Raspberry Pi 3 32GB