Linux中安裝部署OpenResty應用

目錄

1.安裝OpenResty應用

2.新建項目

3.啓動Nginx


1.安裝OpenResty應用

首先我們需要在Linux中安裝OpenResty,安裝步驟參考官網:http://openresty.org/cn/installation.html。官方推薦我們使用官方預編譯包來進行安裝,因此真正的安裝步驟是參考:http://openresty.org/cn/linux-packages.html,文章中詳細列出了不同操作系統系統安裝OpenResty的步驟。

1.使用 cat /proc/version 命令查看系統正在運行得內核版本

[root@VM_0_26_centos ~]# cat /proc/version 
Linux version 3.10.0-693.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017

我的Linux系統版本爲CentOS,所以參考安裝http://openresty.org/cn/linux-packages.html中CentOS系統中安裝預編譯包步驟。

安裝完成後,我們可以看一下安裝好的OpenResty版本,我這裏是1.15.8.2版本

2.注意事項

我們在執行 sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo 這條命令時候可能會報出異常:

File "/bin/yum-config-manager", line 135
  except yum.Errors.RepoError, e:
SyntaxError: invalid syntax

這是因爲我的Linux中Python升級到了Python 3以上版本,需要修改/bin/yum-config-manager文件頭爲python2,修改後如下:

如果安裝一切順利,我們就開始部署OpenResty應用了。

2.新建項目

爲了工作目錄和OpenResty的安裝目錄不相互干擾,我們另外創建一個OpenResty的工作目錄來練習,並新創建一個nginx.conf配置文件。我們選擇在當前用戶目錄下創建/code/openresty目錄,並在該目錄下創建logs和conf子目錄,執行命令如下:

mkdir ~/code/openresty/ ~/code/openresty/logs/ ~/code/openresty/conf/

然後在conf目錄下執行 vim nginx.conf,並寫入如下代碼:

worker_processes  1;
error_log  logs/error.log  info;
events {
    worker_connections  1024;
}
http {
    default_type  application/octet-stream;
    access_log  logs/access.log;

    server {
        listen       8001;
        server_name  localhost;
        default_type text/html;

        location /test {
            content_by_lua_block {
                ngx.say("Hello World")
            }
        }
    }
}

在上述代碼中我們輸出一個Hello World

3.啓動Nginx

萬事具備只欠東風,我們啓動Nginx即可。首先看一下nginx是否已經啓動:

[root@VM_0_26_centos ~]# ps -ef | grep nginx
root      3823     1  0 17:35 ?        00:00:00 nginx: master process ./nginx -p /root/code/openresty/
nobody    3824  3823  0 17:35 ?        00:00:00 nginx: worker process
root     19450 19389  0 19:31 pts/1    00:00:00 grep --color=auto nginx

我的系統中,nginx已經啓動,我先停掉nginx。

kill -QUIT 3823

然後,切換當前目錄到OpenResty的安裝目錄:

cd /usr/local/openresty/

再切換到nginx目錄

cd nginx/sbin/

執行nginx啓動命令,並指定工作目錄爲:~/code/openresty/

[root@VM_0_26_centos sbin]# ./nginx -p ~/code/openresty/
[root@VM_0_26_centos sbin]# ps -ef | grep nginx
root     21783     1  0 19:49 ?        00:00:00 nginx: master process ./nginx -p /root/code/openresty/
nobody   21784 21783  0 19:49 ?        00:00:00 nginx: worker process
root     21800 19389  0 19:49 pts/1    00:00:00 grep --color=auto nginx

使用curl命令測試一下:curl http://localhost:8001/test -i

HTTP/1.1 200 OK
Server: openresty/1.15.8.2
Date: Tue, 07 Jan 2020 11:50:19 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

Hello World

在確定Nginx的時候,有可能出現 [emerg] unknown directive "content_by_lua_block"  異常,這是因爲content_by_lua_block階段沒有被正常識別到,有兩個原因可能引起該現象:

  • 安裝的openresty的版本過低,如果使用官方的預編譯包來安裝的話,不會是這個原因造成的
  • 啓動的 Nginx不是OpenResty自帶的Nginx,需要切換到/usr/local/openresty/目錄下啓動Nginx

Congratulation!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章