前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos7 下 Openresty 从安装到入门

Centos7 下 Openresty 从安装到入门

作者头像
Devops海洋的渔夫
发布2019-05-30 21:49:59
5.7K0
发布2019-05-30 21:49:59
举报
文章被收录于专栏:Devops专栏

官网安装说明

根据官网的描述提供了不同的安装方式,例如:使用yum安装、源码编译安装等等。我目前暂时使用yum安装方式进行部署看看。

设置安装的yum源

你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令)。运行下面的命令就可以添加我们的仓库:

代码语言:javascript
复制
sudo yum install yum-utils -y
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

根据上面的命令,先执行看看,如下:

安装Openresty

然后就可以像下面这样安装软件包,比如 openresty

代码语言:javascript
复制
sudo yum install openresty -y

安装命令行工具 resty

安装 openresty-resty 包:

代码语言:javascript
复制
sudo yum install openresty-resty -y

安装之后,就可以查看一下版本号,如下:

代码语言:javascript
复制
[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 仓库里头的软件包:

代码语言:javascript
复制
sudo yum --disablerepo="*" --enablerepo="openresty" list available

从上图可以看出,还有很多工具包可以安装。但是目前,我先不安装了。等到需要的时候,再进行安装。

入门

在上面已经安装好了Openresty之后,下面可以部署一个Hello world的示例。

创建相关工作目录

创建work目录以及相应的log日志目录、conf配置目录

代码语言:javascript
复制
mkdir ~/work
cd ~/work
mkdir logs/ conf/

配置命令:

代码语言:javascript
复制
[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其中包含以下内容:

代码语言:javascript
复制
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环境:

代码语言:javascript
复制
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH

执行步骤如下

代码语言:javascript
复制
[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服务器:

代码语言:javascript
复制
[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服务:

代码语言:javascript
复制
[root@centos7 work]# curl http://localhost:8080/

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

如果一切正常,我们应该得到输出

代码语言:javascript
复制
<p>hello, world</p>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.05.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 官网安装说明
  • 设置安装的yum源
    • 安装Openresty
      • 安装命令行工具 resty
        • 查看Opm工具
        • 入门
          • 创建相关工作目录
            • 准备nginx.conf配置文件
              • 启动Nginx服务器
                • 访问我们的HelloWorld Web服务
                相关产品与服务
                命令行工具
                腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档