1. 安装依赖
    yum install gcc gcc-c++ make unzip pcre pcre-devel zlib zlib-devel libxml2 libxml2-devel  readline readline-devel ncurses ncurses-devel perl-devel perl-ExtUtils-Embed openssl-devel -y
    
  2. 获取nginx安装包,解压后进入安装目录进行编译安装nginx
    1,进入指定目录,获取安装包
    cd /usr/local/src
    wget -c http://nginx.org/download/nginx-1.16.0.tar.gz
    
    2,解压安装包
    tar -zxvf nginx-1.16.0.tar.gz
    
    3,进入安装目录,执行编译安装nginx
    cd nginx-1.16.0
    ./configure
    make -j4 && make install
    
    4,查询安装的nginx版本
    nginx -v
    
    5,创建nginx用户组和运行nginx的用户
    groupadd www
    useradd -g www www
    
    6,编辑nginx.conf配置文件
    cd /usr/local/nginx/conf
    vi nginx.conf
    
  3. nginx.conf配置文件的具体内容
    user  www www;
    worker_processes  8;
    
    error_log  /usr/local/nginx/logs/error.log;
    pid       /usr/local/nginx/logs/nginx.pid;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    worker_rlimit_nofile 65535;
    
    events {
        use epoll;
        worker_connections  65535;
    }
    
    
    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"';
    
        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile   on;
        tcp_nopush on;
    
        keepalive_timeout 60;
    
        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 256k;
        fastcgi_intercept_errors on;
    
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";
    
        limit_conn_zone $binary_remote_addr zone=perip:10m;
        limit_conn_zone $server_name zone=perserver:10m;
    
        server_tokens off;
        access_log off;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   /web;
                index  index.html index.htm;
            }
    
            #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;
            #}
        }
    
    
        # 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;
        #    }
        #}
    
    }
    
  4. 验证nginx的配置文件无误,创建测试文件
    1,验证配置文件是否无误
    nginx -t
    
    2,在根目录下创建web目录以及测试用的index.html文件,并指定所属用户和组
    mkdir /web
    touch /web/index.html
    chown -R www:www /web
    
  5. 将nginx设置成服务并开机启动
    1,进入/etc/init.d目录,创建nginx的shell文件
    cd /etc/init.d
    vi nginx
    
    2,将以下内容复制到shell文件中
    #! /bin/sh
    # chkconfig: 2345 55 25
    # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
    # run 'update-rc.d -f nginx defaults', or use the appropriate command on your
    # distro. For CentOS/Redhat run: 'chkconfig --add nginx'
    
    ### BEGIN INIT INFO
    # Provides:          nginx
    # Required-Start:    $all
    # Required-Stop:     $all
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: starts the nginx web server
    # Description:       starts nginx using start-stop-daemon
    ### END INIT INFO
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    NAME=nginx
    NGINX_BIN=/usr/local/nginx/sbin/$NAME
    CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
    PIDFILE=/usr/local/nginx/logs/$NAME.pid
    ulimit -n 8192
    case "$1" in
        start)
            echo -n "Starting $NAME... "
                    if [ -f $PIDFILE ];then
                            mPID=`cat $PIDFILE`
                            isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
                            if [ "$isStart" != '' ];then
                                    echo "$NAME (pid `pidof $NAME`) already running."
                                    exit 1
                            fi
                    fi
    
            $NGINX_BIN -c $CONFIGFILE
    
            if [ "$?" != 0 ] ; then
                echo " failed"
                exit 1
            else
                echo " done"
            fi
            ;;
    
        stop)
            echo -n "Stoping $NAME... "
                    if [ -f $PIDFILE ];then
                            mPID=`cat $PIDFILE`
                            isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
                            if [ "$isStart" = '' ];then
                                    echo "$NAME is not running."
                                    exit 1
                            fi
                    else
                            echo "$NAME is not running."
                            exit 1
            fi
            $NGINX_BIN -s stop
    
            if [ "$?" != 0 ] ; then
                echo " failed. Use force-quit"
                exit 1
            else
                echo " done"
            fi
            ;;
    
        status)
                    if [ -f $PIDFILE ];then
                            mPID=`cat $PIDFILE`
                            isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
                            if [ "$isStart" != '' ];then
                                    echo "$NAME (pid `pidof $NAME`) already running."
                                    exit 1
                            else
                                    echo "$NAME is stopped"
                                    exit 0
                            fi
                    else
                            echo "$NAME is stopped"
                            exit 0
            fi
            ;;
        restart)
            $0 stop
            sleep 1
            $0 start
            ;;
    
        reload)
            echo -n "Reload service $NAME... "
                    if [ -f $PIDFILE ];then
                            mPID=`cat $PIDFILE`
                            isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
                            if [ "$isStart" != '' ];then
                                    $NGINX_BIN -s reload
                                    echo " done"
                            else
                                    echo "$NAME is not running, can't reload."
                                    exit 1
                            fi
                    else
                            echo "$NAME is not running, can't reload."
                            exit 1
                    fi
            ;;
    
        configtest)
            echo -n "Test $NAME configure files... "
            $NGINX_BIN -t
            ;;
    
        *)
            echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
            exit 1
            ;;
    esac
    
    3,将nginx服务加入chkconfig服务列表
    chkconfig --add /etc/init.d/nginx
    
    4,启动nginx,并查看是否启动成功
    service nginx start
    ps -aux|grep nginx
    
    5,设置开机启动
    chkconfig nginx on

Logo

鲲鹏展翅 立根铸魂 深耕行业数字化

更多推荐