Linux下安装(centos):
nginx的许多模块都需要依赖一些第三方的工具,因此在安装nginx前需要配置一些系统环境,下面是一些常用工具的配置和简介。
[root@localhost ~]# yum -y install gcc
GCC原本作为GNU操作系统的官方编译器,现已被大多数类Unix操作系统(如Linux、BSD、Mac OS X等)采纳为标准的编译器,GCC同样适用于微软的Windows。GCC 原名为 GNU C 语言编译器(GNU C Compiler),因为它原本只能处理 C语言。GCC 很快地扩展,变得可处理 C++。后来又扩展能够支持更多编程语言,如Fortran、Pascal、Objective-C、Java、Ada、Go以及各类处理器架构上的汇编语言等,所以改名GNU编译器套件(GNU Compiler Collection)
[root@localhost ~]# yum install pcre
[root@localhost ~]# yum install pcre-devel
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括perl兼容的正则表达式库。这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。Boost太庞大了,使用boost regex后,程序的编译速度明显变慢。同样一个程序,使用boost::regex编译时需要3秒,而使用pcre不到1秒。因此改用pcre来解决C语言中使用正则表达式的问题
nginx HTTP rewirite 模块依赖PCRE,如果未安装会报以下错:
the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.
[root@localhost ~]# yum install openssl openssl-devel
OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用
SSL模块依赖Openssl库,需安装openssl以及openssl-devel,如果未安装会报以下错:
SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.
[root@localhost ~]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
[root@localhost ~]# tar zxvf LuaJIT-2.0.5.tar.gz
[root@localhost ~]# cd LuaJIT-2.0.5
[root@localhost ~]# make && sudo make install
LuaJIT is a Just-In-Time Compilerfor the Lua programming language.
LuaJIT 是 Lua运行环境,即采用C语言写的Lua代码的解释器
下载nginx依赖模块 nginx,ngx_devel_kit, lua-nginx-module
wget https://github.com/simpl/ngx_devel_kit/tarball/master -O simpl-ngx_devel_kit.tar.gz
wget https://github.com/agentzh/echo-nginx-module/tarball/master -O echo-nginx-module.tar.gz
wget https://github.com/chaoslawful/lua-nginx-module/tarball/master -O lua-nginx-module.tar.gz
解压
tar zxvf nginx-1.2.2.tar.gz
tar zxvf simpl-ngx_devel_kit.tar.gz
tar zxvf lua-nginx-module.tar.gz
下载nginx(如已下载可跳过),并解压进入到安装包目录
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
执行下面这条命令可配置nginx安装信息:
[root@localhost nginx-1.10.3]# ./configure --with-debug --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --prefix=/usr/local/nginx --add-module=../simpl-ngx_devel_kit-e443262 --add-module=../openresty-lua-nginx-module-e80d292 --add-module=../openresty-echo-nginx-module-7740e11
命令解析:
--with*** 为安装一些内置/集成的模块,如上述中的
--with-http_realip_module 取用户真实ip模块
--with-pcre Perl兼容的达式模块
--with-luajit 集成luajit模块
--add-module 添加自定义的第三方模块,如上述中的
--add-module=../simpl-ngx_devel_kit-e44326
--add-module=../openresty-lua-nginx-module-e80d292
--add-module=../openresty-echo-nginx-module-7740e11
=号后面为module的路径,需为上述下载模块解压包的实际路径
--prefix=/usr/local/nginx 指定安装目录
配置正确会出现类似以下提示
...
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
如果配置错误则会有错误提示,如假如未按上述配置正确安装OpenSSL 会有以下提示:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option.
确保配置正确后,执行编译
make && make install
编译完成后,执行命令查看版本
[root@localhost ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.10.3
# /usr/local/nginx-1.4.2/sbin/nginx -v
./objs/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
解决方法:
# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
安装成功后,我们可以在nginx的sbin目录下运行nginx,执行以下命令,启动nginx:
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
启动后可检查进程
[root@localhost nginx]# ps -ef|grep nginx
root 25859 1 0 17:04 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 25860 25859 0 17:04 ? 00:00:00 nginx: worker process
root 25862 25048 0 17:04 pts/2 00:00:00 grep --color=auto nginx
访问默认配置连接及端口 127.0.0.1:80(80可省略),如果出现以下的欢迎页面(Welcome to nginx!)则nginx安装成功!
[root@localhost nginx]# curl 127.0.0.1:80
<html>
<head>
<title>Welcome to nginx!title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
style>
head>
<body>
<h1>Welcome to nginx!h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.orga>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.coma>.p>
<p><em>Thank you for using nginx.em>p>
body>
html>
下面我们用lua写个hello world来验证lua模块是否成功集成。
修改nginx.conf,加入lua代码,加入以下配置:
location ~* ^/hello(/.*) {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,world !")';
}
完整配置参考
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~* ^/hello(/.*) {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,world !")';
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
修改后,执行以下命令刷新nginx配置(加载修改的配置内容)
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
再使用curl访问验证
[root@localhost ~]# curl 127.0.0.1/hello/
hello,world!
出现 hello world,恭喜你敲开了一扇新大门!
教程结束!