nginx安装部署

                                                         编译安装nginx

1。安装nginx所需要的pcre库

1)查看当前linux环境版本信心,如下:

[root@nginx-server-01 ~]# cat /etc/redhat-release 
CentOS release 6.7 (Final)
[root@nginx-server-01 ~]# uname -r
2.6.32-573.el6.x86_64
[root@nginx-server-01 ~]# uname -m
x86_64
[root@nginx-server-01 ~]#

  

2)采用yum的方式安装pcre和pcre-devel(但凡包,有devel的后缀必须安装),用rpm查看

[root@nginx-server ~]# yum install pcre pcre-devel -y
[root@nginx-server ~]# rpm -qa pcre pcre-devel
pcre-7.8-7.el6.x86_64

pcre-devel-7.8-7.el6.x86_64

  

3)安装openssl和openssl-devel,这两个包是nginx在使用https服务的时候要用的模块,如果不安装,安装nginx的时候会报错

[root@nginx-server ~]# yum install openssl openssl-devel -y
[root@nginx-server ~]# rpm -qa openssl openssl-devel
openssl-devel-1.0.1e-48.el6_8.1.x86_64
openssl-1.0.1e-48.el6_8.1.x86_64

  

2、编译安装nginx
1)首先我们要规划一下以后固定存放各种软件包的一个目录

/application/


[root@nginx-server ~]# mkdir /ruanjianbao
[root@nginx-server ~]# cd /ruanjianbao/
[root@nginx-server ruanjianbao]#

  

2)下载nginx软件包

[root@nginx-server ruanjianbao]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
[root@nginx-server ruanjianbao]# ls -l 
total 788
-rw-r--r--. 1 root root 805253 Apr 8 2015 nginx-1.6.3.tar.gz
[root@nginx-server ruanjianbao]#

  

3)创建一个nginx的进程用户,此用户不登录

[root@nginx-server ruanjianbao]# useradd -s /sbin/nologin nginx -M
[root@nginx-server ruanjianbao]# tail -1 /etc/passwd
nginx:x:500:500::/home/nginx:/sbin/nologin
[root@nginx-server ruanjianbao]#

  

4)解压nginx软件包到当前目录

[root@nginx-server ruanjianbao]# tar zvxf nginx-1.6.3.tar.gz 
[root@nginx-server ruanjianbao]# ls
nginx-1.6.3 nginx-1.6.3.tar.gz
[root@nginx-server ruanjianbao]# cd nginx-1.6.3
[root@nginx-server nginx-1.6.3]#

  

5)开始编译安装nginx

[root@nginx-server nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
–prefix #nginx安装目录,默认在/usr/local/nginx
–pid-path #pid问件位置,默认在logs目录
–lock-path #lock问件位置,默认在logs目录
–with-http_ssl_module #开启HTTP SSL模块,以支持HTTPS请求。
–with-http_dav_module #开启WebDAV扩展动作模块,可为文件和目录指定权限
–with-http_flv_module #支持对FLV文件的拖动播放
–with-http_realip_module #支持显示真实来源IP地址
–with-http_gzip_static_module #预压缩文件传前检查,防止文件被重复压缩
–with-http_stub_status_module #取得一些nginx的运行状态
–with-mail #允许POP3/IMAP4/SMTP代理模块
–with-mail_ssl_module #允许POP3/IMAP/SMTP可以使用SSL/TLS
–with-pcre=../pcre-8.11 #注意是未安装的pcre路径
–with-zlib=../zlib-1.2.5 #注意是未安装的zlib路径
–with-debug #允许调试日志
–http-client-body-temp-path #客户端请求临时文件路径
–http-proxy-temp-path #设置http proxy临时文件路径
–http-fastcgi-temp-path #设置http fastcgi临时文件路径
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #设置uwsgi 临时文件路径
–http-scgi-temp-path=/var/tmp/nginx/scgi #设置scgi 临时文件路径
[root@nginx-server nginx-1.6.3]# make &&make install

  

6)制作软连接,这条ln命令意义十分重大,这可是生产环境的经验,方便后续使用

[root@nginx-server nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx
[root@nginx-server nginx-1.6.3]# ls -l /application/nginx
lrwxrwxrwx. 1 root root 25 Jun 16 00:02 /application/nginx -> /application/nginx-1.6.3/
[root@nginx-server nginx-1.6.3]#

  

7)启动并检测安装结果

[root@nginx-server nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx
[root@nginx-server nginx-1.6.3]# ls -l /application/nginx
lrwxrwxrwx. 1 root root 25 Jun 16 00:02 /application/nginx -> /application/nginx-1.6.3/
[root@nginx-server nginx-1.6.3]#

  

8)在Windows或者本地查看实际效果

[root@nginx-server ~]# wget 127.0.0.1
--2016-06-16 00:07:10-- http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK #返回值200 ok 表示nginx正常启动并能访问
Length: 612 [text/html]
Saving to: “index.html”

100%[======================================>] 612 --.-K/s in 0s

2016-06-16 00:07:10 (43.8 MB/s) - “index.html” saved [612/612]
[root@nginx-server ~]#

#####特别提示:windows访问时注意防火墙,重启的时候记得设置开机自启动
[root@nginx-server ~]# echo "service iptables stop" >>/etc/profile
[root@nginx-server ~]# echo "/application/nginx/sbin/nginx" >>/etc/profile

#############################################################################################################################
nginx疑难杂症
1.当启动nginx时以下报错

[root@nginx-server ~]# /application/nginx/sbin/nginx 
nginx: [emerg] getpwnam("nginx") failed
[root@nginx-server ~]# 

ps:说明再启动时没有nginx进程用户
我们创建一个就好了
[root@nginx-server ~]# c;learuseradd -s /sbin/nologin nginx -M

2.编译安装pcre时,gcc不全导致报错

make[1]:***[pcrecpp.lo] 错误1
make[1]:leaving directory `/home/gjlin/tools/pcre-8.30`
make:*** [all] 错误2
解决方法:
[root@nginx-server ~]# yum install gcc-c++ -y

3.查看nginx编译时的参数

[root@nginx-server ~]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
[root@nginx-server ~]#

4.浏览器,wget或curl等软件访问不了nginx页面
1)关闭selinux
2) 关闭防火墙
3)查看本地端口进程
4)查看http进程是否存在
###############################################################################################################################
nginx目录结构

[root@nginx-server ~]# tree /application/nginx
/application/nginx
├── client_body_temp
├── conf #这是nginx所有配置文件所在的目录
│   ├── fastcgi.conf #fastcgi相关参数的配置文件
│   ├── fastcgi.conf.default #fastcgi.conf原始备份
│   ├── fastcgi_params #fastcgi的参数文件
│   ├── fastcgi_params.default #备份
│   ├── koi-utf 
│   ├── koi-win
│   ├── mime.types #媒体类型
│   ├── mime.types.default #备份
│   ├── nginx.conf #nginx主配置文件
│   ├── nginx.conf.default #备份
│   ├── scgi_params #scgi相关参数文件,一般用不到
│   ├── scgi_params.default #备份
│   ├── uwsgi_params #uwsgi相关参数文件,一般用不过到
│   ├── uwsgi_params.default #备份
│   └── win-utf
├── fastcgi_temp #fastcgi临时数据目录
├── html #这是编译安装时nginx的默认站点目录
│   ├── 50x.html #错误页面优雅替代显示文件
│   └── index.html #默认首页文件
├── logs #这是nginx默认日志路径,包括错误日志和访问日志
│   ├── access.log #默认访问日志
│   ├── error.log #默认错误日志
│   └── nginx.pid #启动nginx自动生成进程号
├── proxy_temp #临时目录
├── sbin #nginx的命令目录
│   └── nginx #nginx启动命令
├── scgi_temp #临时目录
└── uwsgi_temp #临时目录

9 directories, 21 files
[root@nginx-server ~]#

  

##############################################################################################################################
nginx主配置文件详解

[root@nginx-server conf]# egrep -v "#|^$" nginx.conf (此条命令可以去掉nginx配置文件中的井号和空行,使用前先备份)
worker_processes 1; #起始进程数
##以上是main区,nginx的核心功能模块
events { #事件区块开始
worker_connections 1024; #每个worker进程所支持的最大事件连接数
}
##以上是events区,nginx核心功能模块
http { #http区模块开始
include mime.types; #默认支持的媒体类型库文件
default_type application/octet-stream; #默认支持的媒体类型
sendfile on; #开启搞笑传输模式
keepalive_timeout 65; #连接超时事件
##以上是http区,nginx http的核心模块 
server { 
listen 80; #提供服务端口,默认80
server_name localhost; #提供服务的域名主机名
location / { #第一个location区块开始
root html; #站点跟目录。相当于nginx的安装目录
index index.html index.htm; #定义主页文件,多个主页文件空格分开
}
error_page 500 502 503 504 /50x.html;#出现对应的http状态码时,使用50x。html回应
location = /50x.html { #第二个location 访问50x。html
root html; #指定对应的站点目录为html
}
}
##以上为server区
}
[root@nginx-server conf]#

  

原文地址:https://www.cnblogs.com/Carr/p/7389074.html