1. 程式人生 > >Linux中Nginx編譯安裝、解除安裝並新增到service中

Linux中Nginx編譯安裝、解除安裝並新增到service中

  • nginx解除安裝

    刪除相應資料夾和自啟動即可

  • nginx安裝:

    • tar zxvf nginx-1.12.1.tar.gz
    • 更改資料夾名稱為nginx
    • cd nginx
    • ./configure
    • make
    • make install
    • 覆蓋二進位制檔案:cp objs/nginx /usr/local/nginx/sbin/
  • 將nginx新增到service

    • 建立檔案:/etc/init.d/nginx

      # !/bin/bash
      #
      # nginx - this script starts and stops the nginx daemon
      #
      # chkconfig:   - 85 15
      # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
      #   proxy and IMAP/POP3 proxy server
      # processname: nginx
      # config:  /etc/nginx/nginx.conf
      # config:  /etc/sysconfig/nginx
      # pidfile: /var/run/nginx.pid
      
      # Source function library.
      . /etc/rc.d/init.d/functions
      
      # Source networking configuration.
      . /etc/sysconfig/network
      
      # Check that networking is up.
      [ "$NETWORKING" = "no" ] && exit 0
      
      nginx="/usr/local/nginx/sbin/nginx"
      prog=$(basename $nginx)
      
      NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
      
      [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
      
      lockfile=/var/lock/subsys/nginx
      
      make_dirs() {
        # make required directories
        user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
        if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
        useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
      if [ `echo $opt | grep '.*-temp-path'` ]; then
        value=`echo $opt | cut -d "=" -f 2`
        if [ ! -d "$value" ]; then
      # echo "creating" $value
      mkdir -p $value && chown -R $user $value
        fi
      fi
      done
        fi
      }
      
      start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        make_dirs
        echo -n $"Starting $prog: "
        daemon $nginx -c $NGINX_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
      }
      
      stop() {
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
      }
      
      restart() {
        configtest || return $?
        stop
        sleep 1
        start
      }
      
      reload() {
        configtest || return $?
        echo -n $"Reloading $prog: "
        killproc $nginx -HUP
        RETVAL=$?
        echo
      }
      
      force_reload() {
        restart
      }
      
      configtest() {
        $nginx -t -c $NGINX_CONF_FILE
      }
      
      rh_status() {
        status $prog
      }
      
      rh_status_q() {
        rh_status >/dev/null 2>&1
      }
      
      case "$1" in
        start)
      rh_status_q && exit 0
      $1
      ;;
        stop)
      rh_status_q || exit 0
      $1
      ;;
        restart|configtest)
      $1
      ;;
        reload)
      rh_status_q || exit 7
      $1
      ;;
        force-reload)
      force_reload
      ;;
        status)
      rh_status
      ;;
        condrestart|try-restart)
      rh_status_q || exit 0
      ;;
        *)
      echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
      exit 2
      esac
      
    • 執行命令

      cd /etc/rc.d/init.d
       chmod +x nginx
      /sbin/chkconfig --level 345 nginx on
      
    • 服務操作:

      啟動:service nginx start
      停止:service nginx stop
      重啟:service nginx restart
      檢視狀態:service nginx status
      
  • nginx配置檔案demo

    user root;
    worker_processes auto;
    
    error_log /data/wwwlogs/error_nginx.log crit;
    pid /var/run/nginx.pid;
    worker_rlimit_nofile 51200;
    
    events {
      use epoll;
      worker_connections 51200;
      multi_accept on;
    }
    
    http {
      include mime.types;
      default_type application/octet-stream;
      server_names_hash_bucket_size 128;
      client_header_buffer_size 32k;
      large_client_header_buffers 4 32k;
      client_max_body_size 1024m;
      client_body_buffer_size 10m;
      sendfile on;
      tcp_nopush on;
      keepalive_timeout 120;
      server_tokens off;
      tcp_nodelay on;
    
      fastcgi_connect_timeout 300;
      fastcgi_send_timeout 300;
      fastcgi_read_timeout 300;
      fastcgi_buffer_size 64k;
      fastcgi_buffers 4 64k;
      fastcgi_busy_buffers_size 128k;
      fastcgi_temp_file_write_size 128k;
      fastcgi_intercept_errors on;
    
      #Gzip Compression
      gzip on;
      gzip_buffers 16 8k;
      gzip_comp_level 6;
      gzip_http_version 1.1;
      gzip_min_length 256;
      gzip_proxied any;
      gzip_vary on;
      gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
      gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
      #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.
      open_file_cache max=1000 inactive=20s;
      open_file_cache_valid 30s;
      open_file_cache_min_uses 2;
      open_file_cache_errors on;
    
    ######################## default ############################
      server {
        listen 80;
        server_name _;
        access_log /data/wwwlogs/access_nginx.log combined;
        #error_page 404 /404.html;
        #error_page 502 /502.html;
        location /nginx_status {
          access_log off;
          allow 127.0.0.1;
          deny all;
        }
        location / {
          root /ROOT;
          index index.html index.htm;
          proxy_pass http://localhost:8080;
        }
        location @apache {
          proxy_pass http://127.0.0.1:88;
        }
        location ~ [^/]\.php(/|$) {
          proxy_pass http://127.0.0.1:88;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
          root /usr/local/tomcat/webapps/ROOT;
          expires 30d;
          access_log off;
        }
        location ~ .*\.(js|css)?$ {
          root /usr/local/tomcat/webapps/ROOT;
          expires 7d;
          access_log off;
        }
        location ~ /\.ht {
          deny all;
        }
        location ~/group1/M00 {
          alias /data/fastdfs/storage/data;
        }
      }
      server {
        listen 8888;  
    
        location ~/group([0-9])/M00 {
          root /data/fastdfs/storage/data;
          ngx_fastdfs_module;  
        }
      }
    ########################## vhost #############################
      include vhost/*.conf;
    }