Nginx 1.17.8安装部署教程

来自:互联网
时间:2021-09-07
阅读:

概述

根据实际需要部署web服务,并进行适当优化配置。

基础环境配置

安装编译所需的依赖

Yum源配置参考:https://freexyz.cn/server/98288.html

yum -y install gd-devel wget make gcc-c++ gcc pcre-devel openssl openssl-devel
创建Nginx用户
Nginx_User='nginx'
useradd -M -s /sbin/nologin "${Nginx_User}"

创建Nginx安装目录

Nginx_Path='/usr/local/nginx17'
Nginx_Src_Path="${Nginx_Path}/src"
mkdir -p "${Nginx_Src_Path}"

下载nginx并解压

cd "${Nginx_Src_Path}"
Nginx_Version='nginx-1.17.8'
wget "https://nginx.org/download/${Nginx_Version}.tar.gz"
tar xvf "${Nginx_Version}.tar.gz"
cd "${Nginx_Version}"

Nginx安装

下载openssl1.1.1g源包

[root@master ~]# cd /usr/src/
[root@master src]# wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
[root@master src]# tar zxf openssl-1.1.1g.tar.gz 

安装nginx

[root@localhost nginx-1.17.8]# ./configure --prefix="${Nginx_Path}" --sbin-path="${Nginx_Path}/sbin/nginx" --modules-path="${Nginx_Path}/modules" --conf-path="${Nginx_Path}/conf/nginx.conf" --error-log-path="${Nginx_Path}/logs/error.log" --pid-path="${Nginx_Path}/tmp/nginx.pid" --lock-path="${Nginx_Path}/tmp/nginx.lock" --user="${Nginx_User}" --group="${Nginx_User}" --with-threads --with-file-AIo --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-http_sub_module --add-module=/usr/src/ngx_cache_purge-2.3 --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt="-Wl,-E" --with-openssl=/usr/src/openssl1.1.1g/
[root@localhost nginx-1.17.8]# make -j 2 && make install

放行防火墙80,443端口

[root@localhost sbin]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost sbin]# firewall-cmd --zone=public --add-port=443/tcp --permanent
success
[root@localhost sbin]# firewall-cmd --reload
success

修改nginx配置文件

[root@VM_0_9_centos conf]# cat nginx.conf 
#启动用户名及组
user  nginx nginx;
#定义了nginx对外提供web服务时的worker进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为"auto"将尝试自动检测它)
worker_processes auto;
#只记录严重的错误
error_log  /usr/local/nginx17/logs/nginx_error.log  error;
#PID文件存放地址
pid        /usr/local/nginx17/logs/nginx.pid;
#更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和Nginx可以处理比"ulimit -a"更多的文件,所以把这个值设高,这样nginx就不会有"too many open files"问题了。
worker_rlimit_nofile 1920;

events
    {
        #使用epoll
        use epoll;
        #设置可由一个worker进程同时打开的最大连接数。如果设置了worker_rlimit_nofile,可以将这个值设得很高
        worker_connections 2000;
        #告诉nginx收到一个新连接通知后接受尽可能多的连接
        multi_accept on;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        #可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。
        sendfile   on;
        #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。
        tcp_nopush on;
        #给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。将它设置低些可以让ngnix持续工作的时间更长。
        keepalive_timeout 60;
        #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
        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压缩
        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功能。
        gzip_disable   "MSIE [1-6]\.";
        limit_conn_zone $binary_remote_addr zone=perip:10m;
        limit_conn_zone $server_name zone=perserver:10m;
        #关闭在错误页面中显示的nginx版本
        #server_tokens off;
        #设置nginx是否存储访问日志。关闭这个选项可以让读取磁盘IO操作更快
        access_log off;

        #

        #设置默认字符集
        charset UTF-8;
        server
            {
                listen 80;
                return 500;
            }
include /usr/local/nginx17/vhost/*.conf;
}

nginx启停脚本

#!/bin/bash
#Nginx启停脚本 By:admin@ym68.cc
Nginx_Path='/usr/local/nginx17'
Nginx_Path_Sbin="${Nginx_Path}/sbin/nginx"
Nginx_Status() {
	Nginx_Auth_Check=`ps -C nginx |awk '/nginx/ {print $1}'`
}
Nginx_ReStatus() {
	Nginx_Auth_Check=`ps -C nginx |awk '/nginx/ {print $1}'`
	if [ -z "${Nginx_Auth_Check}" ];then
		echo -e "\033[32mNginx Server Not Running!\033[0m"
		exit 1
	fi
}
Nginx_Start() {
	Nginx_Status
	if [ ! -z "${Nginx_Auth_Check}" ];then
		echo -e '\033[31mNginx Server Is Running!\033[0m'
	else
		${Nginx_Path_Sbin}
		Nginx_ReStatus
		echo -e "\033[32mNginx Server Is Running!\033[0m"
	fi
}
Nginx_Reload() {
	Nginx_Status
	if [ ! -z "${Nginx_Auth_Check}" ]
	then
		${Nginx_Path_Sbin} -s reload
		Nginx_ReStatus
		echo -e "\033[32mNginx Server Config Is Reload!\033[0m"
	else
		echo -e '\033[31mNginx Server Not Running!\033[0m'
	fi
}
Nginx_Stop() {
	Nginx_Status
	if [ ! -z "${Nginx_Auth_Check}" ];then
		kill -QUIT ${Nginx_Auth_Check}
		echo -e '\033[32mNginx Server Is Stop!\033[0m'
	else
		echo -e "\033[31mNginx Server Not Running!\033[0m"
	fi
}
if [ ! -f "${Nginx_Path_Sbin}" ];then
	echo "Nginx_Sbin Not Exist!"
	exit 1
fi
case $1 in
start)
	Nginx_Docker
	Nginx_Start
	;;
stop)
	Nginx_Stop
	;;
restart)
	Nginx_Stop
	sleep 0.1
	Nginx_Start
	;;
reload)
	Nginx_Reload
	;;
*)
	echo 'Parameter: start | stop | restart | reload'
esac

检查配置文件并启动nginx

[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx17/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx17/conf/nginx.conf test is successful
[root@localhost sbin]# ./nginx
返回顶部
顶部