Install Nginx+lua development Environment _lua

Source: Internet
Author: User
Tags lua redis

First we choose to use Openresty, which consists of a nginx core plus a number of third-party modules, the biggest bright spot is the default integration of the LUA development environment, so that nginx can be used as a Web server. With Nginx event-driven models and non-blocking IO, high-performance Web applications can be implemented. And Openresty provides a large number of components such as MySQL, Redis, memcached, and so on, making it easier and simpler to develop Web applications on Nginx. At present, the true price in Jingdong, seconds kill, dynamic services, single product page, list page, etc. are using Nginx+lua architecture, other companies such as Taobao, where to go to the network.

Installation Environment

Installation steps can refer to http://openresty.org/.

1, create the directory/usr/servers, we will install all the software in this directory

Copy Code code as follows:

Mkdir-p/usr/servers
cd/usr/servers/

2, installation dependencies (my environment is Ubuntu, you can use the following command to install, others can refer to openresty installation procedures)

Copy Code code as follows:

Apt-get Install Libreadline-dev libncurses5-dev libpcre3-dev Libssl-dev perl

3, download ngx_openresty-1.7.7.2.tar.gz and decompression

Copy Code code as follows:

wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
TAR-XZVF ngx_openresty-1.7.7.2.tar.gz

The Ngx_openresty-1.7.7.2/bundle directory contains the Nginx core and many third-party modules, such as the LUA and Luajit we need.

3. Installation Luajit

Copy Code code as follows:

CD bundle/luajit-2.1-20150120/
Make && make && make install
LN-SF Luajit-2.1.0-alpha/usr/local/bin/luajit

4, download the Ngx_cache_purge module , which is used to clean the Nginx cache

Copy Code code as follows:

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

5, download Nginx_upstream_check_module module , this module is used for Ustream health check

Copy Code code as follows:

Cd/usr/servers/ngx_openresty-1.7.7.2/bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
TAR-XVF v0.3.0.tar.gz

6. Installation Ngx_openresty

Copy Code code as follows:

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

--with*** install some of the built-in/integrated modules
--with-http_realip_module User Real IP module
-with-pcre Perl compatible up-style modules
--with-luajit Integrated Luajit Module

--add-module Add custom third party modules, such as this Ngx_che_purge

7, to the/usr/servers directory

Copy Code code as follows:

cd/usr/servers/
ll

You'll find out more. The following directories indicate successful installation
/usr/servers/luajit:luajit environment, Luajit similar to Java JIT, Just-in-time compilation, Lua is an interpretation language, through Luajit can immediately compile LUA code to machine code, get good performance;
/usr/servers/lualib: The LUA library to use, which provides some default Lua libraries, such as the Redis,json library, or put some of your own development or third party here;
/usr/servers/nginx: Installation of Nginx;

View the Nginx version and installed modules through/USR/SERVERS/NGINX/SBIN/NGINX-V

8, start Nginx

Copy Code code as follows:

/usr/servers/nginx/sbin/nginx

The next step is to configure the Nginx+lua development environment.

Configuring the Environment

Configuration and Nginx Httpluamodule documents can be viewed http://wiki.nginx.org/HttpLuaModule.

1. Edit nginx.conf configuration file

Copy Code code as follows:

Vim/usr/servers/nginx/conf/nginx.conf

2, add the following configuration in the HTTP section

Copy Code code as follows:

#lua模块路径, between multiple ";" Delimited, wherein ";;" Indicates the default search path, and the default is to find the/usr/servers/nginx
Lua_package_path "/usr/servers/lualib/?"  LUA;; "; #lua Module
Lua_package_cpath "/usr/servers/lualib/?"  so;; "; #c模块

3, in order to facilitate the development of our/usr/servers/nginx/conf directory to create a lua.conf

Copy Code code as follows:

#lua. conf
server {
Listen 80;
server_name _;
}

4. Add include lua.conf include this file fragment in the HTTP section of nginx.conf

Copy Code code as follows:

Include lua.conf;

5, the test is normal

Copy Code code as follows:

/usr/servers/nginx/sbin/nginx-t

If the following description shows that the configuration was successful

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

HelloWorld

1. Add the following configuration to the server section in lua.conf

Copy Code code as follows:

Location/lua {
Default_type ' text/html ';
Content_by_lua ' Ngx.say ("Hello World") ';
}

2, the test configuration is correct

Copy Code code as follows:

/usr/servers/nginx/sbin/nginx-t

3, restart Nginx

Copy Code code as follows:

/usr/servers/nginx/sbin/nginx-s Reload

4, visit such as Http://192.168.1.6/lua (their own machine according to the actual situation for IP), you can see the following content

Hello World

5. Lua code files

We put the LUA code in the Nginx configuration, and as Lua's code increases, the configuration files are too long to maintain, so we should move the LUA code to the external file for storage.

Copy Code code as follows:

Vim/usr/servers/nginx/conf/lua/test.lua

Copy Code code as follows:

#添加如下内容
Ngx.say ("Hello World");

Then lua.conf changed

Copy Code code as follows:

Location/lua {
Default_type ' text/html ';
Content_by_lua_file Conf/lua/test.lua; #相对于nginx安装目录
}

Here Conf/lua/test.lua can also use absolute path/usr/servers/nginx/conf/lua/test.lua.

6, Lua_code_cache
Lua_code_cache is turned on by default, that is, caching the LUA code, which means that each LUA code change must be reload nginx before it takes effect, if it can be done at the development stage through lua_code_cache off; This does not require reload nginx each time you modify the LUA code while debugging, but the formal environment must remember to turn on caching.

Copy Code code as follows:

Location/lua {
Default_type ' text/html ';
Lua_code_cache off;
Content_by_lua_file Conf/lua/test.lua;
}

After opening reload Nginx will see the following alarm
Nginx: [Alert] lua_code_cache is off; This'll hurt performance In/usr/servers/nginx/conf/lua.conf:8

7, error log

If an error occurs during the run, do not forget to view the error log.

Copy Code code as follows:

Tail-f/usr/servers/nginx/logs/error.log

Our basic environment has been built up.

Nginx+lua Project Construction

Our Nginx LUA development files will be more and more, and we should project them, which is easy to develop. The project directory structure looks like this:
Example
example.conf---nginx configuration file for this project
LUA---Our own LUA code
Test.lua
Lualib---Lua dependent libraries/third party dependencies
*.lua
*.so

One of the benefits of putting Lualib into the project is that it can be deployed together in the future, preventing some servers from forgetting about replication dependencies and causing a lack of dependency.

We put the item in the/usr/example directory.

The/usr/servers/nginx/conf/nginx.conf configuration file is as follows (we minimized the configuration file here)

Copy Code code as follows:

#user nobody;
Worker_processes 2;
Error_log Logs/error.log;
Events {
Worker_connections 1024;
}
HTTP {
Include Mime.types;
Default_type text/html;

#lua模块路径, of which ";;" Indicates the default search path, and the default is to find the/usr/servers/nginx
Lua_package_path "/usr/example/lualib/?"  LUA;; "; #lua Module
Lua_package_cpath "/usr/example/lualib/?"  so;; "; #c模块
include/usr/example/example.conf;
}

The absolute path contains our Lua dependent libraries and Nginx project configuration files.

The/usr/example/example.conf configuration file is as follows

Copy Code code as follows:

server {
Listen 80;
server_name _;

Location/lua {
Default_type ' text/html ';
Lua_code_cache off;
Content_by_lua_file/usr/example/lua/test.lua;
}
}

Lua files We use absolute path/usr/example/lua/test.lua.

So we can throw example on the SVN.

All right, Nginx+lua. Installation configuration of the development environment we'll talk about it today, and we'll continue to explain some of the Nginx+lua development.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.