系统相关
首页 > 系统相关> > Debian 11 安装 Nginx 1.22.0

Debian 11 安装 Nginx 1.22.0

作者:互联网

阿里云轻量级服务器 Debian 11 安装 Nginx 1.22.0

一、从官网下载 nginx 压缩包

sudo wget https://nginx.org/download/nginx-1.22.0.tar.gz -O /opt/nginx-1.22.0.tar.gz

二、安装 nginx 需要的 依赖

sudo apt install libtool make gcc g++ libpcre3 libpcre3-dev openssl libssl-dev zlib1g zlib1g-dev -y

三、编译并安装 nginx

(1)解压 nginx 压缩包

sudo tar -zxvf /opt/nginx-1.22.0.tar.gz -C /opt

(2)指定安装路径及启用的模块

cd /opt/nginx-1.22.0 && sudo ./configure --prefix=/usr/local/nginx-1.22.0 --with-http_ssl_module --with-http_sub_module --with-http_gunzip_module --with-http_stub_status_module --with-pcre

(3)编译并安装

sudo make && sudo make install

(4)创建 nginx 的配置及工作目录文件夹

sudo mkdir -p /etc/nginx/conf.d /etc/nginx/default.d /var/www/html

(5)复制 html文件 到工作目录

sudo cp /usr/local/nginx-1.22.0/html/* /var/www/html

(6)清除解压出来的资源文件夹

sudo rm -rf /opt/nginx-1.22.0

四、编辑 nginx 的配置文件

(1)(A)编辑 nginx.conf 文件

sudo vi /usr/local/nginx-1.22.0/conf/nginx.conf
########## nginx.conf 配置文件内容
worker_processes auto;                                          #设置值和CPU核心数一致

user nginx nginx;

error_log /usr/local/nginx-1.22.0/logs/error.log crit;          #日志位置和日志级别

pid /usr/local/nginx-1.22.0/nginx.pid;                          #进程id

worker_rlimit_nofile 65535;                                     #指定此进程可以打开的最大文件描述符的值

events
{
  use epoll;
  worker_connections 65535;
}

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';


  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;

  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  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;
  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;
  gzip_vary on;

  #limit_zone crawler $binary_remote_addr 10m;

  include /etc/nginx/default.d/*.conf;
  include /etc/nginx/conf.d/*.conf;

}

(1)(B)或者使用已编辑好的样板

sudo wget https://files.cnblogs.com/files/blogs/755424/nginx.conf.css -O /usr/local/nginx-1.22.0/conf/nginx.conf

(2)(A)编辑 default.conf 文件

sudo vi /etc/nginx/default.d/default.conf
################# default.conf 配置内容
server{
  listen           80;
  listen           [::]:80;
  server_name      localhost;

  location / {
    root           /var/www/html;
    index          index.html;
  }

}

(2)(B)或者使用已编辑好的样本

sudo wget https://files.cnblogs.com/files/blogs/755424/default.conf.css -O /etc/nginx/default.d/default.conf

五、指定 nginx 运行的角色

(1)创建 nginx 组及角色

sudo groupadd nginx && sudo useradd -g nginx -s /sbin/nologin nginx

(2)修改 nginx 文件夹的所有者

sudo chown -R nginx:nginx /usr/local/nginx-1.22.0 /etc/nginx /var/www/html

(3)允许 nginx 以非root用户运行时 监听 80 端口号

sudo setcap cap_net_bind_service=+eip /usr/local/nginx-1.22.0/sbin/nginx

六、配置环境变量

(1)追加nginx路径至 profile

sudo bash -c "echo -e 'export NGINX_HOME=/usr/local/nginx-1.22.0\nexport PATH=\${PATH}:\${NGINX_HOME}/sbin\n' >>/etc/profile"

(2)刷新

source /etc/profile

七、使用nginx角色测试配置是否正确,及运行

(1)测试配置是否正确

sudo -u nginx /usr/local/nginx-1.22.0/sbin/nginx -t

(2)运行nginx

sudo -u nginx /usr/local/nginx-1.22.0/sbin/nginx

标签:11,nginx,sudo,Nginx,usr,conf,1.22,local,Debian
来源: https://www.cnblogs.com/shiningwonders/p/16621436.html