How to install NGINX on NetBSD?

NGINX is a web server with excellent performance and low memory footprint. NGINX can also be used as a reverse proxy (FastCGI, Apache, uWSGI), as a proxy for mail protocols (IMAP, POP3) and as a load balancer.

In this post we will install NGINX as a FastCGI proxy then the PHP requests will be redirected to a PHP-FPM server.

You can be interested in:

Install

# pkgin in nginx
calculating dependencies...done.

2 packages to install:
  nginx-1.19.6 pcre-8.44

0 to refresh, 0 to upgrade, 2 to install
0B to download, 4698K to install

proceed ? [Y/n]

Manage nginx service

In this section we learn how to start, restart and stop the nginx service, for this task we are going to use the excelent tools: init, rc and service.

Add init script

The nginx package provides us an example script located under /usr/pkg/share/examples/rc.d/nginx and we can use it as our init script copying it to /etc/rc.d.

# cp -v  /usr/pkg/share/examples/rc.d/nginx  /etc/rc.d/
/usr/pkg/share/examples/rc.d/nginx -> /etc/rc.d/nginx

Automatic start

I love how *BSD handle services automatic start, simply add to the /etc/rc.conf file this line:

nginx=YES

The above line instructs to the init system that the nginx will start automatically after a system reboot.

Check status

# service nginx status
nginx is not running.

Start

# service nginx start  
Starting nginx.

Check the status again :

# service nginx status
nginx is running as pid 13875.

Restart

If you did any modification to the configuration files you need to refresh the changes with:

# service nginx restart
Stopping nginx.
Starting nginx.

Reload configuration

It is recommended to use the following command to reload configuration since the service will reload the changes without losing connections.

# service nginx reload
Reloading nginx config files.

Stop

# service nginx stop   
Stopping nginx.

Where is the configuration files?

You can find the configuration files under /usr/pkg/etc/nginx/.

/usr/pkg/etc/nginx/
|-- fastcgi.conf
|-- fastcgi_params
|-- koi-utf
|-- koi-win
|-- mime.types
|-- nginx.conf
`-- win-utf

Virtual Hosts

We are going to follow Debian configuration structure because, in my opinion, is very clean and easy to use it, then we need to create sites-enabled, sites-available directories, using these directories and symbolic links we can enable/disable Virtual Hosts very easily also we need to create the conf.d directory to store global configurations and
snippets directory for specific configurations.

# mkdir /usr/pkg/etc/nginx/sites-enabled
# mkdir /usr/pkg/etc/nginx/sites-available
# mkdir /usr/pkg/etc/nginx/conf.d
# mkdir /usr/pkg/etc/nginx/snippets

Add the following lines to the end of the main NGINX configuration file: nginx.conf.

##
# Virtual Host Configs
##

include /usr/pkg/etc/nginx/conf.d/*.conf;
include /usr/pkg/etc/nginx/sites-enabled/*;

Now we have this configuration structure.

/usr/pkg/etc/nginx/
|-- conf.d
|-- fastcgi.conf
|-- fastcgi_params
|-- koi-utf
|-- koi-win
|-- mime.types
|-- nginx.conf
|-- sites-available
|-- sites-enabled
|-- snippets
`-- win-utf

The next step is maiking NGINX and PHP-FPM work together so we create the php-fpm-upstream.conf template under conf.d directory.

# touch /usr/pkg/etc/nginx/conf.d/php-fpm-upstream.conf

Add these lines:

upstream php-fpm {
    server 127.0.0.1:9000;
}

Set server directive to the PHP-FPM pool do you want to connect to, in our case our pool is in the same server where we installed NGINX but it is possible to pointout to another server too.

Next create php-fpm.conf template under snippets directory.

# touch /usr/pkg/etc/nginx/snippets/php-fpm.conf

Add these lines:

fastcgi_pass   php-fpm;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
include        /usr/pkg/etc/nginx/fastcgi_params;

Create proxy-php-fpm template under sites-available directory.

# touch /usr/pkg/etc/nginx/sites-available/proxy-php-fpm

Add these lines:

server {
    listen 80;
    server_name midominio.com;
    # Set the document root
    root /var/www/midominio;
    index index.php;
    
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    
    location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when
        # using query string
        try_files $uri $uri/ /index.php?$args;
    }
    
    location ~ \.php$ {
        include /usr/pkg/etc/nginx/snippets/php-fpm.conf;
    }
    
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

It is time to configure our first site according above configuration

# cp -v /usr/pkg/etc/nginx/sites-available/proxy-php-fpm /usr/pkg/etc/nginx/sites-available/misitio
/usr/pkg/etc/nginx/sites-available/proxy-php-fpm -> /usr/pkg/etc/nginx/sites-available/mysite

Edit mysite file and set server_name directive according your domain and enable your site:

# ln -s /usr/pkg/etc/nginx/sites-available/misitio /usr/pkg/etc/nginx/sites-enabled/mysite 

Test configuration files:

# nginx -t
nginx: the configuration file /usr/pkg/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/pkg/etc/nginx/nginx.conf test is successful

Reload configuration changes.

# service nginx reload
Reloading nginx config files.

Log rotation

Log rotation allows to empty logs every so often, keeping copies of them. NetBSD uses newsyslog to do this, so we add the following lines to /etc/newsyslog.conf.

# echo '/var/log/nginx/access.log nginx:nginx 640 7 * 24 Z  /var/run/nginx.pid SIGUSR1' >> /etc/newsyslog.conf
# echo '/var/log/nginx/error.log  nginx:nginx 640 7 * 24 Z  /var/run/nginx.pid SIGUSR1' >> /etc/newsyslog.conf

Important links

YouTube video

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.