Raspberry Pi : install Nginx with PHP support, CGI and TIMS

It is now time to install a Web server for various services such as monitoring, Download, Owncloud or a music Server. This is what we will see with the implementation of NGinx.

Raspberry Pi Home Server – Etape 11 : installer Nginx avec support PHP, CGI et SCGI

For our Web server, the goal is to have a support of different technologies to not be limited. In addition to Nginx, Let's install php - fpm, fcgiwrap and SCGI support (for the web interface for rtorrent). Since version 0.8.42, the SCGI module is included so not need to recompile everything (Phew !)

For those who want a comparison of different web servers, You can watch article here.

  1. Install additional packages
    apt - get install php5-fpm libgd2-xpm libpcrecpp0 libxpm4
  2. Install Nginx
    apt-get install nginx
  3. Create the directory /var/www
    mkdir/var/www chown-R www-data:www-data /var/www
  4. Create a file index.php in /var/www with the following content :
    <?php phpinfo();
    ?>
  5. Edit the/etc/nginx/sites-available/default file by replacing the old values by your configuration. Here for example :

    root /var/www;

    index index.php index.html index.htm;

    location ~ .php$ {

    fastcgi_pass unix:/var/run/PHP5-fpm.sock;

    fastcgi_index index.php;

    include fastcgi_params;

    }

  6. Restart the server
    service nginx restart
  7. A ride from your browser to the IP of your server, and you'll get the following result

    Viewing PHP file with Nginx

    View a file PHP with Nginx

  8. Install the CGI wrapper if you need to run CGI scripts on your server
    apt - get install fcgiwrap
  9. Reduce the number of PHP and CGI processes to free up memory
    1. Edit the file /etc/php5/fpm/php-fpm.conf
      process.Max = 2
    2. Modify the maximum number of processes. In my case I won't have dozens of connections therefore file /etc/nginx/nginx.conf :
      worker_processes 1
  10. Relaunch the different service
    Service nginx restart service php5-fpm restart

    If you have installed the CGI wrapper :

    Service fcgiwrap restart

This is for the web server in basic configuration.

You may also like...

  • Tom23 (@Tom23mj)

    Great job all these tutorials !

    I allowed myself to do a news on my blog to publicize, they deserve.

    • Thanks much for the news !!! C’est vraiment sympa 😉

  • Sudo

    Yes thanks for all these excellent tutorials ! 🙂

    I heard ( and am test ) a server apparently still more light than Nginx or lighttpd on Rpi, just be you be interested, It's Pancake :

    http://pancakehttp.net/

    J’ai également mis un lien vers ton blog à partir du mien 😉

    @

    • Genial for pancake I'll go see it. Just s power connect with TIMS for access to rtorrent. Sinon je vais devoir trouver un autre logiciel 🙂
      Et je dois faire une blogroll 🙂

  • Arno

    Shell :

    Should read “service php5-fpm restart” in step 10 !

    A chance to have a tutorial to integrate mysql nginx ? (Yes, I know, MySQL is slow !)

    • It's fixed, Thank you.
      For MySql, actually, given the weight it is not intended at the moment. Now once the steps for the Raspberry Pi Home Server completed, je pourrais toujours faire des essais basés sur vos suggestions 😉

  • Hello, I can comment on this article to ask you if it would be possible to have a tutorial for the installation of a cat with nginx, I looked at the rate of phpfreechat which air not evil but I have trouble with its configuration!

    Thank you!

  • Jordan

    Hello,

    I get to create the server, and to y access from the GUI of pi, but impossible from another PC in LAN… An idea of how this problem ?

    Merci 😉

    • Hello,
      The Raspberry is well accessible from other PC ? Type ping or ssh ? No blocking of port through the router ?

  • TEF

    Hello,

    After tests on several cms and the unsuccessful searches on the net, I have a question :

    How do you enable the smtp? (or set it up if it is already)

    Because my home unable to operate the contact form, mails from notifications, newsletter, etc…

    Thank you

    • Hello,
      In WordPress (that I know best) must be in the email settings put a SMTP server with the login and password to be able to use it. After it works “all alone” 🙂

  • TEF

    Thanks for your reply,

    priori my problem would come over the side of nginx/php on the raspberry as cms, I tested in my Web host and I have not had a problem with sending mails (actually it works “all alone”), but since the raspberry it doesnt.

    I already opened the port 25 on the box, I believe that it is the one used for the smtp.

    • If you are at Free, by default the use of SMTP from the local network is blocked. Need to change in the Free interface. It can be also valid at other operators.

  • Hello !

    Thanks for all this work !

    I am going to follow your tutorial and I was wondering if since the date of its drafting, It was still topical ? (packages to add ? etc)

    • (Detail in the case where : I am on Raspbian and not on Arch)

    • Hello,
      Configuring Nginx under Raspbian still works (I often use it in starting point for tutorials)

  • Damien

    Hello, the point 5 is not super clear on what exactly to do with the file. I ended up edit root and index lines and uncomment lines php to make this work.

    Initially I did not know what to do with the file, If ' it was necessary to completely replace its contents (What is recommended in the file itself), Add these lines at the end or something else.

  • dams1710

    Hello and congratulations for your tutorials that I enjoy to follow since my rpi….
    today I have a little trouble, Once the tutorial is finished when I type the ip address of my rpi I stumble upon a beautiful 403 forbidden!
    I think that's a concern of law on the or the file www but I can not found the solution, a little help would be welcome so that I understand what I have wrong or not well done!
    Thank you

    • dams1710

      Small precision if I type “ip_rpi index.php” It works I have the php info page that appears!

      • Hello,
        Is the index of the configuration of the server line well contains the index.php value ?

        • dams1710

          already thanks for your quick reply, so in fact I edit the default file to add indexphp, I therefore simply forgot the “.” between index and php as what it is useful to review!
          Thank you in any case!

  • yukora

    Hi chris

    I have a small problem of deposit I believe

    Here is that I have when I run the command : apt - get install php5-fpm

    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
    libonig2 libqdbm14 php5-common
    Suggested packages:
    php-pear
    The following NEW packages will be installed:
    libonig2 libqdbm14 php5-common php5-fpm
    0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
    Need to get 3,045 kB/3,294 kB of archives.
    After this operation, 8,820 kB of additional disk space will be used.
    Do you want to continue [Y/n]? y
    Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5-common armhf 5.4.4-14 deb7u3
    404 Not Found
    Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5-fpm armhf 5.4.4-14 deb7u3
    404 Not Found
    Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/p/php5/php5-common_5.4.4-14+deb7u3_armhf.deb 404 Not Found
    Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/p/php5/php5-fpm_5.4.4-14+deb7u3_armhf.deb 404 Not Found
    E: Unable to fetch some archives, maybe run apt-get update or try with –fix-missing?

    • Hello,
      as tu essayé de lancer la commande apt-get update avant pour mettre à jour la liste des dépôts?

      • yukora

        malheureusement oui ;(

        • Unable to fetch some archives, maybe run apt-get update or try with –fix-missing?
          Essayes peut etre alors avec l’option -fix-missing ….

          • yukora

            It n work but j have managed with the following command :
            apt-get update && apt-get install -fix-missing php5-fpm

            can that be my apt - get update alone malfunctioning

            Thank you for your help

            j've traveled your blog it is really nice

  • Camille fwix

    Hello,

    It seems to me that the php of the nginx conf file section should look like this:
    rental ~ [^/].php(/|$) {
    fastcgi_split_path_info ^(.+?.php)(/.*)$;
    if (!-f $document_root$ fastcgi_script_name) {
    return 404;
    }
    fastcgi_pass unix:/var/run/php5-rmsf .sock;
    fastcgi_index index.php;
    include fastcgi_params;
    }

    the test “if (!-f $document_root$ fastcgi_script_name)” is important from the point of view of safety see http://wiki.nginx.org/Pitfalls and http://wiki.nginx.org/PHPFcgiExample

    • Thanks for the info. I'm on an article optimization and security of Nginx. Ca va aider 😉

  • Pierre

    Hello,

    At the outset, Thanks much for all your work that is envy and allows beginners (like me) to embark on the adventure!

    I still have a small problem I have trouble understanding :
    When I raise nginx (step 6), It returns me “[Emerg] directive unknow “root/var/www” in/etc/nginx/sites-enabled/default:24″.
    I arrive yet to consult this directory “/var/www” to see the file and “index.php” I created with the command ls.

    Well cordially,
    Pierre

    • Hello,
      A space between root and/var/www is missing so I think your message

      • Pierre

        Thank you very much! It was indeed that! I have yet the nicely formatted look for linux…

  • Nicolas

    Good evening,

    Once these steps have been completed, the NO - IP domain created in step 7 is it supposed to display the PHP page ?

    I followed all your steps and my address no - ip.org continues to display the interface of my Livebox (the ping to the domain still returns the address of the router).

    May be is my port forwarding doesn't work. Here are the parameters :

    Application / Service : inadyn
    External port : 8090
    Internal port : 8090
    Protocol : TCP
    Equipment : raspberrypi

    Thanks again for your tutorials and help,
    Kind regards,

    Nicolas

    • I do not know the box live but maybe that in the zone of destination is better put an ip only name

      • Nicolas

        The Livebox does not leave us the choice and offers a drop down menu of devices that are already connected to the network.

        I'll look at it more in detail.
        Thank you !

        • Stupid question : you access via http://:8090/ ?
          And internal http://
          :8090 Displays well what you should ?

          • Nicolas

            No, precisely that doesn't either. Well, I display the PHP page with http://addresseIP_Pi:8090 but not directly with http://:8090.

            Note that in the file/etc/nginx/sites-available/default, I changed the following line :
            listen 8090;

            In reality, I wonder if this is not the Livebox, which does not highlight the NAT from the LAN…

  • Tony

    Hello,
    I start on this tutorial, and I am having a problem to the point number 7. When I type the IP private of Raspberry, I get the following output : 404 Not Found
    If I input the IP index.php , I get : File not found.
    The file /var/log/nginx/error.log Specifies : [error] 19746#0: *5 “/var / w www/index.php” is not found (2: No. such file or directory).
    Well I searched but I can't get out me of this step. I think that my etc/nginx/sites-available/default file must contain one or more errors.
    Is it possible to have a little help ?
    Thanks in advance

    Kind regards,

    Tony

    Below my default file :
    root@raspberrypi:/etc/nginx/sites-available # cat default
    # You may add here your
    # server {
    # …
    # }
    # statements for each of your virtual hosts to this file

    ##
    # You should look at the following URL's in order to grasp a solid understanding
    # of Nginx configuration files in order to fully unleash the power of Nginx.
    # http://wiki.nginx.org/Pitfalls
    # http://wiki.nginx.org/QuickStart
    # http://wiki.nginx.org/Configuration
    #
    # Generally, you will want to move this file somewhere, and start with a clean
    # file but keep this around for reference. Or just disable in sites-enabled.
    #
    # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
    ##

    server {
    #listen 80; ## listen for ipv4; this line is default and implied
    #listen [::]:80 default_server ipv6only = on; ## listen for ipv6

    #root/usr/share/nginx/www;
    root /var/wwww;

    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    #try_files $uri $uri / index.html;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    # proxy_pass http://127.0.0.1:8080;
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = 50x.html {
    # root/usr/share/nginx/www;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
    # fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # # 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;
    #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;
    # root html;
    # index index.html index.htm;
    #
    # location / {
    # try_files $uri $uri/ =404;
    # }
    #}

    # HTTPS server
    #
    #server {
    # listen 443;
    # server_name localhost;
    #
    # root html;
    # index index.html index.htm;
    #
    # SSL on;
    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;
    #
    # ssl_session_timeout 5m;
    #
    # ssl_protocols SSLv3 TLSv1;
    # ssl_ciphers ALL:!ADH:!EXPORT56:RC4 RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    # ssl_prefer_server_ciphers on;
    #
    # location / {
    # try_files $uri $uri/ =404;
    # }
    #}

    • Hello
      Y a 4 fois w dans le répertoire root (/var/wwww) c’est normal ?

      • Tony

        Thanks for your help and your wise eye, with 3 When www is better.

  • Theo

    Hello,

    I have a little problem I am stuck at step 7, I did not fall on this page. I well typed http://xxx.xxx.x.xx/index.php, but Firefox is my intention just to save a php file. I still reread posts on this subject, I can not find the cause of the problem

    Here is the file/etc/nginx/sites-available/default

    root@raspberrypi:~ # nano/etc/nginx/sites-available/default
    GNU nano 2.2.6 File: /etc/nginx/sites-available/default

    # You may add here your
    # server {
    # …
    # }
    # statements for each of your virtual hosts to this file

    ##
    # You should look at the following URL's in order to grasp a solid understanding
    # of Nginx configuration files in order to fully unleash the power of Nginx.
    # http://wiki.nginx.org/Pitfalls
    # http://wiki.nginx.org/QuickStart
    # http://wiki.nginx.org/Configuration
    #
    # Generally, you will want to move this file somewhere, and start with a clean
    # file but keep this around for reference. Or just disable in sites-enabled.
    #
    # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
    ##

    server {
    #listen 80; ## listen for ipv4; this line is default and implied
    #listen [::]:80 default_server ipv6only = on; ## listen for ipv6

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

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri / index.html;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    # proxy_pass http://127.0.0.1:8080;
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = 50x.html {
    # root/usr/share/nginx/www;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
    # fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # # 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;
    #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen somename:8080;
    # server_name somename alias another.alias;
    # root html;
    # index index.html index.htm;
    #
    # location / {
    # try_files $uri $uri/ =404;
    # }
    #}

    # HTTPS server
    #
    #server {
    # listen 443;
    # server_name localhost;
    #
    # root html;
    # index index.html index.htm;
    #
    # SSL on;
    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;
    #
    # ssl_session_timeout 5m;
    #
    # ssl_protocols SSLv3 TLSv1;
    # ssl_ciphers ALL:!ADH:!EXPORT56:RC4 RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    # ssl_prefer_server_ciphers on;
    #
    # location / {
    # try_files $uri $uri/ =404;
    # }
    #}

    When I ressaye in IE, I get the contents of the file /var/ www/index.php

  • Sébastien Mougel

    celui ci aussi n’apparait pas nomplu

Handpicked links

disk Page Caching using disk: enhanced Database Caching 316/877 queries in 0.453 seconds using disk Served from: pihomeserver.fr @ 2017-08-05 12:20:02 by W3 Total Cache -->