How to Install NGINX on FreeBSD
This tutorial will show you how to install NGINX on FreeBSD. You will also learn to configure NGINX to host a static website.
How to Install NGINX on FreeBSD
FreeBSD simplifies software management with its packaging system and ports collection. Packages and ports automate the install, uninstall, and configuration process of thousands of software packages.
Let’s take a look at how to install NGINX on FreeBSD with the package manager.
Install with Package Manager
1. Switch to the root user.
su -
2. Update FreeBSD repository.
pkg update
If it’s your first time running the pkg update
command, you might be asked to install the package management tool.
The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]:
Type y and hit ENTER to install the package management tool and update the repositories.
3. Now we can install NGINX with the following command:
pkg install nginx
4. Enable NGINX so that it starts when FreeBSD boots.
service nginx enable
5. Start NGINX now.
service nginx start
You should be able to view the default website by entering the FreeBSD server IP address in your web browser.

We are now ready to configure NGINX.
How to Configure NGINX on FreeBSD
The NGINX configuration files are located at /usr/local/etc/nginx
on FreeBSD systems. The nginx folder contains the following files:
fastcgi_params
fastcgi_params-dist
koi-utf
koi-win
mime.types
mime.types-dist
nginx.conf
nginx.conf-dist
scgi_params
scgi_params-dist
uwsgi_params
uwsgi_params-dist
win-utf
The main file we need to edit is the nginx.conf file. The other files are included inside the nginx.conf file depending on what configuration options you want to use. If you open the file, you will see the default configuration has a lot of comments and examples of virtual hosts.
Let’s simplify the file and create a directory to store our virtual hosts in.
1. Delete the nginx.conf file (we can restore it by copying nginx.conf-dist or use it as a reference to see the default configuration).
rm /usr/local/etc/nginx/nginx.conf
2. Create a new nginx.conf file.
vi /usr/local/etc/nginx/nginx.conf
Add the following configuration to the file.
user www;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip off;
include sites/*.conf;
}
You will notice, there are no server blocks in this file. Instead, we have used an include that searches the sites directory for more conf file. This is where we will put our conf files for each website we want to host.
3. Create a directory called sites to store our virtual host configuration files.
mkdir /usr/local/etc/nginx/sites
4. Create a file called default.conf to store the default website configuration.
vi /usr/local/etc/nginx/sites/default.conf
Add the following contents to the file.
server {
listen 80;
server_name localhost;
location / {
root /usr/local/www/nginx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx;
}
}
5. Reload the NGINX configuration.
service nginx reload
After applying the changes to NGINX, the default website should still work. We are now ready to add more websites by adding each configuration to the sites directory.
Hosting Another Website
Okay, now that we have NGINX installed and we have tidied up the nginx.conf file. Let’s create a new configuration file to host an example website.
1. Create a file called example.com.conf inside the sites directory.
vi /usr/local/etc/nginx/sites/example.com.conf
Add the following configuration to the file.
server {
listen 80;
server_name example.com;
location / {
root /usr/local/www/example.com;
index index.html;
}
}
2. Create a directory to store the websites HTML files.
mkdir /usr/local/www/example.com
3. Create index.html file.
vi /usr/local/www/example.com/index.html
Add some HTML to the file.
<!DOCTYPE html>
<html lang="en">
<title> </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css">
<body>
<article class="vh-100 dt w-100 bg-dark-pink">
<div class="dtc v-mid tc white ph3 ph4-l">
<h1 class="f6 f2-m f-subheadline-l fw6 tc">Hello World!</h1>
</div>
</article>
</body>
</html>
4. Reload the NGINX configuration.
service nginx reload
Add the IP address of your FreeBSD server to your /etc/hosts
file (or local DNS server) then navigate to http://example.com
and you should see the website load.

Summary
In this tutorial, we learned how to install NGINX on FreeBSD using the package manager. We also learned how to configure NGINX and make it serve multiple websites on the same server.