Nginx安装配置

 1. 简介

Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

     Nginx是C语言开发,一般建议在Linux上运行,本文Nginx使用Ubuntu作为安装环境,其他Linux系统会有不一样的地方,本文仅供参考。

2. Nginx依赖

Nginx的模块依赖如下:

  1. gzip模块需要 zlib 库
  2. rewrite模块需要 pcre 库
  3. ssl功能需要openssl库

预先编译好需要的包:

1 apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

ububtu平台编译环境可以使用以下指令:

1 apt-get install build-essential
2 apt-get install libtool

一般我们都需要先装pcre、zlib、gcc、openssl等,安装过程如果出现异常,都会有提示,然后根据情况安装对应缺失的依赖

3. 安装步骤

  选定源码目录:可以是任何目录。比如cd /usr/local/src

  3.1 安装开发工具

1 apt-get install build-essential

  3.2 安装zlib

1 apt-get install zlib1g-dev

  3.3 安装openssl

1 apt-get install openssl
2 apt-get install libssl-dev

  3.4 安装PCRE库

1 tar -zxvf pcre-8.39.tar.gz
2 cd pcre-8.39
3 
4 ./configure
5 make && make install
6 
7 pcre-config --version

  3.5 解压Nginx压缩包

    下面是把Nginx安装到'/usr/local/nginx'目录下,首先把下载的gz包上传到Ubuntu服务器上并解压。

1 cd /usr/local/src
2 tar -zxvf nginx-1.10.2.tar.gz 
3 cd nginx-1.10.2/

  3.6 编译安装

  目录:/usr/local/webserver/nginx

1 ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.39
2 
3 make
4 make install

  3.7 查看nginx版本

1 /usr/local/webserver/nginx/sbin/nginx -v

看到显示出对应的Nginx的版本数据表示安装完成

 

  3.8 启动Nginx

1 /usr/local/webserver/nginx/sbin/nginx
2 netstat -ano | grep 80

4. Nginx配置

   4.1 创建 Nginx运行使用的用户

    这里以创建nginx为例(放到ngnix group中)

1 /usr/sbin/groupadd nginx
2 /usr/sbin/useradd -g nginx nginx -m -s /bin/bash
3 passwd nginx

   4.2 配置nginx.conf 

    将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容(配置内容可以根据个人情况修改)

# Nginx用户及组: 用户 组; window下不指定
user nginx nginx;

# 工作进程: 数目. 根据硬件调整, 通常等于CPU数量或者2倍于CPU
worker_processes 1;

# 日志位置和日志级别[debug | info | notice | warn | error | crit | alert | emerg]
error_log /usr/local/webserver/nginx/logs/nginx_error.log error;

# pid(进程标识符) : 存放路径
pid /usr/local/webserver/nginx/nginx.pid;

# Specifies the value for maximum file descriptors that can be opened by this process.
# 指定工作进程可以打开的最大描述符 : 数目
# 现在在Linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535
worker_rlimit_nofile 65535;

events {
  # 使用epoll的I/O模型. linux建议epoll, FreeBSD建议采用kqueue, window下不指定
  use epoll;
  # 每个工作进程的最大连接数量. 根据硬件调整, 和前面工作进程配合起来用
  # 每个进程允许的最多连接数, 理论上每台nginx服务器的最大连接数为:worker_processes*worker_connections
  worker_connections 65535;
}

http {
  # 设定mime类型, 类型由mime.type文件定义
  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';
  
  #charset gb2312;
  
  # 保存服务器名字的hash表大小
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  
  # 超时时间
  keepalive_timeout 60;
  
  tcp_nodelay on;
  
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  # limit_zone crawler $binary_remote_addr 10m;
  # 下面是server虚拟主机的配置
  server {
  
    # 监听端口
    listen 80;
    
    #域名
    server_name localhost;
    index index.html index.htm index.php;
    
    #站点目录
    root /usr/local/webserver/nginx/html;
    
    # 对以php/html结尾的地址进行负载均衡
    location ~ .*\.(php|php5|html)?$ {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
      # access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
      expires 15d;
      # access_log off;
    }
    
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    access_log off; 
  }
}

   4.3 检查配置文件ngnix.conf的正确性命令

1 /usr/local/webserver/nginx/sbin/nginx -t

  4.4 启动Nginx

1 /usr/local/webserver/nginx/sbin/nginx
2 ps -ef | grep nginx

  4.5 Nginx常用命令

1 /usr/local/webserver/nginx/sbin/nginx -s reload            # 重新载入配置文件
2 /usr/local/webserver/nginx/sbin/nginx -s reopen            # 重启 Nginx
3 /usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx

   4.6 异常处理

    1. 进行重启或者停止或者reload操作时出现

1 nginx: [error] invalid PID number "" in "/usr/local/webserver/nginx/nginx.pid"

    使用 'ps -ef | grep nginx' 查出nginx的pid(假设是6651)

    解决方法: 杀掉nginx的进程 kill 6651, 然后再重新启动nginx, 然后再进行对nginx的操作

   4.7 将Nginx设置开机启动

1 vi /etc/rc.local 
2 
3 # join nginx
4 /usr/local/webserver/nginx/sbin/nginx

 

参考资料

  Nginx-百度百科

http://www.runoob.com/linux/nginx-install-setup.html

http://blog.csdn.net/wm_1991/article/details/50493487

http://www.ttlsa.com/linux/the-nginx-log-configuration/

转载于:https://www.cnblogs.com/aarondjc/p/6106249.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值