Nginx安装,目录结构与配置文件详解

1.Nginx简介

  Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。

  

1)查看当前Linux系统环境,命令如下:
[root@www ~]# cat /etc/redhat-release
 CentOS release 6.6(Final)
[root@www ~]# uname -r
 2.6.32-504.el6.x86_64
[root@www ~]# uname -m
 x86_64#<==64位系统
2)采用yum安装方式安装pcre,命令如下:
[root@www ~]# yum install pcre pcre-devel -y
[root@www ~]# rpm -qa pcre pcre-devel

  3)yum安装操作后检查安装结果,命令如下:(pcre-7.8-6.el6.x86_64提示:yum安装的pcre版本有些低,不过一般情况不影响使用)

[root@www ~]# rpm -qa pcre pcre-devel
 pcre-devel-7.8-6.el6.x86_64

2.安装Nginx

  Nginx的英文官方网站是http://nginx.org/,在这里可以查看Nginx的各个软件版本信息。Nginx软件有三种版本:稳定版、开发版和历史稳定版

  1.检查并安装Nginx基础依赖包pcre-devel、openssl-devel操作命令如下:

[root@www ~]# rpm -qa pcre-devel pcre
 pcre-devel-7.8-6.el6.x86_64       #<==pcre的devel包已经安装!
 pcre-7.8-6.el6.x86_64             #<==pcre包已经安装!
[root@www ~]# rpm -qa openssl-devel openssl
 openssl-1.0.1e-30.el6.x86_64       #<==这里没有opensll“devel”字符串的包

  2.安装openssl-devel,安装openssl-devel的命令及检查命令如下:

[root@www ~]# yum install -y openssl openssl-devel
[root@www ~]# rpm -qa openssl openssl-devel
 openssl-devel-1.0.1e-30.el6.8.x86_64
 openssl-1.0.1e-30.el6.8.x86_64
  3.开始安装Nginx
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
#下载软件包,进入http://nginx.org/download/ 复制对应版本的链接地址。提示,如果发现Nginx软件下载地址已不可用,可能版本已更新,可去官方地址http://www.nginx.org下载。

  4.安装的操作过程演示

[root@www ~]# mkdir -p /home/oldboy/tools
[root@www ~]# cd /home/oldboy/tools
[root@www tools]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
[root@www tools]# ls -l nginx-1.6.3.tar.gz 
-rw-r--r--1 root root 804164 11月 23 15:26 nginx-1.6.3.tar.gz
[root@www tools]# useradd nginx -s /sbin/nologin -M
[root@www tools]# tar xf nginx-1.6.3.tar.gz 
[root@www tools]# cd nginx-1.6.3
[root@www nginx-1.6.3]#./configure  --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module  --with-http_ssl_module
[root@www nginx-1.6.3]# make
[root@www nginx-1.6.3]# make install
[root@www nginx-1.6.3]# ln -s /application/nginx-1.6.3 /application/nginx
[root@www nginx-1.6.3]# ls -l /application/nginx/总用量 16
drwxr-xr-x. 2 root root 4096 7月  20 11:19 conf
drwxr-xr-x. 2 root root 4096 7月  20 11:19 html
drwxr-xr-x. 2 root root 4096 7月  20 11:19 logs
drwxr-xr-x. 2 root root 4096 7月  20 11:19 sbin

  安装过程出现如下错误,执行命令:

yum install openssl openssl-devel-y

  出现的错误信息如下:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules,or install the OpenSSL library
into the system,or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

3.启动并检查安装结果

  1- 启动前检查配置文件语法,执行命令

[root@www tools]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

  2-启冬Nginx服务,执行命令

/application/nginx/sbin/nginx

  3-查看Nginx服务对应的端口是否启动

方法一; lsof -i :80

方法二; netstat -lnt|grep 80

  4.检查Nginx启动实际效果

  在浏览器输入 http://10.0.0.8 (10.0.0.8wei an安装Nginx服务器的IP地址)

  在Linux 用wget命令检测    wget 本机的连接的IP地址

  curl命令检测           curl本机连接的ip地址()

4.Nginx软件功能模块说明

  1)Nginx核心功能模块(Coer functionality),主要负责Nginx的全局应用,针对主配置文件Main区块和Events区块,http://nginx.org/en/docs/ngx_core_module.html(更多查看)

  2)标准的http功能模块集合,http://nginx.org/en/docs(可以查看更多的功能模块)

                                   企业应用场景功能模块汇总

                  

5. Nginx目录结构

  执行命令 tree /application/nginx   ;如果tree命令找不到就执行 yum install tree -y安装

  

 1 [root@www ~]# tree /application/nginx/
 2 /application/nginx/
 3 |-- client_body_temp
 4 |-- conf                                  #这是Nginx所有配置文件的目录,极其重要
 5 |   |-- fastcgi.conf                    #fastcgi相关参数的配置文件
 6 |   |-- fastcgi.conf.default                 #fastcgi.conf的原始备份
 7 |   |-- fastcgi_params                   #fastcgi的参数文件
 8 |   |-- fastcgi_params.default
 9 |   |-- koi-utf
10 |   |-- koi-win
11 |   |-- mime.types                      #媒体类型,
12 |   |-- mime.types.default
13 |   |-- nginx.conf                      #这是Nginx默认的主配置文件
14 |   |-- nginx.conf.default
15 |   |-- scgi_params                     #scgi相关参数文件,一般用不到
16 |   |-- scgi_params.default
17 |   |-- uwsgi_params                       #uwsgi相关参数文件,一般用不到
18 |   |-- uwsgi_params.default
19 |   `-- win-utf
20 |-- fastcgi_temp                       #fastcgi临时数据目录
21 |-- html                       #这是编译安装时Nginx的默认站点目录,类似
22                     Apache的默认站点htdocs目录
23 |   |--50x.html     #     错误页面优雅替代显示文件,例如:出现502错误时会调用此页面
24          #     error_page   500502503504  /50x.html;
25 |   `-- index.html   #     默认的首页文件,首页文件名字是在nginx.conf中事先定义好的。
26 |-- logs          #这是Nginx默认的日志路径,包括错误日志及访问日志
27 |   |-- access.log      #     这是Nginx的默认访问日志文件,使用tail -f access.log,可以实时观看网站用户访问情况信息
28 |   |-- error.log      #     这是Nginx的错误日志文件,如果Nginx出现启动故障等问题,一定要看看这个错误日志
29 |   `-- nginx.pid      #     Nginx的pid文件,Nginx进程启动后,会把所有进程的ID号写到此文件
30 |-- proxy_temp       #临时目录
31 |-- sbin      #这是Nginx命令的目录,如Nginx的启动命令nginx
32 |   `-- nginx      #Nginx的启动命令nginx
33 |-- scgi_temp      #临时目录
34 `-- uwsgi_temp      #临时目录
35 9 directories,21 files

6)Nginx主配置文件nginx.conf

  1.Nginx主配置文件nginx.conf是一个纯文本类型的文件(其他配置文件也是如此)Nginx整个配置文件nginx.conf主题框架:

  

  2。查看Nginx配置文件

  

 1 [root@www conf]# egrep -v "#|^$" nginx.conf.default     ←去掉包含#号和空行的内容
 2 worker_processes  1;                ← worker进程的数量
 3 events {                              ← 事件区块开始
 4     worker_connections  1024;            ←每个worker进程支持的最大连接数
 5 }                                    ←事件区块结束
 6 http {                               ← HTTP区块开始
 7     include       mime.types;            ← Nginx支持的媒体类型库文件
 8     default_type  application/octet-stream;     ← 默认的媒体类型
 9     sendfile        on;       ←开启高效传输模式
10     keepalive_timeout  65;       ←连接超时
11     server {            ←第一个Server区块开始,表示一个独立的虚拟主机站点
12         listen       80;      ← 提供服务的端口,默认80
13         server_name  localhost;       ←提供服务的域名主机名
14         location / {            ←第一个location区块开始
15             root   html;       ←站点的根目录,相当于Nginx的安装目录
16             index  index.html index.htm;      ← 默认的首页文件,多个用空格分开
17         }          ←第一个location区块结果
18         error_page   500502503504  /50x.html;     ← 出现对应的http状态码时,使用50x.html回应客户
19         location = /50x.html {          ←location区块开始,访问50x.html
20             root   html;      ← 指定对应的站点目录为html
21         }
22     }

猜你喜欢

转载自www.cnblogs.com/liang-io/p/9340335.html