Centos安装Nginx

Centos 安装 Nginx

版本区别

常用版本分为四大阵营

编译安装

  1. 先下载Nginx开源版

    比如: nginx-1.22.1.tar.gz

  2. 借助工具将下载的文件上传到对应目录并解压

    cd /usr/local/
    mkdir nginx
    cd nginx/
    # 上传文件
    ls # nginx-1.22.1.tar.gz
    tar -zxvf nginx-1.22.1.tar.gz 
    
  3. 安装

    ls
    cd nginx-1.22.1
    ./configure --prefix=/usr/local/nginx # ./configure --prefix=路径
    make
    make install 
    # 两命令可简写为make && make install
    

    ./configure --prefix=路径, 检查安装所需依赖和配置是否存在,注意看输出信息

如果出现警告或报错

  • 提示:

    checking for OS
    + Linux 3.10.0-693.el7.x86_64 x86_64
    checking for C compiler ... not found
    ./configure: error: C compiler cc is not found
    

    安装gcc

    yum install -y gcc

  • 提示:

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.
    

    安装perl库

    yum install -y pcre pcre-devel

  • 提示:

    ./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib=<path> option.
    

    安装zlib库

    yum install -y zlib zlib-devel

  • 接下来执行 make ,make install

报错总结

若执行make显示make: *** 没有规则可以创建“default”需要的目标“build” ,停止

一般是初次安装Nginx时会出现这样的错误,原因是没有安装Nginx需要的相关依赖,依赖包pcre-develzlib zlib-devel openssl openssl-devel

# 在虚拟机中安装相关依赖:
yum install pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel

# 也可用一条命令代替
yum install pcre-devel zlib zlib-devel openssl openssl-devel

安装之后,再次实施 make && make install 命令,安装成功。

启动Nginx

进入安装好的目录 /usr/local/nginx/sbin

./nginx # 启动
./nginx -s stop # 快速停止
./nginx -s quit # 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload # 重新加载配置
cd /usr/local/nginx/sbin
./nginx

访问 http://IP/, nginx默认端口为80,http默认端口也为80, 所以可以不写端口

出现Welcome to nginx!页面即为启动成功

页面内容为/usr/local/nginx/html目录下的index.html文件,可自行修改,例如

pwd # /usr/local/nginx/html
echo "Hello Nginx"! > index.html # 将index.html文件内容改为 Hello Nginx!
cat index.html  # Hello Nginx!
# 刷新页面查看内容

echo命令的重定向功能经常被用于清空文件内容(删除文件)时使用

  • echo "content" > filename
    将content覆盖到filename文件当中去,filename文件当中之前的内容不复存在了,实际上是修改了原文件的内容。

  • echo "content" >> filename
    将content追加到filename文件后,对filename文件之前的内容不修改,只进行增添,也叫追加重定向。

关于防火墙

  • 关闭防火墙

    systemctl stop firewalld.service

  • 禁止防火墙开机启动

    systemctl disable firewalld.service

  • 放行端口

    firewall-cmd --zone=public --add-port=80/tcp --permanent

  • 重启防火墙

    firewall-cmd --reload

安装成系统服务

  • 创建服务脚本

    vi /usr/lib/systemd/system/nginx.service

    服务脚本内容(注意脚本中的nginx目录位置,如果不一致需要调整为自己的安装目录)

    [Unit]
    Description=nginx - web server
    After=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 -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    ExecQuit=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
  • 重新加载系统服务

    systemctl daemon-reload

  • 启动服务

    启动前先检查一下是否有nginx服务已经启动,如果有的话就先关闭

    ps -ef | grep nginx
    ./nginx -s stop # /usr/local/nginx/sbin 目录下
    ps -ef | grep nginx # 查看是否已关闭
    

    systemctl start nginx.service | systemctl start nginx .service可省略
    systemctl status nginx 查看Nginx服务状态

tips

查看nginxlogs 文件

pwd # /usr/local/nginx/logs
ls #access.log  error.log  nginx.pid
cat nginx.pid # 7454

执行ps -ef | grep nginx发现nginxPID正为7454

access.log文件记录Nginx的所有访问信息,会持续变大, ll -h 查看文件信息包括容量

记录解决nginx的access.log持续变大问题

开机启动

systemctl enable nginx.service

本文作者:Zzzy君不见

本文链接:https://www.cnblogs.com/Zzzyyw/p/17020893.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Zzzy君不见  阅读(152)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起