{"id":24869,"date":"2018-06-09T13:26:33","date_gmt":"2018-06-09T04:26:33","guid":{"rendered":"https:\/\/jirak.net\/wp\/nginx-unit-1-2-available-now\/"},"modified":"2018-06-09T13:34:32","modified_gmt":"2018-06-09T04:34:32","slug":"nginx-unit-1-2-available-now","status":"publish","type":"post","link":"https:\/\/jirak.net\/wp\/nginx-unit-1-2-available-now\/","title":{"rendered":"NGINX Unit 1.2 Available Now"},"content":{"rendered":"<p>NGINX Unit 1.2 Available Now<\/p>\n<p>On June&nbsp;7 we released <span>NGINX Unit 1.2<\/span>, the latest version of our dynamic multilingual application and web server, with support for many exciting features that enable its use in more sophisticated environments. This follows the <a href=\"https:\/\/www.nginx.com\/blog\/nginx-unit-1-0-released\/\">general availability (GA) release of version&nbsp;1.0<\/a> on April&nbsp;12 and the release of version&nbsp;1.1, with bug fixes and minor updates, just a couple weeks later.<\/p>\n<p><span>NGINX Unit 1.2<\/span> includes these new features:<\/p>\n<ul>\n<li>All types of applications can now be dynamically configured with their own environment variables.<\/li>\n<li>Configuration options for PHP applications can be defined in the <strong>php.ini<\/strong> file or directly with the <span>NGINX Unit API<\/a>.<\/li>\n<li>Go executables can now be configured with command&#8209;line arguments.<\/li>\n<\/ul>\n<p>These new features make your applications even more configurable. All parameters can be defined dynamically, without any disruption to running services or loss of connectivity. <\/p>\n<p>Let&#8217;s review the features in more detail.<\/p>\n<h2>Managing Environment Variables<\/h2>\n<p>When any application starts, it can get information about its environment from the operating system. As described in <a target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Environment_variable\">Wikipedia<\/a> (as of <span>June 8, 2018<\/span>), environment variables &#8220;are part of the environment in which a process runs. For example, a running process can query the value of the <code>TEMP<\/code> environment variable to discover a suitable location to store temporary files, or the <code>HOME<\/code> or <code>USERPROFILE<\/code> variable to find the directory structure owned by the user running the process.&#8221;<\/p>\n<p>In web applications, environment variables are often used to specify app parameters and credentials for access to services such as databases. (In this post we are not discussing the security of using environment variables for this purpose, and recommend doing your own research on the best ways to pass security credentials to applications.)<\/p>\n<p>Starting in <span>NGINX Unit 1.2<\/a>, you can use the API to dynamically set environment variables for applications.<\/p>\n<p>When you change the list of variables for an application, only the processes for that particular application are restarted, gracefully and without loss of any connections. Other applications running on the same instance of NGINX&nbsp;Unit are not affected.<\/p>\n<p>Following are examples of how to add, change, and delete environment variables. <\/p>\n<h3>Minimal Configuration<\/h3>\n<p>This configuration defines the variable <code>VAR1<\/code> for a Python app called <strong>myapp<\/strong>. We show only the minimal configuration:<\/p>\n<pre><code class=\"config\">{\r\n    \"listeners\": {\r\n        \"*:8081\": {\r\n            \"application\": \"myapp\"\r\n        }\r\n    },\r\n\r\n    \"applications\": {\r\n        \"myapp\": {\r\n            \"type\": \"python\",\r\n            \"path\": \"\/srv\/demo\/var\",\r\n            \"module\": \"wsgi\",\r\n            \"environment\": {\r\n                \"VAR1\": \"Hello world\"\r\n            }\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<p>We tested this configuration with the following minimal Python code, which returns the value of the <code>VAR1<\/code> environment variable:<\/p>\n<pre><code class=\"config\">import os\r\n\r\ndef application(environ, start_response):\r\n    output = os.environ['VAR1']\r\n    start_response('200 OK', [('Content-type', 'text\/html')])\r\n    return [output.encode('utf-8')]<\/code><\/pre>\n<h3>Defining an Environment Variable at Runtime<\/h3>\n<p>With a running instance of NGINX&nbsp;Unit, you can use the API to add a variable either adding it to the existing configuration (as in this example) or by uploading an entire new configuration that includes it. This example defines the new environment variable <code>Darkside<\/code>:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl -X PUT -d '\"We have cookies\"' --unix-socket \/run\/control.unit.sock http:\/\/localhost\/config\/applications\/myapp\/environment\/Darkside<\/span>\r\n{\r\n    \"success\": \"Reconfiguration done.\"\r\n}<\/code><\/pre>\n<h3>Displaying Environment Variables<\/h3>\n<p>To display all environment variables assigned to an application, access its &#8220;<code>environment<\/code>&#8221; object directly:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl --unix-socket \/run\/control.unit.sock http:\/\/localhost\/config\/applications\/myapp\/environment<\/span>\r\n{\r\n    \"VAR1\": \"Hello world\",\r\n    \"Darkside\": \"We have cookies\"\r\n}<\/code><\/pre>\n<p>Or you can display just one variable by appending its name to the previous URL:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl --unix-socket .\/control.unit.sock http:\/\/localhost\/config\/applications\/myapp\/environment\/VAR1<\/span>\r\n\"Hello world\"<\/code><\/pre>\n<h3>Deleting an Environment Variable<\/h3>\n<p>You can use the <code>DELETE<\/code> method to delete one variable at a time (as in this example) or the entire <code>environment<\/code> object:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl -X DELETE --unix-socket \/run\/control.unit.sock http:\/\/localhost\/config\/applications\/myapp\/environment\/Darkside<\/span>\r\n{\r\n    \"success\": \"Reconfiguration done.\"\r\n}<\/code><\/pre>\n<h2>PHP Configuration and the php.ini File<\/h2>\n<p>When PHP starts up, it reads the system&#8209;level <strong>php.ini<\/strong> file, which for an Ubuntu system running PHP&nbsp;7 is located in the <strong>\/etc\/php\/7.0\/embed\/<\/strong> directory by default. By default, all PHP applications read the same configuration file. <\/p>\n<p>There are many configuration options for PHP, which most of our users customize for their particular applications. Examples of these options include memory management, include paths, and temporary file locations. For more information, see the PHP documentation about the <a target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/configuration.file.php\"><strong>php.ini<\/strong> file<\/a> and the <a target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/ini.list.php\">core configuration options<\/a>.<\/p>\n<p>Starting in <span>NGINX Unit 1.2<\/span>, you can define the location of <strong>php.ini<\/strong> on a per&#8209;application basis, and add specific configuration options directly with the API.<\/p>\n<p>PHP reads <strong>php.ini<\/strong> only at startup, so when you change the file, you must restart the application workers for the relevant instances of the PHP application. When you use the <span>NGINX Unit API<\/span> to change configuration options, however, NGINX&nbsp;Unit reloads the PHP application workers automatically, with no dropped connections or effect on the running processes of other applications.<\/p>\n<h3>Full Configuration<\/h3>\n<p>This sample configuration defines <strong>\/etc\/my-configs\/php.ini<\/strong> as the configuration file for the application called <strong>myphp<\/strong>:<\/p>\n<pre><code class=\"config\">{\r\n    \"listeners\": {\r\n        \"*:8081\": {\r\n            \"application\": \"myphp\"\r\n        }\r\n    },\r\n\r\n    \"applications\": {\r\n        \"myphp\": {\r\n            \"type\": \"php\",\r\n            \"root\": \"\/srv\/demo\/phpini\",\r\n            \"index\": \"index.php\",\r\n            \"options\": {\r\n                \"file\": \"\/etc\/my-configs\/php.ini\"\r\n            }\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<h3>Defining PHP Configuration Options at Runtime<\/h3>\n<p>You can use the <span>NGINX Unit API<\/span> to add new configuration options for use by a single application:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl -X PUT -d '{\"expose_php\": \"0\", \"memory_limit\": \"256M\"}' --unix-socket \/run\/control.unit.sock http:\/\/localhost\/config\/applications\/myphp\/options\/admin<\/span>\r\n{\r\n    \"success\": \"Reconfiguration done.\"\r\n}<\/code><\/pre>\n<p><strong>Note:<\/strong> The value <code>0<\/code> (zero) for the <code>expose_php<\/code> option is enclosed in quotes because PHP configuration options are processed as strings, even when they represent numbers; in JSON, strings must be within quotes.<\/p>\n<h3>Displaying PHP Configuration Options<\/h3>\n<p>You can always display the currently defined configuration options:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl --unix-socket \/run\/control.unit.sock http:\/\/localhost\/config\/applications\/myphp\/options<\/span>\r\n{\r\n    \"file\": \"\/etc\/my-configs\/php.ini\",\r\n    \"admin\": {\r\n        \"expose_php\": \"0\",\r\n        \"memory_limit\": \"256M\"\r\n    }\r\n}<\/code><\/pre>\n<h3>Changing the Value of a PHP Configuration Option<\/h3>\n<p>You can change the value of one option at a time (as in the following example):<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">curl -X PUT -d '\"1\"' --unix-socket \/run\/control.unit.sock http:\/\/localhost\/config\/applications\/myphp\/options\/admin\/expose_php<\/span>\r\n{\r\n    \"success\": \"Reconfiguration done.\"\r\n}<\/code><\/pre>\n<p>You can also change multiple options at the same time by uploading the full &#8220;<code>options<\/code>&#8221; object.<\/p>\n<h2>Go Command-Line Arguments<\/h2>\n<p>Go executables are often designed to be configured with a set of command&#8209;line arguments. You can define things like connectivity options, file locations, and database hosts.<\/p>\n<p><strong>Note:<\/strong> We recommend that you do not specify passwords or other sensitive information as command&#8209;line options.<\/p>\n<p>Starting in <span>NGINX Unit 1.2<\/span>, you can define Go command&#8209;line arguments as a JSON array in the configuration.<\/p>\n<p>For example, the command line might look like this for a Go app that is supposed to be executed with arguments to define a temporary file path:<\/p>\n<pre><code class=\"terminal\"># <span style=\"color:#66ff99;font-weight: bold\">\/srv\/demo\/go-apps\/app --tmp-files \/tmp\/go-cache<\/span><\/code><\/pre>\n<p>When you start a Go app with NGINX&nbsp;Unit instead of a command, you need to specify such command&#8209;line arguments in an <code>arguments<\/code> array in the configuration, as in this example:<\/p>\n<pre><code class=\"config\">{\r\n    \"listeners\": {\r\n        \"*:8081\": {\r\n            \"application\": \"mygo\"\r\n        }\r\n    },\r\n\r\n    \"applications\": {\r\n        \"mygo\": {\r\n            \"type\": \"go\",\r\n            \"executable\": \"\/srv\/demo\/go-apps\/app\",\r\n            \"arguments\": [\"--tmp-files\", \"\/tmp\/go-cache\"]\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>The new features in <span>NGINX Unit 1.2<\/span> make it even easier to configure your applications. You can define all parameters dynamically, without any disruption to running services or loss of connectivity.<\/p>\n<p>The full changelog for Unit development is available in the source code repositories (<a target=\"_blank\" href=\"https:\/\/hg.nginx.org\/unit\/file\/tip\/CHANGES\">https:\/\/hg.nginx.org\/unit\/file\/tip\/CHANGES<\/a> and <a target=\"_blank\" href=\"https:\/\/github.com\/nginx\/unit\/blob\/master\/CHANGES\">https:\/\/github.com\/nginx\/unit\/blob\/master\/CHANGES<\/a>) and on the Unit documentation website (<a target=\"_blank\" href=\"https:\/\/unit.nginx.org\/CHANGES.txt\">https:\/\/unit.nginx.org\/CHANGES.txt<\/a>).<\/p>\n<p>Watch our code repository on GitHub and join the discussions and development at <a target=\"_blank\" href=\"https:\/\/github.com\/nginx\/unit\">https:\/\/github.com\/nginx\/unit<\/a>.<\/p>\n<p>If you wish to become a full\u2011time engineer for NGINX, check our <a href=\"http:\/\/nginx.com\/jobs\">job board<\/a>.<\/p>\n<p>NGINX&nbsp;Plus subscribers get support for NGINX&nbsp;Unit at no additional charge. Download a <a href=\"#free-trial\">free 30&#8209;day trial of NGINX&nbsp;Plus<\/a> today! <\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/www.nginx.com\/blog\/nginx-unit-1-2-release-available-now\/\">NGINX Unit 1.2 Available Now<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/www.nginx.com\">NGINX<\/a>.<\/p>\n<p>Source: <a href=\"https:\/\/www.nginx.com\/blog\/nginx-unit-1-2-release-available-now\/\" target=\"_blank\">NGINX Unit 1.2 Available Now<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>NGINX Unit 1.2 Available Now On June&nbsp;7 we released NGINX Unit 1.2, the latest version of our dynamic multilingual application and web server, with support for many exciting features that enable its use in more sophisticated environments. This follows the general availability (GA) release of version&nbsp;1.0 on April&nbsp;12 and the release of version&nbsp;1.1, with bug fixes and minor updates, just a couple weeks later. NGINX Unit 1.2 includes these new features: All types of applications can now be dynamically configured with their own environment variables. Configuration options for PHP applications can be defined in the php.ini file or directly with the NGINX Unit API. Go executables can now be configured with command&#8209;line arguments. These new features make your applications even more configurable. All parameters can be defined dynamically, without any disruption to running services or loss of connectivity. Let&#8217;s review <a class=\"mh-excerpt-more\" href=\"https:\/\/jirak.net\/wp\/nginx-unit-1-2-available-now\/\" title=\"NGINX Unit 1.2 Available Now\">[ more&#8230; ]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[652],"class_list":["post-24869","post","type-post","status-publish","format-standard","hentry","category-news","tag-nginx"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts\/24869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/comments?post=24869"}],"version-history":[{"count":1,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts\/24869\/revisions"}],"predecessor-version":[{"id":24870,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts\/24869\/revisions\/24870"}],"wp:attachment":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/media?parent=24869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/categories?post=24869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/tags?post=24869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}