Nginx + LuaJIT + Redis compilation and configuration tutorial

Source: Internet
Author: User
Tags openssl build tmp file
Recently, the local development machine is required to simulate the server environment. The initial requirement is to set up the configuration of Nginx + Luajit + Redis combination, this blog uses the same configuration method in the host where it is located. At that time, I directly referred to my operation notes and rebuilt a set on the development machine. because these are basic system configuration information, simply

Recently, the local development machine is required to simulate the server environment. The initial requirement is to set up the configuration of Nginx + Luajit + Redis combination, this blog uses the same configuration method in the host where it is located. At that time, I directly referred to my operation notes and rebuilt a set on the development machine. because these are basic system configuration information, simply

The local development machine is required to simulate the server environment in recent work. The initial requirement is
The configuration of Nginx + Luajit + Redis combination needs to be set up, because the host of this blog is located
Is configured in the same way. At that time, I directly referred to my operation notes and re-
Build a set on the development machine, because these are basic system configuration information, simply share
And hope to get suggestions and improvement solutions from readers :)

First, describe the configuration methods and causes:

In fact, it is to select the final binary file deployment path,
I chose/opt:
Eg:
/Opt/nginx/opt/redis/opt/luajit deployment details

1. Download the source code package for compilation and Installation

Makefile and the common PREFIX variables in the script (the compiled path)
In/usr/local/, because I often update my own tools or dependent libraries (non-system command installation ),
So I chose to place it in/opt.
For example, nginx ffmpeg redis mysql luajit lua
Their configuration is
/Opt/nginx/opt/ffmpeg/opt/redis
/Opt/mysql/opt/luajit/opt/lua

For a pure library file (lib) such as boost libevent libev
Put it under/opt/libs:
/Opt/libs/boost/opt/libs/libevent/opt/libs/libev

2. specifying and updating versions: Update Software frequently. I will encounter the following problems:

Development Project A depends on redis 2.6
Development Project B depends on redis 3.0
Most other projects rely on redis 2.8
The version requirements are different, so you can do this:
/Opt/redis/2.6.0
/Opt/redis/3.0.0
/Opt/redis/2.8.0 simultaneously make a soft link under/opt/redis/
That is, default-> 2.8.0:
Cd/opt/redis
Ln-sfn 2.8.0 default Note: The ln parameter n takes effect when the folder soft link is redirected; otherwise, the link fails.

Environment Variables of Tools defined under/etc/profile
The path of the dependent library is defined in the/etc/ld. so. conf. d/file.
Basically, it is formulated as follows:
Eg:
/Opt/nginx/default
/Opt/redis/default
/Opt/libs/libev/default
/Opt/libs/boost/default
Default is actually a soft link to the folder of the specific build version.

The prerequisite description is complete. The following describes the version selection:

Nginx 1.6.2
Redis 2.8.17
LuaJIT 2.0.3

Ubuntu 14.04
The databases to be installed in advance include:
Pcre zlib openssl build-essential

1. Compile Redis 2.8.17

Mkdir-p/opt/redis/2.8.17 # define the compiled path first

Go to the source code directory

Cd redis-2.8.17 make & make install

Src/redis-server src/redis-client compiled in the source code directory
Placed in/opt/redis/2.8.17. Other auxiliary tools can be configured as needed.
(For example, redis-benchmark)

Cd/opt/redis/
Sudo ln-sfn 2.8.17 default

/Opt/redis-defined Startup Script

Mkdir init
Add the init/redis-server script content. See [gist portal] and perform the init script soft connection to/etc/init. d.
The service command looks for the script name under/etc/init. d:
Ln-sf/opt/redis/init/redis-server/etc/init. d/

2. Compile LuaJIT

Cd LuaJIT-2.0.3
Vim Makefile # modify
The content is as follows:
Export PREFIX =/opt/luajit/2.0.3
Save and exit Compilation
Make
Sudo make install
Cd/opt/luajit/
Ln-sfn 2.0.3 default # create soft link

3. Compile nginx

Cd/your_build_path/# Find a directory for compilation
1) first download the nginx third-party module
Nginx development module, directory name: ngx_devel_kit

Git clone https://github.com/simpl/ngx_devel_kit.git
Nginx lua module. The directory name is lua-nginx-modul.

Git clone https://github.com/chaoslawful/lua-nginx-module.git
The redis Interface script called by nginx lua. The directory name is lua-resty-redis.

Git clone https://github.com/agentzh/lua-resty-redis.git
The upload module script called by nginx lua. The directory name is lua-resty-upload.

Git clone https://github.com/openresty/lua-resty-upload.git
Mkdir-p/opt/nginx/1.6.2

Place the init script path. A Soft link will be made later.
Mkdir-p/opt/nginx/init

Put the pid file and read the init script to determine the status.
Mkdir-p/opt/nginx/run

In daemon mode, the init script is locked as a file to prevent repeated start processes.
Mkdir-p/opt/nginx/lock

Tmp file, nginx client buffer cache file will be generated here,
And temporary files generated by processes
Mkdir-p/opt/nginx/tmp/client
Mkdir-p/opt/nginx/tmp/proxy
Mkdir-p/opt/nginx/tmp/fcgi

Load the configuration file/opt/nginx/default/conf/nginx. conf at nginx startup
The include syntax is called to load the specified configuration file. To facilitate management and planning,
We recommend that you put your custom files in the planning directory:
Eg:
Configuration in nginx. conf: include/opt/nginx/sites-available/*. conf;

Store valid configuration files or valid
Mkdir/opt/nginx/sites-available

Store unnecessary or obsolete configurations (for backup)
Mkdir-p/opt/nginx/sites-disable

Path for storing Lua dependent scripts
Mkdir-p/opt/nginx/lua/share
Store Lua c lib
Mkdir-p/opt/nginx/lua/lib
Store the lua script of the business (written by developers)
Mkdir-p/opt/nginx/lua/app

Therefore, in the nginx conf configuration file, the http module or the server Module
Add the following configuration information (the path of the Lua dependency, for example, Lua5.1 ):
Lua_package_path "/opt/nginx/lua/share /?. Lua ;";
Lua_package_cpath "/opt/nginx/lua/lib /?. So;/opt/luajit/default/lib/lua/5.1 /?. So ;";
The Lua script executed in the location block displays the content_by_lua_file syntax,
You can specify that the executed script exists in the/opt/nginx/lua/app path.

Log Path
Mkdir-p/var/log/nginx

The downloaded source code package is under the same root directory as the above module, decompress the package, and enter the source code folder:
Cd nginx-1.6.2
Configure Compilation:

# Configure compilation. /configure \ -- prefix =/opt/nginx/1.6.2/\ -- error-log-path =/var/log/nginx/error. log \ -- pid-path =/opt/nginx/run/nginx. pid \ -- lock-path =/opt/nginx/lock/nginx. lock \ -- user = nginx \ -- group = nginx \ -- with-http_ssl_module \ -- with-http_realip_module \ -- with-http_flv_module \ -- with-http_gzip_static_module \ -- http-log-path =/var/log/nginx/access. log \ -- http-client-body-temp-path =/opt/nginx/tmp/client/\ -- http-proxy-temp-path =/opt/nginx/tmp/proxy /\ -- http-fastcgi-temp-path =/opt/nginx/tmp/fcgi/\ -- add-module = .. /ngx_devel_kit \ -- add-module = .. /lua-nginx-module

Compile and execute:
Make & sudo make install

Deploy the Lua dependency script:
Mkdir/opt/nginx/lua/share/restry
Cp lua-restry-redis/lib/restry/redis. lua/opt/nginx/lua/share/restry/
Cp lua-restry-upload/lib/restry/upload. lua/opt/nginx/lua/share/restry/

Example of configuration in/opt/nginx/default/conf/nginx. conf:

# Example of parameters in the main configuration file # Number of processes is set to the number of cpu Cores-2, nginx master process only accept connections # For example, 4 cpu, 3 worker_processes 3; # set according to the production service scenario, luaJIT-based nginx: lua vm has a maximum memory capacity of 1 GB. # The memory usage is limited. The number of connections should be properly set. events {worker_connections 1024;} will be tested ;} # Set http {lua_code_cache on for http blocks; # Turn off in debug mode to facilitate debugging of lua_package_path "/opt/nginx/lua/share /?. Lua; "; # If luajit is supported, add the path lua_package_cpath"/opt/nginx/lua/lib /?. So;/opt/luajit/default/lib/lua/5.1 /?. So; "; server_tokens off; # disable nginx version information (external) include mime. types; default_type application/octet-stream; log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for "'; error_log/var/log/nginx/error. log error; sendfile off; # disable the nagle algorithm, that is, data is sent out immediately, rather than deliberately controlling congestion tcp_nopush off; keepalive_timeout 65; gzip on; # business configuration include/opt/nginx/sites-available /*. conf ;}

The nginx Service Startup File [gist portal] is stored in/opt/nginx/init/nginx
Soft link:
Ln-sf/opt/nginx/init/nginx/etc/init. d/

The manual process for Nginx + LuaJIT + Redis configuration is described.

Summarize the ideas of the source code compilation service tool:

1. UnderstandMakefileNormallyPREFIXSuch keywords,
It defines the final compilation path, whether it is the configure specified-prefix Parameter
It is still possible to directly modify the Makefile.

2. Try to install it in/usr/local/by default. We recommend that you select/opt/or
/Your_self_define/custom path, which can be placed in a unified manner, whether in the development environment or
Production Environment and clean dependent environment are conducive to debugging and deployment.

3. Define a default soft link in the dependency of the tool version to facilitate version switching. This is a recommended practice.

Digress:

0. In April this year, Nginx + Lua + Redis was deployed on the host, and Lua was based on
LuaJIT interpreter, which uses apache jmeter for concurrent connection testing, on a virtual host with MB of memory
At the same time, the cpu of each connection request to the host is only instantaneous 1000, and all requests are
Normally returned results, the stability of http transient connection requests carrying high concurrency is still reliable in the host test.
If Node. js runs the same service, the request will fail, and the number of failures is around 5%. Of course, the test
Environment conditions also have other influences. For example, JavaScript code and dependent libraries are more complex than Lua code. If
The business is simple. Using Node. js + Redis is the lowest cost solution, while Nginx
The solution focuses on configuration effort and Migration (Nginx binfile, LuaJIT interpreter), and implements Demo
Or in scenarios with frequent changes in requirements, Node. js deployment is more convenient.

1. The deployment tools are constantly updated.DockerThis tool reduces developers' understanding about the release
The difference between the costs, O & M engineers, this profession will also have new challenges and awareness, may turn to more
The background and underlying development direction, orDevOps.

2. Entrepreneurship in the Internet industry is a popular activity. A startup team mainly engaged in development engineers is engaged in O & M.
Lack of experience, the developer-friendly open source O & M will be preferred in the case of labor costs and recruitment difficulties
Tools. For example, common open-source O & M tools include:
Deployment Tool SaltStack, Puppet, Jenkins
Monitoring Tool Nagios Zabbix
Log collection Logstash
Process Management Tool Supervisor
However, this does not mean that qualified O & M engineers or engineers are not required to serve developers.
To the extent required, every developer will feel it based on their work experience and experience :)

Original article, reprinted Please note:Reposted from kaka_ace's blog

Link:Nginx + LuaJIT + Redis compilation and configuration tutorial

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.