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.

Thursday, October 22, 2009

FreeBSD or Linux? II

In the last post, I had a quick glance on the difference between the two operating systems. Actually, I didn't do as much of writing and work as I should, but I wanted to start my blog as soon as I can, and I wanted to start it particularly on that day, as it is my birthday :).


If you are looking for a day-day computing work, like office applications, email, web browsing, finance applications, multimedia and gaming, I suggest you use Linux. You can use one of the major distributions like openSuSE, Slackware, Redhat or Debian. Or, you can use a derivative distro like Ubuntu, Fedora or Centos.


Of course you can install FreeBSD as a personal OS, but you'll have to be sure to have all your hardware supported by FreeBSD. You can bump into the wall of “your wifi adapter isn't supported, you will have to stick with the cable for the rest of your computer's life”, and believe me, this happens a lot, it happened to me. You may have a fancy ATI graphics adapter, and you won't be able to get one tenth of it's features, because the open source driver doesn't know how to make it work.


The situation is different with Linux, as it is getting more and more hardware support by the day. This doesn't mean that FBSD has no support for hardware, but, it is not the kind of support you will find with Linux.


That's being said, I should say that I haven't gotten an issue with hardware support on server machines running FreeBSD. What I mean is that Linux is a more suitable to work on a desktop/laptop machine than FreeBSD is, and the opposite is completely true.


After all, choosing FreeBSD for your servers is the right thing to do, you will be having the best experience ever when it comes to configuration and maintenance. 

Wednesday, September 23, 2009

FreeBSD Or Linux?

So, which one is better?

This question has no definitive answer. It is one of those questions that have open answers and no one can put an end to the debate that will be caused by such questions.

In my openion, I think no one is better than the other, generally speaking of course.

Each has it is own strength and weakness points. I will not go into details like licensing, commercial support or even community contributions. I will shade lights on the usability and functionality of a desktop operating system and a roboust web hosting server.

For the desktop market, Linux wins the competetion, this is due to the numerous hardware support that it makes it able to run almost every hardware, from VGA controllers to wireless network cards. It is really easy to configure a Linux machine to do all the daily computing needs including gaming if you want to.

As for the server environment, FreeBSD wins, hands down. It has decent level of security, very well integrated packaging system.  Managing a FreeBSD server is much more fun and easier than managing Linux server, despite what Linux distribution you are talking about.

More on this topic in upcoming posts, sooner.