nginx unit的初探


安裝介紹:

https://www.oschina.net/p/nginx-unit

可以看到,unit還是很強大的,居然特么都支持go 還有python 

 

在/etc/yum.repos.d/unit.repo加入如下語句:

# cat /etc/yum.repos.d/unit.repo 
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# 

makecache yum源

# yum makecache

執行安裝 由於要測試unit-php,故安裝unit-php ,如果要測試python,可以安裝unit-python,如果要測試go,則安裝unit-go

# yum install unit unit-php

這里有坑,安裝unit-php的時候,默認就安裝了5.3的

所以,如果你自己安裝了php7的,調用phpinfo的時候,會顯示php 5.3 ,原因在此,這個得需要源碼安裝,有點坑,過幾天再去踩踩

編輯配置文件

編輯配置文件,內容如下:

# cat /etc/unit/start.json 
{
    "listeners": {
        "*:8300": {
            "application": "blogs"
        }
    },

    "applications": {
        "blogs": {
            "type": "php",
            "processes": 20,
            "root": "/usr/local/nginx/php",
            "index": "index.php"
        }
    }
}
# 

啟動unit並且加載json文件

# /etc/init.d/unit start

# /usr/local/bin/curl -X PUT -d @/etc/unit/start.json --unix-socket /var/run/control.unit.sock http://localhost
{
"success": "Reconfiguration done."
}

當出現success": "Reconfiguration done.則怎么加載成功,可以用netstat查看端口是否已經開起來

注意,這里有坑,如果要用curl的unix-socket參數,若curl包低於7.4需要進行升級

curl下載地址:https://curl.haxx.se/download/

 

例如,如下安裝方法:

# wget https://curl.haxx.se/download/curl-7.55.0.tar.gz
...解壓,進入目錄...
# ./configure 
# make
# make install
如果在編譯的時候不指定安裝目錄,則會安裝到/usr/local/bin下面
查看版本
# /usr/local/bin/curl --version
curl 7.55.0 (x86_64-pc-linux-gnu) libcurl/7.55.0 OpenSSL/1.0.1e zlib/1.2.3
Release-Date: 2017-08-09
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets HTTPS-proxy 
# 

查看加載的配置

# /usr/local/bin/curl --unix-socket /var/run/control.unit.sock http://localhost
{
    "listeners": {
        "*:8300": {
            "application": "blogs"
        }
    },

    "applications": {
        "blogs": {
            "type": "php",
            "processes": 20,
            "root": "/usr/local/nginx/php",
            "index": "index.php"
        }
    }
}
# 

這時候,通過netstat可以看到8300端口已經打開了,

# cat /usr/local/nginx/php/index.php 
<?php
    phpinfo();
?>
# 

通過8300訪問,就可以看看到phpinfo的界面了,但是前提是,root下,必須有index.php喲

重新加載配置文件

重新編寫配置文件

# cat /etc/unit/start.json
{
    "listeners": {
        "*:8300": {
            "application": "blogs"
        }
    },

    "applications": {
        "blogs": {
            "type": "php",
            "processes": 20,
            "root": "/usr/local/nginx/php",
            "index": "index.php",

        "user": "php",
        "group": "php",

        "options": {
            "file": "/usr/local/php7/lib/php.ini"
        }
        }
    }
}
#

重新加載配置文件

# /usr/local/bin/curl -X PUT -d @/etc/unit/start.json --unix-socket /var/run/control.unit.sock http://localhost
{
"success": "Reconfiguration done."
}

再次查看配置文件

# /usr/local/bin/curl --unix-socket /var/run/control.unit.sock http://localhost
{
    "listeners": {
        "*:8300": {
            "application": "blogs"
        }
    },

    "applications": {
        "blogs": {
            "type": "php",
            "processes": 20,
            "root": "/usr/local/nginx/php",
            "index": "index.php",
            "user": "php",
            "group": "php",
            "options": {
                "file": "/usr/local/php7/lib/php.ini"
            }
        }
    }
}
# 

這時候,可以通過頁面,訪問如下信息:

 

 

Nginx proxy_pass設置

只需要在location中添加如下幾項即可

proxy_pass http://127.0.0.1:8300;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

檢查語法,重啟nginx

# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx -s reload
# 

 

通過Nginx訪問界面如下:

 

可以看到,Server API 是unit

總結:

  0.參考網址:http://unit.nginx.org/

  1.unit 目前用的還很少,主流依然是php-fpm

  2.如果能夠采用源碼編譯unit的話,當然是最好的

  3.curl版本問題

  4.編寫的json文件格式一定要正確,可以用在線工具檢測json是否正確(http://www.bejson.com/

 

興趣才是最好的老師,加油吧。。。這玩意快搞了我加起來兩天時間了,

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM