I usually use Apache and it’s PHP5 module for my day to day php development. However, here came a time, when I needed to work on a project that involved using nginx. Installing nginx was easy with homebrew:
However, the recommended way to use php with nginx is using php-fpm. Since I use the apache module most of the time and building php with fpm builds it “–without-apache”, I didn’t want to go for anything complex for just one project at hand. I knew I could use the native “php-cgi” module, so I just googled, found few tips and set it up.
This is my nginx configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 8080; server_name localhost; location / { root /Users/masnun/Sites; index index.html index.htm index.php; } # pass the PHP scripts to FastCGI server listening on unix socket location ~ \.php$ { root /Users/masnun/Sites; fastcgi_pass unix:/tmp/php.socket; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } |
Basically, all I did was change the root element to point to my current “~/Sites” directory which I use as the htdocs for apache. And I’m using unix sockets for better performance. Please read the nginx docs if you don’t understand any of the directives but I’m pretty confident that all of them are more or less self explanatory.
Now, we bind php-cgi to the socket mentioned in our nginx conf file. That’s quite straightforward:
|
php-cgi -b /tmp/php.socket |
Once we have updated the nginx configuration and started the php-cgi process, we should restart nginx:
This should restart nginx and all files with the “.php” extension will be parsed by php.
If you get “no input file specified”, check your nginx configuration and make sure that the paths are all correct. Specially this one:
|
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
Also check that if the root element is defined in appropriate scope for the block to have access to. If you still run along some issues, do a quick google or ask in stackoverflow. And feel free to experiment with different nginx features, specially url rewrites 🙂