平凉市网站建设_网站建设公司_展示型网站_seo优化
2026/1/11 1:31:30 网站建设 项目流程

Nginx安装及配置

开源Nginx官网地址(https://nginx.org)

Nginx源码包下载地址(https://nginx.org/en/download.html)

  1. Mainline version 主线版本
  2. Stable version 稳定版本
  3. Legacy versions 陈旧版本
源码安装

在安装操作系统的安装软件配置部分,建议选择“Server with GUI”,并选择“Development Tools”和“Compatibility Libraries”两项附加软件。确保gcc、libgcc、gcc-c++等编译器已经正确安装。

在安装Nginx之前,需要安装一些Nginx的依赖程序,Nginx的主要依赖程序有zlib、pcre、openssl三个,其中,zlib用于支持gzip模块,pcre用于支持rewrite模块,openssl用于支持ssl功能,为了简单快捷,推荐通过yum安装zlib、pcre、openssl软件包。

安装方式如下:

yum -y install zlib pcre pcre-devel openssl openssl-devel # 如果安装的操作系统为最小化,那么可以使用yum安装一下软件包组 # 查看软件包组列表 yum grouplist # 下载“Development Tools”和“Compatibility Libraries”两项附加软件 yum -y groupinstall "Development Tools" yum -y groupinstall "Compatibility Libraries"

编译安装完成后,可以使用nginx命令来查看编译安装的配置规则

# -v :显示版本并退出 # -V :显示版本和配置选项然后退出 /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.26.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre
  • 编写启动脚本

    cat < /etc/systemd/system/nginx.service
    [Unit]
    Description=The nginx HTTP and reverse proxy server
    After=network.target remote-fs.target nss-lookup.target

    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/bin/rm -f /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
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target
    EOF

  • 重新加载systemd管理器配置

    systemctl daemon-reload

  • 管理Nginx服务

当我们配置好启动文件之后,就可以通过systemctl来管理nginx服务了。

# 重载nginx服务 systemctl reload nginx # 启动nginx服务 systemctl start nginx # 关闭nginx服务 systemctl stop nginx # 设置nginx服务开机自启动 systemctl enable nginx # 查看nginx服务运行状态 systemctl status nginx # 查看nginx服务是否设置自启动 systemctl is-enabled nginx

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询