Installing and configuring Nginx

Either the binary packages or the ports can be used to install Nginx in FreeBSD. Using the ports is the prefered way of installing Nginx to do it as Nginx consists of a lot of different modules and often we need to build it with a custom set of them.

There are two nginx versions available in the ports:

The mainline version includes more features so lets install it:

make -C /usr/ports/www/nginx-devel install clean

Then we need to enable Nginx in /etc/rc.conf:

nginx_enable="YES"

And start it:

service nginx start

Nginx configuration is location /usr/local/etc/nginx/nginx.conf. There are some examples in the default config but I recommend to create your own configuration file rather that commenting/uncommenting lines. You can always find the default config file contents in /usr/local/etc/nginx/nginx.conf-dist.

Here’s minimal example configuration:

worker_processes 2;

events {
  worker_connections 4096;
}

http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  keepalive_timeout 65;

  server {
    listen 80 default;

    location / {
      root /usr/local/www/nginx;
      index index.html;
    }
  }
}

Please don’t forget to test configuration before reloading:

service nginx configtest

And configuration reload can be done using the command:

service nginx reload
comments powered by Disqus