首页 > 系统相关 >Centos安装Nginx

Centos安装Nginx

时间:2023-01-03 00:11:25浏览次数:56  
标签:Centos nginx 安装 zlib Nginx usr install local

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可省略

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

标签:Centos,nginx,安装,zlib,Nginx,usr,install,local
From: https://www.cnblogs.com/Zzzyyw/p/17020893.html

相关文章

  • nginx的负载均衡如何配置
    负载均衡 nginx的负载均衡有4种模式:1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 2)、weight 指定轮询几率,weight......
  • Win7X64下,opencv安装折腾
    先说环境:win7X64专业版、python3.6.4pip安装了pillow,正常可使用为了不影响原来的环境,新建了一个python虚拟环境,如下:节选自https://zhuanlan.zhihu.com/p/216157886在......
  • nginx-clojure docker 镜像
    主要是一个测试,方便学习使用nginx-clojure强大的能力dockerfile直接基于了openjdk:10-slim基础镜像,同时基于copy文件的格式处理FROMopenjdk:10-slimWOR......
  • centos7 中 安装python 11
     001、查看系统[root@PC1~]#cat/etc/redhat-releaseCentOSLinuxrelease7.9.2009(Core)  002、安装环境依赖[root@PC1home]#yumgroupsmarkinstal......
  • Genymotion安装
    1.本节引言如果你符合下述三种情况的话,你可以考虑安装一个GenymotionAndroid:没有真机调试,只能用模拟器嫌SDK内置的AVD启动速度,运行速度慢电脑配置还可以,最好4G内存以上如......
  • Android安装
    01.Unity中的AndroidBuildSupport下载在Unity中的File>BuildingSettings>Android>OpenDownloadPage或在Unity官网下载对应版本的Unity安装助手自行追加安装02.JDK下......
  • Centos7修改静态IP
    Centos7修改静态IP1、开启虚拟机,执行终端命令vim/etc/sysconfig/network-scripts/ifcfg-ens332、修改下面几个配置(没有的就添加)BOOTPROTO="static"//ip设置为静态......
  • [Grafana监控工具]--安装和部署
    一、参考文档1、安装说明​​http://docs.grafana.org/ ​​2、使用说明​​http://docs.grafana.org/guides/getting_started​​​​http://docs.grafana.org/guides/bas......
  • Linux下安装Anaconda3,这个教程一定要看!
    前言大家好,我是爱写Bug的麦洛。由于工作需要,要为客户搭建Python开发环境。作为从来没有接触过Python的小白,为了完成任务,也是破费周折,请教了身边做Python的朋友,发现大家都是......
  • linux 找回root密码方法(CentOs 7.6)
    1:首先,启动系统,进入开机界面,在界面中按e进入编辑界面。如下图:2:进入编辑界面,使用键盘上的上下间吧光标往下移动,找到以"linux16"开头内容所在的行数,在行的最后面输......