yum安装nginx,编译安装nginx

本文详细介绍了如何通过yum源添加并安装Nginx的稳定版和主分支,包括设置yum仓库、安装依赖、编译安装新版Nginx以及配置基本的nginx.conf文件的过程。
摘要由CSDN通过智能技术生成

1,yum安装nginx新版

1,添加yum源

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

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

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

2,yum安装nginx

[root@localhost yum.repos.d]# yum install -y nginx  #安装nginx,记得检查看源对不对
====================================================================
 Package   Arch       Version                Repository        Size
====================================================================
Installing:
 nginx     x86_64     1:1.24.0-1.el7.ngx     nginx-stable     804 k    #看这个就对了,是稳定版
Installing for dependencies:
 pcre2     x86_64     10.23-2.el7            base             201 k
​
Transaction Summary
====================================================================
[root@localhost yum.repos.d]# nginx     #启动nginx
[root@localhost yum.repos.d]# 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 http://nginx.org/download/nginx-1.22.0.tar.gz  #官网下载包
[root@localhost ~]# tar xzf nginx-1.22.0.tar.gz -C /usr/local/    #解压并且指定路径
[root@localhost ~]# MAKE_NGINX_PATH=/usr/local/nginx-1.22.0
# 添加第三方模块,将nginx路径定义为一个变量
#nginx-upstream-fair 支持第三方负载均衡算法
[root@localhost ~]# wget https://github.com/gnosek/nginx-upstream-fair/archive/master.zip -P $MAKE_NGINX_PATH && \
unzip $MAKE_NGINX_PATH/master.zip -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问题地址:https://github.com/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/nginx.conf \    #配置文件位置
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--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/nginx.pid \
--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/nginx.conf
​
#全局配置块
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
​
#epoll 驱动配置
events {
    worker_connections  1024;
}
​
#协议块,网络请求走这边
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;  #超时时间
    
    #虚拟主机,-A
    server {
        #监听的域名和端口
        listen       80;
        server_name  localhost;
        #url路径
        location / {
            root   html;
            index  index.html index.htm;
        }
     }
​
    include /etc/nginx/conf.d/*.conf;
}
  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值