openresty安裝與簡單示例
@author 0597agentzhu 2016/6/10
OpenResty是可以說是Nginx的一個擴(kuò)展軟件集合,它將Lua的簡單、輕巧嵌入到nginx中。本文著重介紹openresty在ubuntu中的安裝以及一個簡單的示例。
安裝openresty[1]
安裝的環(huán)境是ubuntu14.04,64位。
- 下載最新版的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。
- 安裝相關(guān)軟件包
sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl
- 置與安裝
- 配置
./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)建兩個子文件夾,logs和conf。其中,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)。