Thursday, December 10, 2009

How to install Nginx+PHP on FreeBSD 7.x

First, install Nginx:


cd /usr/ports/www/nginx
make install clean
choose the modules that you wish, and the OK


My configuration for Nginx 0.7.64 is:


DEBUG=off "Enable nginx debugging"
IPV6=off "Enable IPv6"
GOOGLE_PERFTOOLS=off "Enable google perftools module"
HTTP_MODULE=on "Enable HTTP module"
HTTP_ADDITION_MODULE=on "Enable http_addition module"
HTTP_CACHE_MODULE=on (default) "Enable http_cache module"
HTTP_DAV_MODULE=on "Enable http_webdav module"
HTTP_FLV_MODULE=on "Enable http_flv module"
HTTP_GZIP_STATIC_MODULE=on "Enable http_gzip_static module"
HTTP_IMAGE_FILTER_MODULE=off (default) "Enable http_image_filter module"
HTTP_PERL_MODULE=on "Enable http_perl module"
HTTP_RANDOM_INDEX_MODULE=on "Enable http_random_index module"
HTTP_REALIP_MODULE=on "Enable http_realip module"
HTTP_REWRITE_MODULE=on "Enable http_rewrite module"
HTTP_SECURE_LINK_MODULE=on "Enable http_secure_link module"
HTTP_SSL_MODULE=on "Enable http_ssl module"
HTTP_STATUS_MODULE=on "Enable http_stub_status module"
HTTP_SUB_MODULE=on "Enable http_sub module"
HTTP_XSLT_MODULE=on "Enable http_xslt module"
MAIL_MODULE=off "Enable IMAP4/POP3/SMTP proxy module"
MAIL_IMAP_MODULE=off "Enable IMAP4 proxy module"
MAIL_POP3_MODULE=off "Enable POP3 proxy module"
MAIL_SMTP_MODULE=off "Enable SMTP proxy module"
MAIL_SSL_MODULE=off "Enable mail_ssl module"
WWW=on "Enable html sample files"
HTTP_ACCESSKEY_MODULE=on "3rd party http_accesskey module"
HTTP_EVAL_MODULE=off (default) "3rd party eval module"
HTTP_FANCYINDEX_MODULE=on "3rd party http_fancyindex module"
HTTP_MOGILEFS_MODULE=on "3rd party mogilefs module"
HTTP_MP4_H264_MODULE=off (default) "3rd party mp4/h264 module"
HTTP_NOTICE_MODULE=off (default) "3rd party notice module"
HTTP_REDIS_MODULE=off (default) "3rd party http_redis module"
HTTP_RESPONSE_MODULE=on "3rd party http_response module"
HTTP_UPLOAD_MODULE=on "3rd party upload module"
HTTP_UPLOAD_PROGRESS=on "3rd party upload_progress module"
HTTP_UPSTREAM_FAIR=on "3rd party upstream fair module"
HTTP_UPSTREAM_KEEPALIVE=on "3rd party upstream keepalive module"
HTTP_ZIP_MODULE=on "3rd party http_zip module"
PASSENGER_MODULE=off "3rd party passenger module"


after installing is completed, add the following code to rc.conf:


echo “nginx_enable=YES” >> /etc/rc.conf
Then edit /usr/local/etc/nginx/nginx.conf
My nginx.conf looks like:


user www www;
worker_processes 4;

error_log /var/log/nginx-error.log ;
pid /var/run/nginx.pid;

events {
worker_connections 4096;
}

http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size 128;
client_max_body_size 256m;
proxy_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 4 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 120;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
server_tokens off;
server {
listen 80 default;
server_name _;
access_log /var/log/nginx-access.log main;
server_name_in_redirect off;
location / {
index index.html;
root /usr/local/www/nginx;
}


location ~ \.php$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/nginx$fastcgi_script_name;
include fastcgi_params;
}
location /server-status {
stub_status on;
access_log off;
allow all;
}
}
# virtual hosting
include /usr/local/etc/nginx/vhosts/*.conf;
}


Make sure you have the directory /usr/local/etc/nginx/vhosts created, it will contain all your virtual hosts configuration files later.


A template to a virtual host will look like:


server {
listen 80;
server_name ;
access_log /path/to/log/file;
root /path/to/webroot;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}


make sure your virtualhosts config files end with the .conf part and placed in the /usr/local/etc/nginx/vhosts directory.


Before we start the server, make sure you have the log files in place and have the proper permissions:


touch /var/log/nginx-error.log
touch /var/log/nginx-access.log
chown www:www /var/log/nginx*


Then we start the serve:
/usr/local/etc/rc.d/nginx start


Now the server is running, we move to the the PHP:


cd /usr/ports/lang/php5
make install clean


My configuratio for PHP 5.2.11:
CLI=on "Build CLI version"
CGI=on "Build CGI version"
APACHE=on "Build Apache module"
DEBUG=off "Enable debug"
SUHOSIN=on "Enable Suhosin protection system (not for jails)"
MULTIBYTE=on "Enable zend multibyte support"
IPV6=off "Enable ipv6 support"
MAILHEAD=off "Enable mail header patch"
REDIRECT=off "Enable force-cgi-redirect support (CGI only)"
DISCARD=off "Enable discard-path support (CGI only)"
FASTCGI=on "Enable fastcgi support (CGI only)"
PATHINFO=on "Enable path-info-check support (CGI only)"


After the installation, you may need to install some extra php-extensions, use the ports:
cd /usr/ports/lang/php5-extensions
make install clean


Choose what ever extensions you may need, and continue the installation of the meta port.


Now we have a running web server, and installed PHP binaries, all we need to do is to connect the web server to the PHP via a Fast CGI connection. To start a PHP CGI as daemon, you may need to use the spawn-fcgi application by the folks of lighttpd.


cd /usr/ports/www/spawn-fcgi
make install clean


Add the following to rc.conf:


fcgiphp_enable="YES" # enable FastCGI+PHP
fcgiphp_bin_path="/usr/local/bin/php-cgi" # absolute path to the PHP binary
fcgiphp_user="www" # switch to user
fcgiphp_group="www" # switch to group
fcgiphp_children="5" # number of PHP childs to spawn
fcgiphp_port="9000" # bind to tcp-port on localhost
fcgiphp_env="SHELL PATH USER" # allowed environment variables
fcgiphp_max_requests="500" # number of request served by single proc.
fcgiphp_addr="localhost" # adresses to php-cgi server


The port number and bind address are exactly the ones in the nginx configs.


After that we start the php-cgi daemon:


/usr/local/etc/rc.d/fastcgi-php.sh start
Now, if all went well you should have a running Nginx+PHP environment on a FreeBSD.