yum安装nginx,编译安装nginx

时间:2025-05-12 08:58:22

1,yum安装nginx新版

1,添加yum源

  1. nginx: Linux packages,登录官方网站,复制nginx的yum源。

    [nginx-stable]  #打开稳定版
    name=nginx stable repo
    baseurl=/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=/keys/nginx_signing.key
    module_hotfixes=true
    ​
    [nginx-mainline]  #关闭测试版
    name=nginx mainline repo
    baseurl=/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=/keys/nginx_signing.key
    module_hotfixes=true
  2. 进入yum源,添加源

    [root@localhost ~]# cd /etc//    #进入yum源
    [root@localhost ]# vim     #创建源,添加网上的内容

2,yum安装nginx

[root@localhost ]# yum install -y nginx  #安装nginx,记得检查看源对不对
====================================================================
 Package   Arch       Version                Repository        Size
====================================================================
Installing:
 nginx     x86_64     1:1.24.0-1.     nginx-stable     804 k    #看这个就对了,是稳定版
Installing for dependencies:
 pcre2     x86_64     10.23-2.el7            base             201 k
​
Transaction Summary
====================================================================
[root@localhost ]# nginx     #启动nginx
[root@localhost ]# ss -lntp    #查看端口,看起来了没

3,网页访问IP

2,编译安装nginx新版

1,安装依赖包

yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel libtermcap-devel readline-devel ncurses-devel bison libtool-ltdl bzip2-devel libicu-devel libunwind-devel libaio-devel

2,创建用户nginx

[root@localhost ~]# useradd nginx -s /sbin/nologin

3,安装nginx

[root@localhost ~]# wget /download/nginx-1.22.  #官网下载包
[root@localhost ~]# tar xzf nginx-1.22. -C /usr/local/    #解压并且指定路径
[root@localhost ~]# MAKE_NGINX_PATH=/usr/local/nginx-1.22.0
# 添加第三方模块,将nginx路径定义为一个变量
#nginx-upstream-fair 支持第三方负载均衡算法
[root@localhost ~]# wget /gnosek/nginx-upstream-fair/archive/ -P $MAKE_NGINX_PATH && \
unzip $MAKE_NGINX_PATH/ -d $MAKE_NGINX_PATH && \
mv $MAKE_NGINX_PATH/nginx-upstream-fair-master $MAKE_NGINX_PATH/nginx-upstream-fair  #下载第三方模块
[root@localhost ~]# sed -ri "s/default_port/no_port/g" $MAKE_NGINX_PATH/nginx-upstream-fair/ngx_http_upstream_fair_module.c  # 修改版本问题
# github问题地址:/gnosek/nginx-upstream-fair/issues/30
​
[root@localhost ~]# cd $MAKE_NGINX_PATH
[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx \    #nginx的全部存放路径,如果把这个目录删掉,nginx也卸干净了
--group=nginx \
--user=nginx \    #启动nginx的用户和组
--sbin-path=/usr/local/nginx/sbin/nginx \    #nginx启动文件
--conf-path=/etc/nginx/ \    #配置文件位置
--error-log-path=/var/log/nginx/ \
--http-log-path=/var/log/nginx/ \
--http-client-body-temp-path=/usr/local/nginx/tmp/client_body \
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi \
--pid-path=/var/run/ \
--lock-path=/var/lock/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_realip_module \
--with-stream \
--add-module=nginx-upstream-fair  #第三方模块路径
[root@localhost nginx-1.22.0]# make && make install && mkdir -p /usr/local/nginx/tmp/  #编译安装
​
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx  #创建软连接,挂载启动命令
[root@localhost ~]# nginx    #启动nginx

3,nginx配置文件——模块认识

[root@localhost ~]# vim /etc/nginx/
​
#全局配置块
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/ notice;
pid        /var/run/;
​
#epoll 驱动配置
events {
    worker_connections  1024;
}
​
#协议块,网络请求走这边
http {
    include       /etc/nginx/;  #导入其他文件
    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  /var/log/nginx/  main;
    sendfile        on;
    keepalive_timeout  65;  #超时时间
    
    #虚拟主机,-A
    server {
        #监听的域名和端口
        listen       80;
        server_name  localhost;
        #url路径
        location / {
            root   html;
            index   ;
        }
     }
​
    include /etc/nginx//*.conf;
}