linux编译安装nginx

前言

  • 系统:centos7和debian11均验证可行
  • 本文将nginx默认支持的编译参数都加上了,所以需要的依赖比较多,酌情配置。

步骤

  1. 假设安装在/usr/local/nginx,创建安装目录
mkdir -p /usr/local/nginx
  1. 下载源码包:http://nginx.org/en/download.html
  2. 安装依赖包:
# yum
yum -y install gcc pcre-devel zlib-devel openssl-devel libxml2-devel libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel perl-devel perl-ExtUtils-Embeb

# apt
apt install -y libpcre3-dev openssl libssl-dev libxml2-dev libgd-dev libxml2 libgeoip-dev libxslt-dev
  1. 预编译:
./configure \
    --with-threads \
    --with-file-aio \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-http_geoip_module=dynamic \
    --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_auth_request_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_degradation_module \
    --with-http_slice_module \
    --with-http_stub_status_module \
    --with-stream_ssl_module \
    --with-stream_realip_module \
    --with-stream_geoip_module=dynamic \s
    --with-stream_ssl_preread_module \
    --with-compat \
    --with-pcre-jit \
    --prefix=/usr/local/nginx
  1. 编译:make
  2. 编译安装:make install

配置文件简单优化

  • 启动进程数,通常设置成和CPU的数量相等或者auto
worker_processes auto;
worker_cpu_affinity auto;
  • nginx打开的最多文件描述符数量(一个nginx打开的最多文件描述符数目,理论值应为最多打开文件数与nginx进程数相除,但nginx分配请求并不均匀,可以与 ulimit -n 的值保持一致)
worker_rlimit_nofile 102400;
  • event块
events {
    use epoll;(epoll是多路复用IO的一种方式,但是仅用于linux 2.6以上版本的内核,可以大幅提高nginx的性能)
    worker_connections 102400;(单个工作进程的最大并发连接数。最大连接数 = 连接数 * 进程数)
    accept_mutex on;(对多个nginx进程进行序列化,避免多个进程对连接的争抢。)
    multi_accept on;(尽可能地接受请求)
}

基础命令

  • nginx -t:检查nginx配置文件是否语法正确
  • nginx -c /usr/local/nginx/conf/nginx.conf:指定nginx配置文件
  • nginx -s reload:热加载nginx配置
  • nginx -s quit:安全停止nginx
posted @ 2020-11-30 12:41  花酒锄作田  阅读(171)  评论(0编辑  收藏  举报