NGINX Unit - An Open Source Web Application Server

NGINX Unit application serverNginx announced the release of the first beta of Unit - Nginx Unit, a dynamic web application server, designed to run applications in multiple languages.  It is an application server that supports Python, PHP and Go, with support coming for Java, Node.js and Ruby.

Unit is lightweight, polyglot, and dynamically configured via API. NGINX Unit is currently available as a beta so, it is not recommended for use in production.  Unit can be configured by making HTTP requests

Install Nginx Unit

NGINX Unit is tested to compile and run on the following systems: Linux 2.6 or later, FreeBSD 9 or later, MacOS X, Solaris 11. It exists Unit's packages available on Centos 7 and Ubuntu 16.04. For running applications, you need python (2.6, 2.7, 3), php (5, 7), Go (1.6 or later) programming languages

a) On Centos 7

We need to first create the repo /etc/yum.repos.d/unit.repo

# vim /etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

Now we can install

# yum install unit
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.linode.com
 * epel: fedora-epel.mirrors.tds.net
 * extras: mirrors.linode.com
 * updates: mirrors.linode.com
Resolving Dependencies
--> Running transaction check
---> Package unit.x86_64 0:0.1-1.el7.ngx will be installed
--> Processing Dependency: libphp5-5.4.so()(64bit) for package: unit-0.1-1.el7.ngx.x86_64
--> Running transaction check
---> Package php-embedded.x86_64 0:5.4.16-42.el7 will be installed
--> Finished Dependency Resolution

b) On Ubuntu 16.04

We need first to download the PGP key

# wget http://nginx.org/keys/nginx_signing.key

Now we can install the key

# apt-key add nginx_signing.key
OK

Now we must add Unit to our sources.list file

# vim /etc/apt/sources.list
deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx 
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx

Then install it

# apt update
...............
...............
Get:8 http://nginx.org/packages/mainline/ubuntu xenial/nginx Sources [24.7 kB]
Get:9 http://nginx.org/packages/mainline/ubuntu xenial/nginx amd64 Packages [35.2 kB]
# # apt install unit
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following packages were automatically installed and are no longer required:
 libdvdnav4 libdvdread4 libenca0 libguess1 librubberband2v5 libsdl2-2.0-0 libsndio6.1 libva-wayland1 libva-x11-1 mpv rtmpdump
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
 libphp-embed libphp7.0-embed
Suggested packages:
 php-pear
The following NEW packages will be installed:
 libphp-embed libphp7.0-embed unit

Configure and launch the application

The Unit configuration needs to include an application associated with at least one listener. So, the configuration will have two sections: listener and application. You need to use the API to define a JSON object for each application on its section. By default, the Unit API is available in the control socket file control.unit.sock located of at /var/run/control.unit.sock or /run/control.unit.sock when you install unit through the precompiled packages.

You need to define at least one listener if you want your application to be accessible via HTTP. The listeners' section includes the IP address and the port on which Unit listens for client requests to a named application.

"listeners": {
         "*:8300": {
             "application": "myapps"
         }
     }

On the application section, the JSON object defines several characteristics of this one including the language it's written in, the number of application worker processes to run, the directory with the file or files for the application, etc. You must notice that some parameters can vary by language so, each platform has slightly different options.

"applications": {
         "myapps": {
             "type": "php",
             "workers": 20,
             "root": "/var/www/php-app",
             "index": "index.php"
         }
     }

To create an initial configuration, you first need to create a configuration object, specify the JSON data for it in the body of a PUT request. The port you indicated is the one which will be used to launch the application

You can indicate the path of the json file as shown as below:

# curl -X PUT -d @/path/file.json --unix-socket /var/run/control.unit.sock http://localhost:port

For example, we will create the json file as below:

#vim /var/www/html/apps.json
{
     "listeners": {
         "*:4500": {
             "application": "apps"
         }
     },
     "applications": {
         "apps": {
             "type": "php",
             "workers": 10,
             "user": "www-php",
             "group": "www-php",
             "root": "/var/www/html",
             "index": "index.php"
         }
     }
}

Now that we decided to use the port 4500, we should open it for use

# firewall-cmd --zone=public --permanent --add-port=4500/tcp
# firewall-cmd --zone=public --permanent --add-port=4500/udp
# firewall-cmd --reload

Now we will create an initial configuration file with the command below:

# curl -X PUT -d @/var/www/html/apps.json --unix-socket /var/run/control.unit.sock http://localhost:4500
{
	"success": "Reconfiguration done."
}

You can see that the configuration is well done. You can access your web application by using the ip address and the port specified

You can see that the API server is unit instead of apache and the port used is the one configured

Unit is a useful dynamic web application server which offers the possibility to run your application in multiple languages. You can specify the port that you want to use. You can add the another project types to the same json file by respecting the definition of the object type. For more detail, you can see the official documentation.

About Alain Francois

IT Linux administrator passionate of free and open source software, I work on Linux Systems since some years on installations and deployments of certain solutions. I like to share my experiences with a wider audience by training and conferences.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.