Most PHP is deployed onto an Apache webserver, but Nginx is an attractive alternative. The advantages of Nginx are well explained by Martin Fjordvald, so here we'll concern ourselves with actually building the server on an Ubuntu platform (tested in 11.4, "Natty"). Under Nginx, PHP is served via FPM (Fast Process Manager): the Nginx server simply proxies the request to the FPM which maintains a pool of processes to handle the PHP execution.
sudo apt-get -y install nginx
Nginx by default will serve a welcome page for any domain that points at it, if you want to deny arbitrary domains by default update /etc/nginx/sites-available/default to serve a HTTP 444 error.
server {
listen 80 default;
server_name _;
return 444;
}
Next, we need the PHP FPM (for web requests), and PHP CLI (for any command-line scripts).
sudo apt-get -y install php5-fpm php5-cli
Almost everyone uses some PHP extensions, this is my usual list, yours will depend on your deployment requirements. Since I've enabled PEAR, I'll specify where it should configure itself.
sudo apt-get -y install php5-suhosin php5-mcrypt php-soap php5-gd \ php5-mysql php5-memcache php5-sqlite php5-curl php5-pgsql \ php-apc php-pear sudo touch /etc/php5/php5/conf.d/pear.ini sudo pear config-set php_ini /etc/php5/conf.d/pear.ini
PHP needs to be configured in a production-ready way, I add an extra .ini file to /etc/php5/conf.d/default.ini that contains:
short_open_tag = On allow_call_time_pass_reference = Off max_execution_time = 20 max_input_time = 120 max_input_nesting_level = 64 memory_limit = 32M display_errors = Off display_startup_errors = Off log_errors = On ignore_repeated_errors = Off ignore_repeated_source = Off html_errors = Off error_log = syslog register_globals = Off post_max_size = 8M magic_quotes_gpc = Off magic_quotes_runtime = Off default_mimetype = "text/html" default_charset = "utf-8" file_uploads = On upload_max_filesize = 5M allow_url_fopen = On allow_url_include = Off default_socket_timeout = 10 auto_detect_line_endings = On define_syslog_variables = Off [Session] session.use_cookies = 1 session.use_only_cookies = 1 session.name = sid session.cookie_httponly = 1 [mbstring] mbstring.language = Neutral mbstring.internal_encoding = UTF-8 mbstring.http_input = pass mbstring.http_output = pass mbstring.encoding_translation = Off mbstring.detect_order = auto mbstring.substitute_character = none; mbstring.func_overload = 0
And we're done. To create a new site that will be served off Nginx PHP5-FPM:
sudo mkdir /var/www/helloworld
echo 'echo "<?php echo "hello world";' | sudo tee /var/www/helloworld/index.php
echo 'server {
listen 80;
server_name helloworld.com;
index index.php;
root /var/www/helloworld;
location ~* \.php$ {
if (!-f $request_filename) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $host;
}
}' | sudo tee /etc/nginx/sites-available/helloworld
ln -s /etc/nginx/sites-available/helloworld /etc/nginx/sites-enabled/helloworld
sudo service nginx restart