Nginx
Defcon1-Header
Tool-BarfreeBSD ArticlesSearch Our SiteHOMEfreeBSD LinksContribute to FreeBSD HelpFreeBSD FilesFreeBSD Script Corner

Installing nginx with PHP FPM on FreeBSD 8.x-9.0

Create a new user for the website
 adduser

in the home directory for the user create a public_html directory. This is where your website files will live.
 cd ~websiteusername
 mkdir public_html
Change ownership of the directory to the user
 chown websiteusername public_html

Install PCRE from ports
 cd /usr/ports/devel/pcre
 make install clean

Install PHP from ports
 cd /usr/ports/lang/php5
 make install clean
 Select FPM, CLI, CGI and SUHOSIN on the config screen.
 cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini for a good place to start

Edit /etc/rc.conf to add php_fpm_enable="YES"

Install MySQL server from ports
 cd /usr/ports/databases/mysql55-server
 make install clean
 normal options should be fine.

Edit /etc/rc.conf to add mysql_enable="YES"

Install nginx from ports
 cd /usr/ports/www/nginx
 make install clean
 select HTTP options you think you might use, including gzip and HTTP_REWRITE
 no need for email functions or proxy functions.
 (I select all of the HTTP prefixed modules myself)
 check out http://nginx.org/en/docs/ for more info on the modules.

Edit /etc/rc.conf to add nginx_enable="YES"

At this point you can fire up mysql and php-fpm to make sure both are working before we go farther into things.

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

nginx needs to be configured before we start it.
edit /usr/local/etc/nginx/nginx.conf


Here is my config file for example

user nobody;
worker_processes 1;

error_log  /var/log/nginx_error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

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"';

  #access_log logs/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  gzip on;

  server {
    listen    80;
    server_name localhost;

    #charset koi8-r;

    access_log /var/log/nginx_access.log main;

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

    #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  /usr/local/www/nginx-dist;
    }

    # 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$ {
      root      html;
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /home/websiteusername/public_html$fastcgi_script_name;
      include    fastcgi_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 SSLv2 SSLv3 TLSv1;
  #  ssl_ciphers HIGH:!aNULL:!MD5;
  #  ssl_prefer_server_ciphers  on;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}

}


Once you are done editing you can test your config
 nginx -t

As long as the above test was successful, fire up nginx.
 /usr/local/etc/rc.d/nginx start

Create a basic /home/websiteusername/public_html/index.html file to test with.

Open up a browser and go to http://servername.domain.tld/ to see if you get your index.html file. If not, the log files that are relevant can be found in /var/log/nginx_access.log and /var/log/nginx_error.log.


I would like to thank SecaGuy at SecaServer for his article on nginx at http://blog.secaserver.com/2011/07/freebsd-nginx-php-fastcgi-installation/ which helped me particularly with the configuration file.



Article By: Technoid

© 1997 - 20013 Defcon1, www.defcon1.org , Copyrights for all materials on this web site are held by the individual authors, artists, photographers or creators. Materials may not be reproduced or otherwise distributed without permission of www.defcon1.org and the content's original author.

Defcon1-Header2
Tool-Bar-2Defcon1  Webmail