OpenResty--------企業(yè)級入門實(shí)踐篇-創(chuàng)新互聯(lián)

前景

前篇介紹了OpenResty核心概念,優(yōu)勢與架構(gòu)等信息,進(jìn)行本篇之前建議至少觀看一遍。OpenResty--------企業(yè)級理論實(shí)踐篇
本篇將進(jìn)行OpenResty的安裝與編寫第一個應(yīng)用過程。
注:本篇采用CentOS 7(7.4)系統(tǒng)

成都創(chuàng)新互聯(lián)是一家專業(yè)從事網(wǎng)站制作、網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司。作為專業(yè)的建站公司,成都創(chuàng)新互聯(lián)依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、全網(wǎng)整合營銷推廣及網(wǎng)站設(shè)計(jì)開發(fā)服務(wù)!

環(huán)境準(zhǔn)備

系統(tǒng)信息

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)
[root@localhost ~]# uname -r
3.10.0-693.el7.x86_64

網(wǎng)絡(luò)設(shè)備信息

[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.254  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::fb69:bc45:9e19:5b06  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:be:2d:43  txqueuelen 1000  (Ethernet)
        RX packets 3197  bytes 793326 (774.7 KiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 330  bytes 59828 (58.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

關(guān)閉selinux

[root@localhost ~]# setenforce 0

清空防火墻規(guī)則

[root@localhost ~]# iptables -F

依賴準(zhǔn)備

[root@localhost ~]# yum install readline-devel -y
[root@localhost ~]# yum install pcre-devel -y
[root@localhost ~]# yum install openssl-devel -y
[root@localhost ~]# yum install perl -y

安裝過程

下載安裝包[從github中]

[root@localhost ~]# wget https://github.com/openresty/openresty/releases/download/v1.13.6.2/openresty-1.13.6.2.tar.gz
--2019-08-01 02:35:30--  https://github.com/openresty/openresty/releases/download/v1.13.6.2/openresty-1.13.6.2.tar.gz
Resolving github.com (github.com)... 13.229.188.59
Connecting to github.com (github.com)|13.229.188.59|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/480080/5565c880-1ef7-11e9-9fb6-06daa71b014c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190731%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190731T103531Z&X-Amz-Expires=300&X-Amz-Signature=cc703e1769ba14abd394befcd43e7fb26be92ba53e592da53052931ae89f6bf8&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dopenresty-1.13.6.2.tar.gz&response-content-type=application%2Foctet-stream [following]
--2019-08-01 02:35:31--  https://github-production-release-asset-2e65be.s3.amazonaws.com/480080/5565c880-1ef7-11e9-9fb6-06daa71b014c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190731%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190731T103531Z&X-Amz-Expires=300&X-Amz-Signature=cc703e1769ba14abd394befcd43e7fb26be92ba53e592da53052931ae89f6bf8&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dopenresty-1.13.6.2.tar.gz&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.36.4
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.36.4|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4635916 (4.4M) [application/octet-stream]
Saving to: ‘openresty-1.13.6.2.tar.gz’

100%[==========================================================================>] 4,635,916   2.28MB/s   in 1.9s   

2019-08-01 02:35:34 (2.28 MB/s) - ‘openresty-1.13.6.2.tar.gz’ saved [4635916/4635916]

解壓包

[root@localhost ~]# tar xf openresty-1.13.6.2.tar.gz 
[root@localhost ~]# ls -l openresty-1.13.6.2
total 96
drwxrwxr-x 43 1000 1000  4096 May 15  2018 bundle
-rwxrwxr-x  1 1000 1000 48140 May 15  2018 configure
-rw-rw-r--  1 1000 1000 22924 May 15  2018 COPYRIGHT
drwxrwxr-x  2 1000 1000   156 May 15  2018 patches
-rw-rw-r--  1 1000 1000  4689 May 15  2018 README.markdown
-rw-rw-r--  1 1000 1000  8972 May 15  2018 README-windows.txt
drwxrwxr-x  2 1000 1000    52 May 15  2018 util

進(jìn)入安裝包目錄

[root@localhost ~]# cd openresty-1.13.6.2

生成編譯與安裝文件

  • --prefix=/usr/local/openresty-1.13.6.2 指定安裝目錄為/usr/local/openresty-1.13.6.2, 默認(rèn)為/opt/openresty
  • --with-luajit 開啟luajit組件
  • --without-http_redis2_module 禁用 http_redis2_module組件
  • --with-http_iconv_module 開啟 http_iconv_module組件
[root@localhost openresty-1.13.6.2]# ./configure \ 
 --prefix=/usr/local/openresty-1.13.6.2 \ 
 --with-luajit \
 --without-http_redis2_module \
 --with-http_iconv_module

編譯

[root@localhost openresty-1.13.6.2]# gmake

安裝

[root@localhost openresty-1.13.6.2]# gmake install

創(chuàng)建軟鏈接

[root@localhost openresty-1.13.6.2]# ln -s /usr/local/openresty-1.13.6.2 /usr/local/openresty

服務(wù)啟動測試

自帶默認(rèn)配置文件,默認(rèn)服務(wù)監(jiān)聽80端口

[root@localhost openresty-1.13.6.2]# cat /usr/local/openresty/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

使用默認(rèn)的配置文件啟動

[root@localhost openresty-1.13.6.2]# /usr/local/openresty/bin/openresty -p /usr/local/openresty/nginx/

通過瀏覽器訪問測試[測試虛擬機(jī)IP為:192.168.0.254]

OpenResty--------企業(yè)級入門實(shí)踐篇

訪問成功,OpenResty已經(jīng)成功返回頁面。

編寫程序

切換至用戶家目錄

[root@localhost openresty-1.13.6.2]# cd ~

創(chuàng)建工作目錄

[root@localhost ~]# mkdir -vp  openresty-test01/{logs,conf}
mkdir: created directory ‘openresty-test01’
mkdir: created directory ‘openresty-test01/logs’
mkdir: created directory ‘openresty-test01/conf’

[root@localhost ~]# tree openresty-test01/
openresty-test01/
├── conf
└── logs

2 directories, 0 files

編寫配置文件

注意: 如果你安裝的是 openresty 1.9.3.1 及以下版本,請使用 content_by_lua 命令代替示例中的 content_by_lua_block??墒褂?nginx/openresty -V 命令查看版本號。

[root@localhost ~]# cat openresty-test01/conf/nginx.conf
worker_processes 1; # 設(shè)置worker數(shù)量
error_log logs/error.log; # 指定錯誤日志文件路徑
events {
    worker_connections 1024; # 單個worker進(jìn)程大允許同時建立外部連接的數(shù)量
}

http {
    server {
        listen 8888; # 設(shè)置監(jiān)聽端口, 注意系統(tǒng)其它服務(wù)是否已占用該端口

        location / {
            # lua 代碼塊
            content_by_lua_block {
                ngx.say("this is lisea no.1 openresty app")
            }
        }
    }
}

運(yùn)行

[root@localhost ~]# /usr/local/openresty/bin/openresty -p openresty-test01

無報錯就表示運(yùn)行成功

通過命令測試

[root@localhost ~]# curl 127.0.0.1:8888
this is lisea no.1 openresty app

訪問成功,OpenResty已經(jīng)成功返回內(nèi)容。

通過瀏覽器測試[測試虛擬機(jī)IP為:192.168.0.254, 端口為配置文件中監(jiān)聽設(shè)置的:8888]

OpenResty--------企業(yè)級入門實(shí)踐篇

訪問成功,OpenResty已經(jīng)成功返回內(nèi)容。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞名稱:OpenResty--------企業(yè)級入門實(shí)踐篇-創(chuàng)新互聯(lián)
本文鏈接:http://bm7419.com/article42/igeec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站建設(shè)、小程序開發(fā)網(wǎng)站導(dǎo)航、Google、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司