Nginx Web Server with PHP Support through Entware

1 - Install Entware from here

2 - Update entware

opkg update
opkg upgrade

3 - Install nginx and php

opkg install nginx php5-cgi

4 - Backup default configuration file

mv /opt/etc/nginx/nginx.conf /opt/etc/nginx/nginx.conf-orig

5 - Create nginx configuration file, copy/paste in terminal:

cat >> /opt/etc/nginx/nginx.conf << 'EOF'
user nobody;
worker_processes 1;
events {
worker_connections 64;
}
http {
include mime.types;
include /opt/etc/nginx/sites-enabled/*;
default_type application/octet-stream;
server {
listen 82;
server_name localhost;
location / {
root /opt/share/nginx/html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /opt/share/nginx/50x.html {
root html;
}
location ~ \.php$ {
root /opt/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
EOF

Press Enter

6 - Create php5-cgi startup script

cat >> /opt/etc/init.d/S80php5-cgi << 'EOF'
#!/bin/sh
ENABLED=yes
PROCS=php-cgi
ARGS="-b 127.0.0.1:9000"
PREARGS="/usr/bin/env PHP_FCGI_CHILDREN=2 PHP_FCGI_MAX_REQUESTS=1000"
DESC=$PROCS
PATH=/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
. /opt/etc/init.d/rc.func
EOF

Press Enter

7 - Make script executable

chmod +x /opt/etc/init.d/S80php5-cgi

8 - Fix php doc_root for nginx

sed -i 's|doc_root = "/opt/share/www"|doc_root = "/opt/share/www"\
doc_root = "/opt/share/nginx/html"|g' "/opt/etc/php.ini"

- Start server

/opt/etc/init.d/S80nginx start
/opt/etc/init.d/S80php5-cgi start

10 - Go to 192.168.1.1:82 and if you see this page, the nginx web server is configured correctly

 

11 - Create php info page:

cat <<EOF >> /opt/share/nginx/html/info.php
<?php
phpinfo();
?>
EOF

Press Enter

12 - Go to 192.168.1.1:82/info.php and if you see this page, php is configured correctly

ACCESS THE WEBSITE FROM WAN

nano /jffs/scripts/firewall-start

Paste this lines

#!/bin/sh
iptables -I INPUT -p tcp --destination-port 82 -j ACCEPT

Save with CTRL-O / Enter / and exit with CTRL-X
Make script executable and apply rule
chmod a+rx /jffs/scripts/firewall-start
sh /jffs/scripts/firewall-start

Go to Port Forwarding page http://192.168.1.1/Advanced_VirtualServer_Content.asp and redirect port 80 to 82, now you should have access from wan.


Enjoy...

Thanks @mercury for source

  • Abd Kamel

    Hi,
    thanks alot for your article,
    this php5-cgi seem not to support php session, can you describe how to install with php5-mod-session??

    • opkg install php5-mod-session

  • Furqan Razzaq

    Hi, I am having the exact same problem as stebrick. Getting the same Permission denied error. I followed the tutorial line by line. I also ran chmod +x /opt/etc/init.d/S80php5-cgi command. I am still not able to access 192.168.1.1:82. My router is AC88u.

    Appreciate the help.

    • Please replace content of /opt/etc/init.d/S80php5-cgi with:

      #!/bin/sh
      ENABLED=yes
      PROCS=php-cgi
      ARGS="-b 127.0.0.1:9000"
      PREARGS="/usr/bin/env PHP_FCGI_CHILDREN=2 PHP_FCGI_MAX_REQUESTS=1000"
      DESC=$PROCS
      PATH=/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      . /opt/etc/init.d/rc.func

      • Furqan Razzaq

        Thanks TeHashX. The above change worked. Now I can access 192.168.1.1:82 successfully. But I am still not able to get to 192.168.1.1:82/info.php. It shows:

        Sorry, the page you are looking for is currently unavailable.
        Please try again later.

        I have info.php under /opt/share/nginx/html folder.

        • Clear cookies or try with another browser

          • Furqan Razzaq

            Cleared chrome cookies -> same error. Moved to Safari -> same error. The error logs say: [error] 14144#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.2.43, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.2.1:82"

          • Furqan Razzaq

            Above error got resolved. I forgot to restart php5-cgi. But now getting error "No input file specified." :(

          • Furqan Razzaq

            I finally figured it out. /opt/etc/php.ini had doc_root = /opt/share/www; which is wrong. Nginx is now serving files through /opt/share/nginx/html. So either create a synlink ln -s /opt/share/www /opt/share/nginx/html or simple change doc root in /opt/etc/php.ini doc_root = /opt/share/nginx/html. Restart php5-cgi using /opt/etc/init.d/S80php5-cgi restart. Wala! you are good to go with PHP.

          • Thanks a lot for your solution :)

          • I added both /opt/share/www & /opt/share/nginx/html to php.ini as doc_root (see point 8) because if someone has nginx and lighttpd installed can use both at the same time just need to change server port

          • Furqan Razzaq

            Perfect ! Thanks.

  • cryssy

    Salut TeHashX.
    I have some problems with Nginx , PHP don't want to run function fopen (file_get_contents), I got a Request Timeout error in my php error log:

    [13-Jan-2017 13:43:44 Europe/Bucharest] PHP Warning: file_get_contents(http://mysite/somefile.php): failed to open stream: HTTP request failed! HTTP/1.1 504 Gateway Time-out

    I had no problem with Lighttpd and the same php.ini configuration and the same php files. The problem seems to be discussed here: http://stackoverflow.com/a/7260962/4481831 . Seems there is this kind of problem when there is a single worker process for fastcgi.
    I see that in your Lighttpd tutorial https://www.hqt.ro/lighttpd-web-server-with-php-support-through-entware-ng/ you are running the webserver with more then 1 children for fastcgi. That is why probably the same php files are running fine on Lighttpd.

    For Nginx tutorial you don't specify anywhere PHP_FCGI_CHILDREN , and I think the default is 1. Probably this is blocking the php fopen function.

    On debian/ubuntu usualy they specify some arguments when they starting the service https://www.nginx.com/resources/wiki/start/topics/examples/fastcgiexample/ , but the Entware service script look very different from the one used for debian/ubuntu.
    Any idea how to fix this for Entware?

    • cryssy

      manage to resolve the problem. you can do a /opt/etc/init.d/S80php5-cgi like this:

      #!/bin/sh
      ENABLED=yes
      PROCS=php-cgi
      ARGS="-b 127.0.0.1:9000"
      PREARGS="/usr/bin/env PHP_FCGI_CHILDREN=2 PHP_FCGI_MAX_REQUESTS=1000"
      DESC=$PROCS
      PATH=/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      . /opt/etc/init.d/rc.func

      now the fopen function in PHP is working ok

      • Thank you, your script is simpler and better than mine, I modified the guide :)

  • stebrick

    Hello,

    I am sorry but I cant get it to work. I strictly followed this tutorial but it cannot load the page 192.168.1.1:82
    When i enter "/opt/etc/init.d/S80php5-cgi restart", it simply shows:-

    /opt/etc/init.d/S80php5-cgi: line 8: /opt/etc/init.d/rc.func: Permission denied
    php-cgi: /opt/etc/init.d/S80php5-cgi started

    My router is AC86u, just in case it matters.

    Thank you. Any help would be greatly appreciated!!!

    • chmod +x /opt/etc/init.d/S80php5-cgi

    • Furqan Razzaq

      Hi, I am having the exact same problem. Getting the same Permission denied error. I followed the tutorial line by line. I also ran chmod +x /opt/etc/init.d/S80php5-cgi command. I am still not able to access 192.168.1.1:82. My router is AC88u.

      Have you found any solution?

  • Zdeněk Mašek

    anybody has tried to install nginx on entware-ng with owncloud? seems to me it should not be that big deal if nginx works on original entware and owncloud works on entware-ng with lighttpd server. so far owncloud works for me but problem is with some tuning (cache, local files,..) where I have found documentation for apache/nginx but not for lighttpd.
    thx

    • Zdeněk Mašek

      So I have tried to install it together with lighttpd server. Till the moment I want to start the server everything is fine. Nginx server started but issue with php (and ftp as well)

      admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# /opt/etc/init.d/S80lighttpd stop
      Checking lighttpd... alive.
      Shutting down lighttpd... failed.
      admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# /opt/etc/init.d/S80lighttpd stop
      Checking lighttpd... dead.
      admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# /opt/etc/init.d/S80nginx start
      Starting nginx... done.
      admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# /opt/etc/init.d/S80php5-cgi start
      /opt/etc/init.d/S80php5-cgi: line 8: /opt/etc/init.d/rc.func: Permission denied
      php-cgi: /opt/etc/init.d/S80php5-cgi started
      admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# PHP Warning: Module 'ftp' already loaded in Unknown on line 0

      I`m using ftp module for owncloud but wondering if you would recommend to dig into that more or you think lighttpd and nginx servers should not be installed in same time

      • It's ok if lighttpd and nginx are installed in the same time, they only need a different listening port.

        • Zdeněk Mašek

          thanks. I`m using 81 for lighttpd and 82 for nginx. I have resolved rc.func: Permission denied issue but unfortunately all the time there are some other issues :

          admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# /opt/etc/init.d/S80nginx start
          Starting nginx... done.
          admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# /opt/etc/init.d/S80php5-cgi start
          php-cgi: /opt/etc/init.d/S80php5-cgi started
          admin@RT-AC56U:/tmp/mnt/sda1/entware-ng.arm/etc/init.d# PHP Warning: Module 'ftp' already loaded in Unknown on line 0
          Cannot bind/listen socket - [98] Address already in use.
          Couldn't create FastCGI listen socket on port 127.0.0.1:9100

  • Guest

    /opt/etc/init.d/S80php5-cgi: line 8: syntax error: bad function name

    After starting php5-cgi script, terminal showed this error.
    I think below line is the problem.
    /opt/etc/init.d/rc.funcstart()

    I found rc.func file in init.d folder. But this function can't be excutable.
    How can I fix this error?

    • Modify /opt/etc/init.d/S80php5-cgi like this:
      ...
      /opt/etc/init.d/rc.func
      start() {

      ...

      • Guest

        Thank u for your fast reply.

        Your solution is working properly but other error is shown.

        /opt/etc/init.d/S80nginx: line 41: echo: not found

        echo is in /bin directory and work properly.

        What's the problem?

        • Last time when I updated the web server, some scripts was screwed up, this is an example, now I found the culprit, change from
          PATH=/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.
          to
          PATH=/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:

          It's just the dot from the end :)

          • Guest

            Ah. U right.
            Thank u.

            Nginx is working properly.

SUBSCRIBE

To be notified by email when a new tutorial is posted :)

Thanks for Subscribing! I'll keeping you up-to-date with latest tutorials!

Something went wrong, try again...