GIT –  (pronounced “engine x”), is a high performance web server and a reverse proxy server. This server is well-known for its low memory footprint rather than Apache.  is way too better for Apache.

Popular websites that run on Nginx are SourceForgeWordPress, and Hulu. By making Nginx run in , you can deliver light, efficient, powerful, stable and secure web server in a simple way.

What is ? I think you all already know and no need to explain further. The handler we will use is FastCGI Process Manager (FPM), is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. This setup will surely deliver high-performance web service with low specs hardware.

OS: FreeBSD 8 64bit
Nginx version: 0.8.54
PHP version: 5.3.6
Website: http://mydomain.net/
Website IP: 192.168.60.2
Web directory: /home/mydomain/public_html

1. Lets start by installing Nginx web server:

cd /usr/ports/www/nginx
make  clean

Once installation start, it will prompt nginx module selection page. You can select any Nginx module you want, but for me, I will select following module to be compiled right away:

[X] HTTP_MODULE               Enable HTTP module
[X] HTTP_ADDITION_MODULE      Enable http_addition module
[X] HTTP_CACHE_MODULE         Enable http_cache module
[X] HTTP_DAV_MODULE           Enable http_webdav module
[X] HTTP_FLV_MODULE           Enable http_flv module
[X] HTTP_GEOIP_MODULE         Enable http_geoip module
[X] HTTP_GZIP_STATIC_MODULE   Enable http_gzip_static module
[X] HTTP_IMAGE_FILTER_MODULE  Enable http_image_filter module
[X] HTTP_PERL_MODULE          Enable http_perl module
[X] HTTP_RANDOM_INDEX_MODULE  Enable http_random_index module
[X] HTTP_REALIP_MODULE        Enable http_realip module
[X] HTTP_REWRITE_MODULE       Enable http_rewrite module
[X] HTTP_SECURE_LINK_MODULE   Enable http_secure_link module
[X] HTTP_SSL_MODULE           Enable http_ssl module
[X] HTTP_STATUS_MODULE        Enable http_stub_status module
[X] HTTP_SUB_MODULE           Enable http_sub module
[X] HTTP_XSLT_MODULE          Enable http_xslt module

If you receive any prompt after that, just accept all values if you want to have a complete setup, or else select what you want, if you know what you are doing.

2. Web server installation done. Make sure Nginx is enabled by adding following line to /etc/rc.conf:

nginx_enable="YES"

3. Before we configure and start the web server, we need to install PCRE,  libtoolPHP with FPM and PHP extensions. Follow these steps:

cd /usr/ports/devel/pcre
make install clean
cd /usr/ports/devel/libtool
make install clean
cd /usr/ports/lang/php5
make install clean

During the selection module page, select FPM (FastCgi Process Manager).

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

4. Since PHP-FPM is a service, we need to add this in /etc/rc.conf:

php_fpm_enable="YES"

5. By default, there is no php.ini specified. So we need to copy the php.ini which has been prepared during port installation.

cp /usr/local/etc/php.ini-production /usr/local/etc/php/php.ini

6. Start the PHP-FPM service :

/usr/local/etc/rc.d/php-fpm start

7. Now PHP-FPM already started at localhost port 9000. You can check this using netstat -an command. Lets create the web and logs directory used to host website (ignore this if you already have the directory):

mkdir /home/mydomain/public_html
mkdir /home/mydomain/logs

8. Lets configure Nginx so it know where is the home directory, what is the virtual host name, how to handle PHP and so on. Open the configuration file using text editor located under /usr/local/etc/nginx/nginx.conf and make sure following values are not commented (#) or you can copy this example and change to the value you want:

user  nobody;
worker_processes  1;

error_log /home/mydomain/logs/error.log;
pid       /home/mydomain/logs/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;

    keepalive_timeout  65;

    gzip  on;

    # We define the virtual host here
    server {
        listen       192.168.60.2:80;
        server_name  mydomain.net www.mydomain.net;

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

        location / {
            root   /home/mydomain/public_html;
            index  index.html index.htm index.php;
        }
    # Let nginx know how to handle PHP using fastcgi
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/mydomain/public_html$fastcgi_script_name;
            include        fastcgi_params;
        }

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

9. After that, we good to go. But before start Nginx it is good if we check the configuration first:

/usr/local/sbin/nginx -t
/usr/local/etc/rc.d/nginx start

10. Now, Nginx and PHP-FPM should run correctly. If you do any changes on php.ini files or php extension file, you can restart PHP-FPM and reload Nginx:

/usr/local/etc/rc.d/php-fpm restart
/usr/local/etc/rc.d/nginx reload
Print Friendly

Comments

comments

Bài viết liên quan