2

UPDATE: Ok, I can now happily say that what you see below is the recipe for a happily working automated PHP and Nginx dev setup (with automatic domains) and all problems I had are now solved.

I installed what follows, sudo apt-get nginx php5 php5-fpm dnsmasq on Debian 8, Jessie. I installed dnsmasq so that I can have domains named after my dev sites. I made a .conffile in /etc/dnsmasq.d containing

address=/dev/127.0.0.1

and that part of the setup is working nicely, any .dev domains resolve to localhost, which is being served by Nginx.

Now onto the server part of the dev environment and what I have tried so far (the server blocks are below the checklist). Update: all is good! All is working now!

Things-I-have-done-so-far checklist:

  • I now have working set up (this is my latest edit to this post). I found some useful tips in a Sitepoint (website) article. So, I gave some permissions to the parent of my dev site folders, that being chgrp -R www-data /var/www and I did chmod -R g + rw /var/www and also chmod g + s /var/www This last chmod is the best bit! Adding the SGID (g + s) means that any new dev directories added into /var/www will automatically become owned by the www-data putting more automation in this dev setup
  • I reloaded both php5-fpm and Nginx each time I have made changes in the setup, and both are running fine.
  • Update. Now that the new setup is running fast cgi, I have now also done, sudo apt-get php5-cgi spawn-fcgi
  • I added users josh (my main user), root and user www-data (Nginx) to the group named www-data
  • For php5-fpm I checked in /etc/php5/fpm/pool.d/www.conf and saw the default install had given me these user attributes, user = www-data group = www-data, and these listen attributes listen.owner = www-data listen.group = www-data
  • I made the following symlink: ln -s /etc/nginx/sites-available/local_dev.conf /etc/nginx/sites-enabled/

I followed a Nginx dev setup tutorial by goodrobot.com, who was not using PHP; so, I combined the main parts of that tutorial together with part of a PHP non-dev environment tutorial by Christophe at scaleyourcode.com

This is what I came up with for the server blocks in the conf file in /etc/nginx/sites-available (from combining ideas from the two tutorials). Again, the problem is that my index.php page is not being served. (If I add an index.html file to the index below in the server block and then serve an index.html file it will work just fine).Update. I have scrapped that Frankenconfig file I made and have a new one and details just below. All is now good!

UPDATE, WORKING! I have posted a new config file a slightly modified version of one that user denji (on Github) put on Github titled 'dnsmasq'. That version also has blocks for PHPmyadmin, which I don't need. I also added fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; to the location ~ .php block because before that it would not work on my setup. I also removed the dollar sign that was infront of location ~ .php(dollar sign was here, not good) so that when I use any of the below on real live networked server the $ sign will not become a free pass for hackers.

  server {
  index index.php;
  set $basepath "/var/www";

  set $domain $host;

  # check one name domain for simple application
  if ($domain ~ "^(.[^.]*)\.dev$") {
    set $domain $1;
    set $rootpath "${domain}";
    set $servername "${domain}.dev";
  }

  # check multi name domain to multi application
  if ($domain ~ "^(.*)\.(.[^.]*)\.dev$") {
    set $subdomain $1;
    set $domain $2;
    set $rootpath "${domain}/${subdomain}/www/";
    set $servername "${subdomain}.${domain}.dev";
  }

  server_name $servername;

  access_log "/var/log/nginx/server.${servername}.access.log";
  error_log "/var/log/nginx/server.dev.error.log";

  root $basepath/$rootpath;

  # check file exist and send request sting to index.php 
  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  # allow php only in root index.php
  #location ~ "^/index\.php" {
  # allow execute all php files
  location ~ \.php {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;   
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
  }

    location ~ /\.ht {
    deny all;
  }

  # disallow access to git configs path
    location ~ /\.git {
    deny all;
  }

  }
  • Have you checked file permissions of php5-fpm.sock? nginx defaults to run as nginx and won't have write permissions. – Jonathon Oct 2 '15 at 10:33
  • Var /var/run/php5-fpm.sock had permission on the socket of srw-rw---- I tried adding world reading and writable permissions: srw-rw-rw- 1 www-data www-data 0 Oct 2 20:07 php5-fpm.sock Unfortunately it is still not working. The user in /etc/nginx/nginx.conf automatically added on install is www-data – Usefulness Oct 2 '15 at 14:53
  • What do your Nginx and PHP-FPM error logs say? Also, what does Nginx return? – Christophe Oct 3 '15 at 13:34
  • Hi Christophe, the error logs were all clean. I have now ended up with a nicely working setup with a different config file and and installation of fastcgi. And the standard install permissions I put on php5-fpm.sock of srw-rw---- were also fine I didn't need to add world writable to it, for anyone doing a similar set up. I will post my new config file. and new changes – Usefulness Oct 4 '15 at 13:47
  • This is my first post on Stackexchange and I don't know if I have permissions (or where to) mark this as fixed. But it is. Anyone wanting to have an Automated PHP and Nginx dev setup for unlimited development sites with custom domains. This works sweet! Now I just need to make copies of the config file with a new location block for other languages. Say one for Python and one for Ruby and then make new .py and .rub domain names for said languages with dnsmasq. That is my next step. – Usefulness Oct 4 '15 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.