Debian编译安装nginx

1.安装编译器软件及依赖。这里需要注意里面有些依赖是我其他软件是,大家可以根据实际情况取舍.

apt install libjemalloc-dev uuid-dev libatomic1 libatomic-ops-dev expat unzip autoconf automake libtool libgd-dev libmaxminddb-dev libxslt1-dev libxml2-dev g++ curl libpcre3-dev cmake libunwind-dev golang

如果jemalloc无法安装可以自己编译如下:

wget https://github.com/jemalloc/jemalloc/archive/5.2.1.tar.gz
mv 5.2.1.tar.gz jemalloc-5.2.1.tar.gz
tar xvzf jemalloc-5.2.1.tar.gz
cd jemalloc-5.2.1
./autogen.sh
make && make install

2.下载nginx最新版原版和我需要的一些nginx扩展.这里我引入了zlib (Cloudflare优化版本)、Certificate Transparency (CT)、Brotli (eustas版本)以及实现http3测试cloudflare的quiche patch补丁.

wget http://nginx.org/download/nginx-1.19.0.tar.gz
tar xvzf nginx-1.19.0.tar.gz
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
git clone https://github.com/cloudflare/zlib.git zlib-cf
cd zlib-cf
make -f Makefile.in distclean
cd ..
git clone https://github.com/grahamedgecombe/nginx-ct.git
git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli
git submodule update --init --recursive
cd ..
apt install make build-essential clang golang cmake -y
 curl https://sh.rustup.rs -sSf | sh
1
 source $HOME/.cargo/env
cargo -v

vim $HOME/.cargo/config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

 ln -s $HOME/.cargo/bin/cargo /usr/local/bin/cargo

git clone --recursive  https://github.com/cloudflare/quiche
cd nginx-1.19.0
patch -p01 < ../quiche/extras/nginx/nginx-1.16.patch

3.编译安装nginx

cd nginx-1.19.0  
./configure --prefix=/usr/local/nginx  --build="quiche-$(git --git-dir=../quiche/.git rev-parse --short HEAD)" --with-http_v3_module  --with-openssl=../quiche/deps/boringssl --with-quiche=../quiche --with-zlib=../zlib-cf  --add-module=../nginx-ct --add-module=../ngx_brotli --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module  --with-pcre --with-pcre-jit --with-ld-opt='-ljemalloc' 
make && make test && make install

4.配置nginx

#增加系统变量:
vim /etc/profile
#增加以下内容
PATH=$PATH:/usr/local/php/bin:/usr/local/nginx/sbin
export PATH

#激活系统变量
source /etc/profile

#增加nginx系统服务
vim  /lib/systemd/system/nginx.service
#增加以下内容
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000

[Install]
WantedBy=multi-user.target

#为系统服务增加权限
chmod +x  /lib/systemd/system/nginx.service
#设置系统服务为开机启动
systemctl enable nginx
#设置nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
#增加如下内容
user www www;
worker_processes auto;

error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 10m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  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 128k;
  fastcgi_intercept_errors on;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  ##Brotli Compression
  #brotli on;
  #brotli_comp_level 6;
  #brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

  ##If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  #open_file_cache max=1000 inactive=20s;
  #open_file_cache_valid 30s;
  #open_file_cache_min_uses 2;
  #open_file_cache_errors on;

######################## default ############################
  server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      #fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }
########################## vhost #############################
  include vhost/*.conf;
}

#文件内容结束
#建立文件文件夹
mkdir -p /data/wwwlogs/
mkdir -p /data/wwwroot/default
mkdir -p /usr/local/nginx/conf/vhost
vim /data/wwwlogs/access_nginx.log
#启动nginx
service nginx start
#查询nginx状态
service nginx status

至此,我们完成了nginx的手动编译与安装,可以配套php-fpm的设置使用.