1. 程式人生 > >nginx-1.12.2安裝配置

nginx-1.12.2安裝配置

1、下載nginx1.12.2並解壓

2、進入對應目錄下

cd nginx-1.12.2

3、編譯安裝

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre --with-stream

make && make install

4、設定環境變數

vim /etc/profile

export PATH=$PATH:/usr/local/nginx/sbin //檔案末尾增加

source /etc/profile

5、設定配置檔案

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

6、啟動nginx

nginx //啟動
nginx -t //檢查配置檔案是否正常
nginx -s reload //重新載入配置檔案
nginx -s stop //停止
nginx -s reopen //重啟

7、開機啟動指令碼

  新增開機啟動指令碼

vim /etc/init.d/nginx

  指令碼內容

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

  給予指令碼可以執行的許可權

chmod a+x /etc/init.d/nginx

8、設定開機自啟

chkconfig --add nginx
chkconfig nginx on

9、nginx的常用設定

user  nginx nginx;
worker_processes  2;

#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;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
    	server_name  localhost;

        location / {
            proxy_pass  http://101.132.36.189:1028;
        }
    }

    server {
        listen 80;
        server_name xxx.xxx.xxx;
        root /www/xxx;
        index index.html index.htm index.php;

        location / {  
           if (!-e $request_filename) {  
               rewrite ^/(.*)$ /index.php/$1 last;  
           }  
        }

        location ~ \.php { 
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$; 
            fastcgi_param PATH_INFO $fastcgi_path_info; 
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}