Selasa, 18 Januari 2011

Membuat Server dengan PHP dan Nginx


Apa itu Nginx versi Wikipedia :
nginx (pronounced “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 UnixLinuxBSD variantsMac OS XSolaris, andMicrosoft Windows.
Nginx quickly delivers static content with efficient use of system resources. It can deploy dynamic HTTP content on a network using FastCGI handlers forscripts, and can serve as a very capable software load balancer.[2]
Nginx uses an asynchronous event-driven approach to handling requests which provides more predictable performance under load, in contrast to theApache HTTP server model that uses a threaded or process-oriented approach to handling requests.[3]
Step by step :
1. Tambah repo :
$ add-apt-repository ppa:nginx/$nginx
2. Trz update :
$ sudo apt-get update
3. Trz install Nginx :
$ sudo aptitude install nginx
4. Trz install paket php yg d’perlukan (seperti php5-cgi) :
$ sudo aptitude install php5-cgi
Bsa jg menambahkan yg lain seperti
$ apt-get php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
5. Create PHP 5 FastCGI start-up script :
$ sudo nano /etc/init.d/php-fastcgi
Dan masukkan script ini :
#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL
6. Ubah permission-nya :
$ sudo chmod +x /etc/init.d/php-fastcgi
7. Launch php :
$ sudo /etc/init.d/php-fastcgi start
8. Launch pada StartUp
$ sudo update-rc.d php-fastcgi defaults
9. Pada config :
$ sudo gedit /etc/nginx/sites-available/default
Tambahkan line d’bawah ini :
location ~ \.php$ {
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
    include         fastcgi_params;
}
Sehingga menjadi :
# You may add here your
# server {
#    …
# }
# statements for each of your virtual hosts
server {
listen   8080; ## listen for ipv4
listen   [::]:8080 default ipv6only=on; ## listen for ipv6
server_name  localhost;
access_log  /var/log/nginx/localhost.access.log;
location / {
root   /var/www;
index  index.html index.htm;
}
location /doc {
root   /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root   /usr/share;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass    127.0.0.1:9000;
fastcgi_index   index.php;
fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include         fastcgi_params;
}
#error_page  404  /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page   500 502 503 504  /50x.html;
#location = /50x.html {
#    root   /var/www/nginx-default;
#}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#fastcgi_pass   127.0.0.1:9000;
#fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#includefastcgi_params;
#}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
#deny  all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen   8000;
#listen   somename:8080;
#server_name  somename  alias  another.alias;
#location / {
#root   html;
#index  index.html index.htm;
#}
#}
# HTTPS server
#
#server {
#listen   443;
#server_name  localhost;
#ssl  on;
#ssl_certificate  cert.pem;
#ssl_certificate_key  cert.key;
#ssl_session_timeout  5m;
#ssl_protocols  SSLv3 TLSv1;
#ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#ssl_prefer_server_ciphers   on;
#location / {
#root   html;
#index  index.html index.htm;
#}
#}
10. Restart Nginx :
$ sudo /etc/init.d/nginx restart
*start nginx :
$ sudo /etc/init.d/nginx restart
*stop nginx :
$ sudo /etc/init.d/nginx restart
11. Untuk menjalankan nginx bisa menggunakan :
http://127.0.0.1:8080/ (8080 tergantung port brp yg kita buat pada configurai d’atas)
12. Untuk mencoba apakan ngix kita ah berjalan, buat suatu file *.php pada /var/www/nginx-default/..
*Jika ingin mengubah default upload file /var/www/nginx-default/ menjadi /var/www/ edit pada konfigurasi :
SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
menjadi :
SCRIPT_FILENAME /var/www/php$fastcgi_script_name;
Contoh buat sebuah file *.php bernama test.php dan isi dengan <? phpinfo (); ?>..
Nanti akan muncul :

0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites