Linux 安装nginx 1.8.1 及配置

来源:互联网 发布:steam淘宝 编辑:程序博客网 时间:2024/06/06 08:52

1.创建 /usr/local/nginx

2.wget http://nginx.org/download/nginx-1.11.13.tar.gz

3.

[root@Server1 nginx-1.8.1]# ./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
4.生成脚本及配置文件:make
5.安装:make install
复制代码
[root@Server1 sbin]# /usr/local/nginx/sbin/nginx/nginxnginx: [emerg] getpwnam("nginx") failed  #没有nginx用户
解决:useradd -s /sbin/nologin -M nginx
[root@Server1 sbin]# /usr/local/nginx/sbin/nginx/nginxnginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)  #目录不存在
解决:创建var/tmp/nginx/client/  目录[root@Server1 sbin]# /usr/local/nginx/sbin/nginx/nginx  #直到没有报错,才算启动完成
复制代码

9、重读配置文件和关闭服务:

[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx  #启动 服务[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx   -s  reload  #不停止服务重读配置文件[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx -s stop #停止服务  #停止服务

10.验证端口是否开启:

复制代码
[root@Server1 sbin]# ps -ef | grep nginxroot     13228     1  0 Apr23 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx/nginx  #nginx的主进程,只有一个主进程nginx    13229 13228  0 Apr23 ?        00:00:00 nginx: worker  process #nginx工作进程,默认只有一个,可以通过修改nginx.conf中的worker_processes  1; 参数启动多个工作进程root     13295  1400  0 00:01 pts/0    00:00:00 grep --color=auto nginx[root@Server1 local]# lsof -i:8090  #显示占用8090的进程COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 13337 root 6u IPv4 5932680 0t0 TCP *:8090 (LISTEN)nginx 13338 nginx 6u IPv4 5932680 0t0 TCP *:8090 (LISTEN)
复制代码

 11、通过给nginx的主进程ID号发送信号启动或停止nginx:

获取nginx主进程号的办法:

[root@Server1 nginx]# cat /var/run/nginx/nginx.pid   #查看nginx的pid文件,此文件保存的就是nginx的主进程id13337  #次ID是随机的,每次启动都不一样的[root@Server1 nginx]# ps -ef   | grep nginx  #过滤nginx的进程号root     13337     1  0 00:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx/nginxnginx    21568 13337  0 10:58 ?        00:00:00 nginx: worker process

支持的信号:

复制代码
[root@Server1 nginx]# kill  -QUIT 13337  #平缓关闭Nginx,即不再接受新的请求,但是等当前请求处理完毕后再关闭Nginx。[root@Server1 nginx]# kill  -TERM  21665 #快速停止Nginx服务[root@Server1 nginx]# kill  -HUP 21703 #使用新的配置文件启动进程然后平缓停止原有的nginx进程,即平滑重启。 [root@Server1 nginx]# kill -USR1 21703   #重新打开配置文件,用于nginx 日志切割日期切割的脚本:#!/bin/bashPID=`cat /var/run/nginx/nginx.pid`mv   /var/log/nginx/access.log   /var/log/nginx/`date  +%Y_%m_%d:%H:%M:%S`.access.logkill -USR1 $PID[root@Server1 nginx]# kill -USR2 21703   #使用新版本的nginx文件启动服务,然后在平缓停止原有的nginx服务,即平滑升级。[root@Server1 nginx]# kill -WINCH  21703  #平滑停止nginx的工作进程,用于nginx平滑升级。
复制代码

 

三:nginx 主配置文件:nginx.conf

3.1:默认配置:配置文件默认保存在path/conf当中,默认的配置文件为nginx.conf,以下是编译安装后的默认配置:

复制代码
[root@Server1 conf]# grep -v "#" nginx.conf | grep -v  "^$" 
user  root;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}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;    gzip            on;    gzip_min_length 1k;    gzip_buffers 4 16k;    gzip_comp_level 5; gzip_types text/plain  text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;    gzip_vary         on;    tcp_nopush          on;    tcp_nodelay         on;    keepalive_timeout   65;        client_max_body_size 10M;         client_header_timeout 300s;    client_body_timeout 600s;   upstream crm{   ip_hash;server 172.16.217.17:8080;   }    server {        listen       8001;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;              location / {          proxy_pass http://crm;          proxy_redirect default;      }        #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;        #}    } upstream protal{   ip_hash;server 172.16.217.17:9001;   } server {        listen       9001;        server_name  localhost;        location / {          proxy_pass http://protal;          proxy_redirect default;      }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }        upstream gateway{   ip_hash;server 172.16.217.15:8080;server 172.16.217.16:8080;} server {        listen       7001;        server_name  localhost;         location / {          proxy_pass http://gateway;          proxy_redirect default;      }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    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;    #    }    #}}

原创粉丝点击