nginx安装配置

来源:互联网 发布:怎样制作炒股票软件 编辑:程序博客网 时间:2024/06/04 18:51

1、为什么选择nginx

Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.

作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。

2、安装nginx

ngnix依赖以下模块

  • gzip模块需要zlib库

  • rewrit模块需要pcre库

  • ssl功能需要openssl库

1、安装pcre

  • 获取pcre编译安装包,在可以在 http://www.pcre.org/ 点击download获取,地址为 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 我下载的prce-8.37.tar.gz 

  • 解压tar包 ,我这里放入到根目录下的soft目录里 

  1. lin@ubuntu:/soft$ ls
  2. jdk1.7.0_79  jdk-7u79-linux-x64.tar.gz  pcre-8.37.tar.gz
  3. lin@ubuntu:/soft$ tar -zxvf pcre-8.37.tar.gz
  • 进入解压缩目录,执行./configure。

  1. lin@ubuntu:/soft/pcre-8.37$ ./configure
  • make & make install

  1. lin@ubuntu:/soft/pcre-8.37$ make
  2. lin@ubuntu:/soft/pcre-8.37$ sudo make install

2、安装openssl

  • 获取openssl编译安装包,在http://www.openssl.org/source/  上可以获取当前最新的版本。我使用的是openssl-1.0.0t.tar.gz版本

  • 解压tar包 ,我这里放入到根目录下的soft目录里


  1. lin@ubuntu:/soft$ tar -zxvf openssl-1.0.0t.tar.gz
  • 进入解压缩目录,执行./config。

  1. lin@ubuntu:/soft/openssl-1.0.0t$ ./config
  • make & make install

  1. lin@ubuntu:/soft/openssl-1.0.0t$ make
  2. lin@ubuntu:/soft/openssl-1.0.0t$ sudo make install

3、安装zlib

  •  获取zlib编译安装包,在http://www.zlib.net/  上可以获取当前最新的版本。我使用的是zlib-1.2.8.tar.gz版本

  •  解压缩tar.gz包 我的是放入根目录soft里面。


  1. lin@ubuntu:/soft$ tar -xf zlib-1.2.8.tar.gz  
  •  进入解压缩目录,执行./configure。


  1. lin@ubuntu:/soft/zlib-1.2.8$ ./configure
  •  make & make install


  1. lin@ubuntu:/soft/zlib-1.2.8$ make
  2. lin@ubuntu:/soft/zlib-1.2.8$ sudo make install

4、安装nginx

  • 获取nginx,在http://nginx.org/en/download.html  上可以获取当前最新的版本。我使用的是nginx-1.8.0.tar.gz版本

  • 解压缩tar.gz包,我的放入到根目录下的soft里面。


  1. lin@ubuntu:/soft$ tar -zxvf nginx-1.8.0.tar.gz
  • 进入解压缩目录,执行./configure


  1. lin@ubuntu:/soft/nginx-1.8.0$ ./configure
  • make & make install

  1. lin@ubuntu:/soft/nginx-1.8.0$ make
  2. lin@ubuntu:/soft/nginx-1.8.0$ sudo make install

到此安装完毕,环境准备结束

3、简单启动nginx

由于nginx占用的是80端口,所以先确保系统的80端口没有被其他程序占用

1、启动nginx 

  1. lin@ubuntu:/usr/local/nginx$ sudo sbin/nginx

2、检查是否启动成功

  1. netstat -ano|grep 80

有输出就证明启动成功

3、访问80端口,打开浏览器输入nginx所在的电脑ip出现 Welcome to nginx! 则证明成功!

像重启的话 可以用 

  1. lin@ubuntu:/usr/local/nginx$ sudo sbin/nginx -s reload

4、停止服务可以用 

  1. lin@ubuntu:/usr/local/nginx$ sudo killall nginx

4、nginx配置

安装完成后,配置目录conf下有很多配置文件,其中除了nginx.conf,其余的配置文件,使用默认提供的即可。

nginx.conf是主配置文件其中

work_process 表示工作进程的数量,一般设置为cpu的核数。

worker_connections 表示每个工作进程的最大连接数

server{}定义了虚拟机主机

        listen   监听端口;

        server_name 监听域名

        location/{} 是用来为匹配的 URI 进行配置,URI 即语法中的“/uri/”。

        location  / { }匹配任何查询,因为所有请求都以 / 开头。

            root指定对应uri的资源查找路径,这里html为相对路径,完整路径为/opt/ opt/nginx-1.7.7/html/

            index指定首页index文件的名称,可以配置多个,以空格分开。如有多个,按配置顺序查找。

下面是默认配置

  1. #user  nobody;
  2. worker_processes  1;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8.    worker_connections  1024;
  9. }
  10. http {
  11.    include       mime.types;
  12.    default_type  application/octet-stream;
  13.    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.    #                  '$status $body_bytes_sent "$http_referer" '
  15.    #                  '"$http_user_agent" "$http_x_forwarded_for"';
  16.    #access_log  logs/access.log  main;
  17.    sendfile        on;
  18.    #tcp_nopush     on;
  19.    #keepalive_timeout  0;
  20.    keepalive_timeout  65;
  21.    #gzip  on;
  22.    server {
  23.        listen       80;
  24.        server_name  localhost;
  25.        #charset koi8-r;
  26.        #access_log  logs/host.access.log  main;
  27.        location / {
  28.            root   html;
  29.            index  index.html index.htm;
  30.        }
  31.        #error_page  404              /404.html;
  32.        # redirect server error pages to the static page /50x.html
  33.        #
  34.        error_page   500 502 503 504  /50x.html;
  35.        location = /50x.html {
  36.            root   html;
  37.        }
  38.        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39.        #
  40.        #location ~ \.php$ {
  41.        #    proxy_pass   http://127.0.0.1;
  42.        #}
  43.        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44.        #
  45.        #location ~ \.php$ {
  46.        #    root           html;
  47.        #    fastcgi_pass   127.0.0.1:9000;
  48.        #    fastcgi_index  index.php;
  49.        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  50.        #    include        fastcgi_params;
  51.        #}
  52.        # deny access to .htaccess files, if Apache's document root
  53.        # concurs with nginx's one
  54.        #
  55.        #location ~ /\.ht {
  56.        #    deny  all;
  57.        #}
  58.    }
  59.    # another virtual host using mix of IP-, name-, and port-based configuration
  60.    #
  61.    #server {
  62.    #    listen       8000;
  63.    #    listen       somename:8080;
  64.    #    server_name  somename  alias  another.alias;
  65.    #    location / {
  66.    #        root   html;
  67.    #        index  index.html index.htm;
  68.    #    }
  69.    #}
  70.    # HTTPS server
  71.    #
  72.    #server {
  73.    #    listen       443 ssl;
  74.    #    server_name  localhost;
  75.    #    ssl_certificate      cert.pem;
  76.    #    ssl_certificate_key  cert.key;
  77.    #    ssl_session_cache    shared:SSL:1m;
  78.    #    ssl_session_timeout  5m;
  79.    #    ssl_ciphers  HIGH:!aNULL:!MD5;
  80.    #    ssl_prefer_server_ciphers  on;
  81.    #    location / {
  82.    #        root   html;
  83.    #        index  index.html index.htm;
  84.    #    }
  85.    #}
  86. }


很容易看出来 nginx监听了80端口,域名为loccalhost,根路径为html文件夹,默认文件为index.html,index.htm,服务器错误重定向到50x.html    

可以看到nginx html文件夹下面有如下文件

  1. lin@ubuntu:/usr/local/nginx$ cd html
  2. lin@ubuntu:/usr/local/nginx/html$ ls
  3. 50x.html  index.html

这就是刚刚上面所说的启动nginx时候访问的页面,其实就是这里的index.html页面


2 0