1、下载nginx源码

官网地址: http://nginx.org/en/download.html

选择 Stable version:最新稳定版本


下载后可以解压移动到/usr/local/bin目录下:

mv nginx-1.12.2.tar.gz /usr/local/bin

2、安装依赖包

nginx的安装依赖下面软件

OpenSSL

官网下到最新稳定版。 https://www.openssl.org/source/

PCRE

下载地址: https://ftp.pcre.org/pub/pcre/
注意有两个大版本:8.x和10.x,nginx依赖的是8.x,所以选择 pcre-8.*的最高版本。

zlib

下载地址:http://zlib.net/

把以上库的解压目录也移动到/usr/local/bin(和 nginx 同目录):

mv openssl-1.1.0g  pcre-8.41  zlib-1.2.11 /usr/local/bin

3、编译安装

进入之前解压的 nginx 目录:

cd /usr/local/bin/nginx-1.18.0/

执行配置命令,几个依赖包的路径对就可以,官方文档提示要写到一行:

参考官方文档: http://nginx.org/en/docs/configure.html

./configure  --sbin-path=/usr/local/nginx/nginx \
						 --conf-path=/usr/local/nginx/nginx.conf  \
             --pid-path=/usr/local/nginx/nginx.pid \
             --with-http_ssl_module \
             --with-pcre=../pcre-8.44 \
             --with-zlib=../zlib-1.2.11 \
             --with-openssl=../openssl-1.1.1g

注意以上依赖包要改成自己的实际版本。

一阵 checking 无报错信息之后配置成功


然后就可以编译了:

make

一阵编译无报错信息之后安装:

sudo make install

4、使用

/usr/local/nginx/nginx -h

nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: /usr/local/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

5、配置简易操作命令

如果怕nginx命令操作比较麻烦,可以参考lnmp设计的操作脚本

sudo lnmp -h
+-------------------------------------------+
|    Manager for LNMP, Written by Licess    |
+-------------------------------------------+
|              https://lnmp.org             |
+-------------------------------------------+
Usage: lnmp {start|stop|reload|restart|kill|status}
Usage: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}
Usage: lnmp vhost {add|list|del}
Usage: lnmp database {add|list|edit|del}
Usage: lnmp ftp {add|list|edit|del|show}
Usage: lnmp ssl add
Usage: lnmp {dnsssl|dns} {cx|ali|cf|dp|he|gd|aws}
Usage: lnmp onlyssl {cx|ali|cf|dp|he|gd|aws}

或配置alias命令别名:

将以下代码放到 ~/.bash_profile 文件中

alias nginx restart='sudo /usr/local/nginx/nginx -s stop && sudo /usr/local/nginx/nginx'
alias nginx start='sudo /usr/local/nginx/nginx'
alias nginx stop='sudo /usr/local/nginx/nginx -s stop'




source ~/.bash_profile