OpenResty搭建游戏服务器(一)开始

参考:
  OpenResty® - 中文官方站
  OpenResty最佳实践
  Lua | NGINX
  Nginx - 指南 | Guides - 开发者手册 - 云+社区 - 腾讯云

开发环境:
  
centos 7.6
 

、安装OpenResty

#安装前置开发库
sudo yum install pcre-devel openssl-devel gcc curl

#下载, 浏览器中查看候选版本: http://openresty.org/cn/download.html
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz

# 解压
tar -xzvf openresty-1.15.8.1.tar.gz

cd openresty-1.15.8.1/

#安装前配置, 使用 --help 查看可选参数
./configure 

#安装, 可以指定使用核心数, 如:两核 make -j2
make && sudo make install

  或,直接使用yum安装centos上的官方包

#依赖库
yum install readline-devel pcre-devel openssl-devel perl

#安装
sudo yum-config-manager --add-repo https://openresty.org/yum/cn/centos/OpenResty.repo
sudo yum install openresty

二、创建nginx的配置文件

  首先创建openresty的工作空间

#创建工作目录
mkdir ~/openresty-work

#创建日志、配置目录
mkdir ~/openresty-work/logs ~/openresty-work/conf

  创建配置文件并写入内容:

cd ~/openresty-work
vim conf/nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}

三、启动nginx

  注意:openresty是nginx的增强版本。在安装openresty时,实际已经安装了nginx。在第一步中执行 ./configure 命令时可以看到安装位置。也可以自己指定安装位置,具体的配置参数可以用 ./configure --help 查看 :

 

1、配置环境变量。定义 NGINX_HOME 并加入 PATH,使nginx全局可用

#查看环境变量
env

#查看当前用户的PATH值
env |grep PATH 

#查看当前用户sudo权限下的PATH值
sudo env |grep PATH

  添加环境变量(所有用户有效,永久):

#用root权限打开
sudo vim /etc/profile

###  ↓文件尾增加以下内容↓

#使生效
source /etc/profile
NGINX_HOME=/usr/local/openresty/nginx/sbin
PATH=$PATH:$NGINX_HOME
export NGINX_HOME PATH

(也可)在~/.bash_profile 的PATH中补充(仅当前登录用户有效,永久)。

(也可)直接用命令添加(仅当前会话有效):

export PATH=$PATH:/usr/local/openresty/nginx/sbin

2、启动 nginx, 查看效果

#查看nginx帮助
nginx -h

#方式一、直接启动,将默认使用 /usr/local/openresty/nginx/conf/nginx.conf
#注意:该默认配置文件监听的是80端口(linux普通用户只能使用1024以下的端口),应用sudo启动
sudo nginx

#方式二、用指定的 nginx.conf 配置文件启动(刚才创建的)
nginx -p `pwd`/ -c conf/nginx.conf

  nginx的其他操作:

#关闭/快速停止nginx
nginx -s stop

#关闭/正常停止nginx
nginx -s quit

#通过杀进程方式关闭nginx
ps -ef|grep nginx
kill -QUIT 进程号

#通过杀进程方式关闭所有nginx
killall nginx

#重新装载配置文件
nginx -s reload

#重启 nginx
nginx -s reload

  运行效果如下:

四、开始接入自己的lua项目

  通过上面的步骤,可以看到nginx服务已经跑起来了。
  但实际项目并不是一句 hello word 这么简单。
  我们要将读取方式从 “content_by_lua_block” 改为 “content_by_lua_file”。

1、创建入口lua文件

#在工作空间创建 app或game 项目, 目的是让 app/game、log、conf 都在工作空间下。
cd ~/openresty-work
mkdir game
vim main.lua
--lua 内容
ngx.say("<p>hello openresty!</p>")
ngx.say("<p>hello nginx!</p>")
ngx.say("<p>hello nratel!</p>")

2、修改 nginx.conf

vim ~/openresty-work/conf/nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        server_name localhost;
        location /lua {
            default_type 'text/html';
            lua_code_cache off;
            content_by_lua_file ./game/main.lua; 
        }
    }
}

  注意:配置中的 lua_code_cache off; 代表关闭lua代码缓存,即,可不重启nginx的情况下,修改lua并生效。这对调试很友好,但影响性能,故应在正式发布时 改为 lua_code_cache on;

3、重启nginx

  失败:

  

  查看 错误日志:

  failed to load external Lua file "/home/nratel/openresty-work/./game/main.lua": cannot open /home/nratel/openresty-work/./game/main.lua: Permission denied, client: 1.119.163.126, server: localhost, 

  原因是 nginx 的 worker process 用户 没有读取 main.lua 的权限

  分别看看两者的所属

  

  

  解决:修改 nginx.conf, 指定 nginx worer pricess的所属用户,即game目录的所属用户

# nginx.conf文件最上方添加一行
user nratel;

  再次查看nginx worer pricess的所属用户(从 nobody 变为了 nratel)

  

  再次重启nginx。

4、效果 

  

----------------------

声明:该测试用域名因未续费已废弃。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NRatel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值