PHP working with nginx using FastCGI on Ubuntu
How to get PHP working with nginx using FastCGI.
Tested on Ubuntu 10.04 server.
Nginx
- Install nginx, from repo or source, both should be OK.
PHP
Install PHP from the repo's:
# sudo apt-get install php5-cli php5-cgi
Configuring
Create an init script for PHP FastCGI:
Download the fastcgi-php init script
# wget -O /etc/init.d/fastcgi-php \ http://ftp.quzart.com/notes/php-nginx-fcgi/fastcgi-php
As always, check the contents of the script. Does it do what you want and nothing more? Else, make changes!
Make the script executable:
# chmod 0755 /etc/init.d/fastcgi-php
Install init script:
# update-rc.d fastcgi-php defaults
Edit nginx configuration. Example configuration:
server { listen 80; server_name www.quzart.com; access_log /home/quzart/domains/www.quzart.com/logs/access.log; error_log /home/quzart/domains/www.quzart.com/logs/error.log; root /home/quzart/domains/www.quzart.com/public_html; index index.txt index.html index.xhtml index.php; location / { autoindex off; } # This is the important part for PHP: location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/fastcgi-php/fastcgi-php.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/quzart/domains/www.quzart.com/public_html$fastcgi_script_name; } }
Running
Now it's time to test our configuration.
Start fastcgi-php:
# invoke-rc.d fastcgi-php start
Reload nginx's configuration files:
# invoke-rc.d nginx reload
Check whether your PHP scripts run.