当前位置: > Linux服务器 > nginx >

Centos下安装、Nginx笔记(一) 简单安装

时间:2014-05-15 02:06来源:linux.it.net.cn 作者:it
前期安装:
     先简单安装
后期需要考虑内容:
1、要考虑Nginx自身的灾备/集群/负载均衡
2、没有对web节点进行健康检查

说明:
1、环境准备
ngixx反向代理服务器
系统:Centos 5.4
IP60.x.x.55
域名:www.xx.com
Nginx 1.2.1
   wget http://www.nginx.org/download/nginx-1.2.1.tar.gz


2: 安装步骤
安装gcc编译器及相关工具
#yum -y install gcc gcc-c++ autoconf automake make

安装相关依赖的模块
#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel


软件源代码包存放位置:/usr/local/src
# cd /usr/local/src
# wget http://www.nginx.org/download/nginx-1.2.1.tar.gz
# tar -zxvf nginx-1.2.1.tar.gz            // 解压包

#cd nginx-1.2.1
#./configure --prefix=/usr/local/nginx         //通过编译源码的方式进行安装
  //------配置完毕后如下提示-----------//
  Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  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"
 //----------------------------------------//

#make
#make install

配置文件:(配置和注释收集与网络)
#vi /usr/local/nginx/conf/nginx.config 
-----------------------------------------nginx.config----------------------------------------------------------------
#运行用户
#user  nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及PID文件
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

#工作模式及连接数上限
events {
    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections  1024;#单个后台worker process进程的最大并发链接数
    # multi_accept on;
}

http {
         #设定mime类型,类型由mime.type文件定义
        include       /etc/nginx/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  /var/log/nginx/access.log main

      #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
      #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
      sendfile        on;
      #tcp_nopush     on;

        #连接超时时间
        #keepalive_timeout  0;
        keepalive_timeout  65;
        tcp_nodelay        on;

        #开启gzip压缩
        gzip  on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";

 
      server {
        #侦听80端口
            listen       80;
            #定义使用www.xx.com访问
            server_name  www.xx.com;
        
            #设定本虚拟主机的访问日志
            access_log  logs/www.xx.com.access.log  main;
        
        #默认请求
        location / {
              root   /usr/local/nginx/html;      #定义服务器的默认网站根目录位置
              index index.php index.html index.htm;   #定义首页索引文件的名称
        
            }
        
        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;
            location = /50x.html {
            root   /usr/local/nginx/html;
            }
      } #server
} #http
------------------------------------------------------------------------------------------------------------------------------------


3、如何启动Nginx
#启动nginx
#/usr/local/nginx/sbin/nginx -t
显示以下信息为正确的
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
#/usr/local/nginx/sbin/nginx
启动成功后,查看nginx进程信息: # ps -ef | grep nginx ,看是否存在nginx的进程来确认是否成功启动。


#重启动nginx
#/usr/local/nginx/sbin/nginx -s reload

测试http://www.xx.com/
同时记得检查centos防火墙设置,是否开启了相应端口,可使用setup命令来设置防火墙、dns、网络等信息。
如果默认的配置文件未做任何改动,使用浏览器直接访问nginx server,会出现提示:Welcome to Nginx

4、优化
vim /etc/sysctl.conf
在最后添加(待详细研究)
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024  65535


保存退出后执行

sysctl -p


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容