How do I install an Nginx webserver under a FreeBSD Unix operating system? How can I install Nginx web server on FreeBSD?

nginx [engine x] is a HTTP server, reverse proxy and mail proxy server written by Igor Sysoev. It is a lightweight web server, licensed under a BSD-like license. It the fastest growing and one of the most popular web server. This page shows how to install Nginx webserver on FreeBSD based server.

FreeBSD Install Nginx Webserver

The procedure for installing Nginx on FreeBSD is as follows:

  1. Install nginx using pkg command: pkg install nginx
  2. Turn on Nginx service on FreeBSD: sysrc nginx_enable=YES
  3. Configure Nginx server by editing /usr/local/etc/nginx/nginx.conf file
  4. Test the setup

Let us see all steps in details.

Update FreeBSD ports tree

Type the following command to update FreeBSD tree, enter:
# portsnap fetch update
Update or install FreeBSD port tree using portsnap

Installing Nginx using FreeBSD ports

Run the following command to install the Nginx web server port:
# cd /usr/ports/www/nginx/
# make install clean

How To Install an Nginx Web Server Using FreeBSD Ports Tree
Make sure you select important option such as REWRITE_MODULE, SSL_MODULE and others as per your requirements.

Installing nginx using the pkg command

Add the binary package using the following pkg command:
# pkg install nginx
FreeBSD Install Nginx Webserver Tutorial using pkg

Turn on Nginx Webserver Service

Execute the following command to turn on Nginx server
# echo 'nginx_enable="YES"' >> /etc/rc.conf
# sysrc nginx_enable="YES"
nginx_enable: -> YES

How do I start an nginx server?

To start Nginx, enter:
# /usr/local/etc/rc.d/nginx start
# service nginx start
Sample outputs:

Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.

How do I stop an nginx server?

# /usr/local/etc/rc.d/nginx stop
# service nginx stop

How do I restart an nginx server?

# /usr/local/etc/rc.d/nginx restart
# service nginx restart

How do I reload an nginx server after config file changes?

# /usr/local/etc/rc.d/nginx reload
# service nginx reload

Important Information about Nginx webserver on a FreeBSD

  • Nginx Default configuration file: /usr/local/etc/nginx/nginx.conf
  • Nginx Default http / https port: 80 / 443
  • Nginx Default error log file: /var/log/nginx-error.log
  • Nginx Default access log file: /var/log/nginx-access.log
  • Nginx Default DocumentRoot: /usr/local/www/nginx/
  • Nginx service command: service nginx (start|stop|restart|reload|configtest|upgrade|gracefulstop|status|poll)

Testing the setup

Fire the web browser and type the following url in your web-browser:
OR your domain name:
Sample outputs:

Fig.03: Nginx in action
Nginx in action

Creating your first web site

In this example, I am going to install mediawiki software for personal use at /wwwwiki/. Let us create a directory using the mkdir command:
# mkdir /wwwwiki
Create a FreeBSD user for your domain using the pw command:
# pw user add -n wwwwiki -c 'My wiki user' -d /nonexistent -s /usr/sbin/nologin
Lock FreeBSD user named wwwwiki:
# pw lock wwwwiki
Create a sample file for testing purpose:
# vi /wwwwiki/index.html
Append following HTML:

<head> <title>Vivek's personal wiki directory</title>
<body> <h1>Wiki directory</h1> <p>This is a test and I will configure PHP 7.x to host mediawiki</p> <hr> &copy; <a href=""></a>

Setup directory user and group ownership using the chown command and chmod command:
# chown -R wwwwiki:wwwwiki /wwwwiki/
# chmod -R 0555 /wwwwiki/
# ls -ld /wwwwiki/

Sample outputs:

dr-xr-xr-x 2 wwwwiki wwwwiki 2 Sep 23 23:41 /wwwwiki/

Configure Nginx for wiki virtual domain or IP address

VirtualHost is an Apache server term. Nginx does not have concept of Virtual hosts, it has concept of “Server Blocks” that use the server_name for domains or IP address. Create a config file as follows:
# mkdir /usr/local/etc/nginx/vdomains/
# vi /usr/local/etc/nginx/vdomains/http.

Append the following config:

server { server_name; # virtual IP or domain name here access_log /var/log/nginx/; # log files error_log /var/log/nginx/; root /wwwwiki; # root dir for this virtual host ## add more config below such as PHP and so on ##

Save and close the file. Update nginx.conf:
# vi /usr/local/etc/nginx/nginx.conf
Add the following just before final }:

include "vdomains/*.conf";

Save and close the file. Verify nginx config and reload it:
# nginx -t
# service nginx reload

Nginx VirtualHost aka server block config
Test the config:
Installing Nginx on FreeBSD and configuring your first domain


Congratulations. Now you have a fully functional web server powered by Nginx that serve static files. Stay tuned for PHP and MariaDB/MySQL server configuration.

This entry is 1 of 2 in the Nginx, MySQL, and PHP (FEMP) Stack on FreeBSD Tutorial series. Keep reading the rest of the series:

  1. FreeBSD Install Nginx Webserver Tutorial
  2. FreeBSD install PHP 7.2 with FPM for Nginx

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments

More Stuff

How to Find and Install Software Applications in Fedora Linux There are countless software packages are available to install on Fedora Linux distribution from the repository provided by Fedora project. You can al...
5 Cool New Projects to Try in Fedora Linux In this article, we will share five cool new projects to try in Fedora Linux distribution. Note that some of these projects may also be work on other ...
How to Install Memcached (Caching Server) on CentOS 7 Memcached is an open source distributed memory object caching program that allows us to improve and speed up the performance of dynamic web applicatio...
How to Install Adobe Flash Player 32 on Fedora Linux Adobe Flash is a web browser plug-in used to display interactive web pages, online games, and to playback video and audio content. Flash displays text...
Spread the love

Posted by News Monkey