Centos6 安装nginx

一、编译安装nginx

1、安装nginx所需要的库pcre,pcre的全称为:perl compatible regular expression即perl正则表达式,是为了使nginx具备URL重写功能的rewrite模块

[root@web01 ~]# yum install pcre pcre-devel -y    ####安装
[root@web01 ~]# rpm -qa pcre pcre-devel        ####检查
pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64

2、安装nginx

 

[root@web01 ~]# yum install openssl-devel openssl -y  ###安装nginx基础依赖包
[root@web01 ~]# rpm -qa openssl-devel openssl    ###检查是否安装成功
openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64
[root@web01 ~]# mkdir -p /server/tools    ######创建一个目录,管理下载的工具软件
[root@web01 ~]# useradd nginx -s /sbin/nologin -M  #####添加nginx用户,不需要登陆,不需要家目录
[root@web01 ~]# cd /server/tools/
[root@web01 tools]# tar xf nginx-1.6.3.tar.gz    #####解压nginx,软件请自行下载  
[root@web01 tools]# cd nginx-1.6.3
[root@web01 nginx-1.6.3]# mkdir -p /application/nginx-1.6.3    创建nginx安装目录
[root@web01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module  ###配置安装参数
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found    

./configure: error: C compiler cc is not found      #####没有安装编译环境

[root@web01 nginx-1.6.3]# yum install gcc gcc-c++ -y    ######安装编译工具
[root@web01 nginx-1.6.3]# rpm -qa gcc gcc-c++      ###检查
gcc-c++-4.4.7-18.el6_9.2.x86_64
gcc-4.4.7-18.el6_9.2.x86_64
[root@web01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module  ##重新配置
.......省略.......
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library
    
  nginx path prefix: "/application/nginx-1.6.3/"        ###安装目录
  nginx binary file: "/application/nginx-1.6.3//sbin/nginx"    #####执行程序
  nginx configuration prefix: "/application/nginx-1.6.3//conf"    ####配置文件目录
  nginx configuration file: "/application/nginx-1.6.3//conf/nginx.conf"  #####主配置文件
  nginx pid file: "/application/nginx-1.6.3//logs/nginx.pid"      ####pid文件
  nginx error log file: "/application/nginx-1.6.3//logs/error.log"    ####错误日志
  nginx http access log file: "/application/nginx-1.6.3//logs/access.log"  ####访问控制文件
  nginx http client request body temporary files: "client_body_temp"    
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@web01 nginx-1.6.3]# make
[root@web01 nginx-1.6.3]# make install    ####安装
[root@web01 nginx-1.6.3]# ln -s /application/nginx-1.6.3 /application/nginx  ####软连接到/application/nginx,方便管理软件版本;
##################注释################
--prefix=/application/nginx-1.6.3   安装路径
--user=nginx  指定进程用户权限
--group=nginx  指定进程用户组权限
--with-http_stub_status_module  激活nginx状态信息
--with-http_ssl_module  激活ssl加密功能

3、启动并检查nginx

[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -t    ########-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
[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx    ######启动nginx
[root@web01 nginx-1.6.3]# lsof -i :80      #####查看nginx端口是否成功启动
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   3797  root    6u  IPv4  17610      0t0  TCP *:http (LISTEN)
nginx   3798 nginx    6u  IPv4  17610      0t0  TCP *:http (LISTEN)

4、客户端验证

[root@web01 ~]# /etc/init.d/iptables stop  ####关闭防火墙
[root@web01 ~]# chkconfig iptables off    ####关闭防火墙开机启动
[root@web01 ~]# getenforce       ###检查selinux状态,为关闭状态,如果不是请自行关闭
Disabled
[root@web01 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:7d:93:f3 brd ff:ff:ff:ff:ff:ff inet 192.168.127.11/24 brd 192.168.127.255 scope global eth0    #####服务器地址 inet6 fe80::20c:29ff:fe7d:93f3/64 scope link valid_lft forever preferred_lft forever
windows客户端在浏览器输入http://192.168.127.11

也可在本地用命令检测

[root@web01 ~]# wget 127.0.0.1
--2018-06-05 17:22:27--  http://127.0.0.1/
正在连接 127.0.0.1:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:612 [text/html]
正在保存至: “index.html”

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

2018-06-05 17:22:27 (50.2 MB/s) - 已保存 “index.html” [612/612])

二、nginx目录结构和可加载模块功能

1、目录结构

[root@web01 ~]# tree /application/nginx
/application/nginx
├── client_body_temp    
├── conf      #####nginx配置文件目录
│   ├── fastcgi.conf     ######fastcgi相关参数的配置文件       
│   ├── fastcgi.conf.default  ######fastcgi原始备份
│   ├── 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        ####bginx的默认站点目录
│   ├── 50x.html    ####错误页面
│   └── index.html    ###默认首页文件
├── logs
│   ├── access.log    ####nginx默认访问日志文件
│   ├── error.log      ####默认错误日志文件
│   └── nginx.pid      #####pid文件
├── proxy_temp      ####临时目录
├── sbin        ####命令目录
│   └── nginx
├── scgi_temp     ####临时目录
└── uwsgi_temp    ###临时目录

2、功能模块

  • ngx_http_core_module:包括一些核心的http参数配置,对应nginx的配置为http区块部分
  • ngx_http_access_module:访问控制模块,用来控制网站用户对nginx的访问
  • ngx_http_gzip_module:压缩模块,对nginx返回的数据压缩,属于性能优化模块
  • ngx_http_fastcgi_module:fastCGI模块,和动态应用相关的模块
  • ngx_http_proxy_module:proxy代理模块
  • ngx_http_upstream_module:负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
  • ngx_http_rewrite_module:URL地址重写模块
  • ngx_http_limit_conn_module:限制用户并发连接数及请求数模块
  • ngx_http_limit_req_module:根据定义的key限制nginx请求过程的速率
  • ngx_http_log_module:访问日志模块,以指定的格式记录nginx客户访问日志等信息
  • ngx_http_auth_basic_module:web认证模块,设置web用户通过账号,密码访问nginx
  • ngx_http_ssl_module:ssl模块,用于加密的http连接,如https
  • ngx_http_stub_status_module:nginx基本访问状态信息模块

3、nginx主配置文件

[root@web01 ~]# cat /application/nginx/conf/nginx.conf      #####nginx配置文件#号后面内容为默认

#user  nobody;      ###nginx默认权限用户,编译时已指定--user=nginx
worker_processes  1;        #####nginx进程数,默认为1,可根据服务器核心数修改,如单cpu4核心,可修改为4

#error_log  logs/error.log;      ###默认错误日志文件
#error_log  logs/error.log  notice;      ####notice错误级别日志
#error_log  logs/error.log  info;       ###info日志

#pid        logs/nginx.pid;      ####pid文件


events {
    worker_connections  1024;      ####nginx默认支持的最大连接数
}


http {       ########http全局配置模块
    include       mime.types;        ####调用支持的媒体类型文件
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    ###默认日志内容格式
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;      ####默认访问日志

    sendfile        on;        ####高效传输模式
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;      ####超时时间

    #gzip  on;        ####支持gzip压缩

    server {                    #####虚拟主机配置模块
        listen       80;      ####监听80端口
        server_name  localhost;    

        #charset koi8-r;

        #access_log  logs/host.access.log  main;      #####该虚拟主机访问日志

        location / {            ####location模块,
            root   html;          #####站点数据文件目录
            index  index.html index.htm;    ####默认网站首页
        }

        #error_page  404              /404.html;      ####错误页面

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;    #####错误页面
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {    
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {          
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {          ######基于ip,端口的虚拟主机范例
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {            #####基于ssl认证的范例
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

 

 

转载于:https://www.cnblogs.com/suffergtf/p/9135204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值