linux下安装nginx

来源:互联网 发布:linux 看文件权限 编辑:程序博客网 时间:2024/06/01 23:54

准备操作:



第一步:安装make

sudo yum -y install gcc automake autoconf libtool make

第二步:安装g++

sudo yum install gcc gcc-c++

正式安装:


   一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。


  第一步:选定源码目录


        可以是任何目录,我选定的是/home/q/


   第二步:安装PCRE库


       ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:


cd /home/q/wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz sudo tar -zxvf pcre-8.37.tar.gzcd pcre-8.37sudo ./configuresudo makesudo make install


第三步:安装zlib库

http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /home/qwget http://zlib.net/zlib-1.2.8.tar.gzsudo tar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8sudo ./configuresudo makemake install

第四步:安装ssl(某些vps默认没装ssl)


cd /home/q/sudo wget http://www.openssl.org/source/openssl-1.0.1c.tar.gzsudo tar -zxvf openssl-1.0.1c.tar.gz


第五步:安装nginx


Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:


cd /home/q/sudo wget http://nginx.org/download/nginx-1.4.2.tar.gzsudo tar -zxvf nginx-1.4.2.tar.gzsudo mkdir nginxcd nginx-1.4.2sudo ./configure --sbin-path=/home/q/nginx/nginx --conf-path=/home/q/nginx/nginx.conf --pid-path=/home/q/nginx/nginx.pid --with-http_ssl_module --with-pcre=/home/q/pcre-8.37 --with-zlib=/home/q/zlib-1.2.8 --with-openssl=/home/q/openssl-1.0.1csudo makesudo make install


--with-pcre=/home/q/pcre-8.34 指的是pcre-8.34 的源码路径。
--with-zlib=/home/q/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。


安装成功后 /home/q/nginx 目录下如下:




6.启动


确保系统的 80 端口没被其他程序占用,运行sudo /home/q/nginx/nginx 命令来启动 Nginx,

netstat -ano|grep 80



在浏览器上输入机器的ip得到如下界面说明安装成功




7.重启


sudo /home/q/nginx/nginx –s reload

8.常用配置信息:

#nginx运行用户和组user    www www;  #启动进程,通常设置成和cpu的数量相等worker_processes  4; #全局错误日志及PID文件pid /var/run/nginx.pid;error_log  /var/log/nginx/error.log; events {        #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能use epoll;                   #单个后台worker process进程的最大并发链接数        worker_connections  10240;}#设定http服务器,利用它的反向代理功能提供负载均衡支持http {        include       mime.types;         default_type  application/octet-stream;          error_page 400 403 500 502 503 504  /50x.html;         index index.html index.shtml         autoindex off;          fastcgi_intercept_errors on;         sendfile        on;         # These are good default values.        tcp_nopush      on;        tcp_nodelay     off;         # output compression saves bandwidth        gzip  off;         #gzip_static on;        #gzip_min_length  1k;        gzip_http_version 1.0;        gzip_comp_level 2;        gzip_buffers  4 16k;        gzip_proxied any;        gzip_disable "MSIE [1-6]\.";        gzip_types  text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;        #gzip_vary on;         server_name_in_redirect off; #设定负载均衡的服务器列表        upstream portals {                  server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;                  server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;                            server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;                  server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;        }         #upstream overflow {         #       server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;                #       server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;               #}         server {                                     #侦听8080端口                listen       8080;                server_name  127.0.0.1;                    #403、404页面重定向地址                   error_page  403 = http://www.e100.cn/ebiz/other/217/403.html;                   error_page  404 = http://www.e100.cn/ebiz/other/218/404.html;                   proxy_connect_timeout      90;                   proxy_send_timeout         180;                   proxy_read_timeout         180;                    proxy_buffer_size 64k;                   proxy_buffers 4 128k;                   proxy_busy_buffers_size 128k;                     client_header_buffer_size 16k;                   large_client_header_buffers 4 64k;                 #proxy_send_timeout         3m;                #proxy_read_timeout         3m;                #proxy_buffer_size          4k;                #proxy_buffers              4 32k;                 proxy_set_header Host $http_host;                proxy_max_temp_file_size 0;                #proxy_hide_header Set-Cookie;                           #       if ($host != 'www.e100.cn' ) {         #                rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;         #       }                 location / {                       deny all;               }                    location ~ ^/resource/res/img/blue/space.gif {                    proxy_pass http://tecopera;               }                location = / {                   rewrite ^(.*)$  /ebiz/event/517.html last;               }                      location = /ebiz/event/517.html {                    add_header Vary Accept-Encoding;                    root /data/web/html;                    expires 10m;               }                   location = /check.html {                    root /usr/local/nginx/html/;                    access_log off;               }                location = /50x.html {                    root /usr/local/nginx/html/;                    expires 1m;                    access_log off;               }                location = /index.html {                       add_header Vary Accept-Encoding;#定义服务器的默认网站根目录位置                    root /data/web/html/ebiz;                    expires 10m;               }#定义反向代理访问名称                   location ~ ^/ecps-portal/* {                   # expires 10m;#重定向集群名称                    proxy_pass http://portals;                    #proxy_pass http://172.16.68.134:8082;               }                    location ~ ^/fetionLogin/* {                   # expires 10m;                    proxy_pass http://portals;                    #proxy_pass http://172.16.68.134:8082;                }                    #location  ~ ^/business/* {                                                                                      #   # expires 10m;                                                                                                #    proxy_pass http://172.16.68.132:8088;                                                                                   #    #proxy_pass http://172.16.68.134:8082;                                                                       #}                    location ~ ^/rsmanager/* {                    expires 10m;                    root /data/web/;                    #proxy_pass http://rsm;               }#定义nginx处理的页面后缀                   location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$  {                            root /data/web/html/;#页面缓存时间为10分钟                         expires 10m;                   } #设定查看Nginx状态的地址                    location ~* ^/NginxStatus/ {                    stub_status on;                    access_log off;                    allow 10.1.252.126;                    allow 10.248.6.49;                    allow 127.0.0.1;                    deny all;               }         #       error_page   405 =200 @405;         #       location @405         #       {         #                proxy_pass http://10.248.6.45:8080;         #       }                  access_log  /data/logs/nginx/access.log combined;               error_log   /data/logs/nginx/error.log;        }         server {                listen       8082;                 server_name  _;               location = /check.html {                    root /usr/local/nginx/html/;                    access_log off;               }                          }         server {                   listen       8088;                   server_name  _;                   location ~ ^/* {                   root /data/web/b2bhtml/;                   access_log off;         }                         }        server {                listen       9082;                server_name  _;         #        location ~ ^/resource/* {        #            expires 10m;         #           root /data/web/html/;         #       }                 location  / {                     root /data/web/html/sysMaintain/;                       if (!-f $request_filename) {                            rewrite ^/(.*)$ /sysMaintain.html last;                           }                }        } }



1 0