Linux下Nginx的安装与配置

安装前需要安装pcre:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
1、解压缩:
     tar xjpf pcre-7.8.tar.bz2
2、配置:
     cd pcre-7.8
     ./configure --prefix=/usr/local/pcre-7.8 --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
     configure有许多参数可配,具体参见./configure --help及手册
3、编译:
     make
4、安装:
     make install
5、检查:
     ls /usr/local 检查是否有pcre-7.8目录
     ls /usr/local/lib   检查是否有pcre目录
     ls /usr/local/include   检查是否有pcre目录
6、将库文件导入cache:
     方法1:在/etc/ld.so.conf中加入: /usr/local/lib/pcre,然后运行ldconfig
     方法2:在/etc/ld.so.conf.d/下新生成一个文件(或在其中的文件中加入同样内容),文件内容为:
               /usr/local/lib/pcre,然后运行ldconfig

Nginx在Linux环境下可以通过编译源码的方式安装,最简单的安装命令如下
wget http://nginx.org/download/nginx-1.2.0.tar.gz
tar zxvf nginx-1.2.0.tar.gz
cd nginx-1.2.0
./configure
make
sudo make install
按照以上命令,Nginx将被默认安装到/usr/local/nginx目录下。用户可以通过./configure –help命令查看Nginx可选择的编译选项进行自定义安装配置。
通过命令 /usr/local/nginx/sbin/nginx/ -c /usr/local/nginx/conf/nginx.conf 来启动服务。
测试

        将Nginx conf文件的server block部分的配置如下:

server {
    listen 80;
    server_name localhost;

    location / {
        root html;
        index index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}

  在linux下查看Nginx的进程

ps aux|grep nginx
admin 24913 0.0 0.0 58596 1048 ? Ss Feb27 0:00 nginx: master process ./nginx
admin 24914 0.0 0.0 72772 5420 ? S Feb27 0:03 nginx: worker process

  

调试日志

用户在使用Nginx的过程中,可能会遇到所请求的资源不正确,Nginx Core Dump,段错误等异常情况,这时需要有相应的机制来进行调试及问题定位,特别是面对大量的日志信息,合理的调试处理机制对用户来说是一件非常重要的事情。

开启调试日志:

要开启调试日志,首先需要在配置Nginx时打开调试功能,然后编译:

./configure --with-debug ...

然后在配置文件中设置error_log的级别为:

error_log /path/to/log debug;

日志级别分析:

在此,我们通过分析Nginx源码了解下Nginx将日志分为几个等级及不同日志等级之间的相互关系:

Ngx_log.h代码

#define NGX_LOG_STDERR 0
#define NGX_LOG_EMERG 1
#define NGX_LOG_ALERT 2
#define NGX_LOG_CRIT 3
#define NGX_LOG_ERR 4
#define NGX_LOG_WARN 5
#define NGX_LOG_NOTICE 6
#define NGX_LOG_INFO 7
#define NGX_LOG_DEBUG 8

#define NGX_LOG_DEBUG_CORE 0x010
#define NGX_LOG_DEBUG_ALLOC 0x020
#define NGX_LOG_DEBUG_MUTEX 0x040
#define NGX_LOG_DEBUG_EVENT 0x080
#define NGX_LOG_DEBUG_HTTP 0x100
#define NGX_LOG_DEBUG_MAIL 0x200
#define NGX_LOG_DEBUG_MYSQL 0x400

#define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE
#define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_MYSQL
#define NGX_LOG_DEBUG_CONNECTION 0x80000000
#define NGX_LOG_DEBUG_ALL 0x7ffffff0

  

 

原文地址:https://www.cnblogs.com/huangxiaohen/p/3310080.html