How to Install Nginx, PHP-FPM, MySQL and Xcache in FreeBSD 7.2

what is nginx ?

nginx (pronounced as "engine X") is a lightweight, high performance web server/reverse proxy and e-mail (IMAP/POP3) proxy, licensed under a BSD-like license. It runs on UNIX, GNU/Linux, BSD variants, Mac OS X, Solaris, and Microsoft Windows[1].

What is php-fpm ?

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busy sites.

Installing MySQL

#cd /usr/ports/databases/mysql51-server
#make install clean
#/usr/local/bin/mysql_install_db

Installing Dependencies

#cd /usr/ports/devel/libevent
#make install clean

#cd /usr/ports/textproc/libxml2
#make install clean

#cd /usr/ports/graphics/png
#make install clean

#cd /usr/ports/graphics/jpeg
#make install clean

#cd /usr/ports/print/freetype
#make install clean

#cd /usr/ports/devel/libtool22
#make install clean
 
#cd /usr/ports/security/libmcrypt
#make install clean

Installing Nginx Web Server

#cd /var/db/ports/nginx
#make install clean

Download, Patch and Install Php

#cd /root
#wget http://us.php.net/distributions/php-5.2.12.tar.gz
#wget http://php-fpm.org/downloads/php-5.2.12-fpm-0.5.13.diff.gz

Extract php & patch with php-fpm

#tar -zxvf php-5.2.12.tar.gz
#gzip -cd php-5.2.12-fpm-0.5.13.diff.gz |patch -d php-5.2.12 -p1
#cd php-5.2.12

Configure PHP

#./configure --enable-fpm \
--enable-fastcgi \
--with-mcrypt \
--with-zlib \
--enable-mbstring \
--disable-debug \
--enable-inline-optimization \
--enable-sockets \
--enable-zip \
--with-mysql=/usr/local
#make && make install

Modify php-fpm.conf

#pico /usr/local/etc/php-fpm.conf

### edit few lines below, change it if needed ###
...
<value name="emergency_restart_threshold">10</value>
<value name="emergency_restart_interval">1m</value>
<value name="process_control_timeout">5s</value>
<value name="owner">www</value>
<value name="group">www</value>

### remove arrow " --- in <value name="user"> and <value name="group">"

<value name="user">www</value>
<value name="group">www</value>
<value name="max_children">20</value>
<value name="StartServers">20</value>
<value name="MinSpareServers">5</value>
<value name="MaxSpareServers">35</value>
<value name="request_terminate_timeout">5s</value>
<value name="request_slowlog_timeout">5s</value>
<value name="max_requests">500</value>
...
## save this file

Install Xcache PHP modules

#cd /usr/ports/www/xcache
#make install clean

Copy php.ini-dist to /usr/local/lib

#cd /root/php-5.2.12
#cp php.ini-dist /usr/local/lib/php.ini

Add xcache.ini to php.ini

#cat /usr/local/share/examples/xcache/xcache.ini >> /usr/local/lib/php.ini

Installing Suhosin PHP modules

#cd /usr/ports/security/php-suhosin
#make install clean 

Installing PHP IMAP, GD and Curl modules

#cd /usr/ports/lang/php5-extensions
#make config

## just choose CURL,IMAP,GD, and unchecked all php modules there because we just need php-imap to installed on the system. if you want to install more extension you can check other php extension there .
#make install clean.

All php modules will install in /usr/local/lib/php/20060613
Note : 20060613 is PHP extension

Modify PHP extension_dir

#pico /usr/local/lib/php.ini

## find extension_dir = ./ ##
## change it like below ##

extension_dir = "/usr/local/lib/php/20060613"

NOTE : xxx is a random number generate by system when installing php extensions

Modify php.ini and added php modules

## modify xcache ##
[xcache-common]
;; install as zend extension (recommended, but not working yet)
zend_extension = /usr/local/lib/php/20060613/xcache.so
; zend_extension_ts = /usr/local/lib/php/20060613/xcache.so
;; or install as extension
; extension = xcache.so

[xcache]
; Change xcache.size to tune the size of the opcode cache
xcache.size         = 24M
xcache.shm_scheme   = "mmap"
xcache.count        = 2
xcache.slots        = 8K
xcache.ttl          = 0
xcache.gc_interval  = 0

; Change xcache.var_size to adjust the size of variable cache
xcache.var_size     = 8M
xcache.var_count    = 1
xcache.var_slots    = 8K
xcache.var_ttl      = 0
xcache.var_maxttl   = 0
xcache.var_gc_interval =     300
xcache.test         = Off
xcache.readonly_protection = On
xcache.mmap_path    = "/tmp/xcache"
xcache.coredump_directory =   ""
xcache.cacher       = On
xcache.stat         = On
xcache.optimizer    = Off

[xcache.coverager]
xcache.coverager    = On
xcache.coveragedump_directory = ""

# add suhosin extension
extension=suhosin.so
extension=imap.so
extension=gd.so

## save this file

Add nginx, mysql and php-fpm in rc.local

#pico /etc/rc.local

## add lines below into rc.local
/usr/local/bin/mysqld_safe --user=mysql &
/usr/local/bin/php-cgi --fpm --fpm-config /usr/local/etc/php-fpm.conf
/usr/local/sbin/nginx
## save rc.local

Modify nginx.conf and nginx vhost

#cd /usr/local/etc/nginx
#pico nginx.conf

## modify user [nginx user in freebsd is www]
user  www www;
worker_processes  4;

# main server error log
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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;
    tcp_nopush     on;
    tcp_nodelay         on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    keepalive_timeout  30;
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

# virtual hosting
    include /usr/local/etc/nginx/vhosts/*;
}

## save nginx.conf

Create nginx vhosts

#cd /usr/local/etc/nginx
#mkdir vhosts
#pico wowtutorial.org.conf
server {
        listen  80;
        server_name  wowtutorial.org www.wowtutorial.org;

        access_log  /home/wowtutorial/logs/access.log  main;

        location / {
            root   /home/wowtutorial/public_html;
            index  index.php index.html index.htm;
            error_page 404 = @drupal;
        }

        location @drupal {
                rewrite ^(.*)$ /index.php?q=$1 last;
        }

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  /home/wowtutorial/public_html$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include        /usr/local/etc/nginx/fastcgi_params;
        }


        location ~ /\.ht {
            deny  all;
        }
}

## save this vhost file ##

Note :
If you have another domain. example : domain1.com
you need to created a conf file like domain1.com.conf

#cp wowtutorial.org.conf domain1.com.conf
#pico domain1.com.conf

server {
        listen  80;
        server_name  domain1.com www.domain1.com;

        access_log  /home/domain1/logs/access.log  main;

        location / {
            root   /home/domain1/public_html;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  /home/domain1/public_html$fastcgi_script_name;
                include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
}
## save this file

Create nginx Log directory

#mkdir -p /var/log/nginx
#mkdir -p /home/wowtutorial/logs
#mkdir -p /home/domain1/logs

Starting up Nginx and PHP-FPM

#/usr/local/bin/php-cgi --fpm --fpm-config /usr/local/etc/php-fpm.conf
#/usr/local/sbin/nginx
#/usr/local/bin/mysqld_safe --user=mysql &


Facebook Fans