What is Nginx ?
Nginx is (pronounced “engine X”) is a lightweight web server/reverse proxy and e-mail (IMAP/POP3) proxy server, licensed under a BSD-like license. Nginx is written by Igor Sysoev. Nginx has been running for more than three years on many heavily loaded Russian sites including Rambler (RamblerMedia.com). In March 2007 about 20% of all Russian virtual hosts were served or proxied by nginx. According to Google Online Security Blog year ago nginx served or proxied about 4% of all Internet virtual hosts, although Netcraft showed much less percent. According to Netcraft in March 2008 nginx served or proxied 1 million virtual hosts. 2 of Alexa US Top100 sites use nginx.
Firstly, please get freebsd server up and running.
We will using port to install all packages needed it this tutorial.
Installing MySQL5 :
# cd/usr/ports/databases/mysql51-server
# make install clean
# cp /usr/local/share/mysql/my-small.cnf /etc/my.cnf
# rehash
# /usr/local/bin/mysql_install_db –user=mysql
Installing PHP5 :
# cd /usr/ports/lang/php5
# make config
[X] CLI Build CLI version
[X] CGI Build CGI version
[X] APACHE Build Apache module
[ ] DEBUG Enable debug
[X]] SUHOSIN Enable Suhosin protection system
[X] MULTIBYTE Enable zend multibyte support
[ ] IPV6 Enable ipv6 support
[ ] REDIRECT Enable force-cgi-redirect support (CGI only)
[ ] DISCARD Enable discard-path support (CGI only)
[X] FASTCGI Enable fastcgi support (CGI only)
[X] PATHINFO Enable path-info-check support (CGI only)
# make install clean
# cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini
Installing PHP5 Extension :
# cd /usr/ports/lang/php5-extensions/
# make config
Options for php5-extensions 1.0
————————————————-
[X] FTP FTP support
[X] GD
[X] GETTEXT
[X] MBSTRING
[X] MYSQL
[ ] POSIX //去掉.
[ ] SQLITE //去掉.
[X] ZLIB
# make install clean
Installing Zend Optimizer :
# cd /usr/ports/devel/ZendOptimizer/
# make install clean
You will need to make sure your php.ini will have zend module like below :
[Zend]
zend_optimizer.optimization_level=15
zend_extension_manager.optimizer=”/usr/local/lib/php/20060613/Optimizer”
zend_extension_manager.optimizer_ts=”/usr/local/lib/php/20060613/Optimizer_TS”
zend_extension=”/usr/local/lib/php/20060613/ZendExtensionManager.so”
zend_extension_ts=”/usr/local/lib/php/20060613/ZendExtensionManager_TS.so”
Installing Nginx :
# cd /usr/ports/www/nginx/
# make install clean
Installing Lighttpd (this is needed for “spawn-fcgi” binary package) :
lighttpd
# cd /usr/ports/www/lighttpd/
# make install clean
Added Nginx and Mysql to auto start in /etc/rc.conf :
# pico /etc/rc.conf
mysql_enable=”YES”
nginx_enable=”YES”
Edit Nginx.conf :
# pico /usr/local/etc/nginx/nginx.conf
## Change user nobody to www
user www;
…
…
…
location / {
root /usr/local/www/nginx;
index index.php index.html index.htm;
}
….
….
location ~ /.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/nginx$fastcgi_script_name;
include fastcgi_params;
}
### save this file
Added spawn-fcgi and nginx to auto start when the server is rebooted :
# pico /etc/rc.local
### Added these line below
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -C 4 -f /usr/local/bin/php-cgi
/usr/local/sbin/nginx -c /usr/local/etc/nginx/nginx.conf
#save this file
For more option about spawn-fcgi please see this result below :
# /usr/local/bin/spawn-fcgi
Usage: spawn-fcgi [options] — <fcgiapp> [fcgi app arguments]
spawn-fcgi v1.4.19 - spawns fastcgi processes
Options:
-f <fcgiapp> filename of the fcgi-application
-a <addr> bind to ip address
-p <port> bind to tcp-port
-s <path> bind to unix-domain socket
-C <childs> (PHP only) numbers of childs to spawn (default 5)
-P <path> name of PID-file for spawed process
-n no fork (for daemontools)
-v show version
-h show this help
(root only)
-c <dir> chroot to directory
-u <user> change to user-id
-g <group> change to group-id
Running Spawn-fcgi and Nginx Webserver :
# /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -C 4 -f /usr/local/bin/php-cgi
# /usr/local/sbin/nginx -c /usr/local/etc/nginx/nginx.conf
We also can see the Process with this command :
# ps -ax |grep nginx
1969 ?? Is 0:00.00 nginx: master process /usr/local/sbin/nginx -c /usr/local/etc/nginx/nginx.conf (nginx)
1970 ?? S 0:30.79 nginx: worker process (nginx)
1971 ?? S 0:28.95 nginx: worker process (nginx)
1972 ?? S 0:27.54 nginx: worker process (nginx)
1973 ?? R 0:26.73 nginx: worker process (nginx)
2211 p1 S+ 0:00.00 grep nginx
You can trying to access your webserver from Firefox or any other browser,
example http://localhost you will see a page display “welcome to nginx”.