安装步骤

  1. 下载 nginx 源码包 官网

    $ wget http://nginx.org/download/nginx-1.16.0.tar.gz
  2. 解压 nginx 压缩包

    $ tar -zxvf nginx-1.16.0.tar.gz
  3. 创建 www 用户 (已存在可以跳过)

    $ groupadd www
    $ useradd -g www -s /sbin/nologin -M www
  4. 运行 configure 文件检测程序

    $ cd nginx-1.16.0 
    
    $ ./configure --prefix=/usr/local/nginx --user=www --group=www
    
    checking for OS 
    
    + Linux 3.10.0-957.12.2.el7.x86_64 x86_64 
    
    checking for C compiler ... not found 
    
    ./configure: error: C compiler cc is not found
    # 此处是因为没有 gcc 和 gcc-c++ 依赖包,由于我是 centos 可以直接通过yum进行安装
    $ yum -y install gcc gcc-c++
    ... 此处省略安装步奏
    # 再次执行上面的 configure 步骤
    $ ./configure --prefix=/usr/local/nginx --user=www --group=www
    ... 此处省略一部分
    ./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.
    # 这次根据提示是因为没有 PCRE 库 ,执行 yum 安装 pcre-devel即可
    $ yum -y install pcre-devel
    ... 此处省略一部分
    Running transaction
    正在安装 : pcre-devel-8.32-17.el7.x86_64 1/1
    验证中 : pcre-devel-8.32-17.el7.x86_64 1/1 已安装:
    pcre-devel.x86_64 0:8.32-17.el7 完毕!
    # 再次执行 configure 步骤
    $ ./configure --prefix=/usr/local/nginx --user=www --group=www
    ... 此处省略一部分
    ./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 安装 zlib 和 zlib-devel 即可
    $ yum -y install zlib zlib-devel
    ... 此处省略一部分
    Running transaction
    正在安装 : zlib-devel-1.2.7-18.el7.x86_64 1/1
    验证中 : zlib-devel-1.2.7-18.el7.x86_64 1/1 已安装:
    zlib-devel.x86_64 0:1.2.7-18.el7 完毕!
    # 再次执行 configure 步骤
    $ ./configure --prefix=/usr/local/nginx --user=www --group=www
    ... 此处省略一部分
    Configuration summary
    + using system PCRE library
    + OpenSSL library is not used
    + using system zlib library nginx path prefix: "/usr/local/nginx"
    nginx binary file: "/usr/local/nginx/sbin/nginx"
    nginx modules path: "/usr/local/nginx/modules"
    nginx configuration prefix: "/usr/local/nginx/conf"
    nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
    nginx pid file: "/usr/local/nginx/logs/nginx.pid"
    nginx error log file: "/usr/local/nginx/logs/error.log"
    nginx http access log file: "/usr/local/nginx/logs/access.log"
    nginx http client request body temporary files: "client_body_temp"
    nginx http proxy temporary files: "proxy_temp"
    nginx http fastcgi temporary files: "fastcgi_temp"
    nginx http uwsgi temporary files: "uwsgi_temp"
    nginx http scgi temporary files: "scgi_temp"
    # 总算通过了下面进行编译和安装
    $ make && make install
    # 检测是否安装成功
    $ /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    # 辛苦总算没有白费,成功了.
  5. 启动 nginx 服务

    $ /usr/local/nginx/sbin/nginx
    # 启动了一个nginx服务,然我测试下是否启动成功
    $ lsof -i:80
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    nginx 17440 root 6u IPv4 35217 0t0 TCP *:http (LISTEN)
    nginx 17441 nobody 6u IPv4 35217 0t0 TCP *:http (LISTEN)
    # 完成。
    # PS:如果 lsof 没有这个操作可以使用 yum -y install lsof 安装
  6. 配置防火墙80端口

    $ firewall-cmd --zone=public --add-port=80/tcp --permanent
    $ systemctl restart firewalld

安装总结

# 下载、解压 源码包
$ wget http://nginx.org/download/nginx-1.16.0.tar.gz
$ tar -zxvf nginx-1.16.0.tar.gz # 创建 www 用户 (已存在可以跳过)
$ groupadd www
$ useradd -g www -s /sbin/nologin -M www # 安装 前置服务
$ yum -y install gcc gcc-c++ pcre-devel zlib zlib-devel # 检测安装环境
$ ./configure --prefix=/usr/local/nginx --user=www --group=www # 编译 和 安装
$ make && make install # 启动服务
$ /usr/local/nginx/sbin/nginx # 打开防火墙
$ firewall-cmd --zone=public --add-port=80/tcp --permanent
$ systemctl restart firewalld

操作命令

# 停止
[1]
$ pkill -9 nginx
[2]
$ nginx -s stop
# 检查配置是否正确
$ nginx -t
# 重启
$ nginx -s reload

配置开机自启

由于我是 centos 7 ,我打算用 systemctl 作为我的管理工具,下面是我的操作步骤。

还有一个原因是因为我配置 nginx 启动脚本总是报错,我懒得去弄官网给的那个脚本了。

$ vim /lib/systemd/system/nginx.service
# 输入以下代码,并且保存
[Unit]
Description=nginx
After=network.target [Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true [Install]
WantedBy=multi-user.target
$ systemctl start nginx.service
Failed to execute operation: Access denied
# 这里报了一个错误,提示我没有权限
$ setenforce 0 # 临时关闭 selinux ,如果想长久关闭请自行搜索
$ systemctl start nginx
# 没有报错,测试一下
$ lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1398 root 6u IPv4 19609 0t0 TCP *:http (LISTEN)
nginx 1399 www 6u IPv4 19609 0t0 TCP *:http (LISTEN)
# 成功,设置为开机自动启动
$ systemctl enable nginx.service

centos7 源码编译安装 nginx的更多相关文章

  1. CentOS7 源码编译安装Nginx

    源码编译安装nginx     1.下载nginx源码包(这里以nginx-1.18.0为例) wget http://nginx.org/download/nginx-1.18.0.tar.gz 2 ...

  2. centos7 源码编译安装nginx教程 nginx安装脚本 lua-nginx-module

    安装nginx需要pcre zlib openssl的库,下文都是在官网直接下载用作编译安装 该nginx安装教程,有安装maxmind IP 库 该nginx安装教程有安装lua-nginx-mod ...

  3. Centos7源码编译安装PHP7.2(生产环境)

    安装PHP依赖包,否则在编译的过程中可能会出现各种报错 # Centos 安装epel-release源并将系统包更新到最新版本 $ yum install epel-release-y $ yum ...

  4. centos7源码编译安装lamp/lnmp

    centos7源码编译安装lamp/lnmp 进程:是包工头(相当于是个门,只管开门关门,不管门内的事儿) 线程:是各种工种(cpu调度的是线程) 进程 是一件事情, 线程 是 同一个时间范围内 同时 ...

  5. 源码编译安装nginx及设置开机启动项

    1.上传nginx文档:解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ...

  6. Centos7通过yum跟源码编译安装Nginx

    源码编译安装 http://nginx.org/en/download.html 到官网下载,然后用XFTP上传到root目录 把文件解压出来 tar -zxvf nginx-1.16.0.tar.g ...

  7. Linux系统 - 源码编译安装Nginx

    什么是Nginx? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,在高连接并发的情况下N ...

  8. centos7 源码编译安装TensorFlow CPU 版本

    一.前言 我们都知道,普通使用pip安装的TensorFlow是万金油版本,当你运行的时候,会提示你不是当前电脑中最优的版本,特别是CPU版本,没有使用指令集优化会让TensorFlow用起来更慢. ...

  9. Centos7源码编译安装mysql8

    前面介绍了很多关于mysql的文章,下面主要介绍一下mysql8的源码编译安装 一 基本环境 [root@CentOS-7-x86-64-Minimal-1810 ~]# cd /usr/local/ ...

随机推荐

  1. cordova打包项目下载gradle失败

    在使用cordova打包项目的时候有时会因为网络环境原因导致下载gradle失败的情况,个人找到两种解决方案. 方案一: 根据报错的提示选择要下载的gradle去浏览器中下载,然后将gradle压缩包 ...

  2. POJ 3683 神父赶婚宴 2-SAT+输出模板

    题意:一个小镇里面只有一个牧师,现在有些新人要结婚,需要牧师分别去主持一个仪式,给出每对新人婚礼的开始时间 s 和结束时间 t ,还有他们俩的这个仪式需要的时间(每对新人需要的时间长短可能不同) d ...

  3. 关于Java 8 forEach

    1. forEach and Map 1.1 通常这样遍历一个Map Map<String, Integer> items = new HashMap<>(); items.p ...

  4. C++入门经典-例5.9-使用空类型指针执行函数

    1:运行代码: // 5.9.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  5. C++入门经典-例5.1-输出变量的指针

    1:代码如下: // 5.1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  6. Struts初学

    自我概述 今天看了Struts,感觉这是个非常好用的东西!虽然它已经过时了,被springMVC取代了,但是仍然有了解的价值. 可是在学习过程中遇到了很多问题,比如其中一些原理很是抽象,有一些问题莫名 ...

  7. IDEA 创建maven jar、war、 pom项目

    创建java jar.pom项目时创建maven-archetype-quickstart 创建java war项目时创建maven-archetype-webapp

  8. Dubbo HelloWord 与 Spring Boot 整合

    实现消费者项目代码调用提供者项目代码,使用 zookeeper 做为注册中心 interface 项目 pom.xml <?xml version="1.0" encodin ...

  9. CSS + ul li 横向排列的两种方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 阶段3 2.Spring_06.Spring的新注解_8 spring整合junit完成

    Junit的核心Runner在执行的时候不会创建容器.同时它字节码文件,也改不了 spring整合junit 想办法把junit里面的不能加载容器的main方法换掉.从而实现创建容器.有了容器就可以实 ...