Previous post: Nginx – Installation on Ubuntu Precise
Most of the time we would like to run PHP on our web server. After you have installed Nginx following the post above. We can now enable PHP.
1. Install the PHP packages.
apt-get install php5-fpm apt-get install php5-cli apt-get install php5-gd apt-get install php-pear
2. Edit /etc/php5/fpm/php.ini and set cgi.fix_pathinfo = 0.
cgi.fix_pathinfo=0
3. Edit /etc/php5/fpm/pool.d/www.conf such that it listens to socket, which is faster, instead of port.
;listen = 127.0.0.1:9000 listen = /var/run/php5-fpm.sock
4. Here is an example of /etc/php5/fpm/pool.d/www.conf.
[www] user = www-data group = www-data ;listen = 127.0.0.1:9000 ;listen.allowed_clients = 127.0.0.1 listen = /var/run/php5-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0666 pm = dynamic pm.max_children = 5 pm.start_servers = 1 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.max_requests = 500 pm.process_idle_timeout = 10s; chdir = /
5. Edit the /etc/nginx/sites-available/default or other VirtualHost which you want to enable PHP.
server { listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 #root /usr/share/nginx/www; root /srv/www; index index.php index.html index.htm; # Make site accessible from http://localhost/ #server_name localhost; server_name <your domain name>; location / { # First attempt to serve request as file, then # as directory, then fall back to index.html 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; deny all; } # Only for nginx-naxsi : process denied requests #location /RequestDenied { # For example, return an error code #return 418; #} #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; #} }
5. Restart the PHP-FPM and Nginx.
/etc/init.d/php5-fpm restart /etc/init.d/nginx restart
6. Put the phpinfo.php in the webroot folder (/srv/www in this example).
<?php phpinfo(); ?>
Done =)
Next post: Nginx – Enable Microcaching
Reference: Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support On Ubuntu 11.10
3 thoughts on “Nginx + PHP-FPM on Ubuntu Precise”