Unit: Hello world samples ?

Created on 7 Sep 2017  路  20Comments  路  Source: nginx/unit

Hey,

this project looks very promising, but I got hard time trying to understand what you expect an application to look like.

Is there a place where I could find hello world application for the different supported language?

All 20 comments

@maxired you can find "hello world" examples for PHP, Python and Go in pre-compiled packages:
http://unit.nginx.org/docs-installation.html#precompiled-packages

(Also, we're going to add package sources to this repo as well.)

@defanator thanks.
so from what I could read in examples files of http://nginx.org/packages/mainline/ubuntu/pool/nginx/u/unit/unit_0.1-1~xenial_amd64.deb

For php : just write regular php that would be executed on any webserver

For python: use wsgi for now. Here Hello world from wikipedia :

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield 'Hello World\n'

For go: this is a different story. you should write an app, creating a net/http server, and bind it with unit.ListenAndServe("8000", nil) . Does this port value mean anything ?

@maxired no, in case of plugging Go app into unit you may use any address/port there. In order to build and test "hello world" Go app from examples provided in the package, please do the following:

# sudo apt-get install unit-dev
# go build -o /tmp/go-app /usr/share/doc/unit/examples/go-app/let-my-people.go
# sudo service unitd restoreconfig /usr/share/doc/unit/examples/example.config
# curl http://localhost:8500/foo/bar/
Method : GET
URL    : /foo/bar/
Host   : localhost:8500

Hi @maxired

If the Go application is executed directly, the unit module will fall back to the http module. If the Go application is launched by Unit, it will communicate with the Unit router via shared memory.

from http://unit.nginx.org/docs-installation.html#source-code

yould you have an example application for php as well?

@docteurklein, looks like you can't just write any php code as you would with apache or any other php cgi.

for example the sample in the repo is

<?php phpinfo(); ?>

On 12 Sep 2017, at 12:48, Maxence Dalmais notifications@github.com wrote:

@docteurklein, looks like you can't just write any php code as you would with apache or any other php cgi.

No. You should be able to run any existent PHP code without changes.

--
Igor Sysoev
http://nginx.com

I can confirm it starts working on my legacy app.
here is a working config for symfony:

{
    "listeners": {
        "*:8300": {
            "application": "api"
        }
    },
    "applications": {
        "api": {
            "type": "php",
            "workers": 20,
            "root": "/usr/src/app/web",
            "index": "app.php",
            "script": "app.php"
        }
    }
}

on debian, the php.ini is located at /etc/php/7.0/embed/php.ini

Would love to see a official list of usage examples of Nginx Unit submitted by users especially for python and go apps which I am not that familiar with.

Example is also needed for recently-added Perl support.

Thanks!

@rpv-tomsk there's one in the blog post: https://www.nginx.com/blog/unit-0-6-beta-release-available-now/

@defanator @maxired
when I use these command, it doesn't work:

# yum install unit-devel 
# go build -o /tmp/go-app /usr/share/doc/unit/examples/go-app/let-my-people.go
# sudo service unitd restoreconfig /usr/share/doc/unit/examples/example.config

```json
{
"success": "Reconfiguration done."
}

```bash
curl http://localhost:8500/foo/bar/
Read header timeout

Golang example doesn't work in my centos7 system, can you help me?

My compiled unitd verson:

#unitd version
unit version: 0.1
configured as ./configure --prefix=/usr --modules=/usr/lib64/unit/modules --control=unix:/var/run/control.unit.sock --pid=/var/run/unitd.pid --log=/var/log/unitd.log --debug --tests

My problem is: Unit binary version and Unit source code version is not same, details in here: https://github.com/nginx/unit/issues/126.

@defanator - would it be possible to get a POST example, ideally in Perl? thanks.

@dac88

should work like that:

app.psgi

package APP;

use 5.24.0;
use strict;
use warnings;

use Data::Dumper;

my $app = sub {

  my $env = shift;

  warn( Dumper($env) );

  my $method = $env->{'REQUEST_METHOD'};
  my $uri    = $env->{'REQUEST_URI'};

  my $input;
  if ( $method eq 'POST' ) {
    $env->{'psgi.input'}->read( $input, 5000 );
  }

  # POST -> /ping
  if ( $method.$uri eq 'POST/ping' ) {
    return [
      '200',
      [ 'Content-Type' => 'text/plain' ],
      [ "Hello World!\nPOST:\n" . $input . "\nENV:\n" . Dumper($env) ],
    ];
  }

};

1;

@sysdef - much appreciated.

Asimple php/html page :

<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="/index.css">
<?php echo "Hello, "; ?>
<span class="red">app1</span>
<pre><?php print_r($_SERVER); ?>
</pre>

this kind of php code dont work. cuse 'index.css' is fetched as
Content-type:text/html
so the browser dont apply it.
I do something wrong, or this is normal fot unit ???

----- index.css:
.red{ color:red;}

@gkapad It's not related to php code. Similar to php-fpm, Unit doesn't serve static files. Currently, you need to put static files to CDN or use nginx to serve them.

Static file support is available since v1.11.0. See documentation for configuration and a lot of examples.

@VBart: could be closed, right?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hongzhidao picture hongzhidao  路  3Comments

yujinqiu picture yujinqiu  路  5Comments

xiongxin picture xiongxin  路  5Comments

hongzhidao picture hongzhidao  路  4Comments

Madhu1512 picture Madhu1512  路  4Comments