安装步骤

  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

最新文章

  1. 微软.NET Core RC2正式发布,横跨所有平台
  2. UE3名称结构(Name)
  3. Storm入门1-基本概念
  4. shell 命令集
  5. Async and Await
  6. Java-接口练习1
  7. &amp;12 二叉搜索树
  8. JSP-declareAndOutput
  9. Cocos2d-x3.0 DrawNode吸取
  10. use this method get wifi from notebook
  11. js中的数值转换
  12. 7.20 python线程3
  13. 如何修改路由器的登录IP地址?
  14. Android开发关闭虚拟按钮、底部导航条
  15. 解决:oracle+myBatis ResultMap 类型为 map 时返回结果中存在 timestamp 时使用 jackson 转 json 报错
  16. Hadoop HBase概念学习系列之HBase表的一些设置(强烈推荐好好领悟)(十三)
  17. 字幕字体滚动插件——scroxt.js
  18. [MAC OS ] UserDefaults
  19. #pragma once含义及用法
  20. MySQL中SYSDATE()和NOW()函数的区别和联系

热门文章

  1. 51 Nod 不一样的猜字游戏
  2. input输入框如何只能输入非零开头的正整数
  3. VUE里面的$(this)
  4. MongoDB下载以及安装
  5. BZOJ 3622 Luogu P4859 已经没有什么好害怕的了 (容斥原理、DP)
  6. JIRA7.13版本创建项目:工作流(二)
  7. linux输出与查看的几种方式
  8. python3笔记十五:python函数
  9. 【python学习】字符串相关
  10. 使用C#语言,将DataTable 转换成域模型