zoukankan      html  css  js  c++  java
  • linux下安装nginx与配置

    linux系统为Centos 64位

    准备目录

    [root@instance-3lm099to ~]# mkdir /usr/local/nginx
    [root@instance-3lm099to ~]# cd /usr/local/nginx/

    下载

    从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1.5.9.tar.gz直接在Linux上用命令下载)

    解压

    解压 tar -zxvf nginx-1.5.9.tar.gz 

    解压好后移至目录

    [root@instance-3lm099to nginx]# cd nginx-1.5.9/

    设置Nginx安装路径,如果没有指定,默认为/usr/local/nginx

    [root@instance-3lm099to nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
    checking for OS
     + Linux 3.10.0-862.3.2.el7.x86_64 x86_64
    checking for C compiler ... not found

    如果出现红色字体错误,需要执行下

    [root@instance-3lm099to nginx-1.14.0]# yum -y install gcc gcc-c++ autoconf automake make

    再次执行

    [root@instance-3lm099to nginx-1.14.0]# ./configure --prefix=/usr/local/nginx

    可能会出现这个错误

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.

    如果出现了,就执行下这个

    [root@instance-3lm099to nginx-1.14.0]# yum -y install openssl openssl-devel

    再次执行

    复制代码
    [root@instance-3lm099to nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
    checking for OS
     + Linux 3.10.0-862.3.2.el7.x86_64 x86_64
    checking for C compiler ... found
     + using GNU C compiler
     + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
    checking for gcc -pipe switch ... found
    checking for -Wl,-E switch ... found
    checking for gcc builtin atomic operations ... found
    checking for C99 variadic macros ... found
    checking for gcc variadic macros ... found
    checking for gcc builtin 64 bit byteswap ... found
    checking for ioctl(FIONBIO) ... found
    checking for struct tm.tm_gmtoff ... found
    checking for struct dirent.d_namlen ... not found
    checking for struct dirent.d_type ... found
    checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
    checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
    checking for openat(), fstatat() ... found
    checking for getaddrinfo() ... found
    checking for PCRE library ... found
    checking for PCRE JIT support ... found
    checking for zlib library ... found
    creating objs/Makefile
    
    Configuration summary
      + using system PCRE library
      + OpenSSL library is not used
      + using system zlib library
    
      nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/nginx/sbin/nginx"
      nginx modules path: "/usr/local/nginx/modules"
      nginx configuration prefix: "/usr/local/nginx/conf"
      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
      nginx pid file: "/usr/local/nginx/logs/nginx.pid"
      nginx error log file: "/usr/local/nginx/logs/error.log"
      nginx http access log file: "/usr/local/nginx/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"
    复制代码

    编译

    make (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)

    复制代码
    [root@instance-3lm099to nginx-1.14.0]# make
    make -f objs/Makefile
    make[1]: Entering directory `/usr/local/nginx/nginx-1.14.0'
    cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
            -o objs/src/core/nginx.o 
            src/core/nginx.c
    cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
            -o objs/src/core/ngx_log.o 
            src/core/ngx_log.c
    
            -o objs/src/http/modules/ngx_http_upstream_keepalive_module.o 
            src/http/modules/ngx_http_upstream_keepalive_module.c
    cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules 
            -o objs/src/http/modules/ngx_http_upstream_zone_module.o 
            src/http/modules/ngx_http_upstream_zone_module.c
    cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
            -o objs/ngx_modules.o 
            objs/ngx_modules.c
    cc -o objs/nginx 
    objs/src/core/nginx.o 
    objs/src/core/ngx_log.o 
    objs/src/core/ngx_palloc.o 
    objs/src/core/ngx_array.o 
    objs/src/core/ngx_list.o 
    objs/src/core/ngx_hash.o 
    objs/src/core/ngx_buf.o 
    objs/src/core/ngx_queue.o 
    objs/src/core/ngx_output_chain.o 
    objs/src/core/ngx_string.o 
    objs/src/core/ngx_parse.o 
    objs/src/core/ngx_parse_time.o 
    objs/src/core/ngx_inet.o 
    objs/src/core/ngx_file.o 
    objs/src/http/modules/ngx_http_upstream_keepalive_module.o 
    objs/src/http/modules/ngx_http_upstream_zone_module.o 
    objs/ngx_modules.o 
    -ldl -lpthread -lcrypt -lpcre -lz 
    -Wl,-E
    sed -e "s|%%PREFIX%%|/usr/local/nginx|" 
            -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" 
            -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" 
            -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" 
            < man/nginx.8 > objs/nginx.8
    make[1]: Leaving directory `/usr/local/nginx/nginx-1.14.0'
    复制代码

    安装

    make install  (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)

    复制代码
    [root@instance-3lm099to nginx-1.14.0]# make install
    make -f objs/Makefile install
    make[1]: Entering directory `/usr/local/nginx/nginx-1.14.0'
    test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
    test -d '/usr/local/nginx/sbin' 
            || mkdir -p '/usr/local/nginx/sbin'
    test ! -f '/usr/local/nginx/sbin/nginx' 
            || mv '/usr/local/nginx/sbin/nginx' 
                    '/usr/local/nginx/sbin/nginx.old'
    cp objs/nginx '/usr/local/nginx/sbin/nginx'
    test -d '/usr/local/nginx/conf' 
            || mkdir -p '/usr/local/nginx/conf'
    cp conf/koi-win '/usr/local/nginx/conf'
    cp conf/koi-utf '/usr/local/nginx/conf'
    cp conf/win-utf '/usr/local/nginx/conf'
    test -f '/usr/local/nginx/conf/mime.types' 
            || cp conf/mime.types '/usr/local/nginx/conf'
    cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
    test -f '/usr/local/nginx/conf/fastcgi_params' 
            || cp conf/fastcgi_params '/usr/local/nginx/conf'
    cp conf/fastcgi_params 
            '/usr/local/nginx/conf/fastcgi_params.default'
    test -f '/usr/local/nginx/conf/fastcgi.conf' 
            || cp conf/fastcgi.conf '/usr/local/nginx/conf'
    cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
    test -f '/usr/local/nginx/conf/uwsgi_params' 
            || cp conf/uwsgi_params '/usr/local/nginx/conf'
    cp conf/uwsgi_params 
            '/usr/local/nginx/conf/uwsgi_params.default'
    test -f '/usr/local/nginx/conf/scgi_params' 
            || cp conf/scgi_params '/usr/local/nginx/conf'
    cp conf/scgi_params 
            '/usr/local/nginx/conf/scgi_params.default'
    test -f '/usr/local/nginx/conf/nginx.conf' 
            || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
    cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
    test -d '/usr/local/nginx/logs' 
            || mkdir -p '/usr/local/nginx/logs'
    test -d '/usr/local/nginx/logs' 
            || mkdir -p '/usr/local/nginx/logs'
    test -d '/usr/local/nginx/html' 
            || cp -R html '/usr/local/nginx'
    test -d '/usr/local/nginx/logs' 
            || mkdir -p '/usr/local/nginx/logs'
    make[1]: Leaving directory `/usr/local/nginx/nginx-1.14.0'
    复制代码

    启动

    参数 -c 指定了配置文件的路径,如果不加的话就是使用默认的配置文件

    [root@instance-3lm099to nginx-1.14.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    停止

    停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的

    查询nginx主进程号

    ps -ef | grep nginx
    在进程列表里 面找master进程,它的编号就是主进程号了。

    发送信号

    从容停止Nginx

    kill -QUIT 主进程号

    快速停止Nginx

    kill -TERM 主进程号

    强制停止Nginx

    pkill -9 nginx

    另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
    kill -信号类型 '/usr/nginx/logs/nginx.pid'

    平滑重启

    如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。
    平滑重启命令:
    kill -HUP 主进程号或进程号文件路径

    或者使用

    /usr/nginx/sbin/nginx -s reload

    注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。

    判断Nginx配置是否正确命令

    nginx -t -c /usr/nginx/conf/nginx.conf

    或者

    /usr/nginx/sbin/nginx -t
     
    如下图:
     

    访问

    在浏览器中输入IP:端口号(默认80),出现如下图所示,说明安装成功。

    加了中文注解的nginx.conf

    复制代码
      1 user www www;
      2 # 工作进程个数,可配置多个
      3 worker_processes auto;
      4 
      5 error_log /data/wwwlogs/error_nginx.log crit;
      6 pid /var/run/nginx.pid;
      7 worker_rlimit_nofile 51200;
      8 
      9 events {
     10   use epoll;
     11   # 单个进程最大连接数
     12   worker_connections 51200;
     13   multi_accept on;
     14 }
     15 
     16 http {
     17   include mime.types;
     18   default_type application/octet-stream;
     19   server_names_hash_bucket_size 128;
     20   client_header_buffer_size 32k;
     21   large_client_header_buffers 4 32k;
     22   client_max_body_size 1024m;
     23   client_body_buffer_size 10m;
     24   sendfile on;
     25   tcp_nopush on;
     26   keepalive_timeout 120;
     27   server_tokens off;
     28   tcp_nodelay on;
     29 
     30   fastcgi_connect_timeout 300;
     31   fastcgi_send_timeout 300;
     32   fastcgi_read_timeout 300;
     33   fastcgi_buffer_size 64k;
     34   fastcgi_buffers 4 64k;
     35   fastcgi_busy_buffers_size 128k;
     36   fastcgi_temp_file_write_size 128k;
     37   fastcgi_intercept_errors on;
     38 
     39   #Gzip Compression
     40   gzip on;
     41   gzip_buffers 16 8k;
     42   gzip_comp_level 6;
     43   gzip_http_version 1.1;
     44   gzip_min_length 256;
     45   gzip_proxied any;
     46   gzip_vary on;
     47   gzip_types
     48     text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
     49     text/javascript application/javascript application/x-javascript
     50     text/x-json application/json application/x-web-app-manifest+json
     51     text/css text/plain text/x-component
     52     font/opentype application/x-font-ttf application/vnd.ms-fontobject
     53     image/x-icon;
     54   gzip_disable "MSIE [1-6].(?!.*SV1)";
     55 
     56   #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
     57   open_file_cache max=1000 inactive=20s;
     58   open_file_cache_valid 30s;
     59   open_file_cache_min_uses 2;
     60   open_file_cache_errors on;
     61 
     62 ######################## default ############################
     63   # 服务器集群名称  和下面的location地址对应
     64   upstream myServer {
     65     # weigth参数表示权值,权值越高被分配到的几率越大
     66     # server 127.0.0.1:8080 weight=1;
     67     # server 127.0.0.1:8060 weight=1;
     68     server 47.93.10.184:8080;
     69     server 47.93.10.184:8081;
     70 
     71   }
     72 
     73   # 每一个server相当于一个代理服务器
     74   server {
     75   # 监听端口,默认80
     76   listen 8848;
     77   # 当前服务的域名,可以有多个,用空格分隔(我们是本地所以是localhost)  www.kolbe.cn,如果想局域网访问 需要改成当前的ip
     78   server_name localhost;
     79   #server_name _;
     80   access_log /data/wwwlogs/access_nginx.log combined;
     81   root /data/wwwroot/default;
     82   # 当没有指定主页时,默认会选择这个指定的文件,可多个,空格分隔
     83   index index.html index.htm index.php;
     84   # 表示匹配的路径,这时配置了/表示所有请求都被匹配到这里
     85   location / {
     86       # 请求转向自定义的服务器列表
     87       proxy_pass http://myServer;
     88   }
     89   location /nginx_status {
     90     stub_status on;
     91     access_log off;
     92     allow 127.0.0.1;
     93     deny all;
     94     }
     95   location ~ [^/].php(/|$) {
     96     #fastcgi_pass remote_php_ip:9000;
     97     fastcgi_pass unix:/dev/shm/php-cgi.sock;
     98     fastcgi_index index.php;
     99     include fastcgi.conf;
    100     }
    101   location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    102     expires 30d;
    103     access_log off;
    104     }
    105   location ~ .*.(js|css)?$ {
    106     expires 7d;
    107     access_log off;
    108     }
    109   location ~ /.ht {
    110     deny all;
    111     }
    112   }
    113 
    114 ########################## vhost #############################
    115   include vhost/*.conf;
    116 }
    复制代码
     如果nginx无法访问 需要做如下操作

    查看Linux服务是否启动。

    ps -ef | grep nginx

    解决办法:
    1,添加 80 段端口配置

                             firewall-cmd --zone=public --add-port=80/tcp --permanent
    2,重启防火墙

                      systemctl restart firewalld.service

     
  • 相关阅读:
    【华为云技术分享】从自建MongoDB聊聊云数据库MongoDB的蓬勃张力
    【华为云技术分享】【Python算法】分类与预测——支持向量机
    Python 中更优雅的环境变量设置方案
    Python解析照片EXIF信息,获取坐标位置
    【华为云技术分享】【Python算法】分类与预测——决策树
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】小熊派IoT开发板华为物联网操作系统LiteOS内核实战教程01-IoT-Studio介绍及安装
    【华为云技术分享】【开发记录】Linux服务器维护常用命令(二)
    【PHP输出两位小数】使用PHP来输出保留两位小数的数字【原创】
    【SERVER_NAME】PHP中的SERVER_NAME【原创】
  • 原文地址:https://www.cnblogs.com/dxqNet/p/13356816.html
Copyright © 2011-2022 走看看