温馨提示×

centos如何安装nginx

小樊
35
2025-02-18 19:26:56
栏目: 智能运维
Centos服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在CentOS上安装Nginx有多种方法,以下是两种常用的方法:

方法一:使用yum包管理器安装

  1. 更新系统包

    sudo yum update -y
    
  2. 安装EPEL仓库(可选,但推荐) EPEL(Extra Packages for Enterprise Linux)提供了许多额外的软件包。

    sudo yum install epel-release -y
    
  3. 安装Nginx

    sudo yum install nginx -y
    
  4. 启动Nginx服务

    sudo systemctl start nginx
    
  5. 设置Nginx开机自启

    sudo systemctl enable nginx
    
  6. 检查Nginx状态

    sudo systemctl status nginx
    
  7. 访问Nginx默认页面 打开浏览器,访问服务器的IP地址或域名,应该能看到Nginx的默认欢迎页面。

方法二:从源码编译安装

如果你需要最新版本的Nginx或者有特殊需求,可以从源码编译安装。

  1. 安装依赖包

    sudo yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
    
  2. 下载Nginx源码 访问Nginx官方网站,找到最新版本的下载链接,然后使用wget下载。

    wget http://nginx.org/download/nginx-1.21.6.tar.gz
    
  3. 解压源码

    tar -zxvf nginx-1.21.6.tar.gz
    cd nginx-1.21.6
    
  4. 配置编译选项

    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v3_module
    
  5. 编译并安装

    make
    sudo make install
    
  6. 启动Nginx

    sudo /usr/local/nginx/sbin/nginx
    
  7. 设置Nginx开机自启(可选) 你可以创建一个systemd服务文件来管理Nginx服务。

    sudo vi /etc/systemd/system/nginx.service
    

    添加以下内容:

    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    然后启用并启动服务:

    sudo systemctl daemon-reload
    sudo systemctl enable nginx
    sudo systemctl start nginx
    
  8. 检查Nginx状态

    sudo systemctl status nginx
    

通过以上两种方法,你可以在CentOS上成功安装Nginx。选择哪种方法取决于你的具体需求和环境。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:如何在CentOS上安装Nginx

0