How To Install Nginx From Source on FreeBSD 11.1

To Install Nginx From Source on FreeBSD 11.1

Nginx is an open-source, high-performance web server that delivers static content by using the system resources. It also hosts several highest traffic internet sites. Nginx is packed with several features and some of them are listed below.

  • It serves static and index files
  • Reverse proxy with caching
  • Supports SSL
  • Supports rewrite module
  • Supports gzip, XSLT, SSI and image resizing filters

It is so simple to install Nginx from its source on FreeBSD, and the method to install it is covered here.

Installation

First, we need to Install dependencies that are required for the installation.

root@linuxhelp11:~ # pkg install libxslt libxml2 libxml2 GeoIP  openssl-devel pcre
Updating FreeBSD repository catalogue...
Fetching meta.txz: 100%    944 B   0.9kB/s    00:01   
Fetching packagesite.txz: 100%    6 MiB   6.4MB/s    00:01   
Processing entries: 100%
FreeBSD repository update completed. 31152 packages processed.
All repositories are up to date.
The following 2 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
    GeoIP: 1.6.12
    openssl-devel: 1.1.0h_1
Number of packages to be installed: 2
The process will require 13 MiB more space.
3 MiB to be downloaded.
Proceed with this action? [y/N]: y
[1/2] Fetching GeoIP-1.6.12.txz: 100%  131 KiB 134.1kB/s    00:01   
[2/2] Fetching openssl-devel-1.1.0h_1.txz: 100%    3 MiB   3.5MB/s    00:01   
Checking integrity... done (0 conflicting)
[1/2] Installing GeoIP-1.6.12...
[1/2] Extracting GeoIP-1.6.12: 100%
[2/2] Installing openssl-devel-1.1.0h_1...
[2/2] Extracting openssl-devel-1.1.0h_1: 100%
Message from GeoIP-1.6.12:
GeoIP does not ship with the actual data files. You must download
them yourself! Please run:
# /usr/local/bin/geoipupdate.sh

Now download the nginx source file from its official site through the terminal as follows.

root@linuxhelp11:~ #  wget https://nginx.org/download/nginx-1.14.0.tar.gz
--2018-05-06 18:43:09--  https://nginx.org/download/nginx-1.14.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ...
Connecting to nginx.org (nginx.org)|95.211.80.227|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1016272 (992K) [application/octet-stream]
Saving to: ' nginx-1.14.0.tar.gz' 
nginx-1.14.0.tar.gz             100%[=====================================================> ] 992.45K   475KB/s    in 2.1s   
2018-05-06 18:43:12 (475 KB/s) - ' nginx-1.14.0.tar.gz'  saved [1016272/1016272]

After downloading, extract the downloaded files.

root@linuxhelp11:~ # tar -xzf nginx-1.14.0.tar.gz

After Extracting let' s list the files inside the extracted directory.

root@linuxhelp11:~ # cd nginx-1.14.0/
root@linuxhelp11:~/nginx-1.14.0 # ll
total 800
-rw-r--r--  1 user  user  286953 Apr 17 20:52 CHANGES
-rw-r--r--  1 user  user  437286 Apr 17 20:52 CHANGES.ru
-rw-r--r--  1 user  user    1397 Apr 17 20:52 LICENSE
-rw-r--r--  1 user  user      49 Apr 17 20:52 README
drwxr-xr-x  6 user  user     512 Apr 17 20:52 auto/
drwxr-xr-x  2 user  user     512 Apr 17 20:52 conf/
-rwxr-xr-x  1 user  user    2502 Apr 17 20:52 configure*
drwxr-xr-x  4 user  user     512 Apr 17 20:52 contrib/
drwxr-xr-x  2 user  user     512 Apr 17 20:52 html/
drwxr-xr-x  2 user  user     512 Apr 17 20:52 man/
drwxr-xr-x  9 user  user     512 Apr 17 20:52 src/

Now to configure Nginx installation procedure, run the configuration script command with the path of nginx files for installation.

root@linuxhelp11:~/nginx-1.14.0 #  ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx
checking for OS
 + FreeBSD 11.1-RELEASE amd64
checking for C compiler ... found
 + using Clang C compiler
 + clang version: 4.0.0 (tags/RELEASE_400/final 297347) (based on LLVM 4.0.0)
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
.
.
.
 nginx pid file: " /var/run/nginx.pid" 
 nginx error log file: " /var/log/nginx/error.log" 
 nginx http access log file: " /etc/nginx/logs/access.log" 
 nginx http client request body temporary files: " client_body_temp" 
 nginx http proxy temporary files: " proxy_temp" 
 nginx http fastcgi temporary files: " fastcgi_temp" 
 nginx http uwsgi temporary files: " uwsgi_temp" 
 nginx http scgi temporary files: " scgi_temp" 

Now, let' s compile the configured files.

root@linuxhelp11:~/nginx-1.14.0 # make
make -f objs/Makefile
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/nginx.o  src/core/nginx.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_log.o  src/core/ngx_log.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_palloc.o  src/core/ngx_palloc.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_array.o  src/core/ngx_array.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_list.o  src/core/ngx_list.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_hash.o  src/core/ngx_hash.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_buf.o  src/core/ngx_buf.c
cc -c -pipe  -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Werror -g -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I /usr/local/include  -I objs  -o objs/src/core/ngx_queue.o  src/core/ngx_queue.c
.
.
.
.
objs/src/http/modules/ngx_http_auth_basic_module.o  objs/src/http/modules/ngx_http_access_module.o  objs/src/http/modules/ngx_http_limit_conn_module.o  objs/src/http/modules/ngx_http_limit_req_module.o  objs/src/http/modules/ngx_http_geo_module.o  objs/src/http/modules/ngx_http_map_module.o  objs/src/http/modules/ngx_http_split_clients_module.o  objs/src/http/modules/ngx_http_referer_module.o  objs/src/http/modules/ngx_http_rewrite_module.o  objs/src/http/modules/ngx_http_proxy_module.o  objs/src/http/modules/ngx_http_fastcgi_module.o  objs/src/http/modules/ngx_http_uwsgi_module.o  objs/src/http/modules/ngx_http_scgi_module.o  objs/src/http/modules/ngx_http_memcached_module.o  objs/src/http/modules/ngx_http_empty_gif_module.o  objs/src/http/modules/ngx_http_browser_module.o  objs/src/http/modules/ngx_http_upstream_hash_module.o  objs/src/http/modules/ngx_http_upstream_ip_hash_module.o  objs/src/http/modules/ngx_http_upstream_least_conn_module.o  objs/src/http/modules/ngx_http_upstream_keepalive_module.o  objs/src/http/modules/ngx_http_upstream_zone_module.o  objs/ngx_modules.o  -lcrypt -L/usr/local/lib -lpcre -lz  -Wl,-E
sed -e " s|%%PREFIX%%|/etc/nginx|"   -e " s|%%PID_PATH%%|/var/run/nginx.pid|"   -e " s|%%CONF_PATH%%|/etc/nginx/conf/nginx.conf|"   -e " s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|"   <  man/nginx.8 >  objs/nginx.8

After compiling build installation as follows.

root@linuxhelp11:~/nginx-1.14.0 # make install
make -f objs/Makefile install
test -d ' /etc/nginx'  || mkdir -p ' /etc/nginx' 
test -d ' /usr/sbin'   || mkdir -p ' /usr/sbin' 
test ! -f ' /usr/sbin/nginx'   || mv ' /usr/sbin/nginx'   ' /usr/sbin/nginx.old' 
cp objs/nginx ' /usr/sbin/nginx' 
test -d ' /etc/nginx/conf'   || mkdir -p ' /etc/nginx/conf' 
cp conf/koi-win ' /etc/nginx/conf' 
cp conf/koi-utf ' /etc/nginx/conf' 
cp conf/win-utf ' /etc/nginx/conf' 
test -f ' /etc/nginx/conf/mime.types'   || cp conf/mime.types ' /etc/nginx/conf' 
cp conf/mime.types ' /etc/nginx/conf/mime.types.default' 
test -f ' /etc/nginx/conf/fastcgi_params'   || cp conf/fastcgi_params ' /etc/nginx/conf' 
cp conf/fastcgi_params  ' /etc/nginx/conf/fastcgi_params.default' 
test -f ' /etc/nginx/conf/fastcgi.conf'   || cp conf/fastcgi.conf ' /etc/nginx/conf' 
cp conf/fastcgi.conf ' /etc/nginx/conf/fastcgi.conf.default' 
test -f ' /etc/nginx/conf/uwsgi_params'   || cp conf/uwsgi_params ' /etc/nginx/conf' 
cp conf/uwsgi_params  ' /etc/nginx/conf/uwsgi_params.default' 
test -f ' /etc/nginx/conf/scgi_params'   || cp conf/scgi_params ' /etc/nginx/conf' 
cp conf/scgi_params  ' /etc/nginx/conf/scgi_params.default' 
test -f ' /etc/nginx/conf/nginx.conf'   || cp conf/nginx.conf ' /etc/nginx/conf/nginx.conf' 
cp conf/nginx.conf ' /etc/nginx/conf/nginx.conf.default' 
test -d ' /var/run'   || mkdir -p ' /var/run' 
test -d ' /etc/nginx/logs'   || mkdir -p ' /etc/nginx/logs' 
test -d ' /etc/nginx/html'   || cp -R html ' /etc/nginx' 
test -d ' /var/log/nginx'   || mkdir -p ' /var/log/nginx' 

Now the installation is completed, and you can check for nginx version using the following command.

root@linuxhelp11:~/nginx-1.14.0 # nginx -v
nginx version: nginx/1.14.0

Now we need to add a user for nginx.

root@linuxhelp11:~ # adduser
Username: nginx
Full name: nginx
Uid (Leave empty for default):
Login group [nginx]:
Login group is nginx. Invite nginx into other groups? []:
Login class [default]:
Shell (sh csh tcsh bash rbash nologin) [sh]:
Home directory [/home/nginx]:
Home directory permissions (Leave empty for default):
Use password-based authentication? [yes]:
Use an empty password? (yes/no) [no]:
Use a random password? (yes/no) [no]:
Enter password:
Enter password again:
Use an empty password? (yes/no) [no]: yes
Lock out the account after creation? [no]:
Username   : nginx
Password   : < blank> 
Full Name  : nginx
Uid        : 1002
Class      :
Groups     : nginx
Home       : /home/nginx
Home Mode  :
Shell      : /bin/sh
Locked     : no
OK? (yes/no): yes
adduser: INFO: Successfully added (nginx) to the user database.
Add another user? (yes/no): no
Goodbye!

After adding the user change the ownership of directory /etc/nginx.

root@linuxhelp11:~ # chown -R nginx:nginx /etc/nginx

Now, we going to configure Daemon for nginx. so create a file in the /usr/local/etc/rc.d/ location named nginx.

root@linuxhelp11:~ # vim /usr/local/etc/rc.d/nginx

Add the following lines.

. /etc/rc.subr
name=" nginx" 
rcvar=" nginx_enable" 
command=/usr/sbin/nginx
_pidprefix=" /var/run/nginx" 
pidfile=" ${_pidprefix}.pid" 
required_files=/etc/nginx/conf/nginx.conf
start_cmd=" test_start" 
stop_cmd=" test_stop" 
status_cmd=" test_status" 
test_start() {
         ${command}
}
test_status() {
        if [ -e ${pidfile} ]  then
                echo ${name} is running...
        else
                echo ${name} is not running.
        fi
}
test_stop() {
        if [ -e ${pidfile} ]  then
                kill `cat ${pidfile}` 
        else
                echo ${name} is not running?
        fi
}
load_rc_config $name
run_rc_command " $1" 

And then, enable the nginx by the following command.

root@linuxhelp11:~ # echo ' nginx_enable=" YES" '  > >  /etc/rc.conf

Now we have to start the nginx service.

root@linuxhelp11:~ # service nginx start

After starting the nginx service check for the status of nginx

root@linuxhelp11:~ # service nginx status
nginx is running...

And then open a browser, and navigate to the following URL http://localhost, you will see the following screen on your browser.

With this, the method to Install Nginx From Source on FreeBSD 11.1 comes to an end.

Tag : Nginx FreeBSD
Comment
parthiban
Aug 27 2018
If the file does not exist try to create a new file for nginx and add the content as per your configuration then set proper permission
acesgroup
Jul 03 2018
nginx does not exist in /etc/rc.d or the local startup no permission as root on freebsd ? 11.1 cant start nginx service nginx restart maybe u can help me out with freeBSD root / permission
Add a comment
FAQ
Q
Why would you use a reverse proxy in inginx?
A
A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server.
Q
Is nginx a Load Balancer?
A
Nginx, which is a popular web server software, can be configured as a simple yet powerful load balancer to improve your servers resource availability and efficiency. In a load balancing configuration, nginx acts as a single entry point to a distributed web application working on multiple separate servers.
Q
I’d like to use nginx distribution as part of my proprietary software package. How can nginx copyright be acknowledged when using nginx as part of a proprietary software distribution?
A
The text below should be added to your license conditions, followed by the text of the applicable 2-clause BSD license described here. This product contains software provided by Nginx, I
Q
Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates?
A
Variables should not be used as template macros. Variables are evaluated in the run-time during the processing of each request, so they are rather costly compared to plain static configuratio
Q
While building nginx version 0.7.66, 0.8.35 or higher on Linux the following warning messages are issued: warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' ins
A
This is normal: nginx has to use the deprecated sys_errlist[] and sys_nerr in signal handlers because strerror() and strerror_r() functions are not Async-Signal-Safe.