安装
centos
配置安装源
在文件夹 /etc/yum.repos.d/ 下创建 unit.repo 文件,内容如下:
[unit]
name=unit repo
baseurl=http://packages.nginx.org/unit/centos/7/$basearch/
gpgcheck=0
enabled=1
安装 unit
$ sudo yum install unit
$ sudo yum install unit-php unit-python unit-go unit-perl
ubuntu
配置 apt-key
将下面的 key 内容保存到一个文件,例如:nginx_signing.key
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)
mQENBE5OMmIBCAD+FPYKGriGGf7NqwKfWC83cBV01gabgVWQmZbMcFzeW+hMsgxH
W6iimD0RsfZ9oEbfJCPG0CRSZ7ppq5pKamYs2+EJ8Q2ysOFHHwpGrA2C8zyNAs4I
QxnZZIbETgcSwFtDun0XiqPwPZgyuXVm9PAbLZRbfBzm8wR/3SWygqZBBLdQk5TE
fDR+Eny/M1RVR4xClECONF9UBB2ejFdI1LD45APbP2hsN/piFByU1t7yK2gpFyRt
97WzGHn9MV5/TL7AmRPM4pcr3JacmtCnxXeCZ8nLqedoSuHFuhwyDnlAbu8I16O5
XRrfzhrHRJFM1JnIiGmzZi6zBvH0ItfyX6ttABEBAAG0KW5naW54IHNpZ25pbmcg
a2V5IDxzaWduaW5nLWtleUBuZ2lueC5jb20+iQE+BBMBAgAoAhsDBgsJCAcDAgYV
CAIJCgsEFgIDAQIeAQIXgAUCV2K1+AUJGB4fQQAKCRCr9b2Ce9m/YloaB/9XGrol
kocm7l/tsVjaBQCteXKuwsm4XhCuAQ6YAwA1L1UheGOG/aa2xJvrXE8X32tgcTjr
KoYoXWcdxaFjlXGTt6jV85qRguUzvMOxxSEM2Dn115etN9piPl0Zz+4rkx8+2vJG
F+eMlruPXg/zd88NvyLq5gGHEsFRBMVufYmHtNfcp4okC1klWiRIRSdp4QY1wdrN
1O+/oCTl8Bzy6hcHjLIq3aoumcLxMjtBoclc/5OTioLDwSDfVx7rWyfRhcBzVbwD
oe/PD08AoAA6fxXvWjSxy+dGhEaXoTHjkCbz/l6NxrK3JFyauDgU4K4MytsZ1HDi
MgMW8hZXxszoICTTiQEcBBABAgAGBQJOTkelAAoJEKZP1bF62zmo79oH/1XDb29S
YtWp+MTJTPFEwlWRiyRuDXy3wBd/BpwBRIWfWzMs1gnCjNjk0EVBVGa2grvy9Jtx
JKMd6l/PWXVucSt+U/+GO8rBkw14SdhqxaS2l14v6gyMeUrSbY3XfToGfwHC4sa/
Thn8X4jFaQ2XN5dAIzJGU1s5JA0tjEzUwCnmrKmyMlXZaoQVrmORGjCuH0I0aAFk
RS0UtnB9HPpxhGVbs24xXZQnZDNbUQeulFxS4uP3OLDBAeCHl+v4t/uotIad8v6J
SO93vc1evIje6lguE81HHmJn9noxPItvOvSMb2yPsE8mH4cJHRTFNSEhPW6ghmlf
Wa9ZwiVX5igxcvaIRgQQEQIABgUCTk5b0gAKCRDs8OkLLBcgg1G+AKCnacLb/+W6
cflirUIExgZdUJqoogCeNPVwXiHEIVqithAM1pdY/gcaQZmIRgQQEQIABgUCTk5f
YQAKCRCpN2E5pSTFPnNWAJ9gUozyiS+9jf2rJvqmJSeWuCgVRwCcCUFhXRCpQO2Y
Va3l3WuB+rgKjsQ=
=EWWI
-----END PGP PUBLIC KEY BLOCK-----
将这个 key 添加到 apt
$ sudo apt-key add nginx_signing.key
配置安装源
在文件夹 /etc/apt/sources.list.d/ 下创建 unit.list 文件
Ubuntu v16.04 填写下面内容:
deb https://packages.nginx.org/unit/ubuntu/ xenial unit
deb-src https://packages.nginx.org/unit/ubuntu/ xenial unit
Ubuntu v17.10 填写下面内容:
deb https://packages.nginx.org/unit/ubuntu/ artful unit
deb-src https://packages.nginx.org/unit/ubuntu/ artful unit
更新源
$ sudo apt-get update
安装 unit
$ sudo apt-get install unit
Ubuntu v16.04 执行下面的命令:
$ sudo apt-get install unit-php unit-python2.7 unit-python3.5 unit-go unit-perl unit-ruby
Ubuntu v17.10 执行下面的命令:
$ sudo apt-get install unit-php unit-python2.7 unit-python3.6 unit-go1.8 unit-go1.9 unit-perl unit-ruby
查看版本号
$ unitd --version
此时会显示出 --control 值,可用于后面的通过 unix-socket 进行配置和配置项查看
启动与停止 unit
$ sudo service unit start
$ sudo service unit stop
配置应用
在 /etc/unit/ 目录下新建一个 json 格式的配置文件,可以配置多个应用
{
"listeners": {
"*:8100": {
"application": "php_app"
},
"127.0.0.1:8101": {
"application": "python_app"
},
"*:8102": {
"application": "go_app"
}
},
"applications": {
"php_app": {
"type": "php",
"processes": 20,
"root": "/home/yangyuqi/work/test_yyq_frame",
"index": "test.php"
},
"python_app": {
"type": "python",
"processes": 5,
"user": "yangyuqi",
"group": "yangyuqi",
"path": "/home/yangyuqi/work/unit_python_web",
"module": "test"
},
"go_app": {
"type": "go",
"processes": 8,
"working_directory": "/home/yangyuqi/work/unit_go_web",
"executable": "test"
}
}
}
配置完成,重新 load
$ sudo service unit loadconfig
如果还不生效,那么重启 unit 服务
$ sudo service unit restart
可以通过命令行查看配置
$ sudo curl --unix-socket /xxxxxxxxxxxx/control.unit.sock http://localhost/config/
其中的 unix-socket 值需要填写 unitd --version 显示出的 --control 的值
unit-php
不再需要 php-fpm,可以关闭 php-fpm 服务后尝试访问,会看到依然可以访问。
$ sudo service php7.0-fpm stop
每次改完文件不需要重启 unit 就能生效,代码最简单,更新最快捷
unit-python
需要 bottle
$ sudo pip install bottle
编写 test.py 文件
$ cd /home/yangyuqi/work/unit_python_web
$ vim test.py
from bottle import Bottle
app = Bottle()
@app.get('/')
def index():
return 'unit python OK'
application = app
每次改完文件需要重启 unit 才能生效
$ sudo service unit restart
unit-go
需要 nginx/unit,需要下载和编译
$ git clone https://github.com/nginx/unit.git $GOPATH/src/github.com/nginx/unit
$ cd $GOPATH/src/github.com/nginx/unit
$ git checkout tags/1.1 注意这个版本号必须跟安装的 unit 版本号一致,可以使用 unitd --version 查看
$ ./configure
$ ./configure go
$ make go-install
编写 test.go
$ cd /home/yangyuqi/work/unit_go_web
$ vim test.go
package main
import (
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("unit go OK"))
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe(":8089", nil)
}
编译 test 模块
$ cd /home/yangyuqi/work/unit_go_web
$ go build -o test test.go
每次编译完,需要重启 unit 才能生效
$ sudo service unit restart
性能测试
每请求基准为:计算 1 到 100 的阶乘
测试命令为:/usr/local/apache2/bin/ab -n 20000 -c 1000 http://127.0.0.1:8102/;
每类 unit 都设置 processes 为 1
unit-php
Document Length: 0 bytes
Time taken for tests: 0.946 seconds
Total transferred: 2660000 bytes
HTML transferred: 0 bytes
Requests per second: 21139.93 [#/sec] (mean)
Time per request: 47.304 [ms] (mean)
unit-python
Document Length: 36 bytes
Time taken for tests: 1.091 seconds
Total transferred: 3468894 bytes
HTML transferred: 788894 bytes
Requests per second: 18325.34 [#/sec] (mean)
Time per request: 54.569 [ms] (mean)
unit-go
Document Length: 194 bytes
Time taken for tests: 1.023 seconds
Total transferred: 6228894 bytes
HTML transferred: 3948894 bytes
Requests per second: 19548.97 [#/sec] (mean)
Time per request: 51.154 [ms] (mean)
每类 unit 都设置 processes 为 8
unit-php
Document Length: 0 bytes
Time taken for tests: 1.010 seconds
Total transferred: 2660000 bytes
HTML transferred: 0 bytes
Requests per second: 19810.04 [#/sec] (mean)
Time per request: 50.479 [ms] (mean)
unit-python
Document Length: 36 bytes
Time taken for tests: 1.021 seconds
Total transferred: 3451144 bytes
HTML transferred: 771144 bytes
Requests per second: 19595.62 [#/sec] (mean)
Time per request: 51.032 [ms] (mean)
unit-go
Document Length: 194 bytes
Time taken for tests: 0.947 seconds
Total transferred: 6220000 bytes
HTML transferred: 3940000 bytes
Requests per second: 21117.52 [#/sec] (mean)
Time per request: 47.354 [ms] (mean)
Gin 性能测试
安装
$ go get -u github.com/gin-gonic/gin
安装完毕 可以检查下是否有 Gin 的包
$ ll $GOPATH
每请求基准为:计算 1 到 100 的阶乘
测试命令为:/usr/local/apache2/bin/ab -n 20000 -c 1000 http://127.0.0.1:9090/;
Document Length: 183 bytes
Time taken for tests: 0.953 seconds
Total transferred: 6088911 bytes
HTML transferred: 3728911 bytes
Requests per second: 20991.14 [#/sec] (mean)
Time per request: 47.639 [ms] (mean)
网友评论