NGINX安装与基础配置说明

NGINX安装与基础配置说明

  • 下载Nginx

    https://nginx.org/en/download.html nginx官网,通过该网站进行NGINX源码包下载。

  • 前置条件检查

    • 安装前需要具备以下依赖包

      gcc -v
      g++ -v
      rpm -qa | grep pcre
      rpm -qa | grep zlib
      openssl version
      
    • 可以通过该命令一次性安装

      yum install -y gcc-c++  zlib zlib-devel openssl openssl-devel pcre pcre-devel
      
  • 安装Nginx

    • 将tar包放在任意目录,对tar包进行解压

      # 解压
      tar -zxvf nginx-1.26.1.tar.gz
      
    • 进入解压后的nginx目录

      [root@nginx ~]# cd /opt/nginx-1.29.0
      [root@nginx nginx-1.29.0]# ls
      auto  CHANGES  CHANGES.ru  CODE_OF_CONDUCT.md  conf  configure  contrib  CONTRIBUTING.md  html  LICENSE  Makefile  man  objs  README.md  SECURITY.md  src
      
      
    • 选择configure脚本,带参数执行。

      ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
       ##选择文件安装目录   --prefix=/usr/local/nginx
       ##启用 http_stub_status 模块,用于监控服务器状态 --with-http_stub_status_module
       ##启用 http_ssl 模块,用于支持 HTTPS。 --with-http_ssl_module
      
    • 编译安装

      make & make install
      
  • 启动Nginx

    • 启动命令

      /usr/local/nginx/sbin/nginx ##启动nginx
      /usr/loacl/nginx/sbin/nginx -s relaod ##重新启动
      /usr/loacl/nginx/sbin/nginx -s stop ##停止
      /usr/loacl/nginx/sbin/nginx -s quit ##杀死进程
      
    • 查看是否启动成功

      ps -ef | grep nginx #查看进程是否在线
      也可以在浏览器访问nginx服务器端口,默认80
      
  • Nginx配置文件说明

    • 文件目录位置

      配置文件目录如图所示,nginx.conf是配置文件,nginx.conf.default是配置的初始化备份文件

      image

    • 文件说明

      #user  nobody; 
      worker_processes  1;
      
      #error_log  logs/error.log;
      #error_log  logs/error.log  notice;
      #error_log  logs/error.log  info;
      
      #pid        logs/nginx.pid;
      
      
      events {
          worker_connections  1024;
      }
      
      
      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"';
      
          #access_log  logs/access.log  main;
      
          sendfile        on;
          #tcp_nopush     on;
      
          #keepalive_timeout  0;
          keepalive_timeout  65;
      
          #gzip  on;
      
          server {
              listen       80; 			// 端口监听接口
              server_name  localhost;  	//设置服务器名称localhost为本机名,可以写域名。
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  root   html;        			 //指定html文件目录
                  index  index.html index.htm;     //html文件名称
              }
      
              #error_page  404              /404.html;
      
              # redirect server error pages to the static page /50x.html
              #
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
      
              # proxy the PHP scripts to Apache listening on 127.0.0.1:80
              #
              #location ~ \.php$ {
              #    proxy_pass   http://127.0.0.1;
              #}
      
              # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
              #
              #location ~ \.php$ {
              #    root           html;
              #    fastcgi_pass   127.0.0.1:9000;
              #    fastcgi_index  index.php;
              #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
              #    include        fastcgi_params;
              #}
      
              # deny access to .htaccess files, if Apache's document root
              # concurs with nginx's one
              #
              #location ~ /\.ht {
              #    deny  all;
              #}
          }
      
      
          # another virtual host using mix of IP-, name-, and port-based configuration
          #
          #server {
          #    listen       8000;
          #    listen       somename:8080;
          #    server_name  somename  alias  another.alias;
      
          #    location / {
          #        root   html;
          #        index  index.html index.htm;
          #    }
          #}
      
      
          # HTTPS server
          #
           server {
               listen       443 ssl;
               server_name  localhost;
      
               ssl_certificate      cert.pem;  //私钥文件位置
               ssl_certificate_key  cert.key;  //公钥文件位置
      
               ssl_session_cache    shared:SSL:1m;
               ssl_session_timeout  5m;
      
               ssl_ciphers  HIGH:!aNULL:!MD5;
               ssl_prefer_server_ciphers  on;
      
               location / {
                   root   html;
                   index  index.html index.htm;
               }
           }
      
      }
      
      
posted @ 2025-07-05 15:16  Epiphany(qrx)  阅读(15)  评论(0)    收藏  举报