Nginx安装和基础配置

本文详细指导如何在Linux环境中安装Nginx 1.16.1版本,涉及依赖环境配置、源码编译、基本配置文件编辑,包括监听端口、虚拟主机设置、SSL支持等,并提供启动脚本和开机自启设置,同时展示了防火墙端口开放步骤。
摘要由CSDN通过智能技术生成

Nginx安装和基础配置

  1. nginx官网

    http://nginx.org

  2. 安装环境

    部署ip部署软件开放端口
    192.168.199.10nginx-1.16.180
  3. 依赖环境安装

    yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++ make libtool wget lrzsz -y

  4. nginx安装

    tar -zxvf nginx-1.16.1.tar.gz && cd nginx-1.16.1
    ./configure --prefix=/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module  --with-http_sub_module --without-http_access_module
    make && make install
    
  5. 基础配置

    vim conf/nginx.conf
    
    #定义nginx的启动用户,不建议使用root
    #user  nobody;
    #定位为cpu的内核数量,因为我的环境配置是2核,所以就写2。不过这值最多也就是8,8个以上也就没什么意义了
    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 {
            #客户端线程轮询方法、内核2.6版本以上的建议使用epoll
            use epoll;
            #设置一个worker可以打开的最大连接数
            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;
        #为错误页面上的nginx版本信息,建议关闭,提升安全性
        server_tokens  off;
        sendfile        on;
        tcp_nopush     on;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 20m;
        #keepalive_timeout  0;
        keepalive_timeout  120;
        #gzip是告诉nginx采用gzip后的数据来传输文件,会大量减少我们的发数据的量,如果作为图片服务器可以打开此配置
        #gzip  on;
        #gzip_min_length  1k;
        #gzip_buffers     4 16k;
        #gzip_http_version 1.0;
        #gzip_comp_level 2;
        #gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        #gzip_vary on;
        #gzip_disable msie6;
        server {
            listen       80;
            server_name  localhost;
            #charset koi8-r;
            #access_log  logs/host.access.log  main;
            location / {
                root   html;
                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;
        #    }
        #}
    }
    
  6. 配置启动脚本

    vim /usr/lib/systemd/system/nginx.service
    
    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    Documentation=http://nginx.org/en/docs/
    After=syslog.target network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    PIDFile=/data/server/nginx/logs/nginx.pid
    ExecStartPre=/data/server/nginx/sbin/nginx -t
    ExecStart=/data/server/nginx/sbin/nginx
    ExecReload=/data/server/nginx/sbin/nginx -s reload
    ExecStop=/usr/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
  7. 开机自启设置

    # 加入开机启动
    systemctl daemon-reload
    systemctl enable nginx
    # 查看开机是否启动成功
    systemctl is-enabled nginx
    # 启动服务
    systemctl start nginx
    # 停止服务
    systemctl stop nginx
    
  8. 防火墙开放端口

    firewall-cmd --zone=public --add-port=80 --permanent
    firewall-cmd --reload
    setenforce 0
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值