Linux中Nginx编译安装、卸载并添加到service中

nginx卸载

删除相应文件夹和自启动即可

nginx安装:

安装依赖

	yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

编辑安装

[root@os1 lib]# wget http://nginx.org/download/nginx-1.13.6.tar.gz

[root@os1 lib]# tar -zvxf nginx-1.13.6.tar.gz

[root@os1 lib]# cd nginx-1.13.6

[root@os1 nginx-1.13.6]# ./configure --with-http_ssl_module --with-http_gzip_static_module --prefix=/usr/lib/nginx

[root@os1 nginx-1.13.6]# make

[root@os1 nginx-1.13.6]# make install

安装完成后位置为:/usr/lib/nginx

常用命令

  • 启动:

    [root@os1 nginx-1.13.6]# cd /usr/lib/nginx/sbin
    
    [root@os1 sbin]# ./nginx
    
  • 停止:

    [root@os1 sbin]# ./nginx -s stop
    
  • 重启

    [root@os1 sbin]# ./nginx -s reload
    
  • 检查配置文件正确性

    [root@os1 sbin]# ./nginx -t
    
  • 查看nginx版本

    [root@os1 sbin]# ./nginx -v
    
  • 查看nginx安装配置

    [root@os1 sbin]# ./nginx -V
    
  • 查看线程:

    [root@os1 nginx]# ps -ef | grep nginx
    root     13850     1  0 09:03 ?        00:00:00 nginx: master process ./nginx
    nobody   14323 13850  0 09:08 ?        00:00:00 nginx: worker process
    root     15009  1891  0 09:17 pts/0    00:00:00 grep nginx
    

简单转发配置

http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  sendfile on;

  keepalive_timeout 65;

  server {
    listen 80;

    server_name localhost;

    location / {
      proxy_pass http://localhost:18080;
    }

  }
}

将nginx添加到service

  • 创建文件:/etc/init.d/nginx

      vi/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/lib/nginx/sbin/nginx"
      prog=$(basename $nginx)
      
      NGINX_CONF_FILE="/usr/lib/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;
}

你可能感兴趣的:(架构设计)