Nginx安装配置


01.建立用户及安装依赖包:

  • 创建nginx用户及用户组:
groupadd nginx && useradd -r -g nginx nginx
  • 安装依赖包:
yum -y install gcc gcc-c++ glibc  openssl openssl-devel pcre-devel zlib-devel
  • 安装开发环境:
yum group install "Development Tools"


02.安装nginx:

  • 进入下载目录,操作都在此目录中完成;如果没有,则创建此目录:
cd /opt/Downloads
  • 下载nginx:
wget http://download.wenjun1984.cn/Nginx/nginx-1.16.0.tar.gz
  • 解压nginx文件并进入源码目录:
tar -zxvf nginx-1.16.0.tar.gz && cd nginx-1.16.0
  • 编译nginx:
./configure --user=nginx --group=nginx \
--prefix=/opt/Apps/nginx \
--pid-path=/opt/Apps/nginx/nginx.pid \
--lock-path=/opt/Apps/nginx/nginx.lock \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module
  • 安装nginx:
make && make install
  • 查看nginx安装版本及编译参数:
/opt/Apps/nginx/sbin/nginx -V


03.配置nginx:

  • 创建子配置文件目录:
mkdir /opt/Apps/nginx/conf/conf.d
  • 修改nginx配置文件为如下配置(全部删除):
vim /opt/Apps/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_rlimit_nofile 65536;

events {
    use epoll;
    worker_connections 10240;
    }

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;

    log_format main  
        '{"@timestamp":"$time_iso8601",'
        '"@source":"$server_addr",'
        '"hostname":"$hostname",'
        '"ip":"$http_x_forwarded_for",'
        '"client":"$remote_addr",'
        '"request_method":"$request_method",'
        '"scheme":"$scheme",'
        '"domain":"$server_name",'
        '"referer":"$http_referer",'
        '"request":"$request_uri",'
        '"args":"$args",'
        '"size":$body_bytes_sent,'
        '"status": $status,'
        '"responsetime":$request_time,'
        '"upstreamtime":"$upstream_response_time",'
        '"upstreamaddr":"$upstream_addr",'
        '"http_user_agent":"$http_user_agent",'
        '"https":"$https"'
        '}';

    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_cache_path /opt/Apps/nginx/nginx_fastcgi levels=1:2 keys_zone=fastcgi:30m max_size=100m;

    proxy_connect_timeout 120;
    proxy_send_timeout 120;
    proxy_read_timeout 120;
    proxy_buffer_size 64k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_cache_path /opt/Apps/nginx/nginx_proxy levels=1:2 keys_zone=proxy:30m max_size=100m;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types text/plain application/x-javascript text/css application/xml image/png image/jpeg image/gif audio/ogg audio/mp3;
    gzip_vary on;

    include /opt/Apps/nginx/conf/conf.d/*.conf;
    }

stream {
    include /opt/Apps/nginx/conf/conf.d/*.stream;
    }
  • 创建一个主配置文件,添加如下内容:
vim /opt/Apps/nginx/conf/conf.d/main.conf
server {
    listen 80;
    server_name nginx.test.com;
    error_log logs/nginx_test_error.log error;
    access_log logs/nginx_test_access.log main gzip flush=5m;

    location / {
        root html/main;
        index index.html index.htm index.php;
    }

    location /status {
        stub_status on;
    }
}
  • 创建与配置文件对应的子目录:
mkdir /opt/Apps/nginx/html/main,
  • 创建子目录的首页文件:
echo "hello world" >> /opt/Apps/nginx/html/main/index.html
  • 更改nginx目录的属主属组:
chown -R nginx:nginx /opt/Apps/nginx/


04.nginx服务的启动和停止:

  • 创建一个nginx的服务文件,添加如下内容:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/Apps/nginx/nginx.pid
ExecStartPre=/usr/bin/rm -f /opt/Apps/nginx/nginx.pid
ExecStartPre=/opt/Apps/nginx/sbin/nginx -t
ExecStart=/opt/Apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置nginx开机自启并且启动nginx:
systemctl enable nginx.service
systemctl start nginx.service
  • /opt/Apps/nginx/sbin/nginx -s reload,重新加载配置文件而不重启服务。
  • 查看80端口是否启动:
netstat -ntlp | grep 80
  • 在hosts文件中添加一条记录,将nginx.test.com域名指向该IP地址:
10.1.32.200 nginx.test.com
文档更新时间: 2020-03-10 17:57   作者:闻骏