• OpenResty简单入门


    OpenResty 介绍

    OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。

    OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。

    360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。


    OpenResty 安装

    菜鸟教程是居于 ubuntu14.04 安装 OpenResty。

    OpenResty 依赖库有: perl 5.6.1+, libreadline, libpcre, libssl。

    所以我们需要先安装好这些依赖库,也非常简单:

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

    如果你的系统是 Centos 或 RedHat 可以使用以下命令:

    [root@iZbp143t3oxhfc3ar7jey0Z ~]# yum install readline-devel pcre-devel openssl-devel
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    base                                                                                                                                         | 3.6 kB  00:00:00
    epel                                                                                                                                         | 5.4 kB  00:00:00
    extras                                                                                                                                       | 2.9 kB  00:00:00
    updates                                                                                                                                      | 2.9 kB  00:00:00
    (1/3): epel/x86_64/updateinfo                                                                                                                | 1.0 MB  00:00:00
    (2/3): updates/7/x86_64/primary_db                                                                                                           | 6.7 MB  00:00:00
    (3/3): epel/x86_64/primary_db                                                                                                                | 6.7 MB  00:00:00
    Resolving Dependencies
    --> Running transaction check
    ---> Package openssl-devel.x86_64 1:1.0.2k-19.el7 will be installed
    --> Processing Dependency: zlib-devel(x86-64) for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
    --> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
    ---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed 

    接下我们可以在官方(https://openresty.org/cn/)下载最新的 OpenResty 源码包并解压编译安装:

    [root@iZbp143t3oxhfc3ar7jey0Z ~]# wget https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz
    --2020-02-21 19:37:46--  https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz
    Resolving openresty.org (openresty.org)... 120.26.162.249
    Connecting to openresty.org (openresty.org)|120.26.162.249|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 3548444 (3.4M) [application/x-gzip]
    Saving to: ‘ngx_openresty-1.9.7.1.tar.gz’
    
    100%[==========================================================================================================================>] 3,548,444   --.-K/s   in 0.1s
    
    2020-02-21 19:37:47 (27.6 MB/s) - ‘ngx_openresty-1.9.7.1.tar.gz’ saved [3548444/3548444]
    
    [root@iZbp143t3oxhfc3ar7jey0Z ~]# ll
    total 310692
    -rw-r--r-- 1 root root        28 Feb 20 21:50 aaa.text
    drwxr-xr-x 2 root root      4096 Feb 17 21:39 mysql-5.6.35-linux-glibc2.5-x86_64
    -rw-r--r-- 1 root root 314581668 Feb 17 21:38 mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
    -rw-r--r-- 1 root root   3548444 Dec 25  2015 ngx_openresty-1.9.7.1.tar.gz
    [root@iZbp143t3oxhfc3ar7jey0Z ~]# ll -h
    total 304M
    -rw-r--r-- 1 root root   28 Feb 20 21:50 aaa.text
    drwxr-xr-x 2 root root 4.0K Feb 17 21:39 mysql-5.6.35-linux-glibc2.5-x86_64
    -rw-r--r-- 1 root root 301M Feb 17 21:38 mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
    -rw-r--r-- 1 root root 3.4M Dec 25  2015 ngx_openresty-1.9.7.1.tar.gz
    [root@iZbp143t3oxhfc3ar7jey0Z ~]# tar -zvxf ngx_openresty-1.9.7.1.tar.gz
    ngx_openresty-1.9.7.1/
    ngx_openresty-1.9.7.1/README.markdown
    ngx_openresty-1.9.7.1/configure
    ngx_openresty-1.9.7.1/util/
    cd ngx_openresty-1.9.7.1/ 
    ./configure
    make 
    make install

    Hello World 实例

    安装成功后,我们就可以使用 openresty 直接输出 html 页面。

    首先我们可以创建一个工作目录:

    [root@iZbp143t3oxhfc3ar7jey0Z ngx_openresty-1.9.7.1]# cd /home/www/
    [root@iZbp143t3oxhfc3ar7jey0Z www]# mkdir logs/ conf/
    [root@iZbp143t3oxhfc3ar7jey0Z www]# ll
    total 8
    drwxr-xr-x 2 root root 4096 Feb 21 19:46 conf
    drwxr-xr-x 2 root root 4096 Feb 21 19:46 logs
    [root@iZbp143t3oxhfc3ar7jey0Z www]# cd conf/
    [root@iZbp143t3oxhfc3ar7jey0Z conf]# vim nginx.conf
    worker_processes  1;
    error_log logs/error.log;
    events {
        worker_connections 1024;
    }
    http {
        server {
            listen 9000;
            location / {
                default_type text/html;
                content_by_lua '
                    ngx.say("<p>Hello, World!</p>")
                ';
            }
        }
    }

    启动 openresty

    默认情况下 openresty 安装在 /usr/local/openresty 目录中,启动命令为:

    [root@iZbp143t3oxhfc3ar7jey0Z ngx_openresty-1.9.7.1]# cd /usr/local/openresty/nginx/sbin
    [root@iZbp143t3oxhfc3ar7jey0Z sbin]# ll
    total 11484
    -rwxr-xr-x 1 root root 11757816 Feb 21 19:45 nginx
    [root@iZbp143t3oxhfc3ar7jey0Z sbin]# cd /home/www
    [root@iZbp143t3oxhfc3ar7jey0Z www]# /usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf 

    如果没有任何输出,说明启动成功,-p 指定我们的项目目录,-c 指定配置文件。

    接下来我们可以使用 curl 来测试是否能够正常范围:

    [root@iZbp143t3oxhfc3ar7jey0Z www]# curl http://localhost:9000/
    <p>Hello, World!</p>
    [root@iZbp143t3oxhfc3ar7jey0Z www]#

  • 相关阅读:
    15 | 二分查找(上):如何用最省内存的方式实现快速查找功能?
    11 | 线程:如何让复杂的项目并行执行?
    数据结构与算法-10-递归调用
    (图文并茂,权威最详细)Wireshark抓包分析 TCP三次握手/四次挥手详解
    总结-自己傻的坑学习java spingboot不仔细
    网络抓包
    数据库简介
    JavaSE基础之Map与Collection
    JavaSE基础之抽象类与接口
    JavaSE基础之重载和重写
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12342804.html
Copyright © 2020-2023  润新知