Centos7 下 Openresty 從安裝到入門

官網安裝說明

根據官網的描述提供了不同的安裝方式,例如:使用yum安裝、源碼編譯安裝等等。我目前暫時使用yum安裝方式進行部署看看。

設置安裝的yum源

你可以在你的 CentOS 系統中添加 openresty 倉庫,這樣就可以便於未來安裝或更新我們的軟件包(通過 yum update 命令)。運行下面的命令就可以添加我們的倉庫:

sudo yum install yum-utils -y
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

根據上面的命令,先執行看看,如下:

安裝Openresty

然後就可以像下面這樣安裝軟件包,比如 openresty

sudo yum install openresty -y

安裝命令行工具 resty

安裝 openresty-resty 包:

sudo yum install openresty-resty -y

安裝之後,就可以查看一下版本號,如下:

[root@centos7 ~]# resty -V
resty 0.21
nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.1.0h  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl/include' --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.13 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.5 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-dtrace-probes --with-stream --with-stream_ssl_module --with-http_ssl_module
[root@centos7 ~]# 

查看Opm工具

命令行工具 opmopenresty-opm 包裏,而 restydoc 工具在 openresty-doc 包裏頭。

列出所有 openresty 倉庫裏頭的軟件包:

sudo yum --disablerepo="*" --enablerepo="openresty" list available

從上圖可以看出,還有很多工具包可以安裝。但是目前,我先不安裝了。等到需要的時候,再進行安裝。

入門

在上面已經安裝好了Openresty之後,下面可以部署一個Hello world的示例。

創建相關工作目錄

創建work目錄以及相應的log日誌目錄、conf配置目錄

mkdir ~/work
cd ~/work
mkdir logs/ conf/

配置命令:

[root@centos7 ~]# cd ~
[root@centos7 ~]# 
[root@centos7 ~]# mkdir work
[root@centos7 ~]# 
[root@centos7 ~]# cd work/
[root@centos7 work]# ls
[root@centos7 work]# mkdir logs
[root@centos7 work]# 
[root@centos7 work]# mkdir conf
[root@centos7 work]# 
[root@centos7 work]# ls
conf  logs
[root@centos7 work]# pwd
/root/work
[root@centos7 work]# 

創建logs/用於記錄文件和conf/配置文件的目錄。

準備nginx.conf配置文件

創建一個簡單的純文本文件,conf/nginx.conf其中包含以下內容:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

如果您熟悉Nginx配置,那麼您應該非常熟悉它。無論如何,OpenResty只是一個增強版的 Nginx。您可以充分利用Nginx世界中所有現有的好東西。

啓動Nginx服務器

假設你已經安裝了OpenResty/usr/local/openresty(這是默認值),我們使我們的nginx我們的可執行OpenResty我們可用的安裝PATH環境:

PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH

執行步驟如下

[root@centos7 conf]# ls /usr/local/openresty
bin  COPYRIGHT  luajit  lualib  nginx  openssl  pcre  site  zlib
[root@centos7 conf]# 
[root@centos7 conf]# ls /usr/local/openresty/nginx/
conf  html  logs  sbin  tapset
[root@centos7 conf]# ls /usr/local/openresty/nginx/sbin/
nginx  stap-nginx
[root@centos7 conf]# 
[root@centos7 conf]# PATH=/usr/local/openresty/nginx/sbin:$PATH
[root@centos7 conf]# export PATH
[root@centos7 conf]# env | grep open
PATH=/usr/local/openresty/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 conf]# 
[root@centos7 conf]# nginx -V
nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.1.0h  27 Mar 2018

然後我們以這種方式用我們的配置文件啓動nginx服務器:

[root@centos7 conf]# cd ..
[root@centos7 work]# ls
conf  logs
[root@centos7 work]# pwd
/root/work
[root@centos7 work]# 
[root@centos7 work]# nginx -p `pwd`/ -c conf/nginx.conf 
[root@centos7 work]# ps -ef | grep nginx
root       2199   2181  0 14:57 ?        00:00:00 nginx: master process nginx -g daemon off;
104        2227   2199  0 14:57 ?        00:00:00 nginx: worker process
root       2489      1  0 15:41 ?        00:00:00 nginx: master process nginx -p /root/work/ -c conf/nginx.conf
nobody     2490   2489  0 15:41 ?        00:00:00 nginx: worker process
root       2504   1933  0 15:42 pts/0    00:00:00 grep --color=auto nginx
[root@centos7 work]# 

錯誤消息將轉到stderr設備或logs/error.log(當前工作目錄中的默認錯誤日誌文件)。

訪問我們的HelloWorld Web服務

我們可以使用curl訪問我們的HelloWorld新Web服務:

[root@centos7 work]# curl http://localhost:8080/

<p>hello, world</p>
[root@centos7 work]# 

如果一切正常,我們應該得到輸出

<p>hello, world</p>

文章轉載至:https://cloud.tencent.com/developer/article/1439324

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