1.nginx服务器部署,1.nginx部署
1.nginx服务器部署,1.nginx部署
1。部署第一个nginx,作为应用层nginx因为我们要用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty
nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件
(1)部署openresty(我在111.230.234.30上弄)
安装需要的环境
yum install -y readline-devel pcre-devel openssl-devel gcc
创建目录
mkdir -p /usr/servers
cd /usr/servers/
下载相关包并解压
wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar -xzvf ngx_openresty-1.7.7.2.tar.gz
rm -rf ngx_openresty-1.7.7.2.tar.gz
cd /usr/servers/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120/
make clean && make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit 强行创建软链接,-s为创建软链接参数,-f强制创建
cd /usr/servers/ngx_openresty-1.7.7.2/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0.tar.gz
rm -rf 2.3.tar.gz
rm -rf v0.3.0.tar.gz
cd /usr/servers/ngx_openresty-1.7.7.2
./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
make && make install
查看nginx版本
/usr/servers/nginx/sbin/nginx -V
启动nginx:
/usr/servers/nginx/sbin/nginx
(2)nginx+lua开发的hello world
编辑/usr/servers/nginx/conf/nginx.conf文件
vi /usr/servers/nginx/conf/nginx.conf
在http部分添加:
lua_package_path "/usr/servers/lualib/?.lua;;";
lua_package_cpath "/usr/servers/lualib/?.so;;";
在/usr/servers/nginx/conf下,创建一个lua.conf,写入以下内容
vi /usr/servers/nginx/conf/lua.conf
server {
listen 80;
server_name _;
}
在nginx.conf的http部分添加:
include lua.conf;
验证配置是否正确:
/usr/servers/nginx/sbin/nginx -t
成功显示:
nginx: the configuration file /usr/servers/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/servers/nginx/conf/nginx.conf test is successful
失败显示:
nginx: [emerg] invalid number of arguments in "lua_package_cpath" directive in /usr/servers/nginx/conf/nginx.conf:20
nginx: configuration file /usr/servers/nginx/conf/nginx.conf test failed
在lua.conf的server部分添加:
vi /usr/servers/nginx/conf/lua.conf
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")'; 直接简单测试输出hello world
}
验证配置是否正确:
/usr/servers/nginx/sbin/nginx -t
重新nginx加载配置
/usr/servers/nginx/sbin/nginx -s reload
测试nginx:
访问http: http://111.230.234.30/lua
访问http: http://139.199.6.253/lua
访问http: http://111.230.234.30/lua
在/usr/servers/nginx/conf/lua下创建test.lua,写入内容
mkdir /usr/servers/nginx/conf/lua
vi /usr/servers/nginx/conf/lua/test.lua
ngx.say("hello world"); 写入规则内容
修改/usr/servers/nginx/conf/lua.conf
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; 导入文件的方式
}
重新nginx加载配置
/usr/servers/nginx/sbin/nginx -s reload
查看异常日志
tail -f /usr/servers/nginx/logs/error.log
(3)工程化的nginx+lua项目结构,example为自己定义的名字
项目工程结构
hello
hello.conf
lua
hello.lua
lualib
*.lua
*.so
mkdir /usr/example
mkdir /usr/example/lua
cp -r /usr/servers/lualib /usr/hello/
vi /usr/servers/nginx/conf/nginx.conf
在http部分加入
lua_package_path "/usr/example/lualib/?.lua;;";
lua_package_cpath "/usr/example/lualib/?.so;;";
include /usr/example/example.conf;
vi /usr/example/example.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/example/lua/test.lua;
}
}
vi /usr/example/lua/test.lua
编写自己的lua脚本
重新nginx加载配置
/usr/servers/nginx/sbin/nginx -s reload
评论暂时关闭