在Linux里安装、启动nginx

来源:互联网 发布:mac切换中文输入法 编辑:程序博客网 时间:2024/06/10 08:57

由于nginx依赖于pcre和zlib,所以在安装nginx时,需要先安装pcre和zlib(为了方便起见,我把pcre、zlib、nginx的包放到了同一个目录下)。

1、安装pcre

从pcre的官网下载tar.gz包,官网地址为:https://sourceforge.net/projects/pcre/files/pcre/,在这里我下载的是:pcre-8.39.tar.gz

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. tar -zxvf pcre-8.39.tar.gz  
  2. cd pcre-8.39  
  3. ./configure   
  4. make  
  5. make install  

2、安装zlib

从zlib的官网下载tar.gz包,官网地址为:http://zlib.net/,在这里我下载的是:zlib-1.2.8.tar.gz

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. tar -xvf zlib-1.2.8.tar.gz   
  2. cd zlib-1.2.8  
  3. ./configure   
  4. make  
  5. make install  

3、安装nginx

从nginx的官网下载tar.gz包,官网地址为:http://nginx.org/en/download.html,在这里我下载的是: nginx-1.11.5.tar.gz

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. tar -zxvf nginx-1.11.5.tar.gz  
  2. cd nginx-1.11.5  
  3. ./configure --sbin-path=/usr/local/nginx/sbin/nginx  --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/sbin/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.8  
  4. make  
  5. make install  
至此,nginx就安装完毕

4、配置并启动nginx

经过上面的安装步骤和目录设置,nginx的启动程序就是/usr/local/nginx/sbin/nginx,配置文件是/usr/local/nginx/conf/nginx.conf,可在配置文件里修改端口、反向代理路径等

启动nginx的命令为:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /usr/local/nginx/sbin/nginx -t   //测试nginx.conf的配置是否正确  
  2.   
  3. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  //根据nginx.conf里的配置,启动nginx服务  






摘要: Linux下Nginx1.8安装

 

安装pcre

 

   wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gzcd pcre-8.37./configuremakemake install

 

安装openssl

 

wget http://www.openssl.org/source/openssl-1.0.1c.tar.gztar -zxvf openssl-1.0.1c.tar.gz

cd openssl-1.0.1c/

./configmakemake install

 

安装zlib         

 

wget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremakemake install

 

下载Nginx1.8至/usr/local,看命令吧:

$ tar xzf nginx-1.8.0.tar.gz$ ./configure --prefix=/home/nginx/nginx$ make$ make install

若出现cc找不到执行

 

 yum -y install gcc automake autoconf libtool make

若启动出现如下错误

 error while loading shared libraries: libpcre.so.1: cannot open shared object file:No such file or directory

执行以下命令

ln -s /usr/local/lib/libpcre.so.1 /lib64

安装完成之后生成一个nginx的文件夹,cd进去并切换到sbin目录

启动命令

$ ./nginx

关闭命令:

$ ./nginx -s stop

优雅重启

$ ./nginx -s reload

下面是配置:    

1、反向代理配置

修改部署目录下conf子目录的nginx.conf文件(如nginx-1.5.13\conf\nginx.conf)内容,可调整相关配置。

反向代理配置示例:

location / {#设置主机头和客户端真实地址,以便服务器获取客户端真实IP proxy_set_header Host $hostproxy_set_header X-Real-IP $remote_addrproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for#禁用缓存 proxy_buffering off#设置反向代理的地址 proxy_pass http://192.168.1.1;}

代理地址根据实际情况修改。

2、负载均衡配置

nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

负载均衡配置示例:

upstream test{ #ip_hash; server 192.168.1.251server 192.168.1.252server 192.168.1.247; }server {listen       80;server_name  helloword;location / { #反向代理的地址 proxy_pass http://test;     }}

Upstream命名和服务器地址根据实际情况修改。

3、负载均衡+反向代理完整配置示例

nginx.conf:worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    upstream test#ip_hash; server 192.168.1.251; server 192.168.1.252; server 192.168.1.247; }    server {        listen       80;        server_name  2;        location / {        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP             proxy_set_header Host $host;             proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             #禁用缓存             proxy_buffering off;             #反向代理的地址             proxy_pass http://test;           }    }}

4、动静分离配置    

worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    upstream test { #ip_hash; server 192.168.1.251server 192.168.1.252server 192.168.1.247; }    server {        listen       80;        server_name  2;#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {   root /usr/local/nginx/html/myloan;   #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力  expires      7d;         } #所有jsp、do的动态请求都交给后面的tomcat处理          location ~ (\.jsp)|(\.do)$ {   #tomcat地址  proxy_pass http://test;     proxy_redirect 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;    client_max_body_size 10m;    client_body_buffer_size 128k;    proxy_connect_timeout 90;    proxy_send_timeout 90;    proxy_read_timeout 90;    proxy_buffer_size 4k;    proxy_buffers 4 32k;    proxy_busy_buffers_size 64k;    proxy_temp_file_write_size 64k;  }          }}

附录:nginx.conf配置详解

#运行用户user www-data;    #启动进程,通常设置成和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服务器,利用它的反向代理功能提供负载均衡支持http {     #设定mime类型,类型由mime.type文件定义    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    #设定日志格式    access_log    /var/log/nginx/access.log;    #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)";    #设定请求缓冲    client_header_buffer_size    1k;    large_client_header_buffers  4 4k;    include /etc/nginx/conf.d/*.conf;    include /etc/nginx/sites-enabled/*;    #设定负载均衡的服务器列表     upstream mysvr {    #weigth参数表示权值,权值越高被分配到的几率越大    #本机上的Squid开启3128端口    server 192.168.8.1:3128 weight=5;    server 192.168.8.2:80  weight=1;    server 192.168.8.3:80  weight=6;    }   server {    #侦听80端口        listen       80;        #定义使用www.xx.com访问        server_name  www.xx.com;        #设定本虚拟主机的访问日志        access_log  logs/www.xx.com.access.log  main;    #默认请求    location / {          root   /root;      #定义服务器的默认网站根目录位置          index index.php index.html index.htm;   #定义首页索引文件的名称          fastcgi_pass  www.xx.com;         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;           include /etc/nginx/fastcgi_params;        }    # 定义错误提示页面    error_page   500 502 503 504 /50x.html;          location = /50x.html {        root   /root;    }    #静态文件,nginx自己处理    location ~ ^/(images|javascript|js|css|flash|media|static)/ {        root /var/www/virtual/htdocs;        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。        expires 30d;    }    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.    location ~ \.php$ {        root /root;        fastcgi_pass 127.0.0.1:9000;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;        include fastcgi_params;    }    #设定查看Nginx状态的地址    location /NginxStatus {        stub_status            on;        access_log              on;        auth_basic              "NginxStatus";        auth_basic_user_file  conf/htpasswd;    }    #禁止访问 .htxxx 文件    location ~ /\.ht {        deny all;    }          }}

0 0
原创粉丝点击