openresty安裝與簡單示例

openresty安裝與簡單示例

@author 0597agentzhu 2016/6/10

OpenResty是可以說是Nginx的一個擴(kuò)展軟件集合,它將Lua的簡單、輕巧嵌入到nginx中。本文著重介紹openresty在ubuntu中的安裝以及一個簡單的示例。

安裝openresty[1]

安裝的環(huán)境是ubuntu14.04,64位。

  1. 下載最新版的openresty

首先我們在主目錄下創(chuàng)建一個tool目錄,用于存放openresty壓縮包。同時在tool目錄下創(chuàng)建一個用于存放第三方模塊的目錄,openresty-bundle。接下來就在openresty的官方網(wǎng)站下載最新版的openresty,我們下載的版本為openresty-1.9.15.1:

wget https://openresty.org/download/openresty-1.9.15.1.tar.gz

下載完畢之后進(jìn)行解壓: tar xvf openresty-1.9.15.1,然后進(jìn)入openresty-1.9.15.1目錄:cd openresty-1.9.15.1

  1. 安裝相關(guān)軟件包

sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl

  1. 置與安裝
  • 配置
./configure --prefix=/opt/openresty \
--with-luajit 
--with-http_iconv_module \
--with-http_postgres_module \
--with-pcre-jit \
--with-debug \
--add-module=/home/mike/tools/openresty-bundle/ngx_http_auth_request_module \
-j2
其中的相關(guān)參數(shù)說明如下:

```
--with-*** 安裝一些內(nèi)置的模塊
--add-module 添加自定義的第三方模塊
```

更多的配置參數(shù)可以使用命令: configure --help 查看。需要說明的是我們在配置的出現(xiàn)了某些錯誤,如下:

Error: You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.

我們采取如下的步驟來解決這個問題步驟如下:

  • 看看自己的主機(jī)環(huán)境是否安裝了postgresql

dpkg -l | grep postgres

我們的主機(jī)中是安裝了postgres,但是依舊報錯,我想是不是版本太低了,所以我們把系統(tǒng)的中postgresql卸載[2]:

sudo apt-get --purge remove postgresql postgresql-9.3 postgresql-client postgresql-client-9.3 postgresql-client-common postgresql-common postgresql-contrib postgresql-contrib-9.3
并且刪除相關(guān)的目錄:

sudo rm -rf /var/lib/postgresql/
sudo rm -rf /var/log/postgresql/
sudo rm -rf /etc/postgresql/

然后在安裝一個新版本[3],具體步驟如下:

  • 添加postgresql源

創(chuàng)建一個 /etc/apt/sources.list.d/pgdg.list文件,并且在文件中添加如下內(nèi)容:

`deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main`
  • 導(dǎo)入簽名密鑰以及更新包列表
```
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
sudo apt-key add -
sudo apt-get update
```

但是有出現(xiàn)如下問題:

Looks like you are on Linux. Try installing libpq-dev for Debian/Ubuntu, or postgresql-devel for RHEL systems.

然后我們按照提示安裝了 libq-dev, sudo apt-get install libpq-dev

  • 編譯與安裝

    make&&sudo make install

  • 配置一些環(huán)境變量

    我們是在.bashrc文件中配置了相關(guān)的環(huán)境變量

    export PATH=/opt/openresty/bin:/opt/openresty/nginx/sbin:$PATH

簡單示例[4]

首先創(chuàng)建工作目錄:nginx_work,使用命令:mkdir ~/nginx_work,然后進(jìn)入nginx_work創(chuàng)建兩個子文件夾,logsconf。其中,logs用于存放日志跟蹤文件;conf用于存放配置文件。

下面在conf中創(chuàng)建一個nginx.conf,用于配置nginx。具體內(nèi)容如下:

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

接著就是使用我們的配置文件啟動nginx服務(wù),命令如下:

cd ~/nginx_work/
nginx -p `pwd`/ -c conf/nginx.conf

這條命令一旦啟動,就會把錯誤信息送往stderr 設(shè)備或者當(dāng)前工作目錄下的logs/error.log文件中。

之后,就可以使用curl命令進(jìn)行測試或直接在瀏覽器中訪問我們的 hello_testweb服務(wù)了。

mike@mike-X450JF:~/nginx_work$ curl http://localhost:8080/hello_test
<p>hello, world</p>

注明:本文遵守MIT版權(quán)



  1. openresty安裝: https://openresty.org/cn/installation.html ?

  2. 卸載postgres: http://askubuntu.com/questions/32730/how-to-remove-postgres-from-my-installation ?

  3. 安裝postgresl: https://www.postgresql.org/download/linux/ubuntu/ ?

  4. 測試示例: http://openresty.org/en/getting-started.html ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容