Nginx 源码安装

1.1 下载包

# 选择想要下载的版本,直接单击右键复制地址下载: 
wget http://nginx.org/download/nginx-1.16.1.tar.gz

1.2 安装依赖包

yum install gcc  gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

1.3 解压包

tar zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1

1.4 预编译

预编译主要是用来检查系统环境是否满足安装软件包的条件, 并生成Makefile文件,该文件为编译、安装、升级nginx指明了相应参数。
./configure --help 可以查看预编译参数
--prefix      指定nginx编译安装的目录
--user=***    指定nginx的属主 
--group=***   指定nginx的属主与属组
--with-***     指定编译某模块 
--without-**   指定不编译某模块
--add-module  编译第三方模块

预编译:

./configure --prefix=/usr/local/nginx

预编译后会生成Makefile 文件和objs 目录

[root@test01 nginx-1.16.1]# cat Makefile 

default:	build

clean:
	rm -rf Makefile objs

build:
	$(MAKE) -f objs/Makefile

install:
	$(MAKE) -f objs/Makefile install

modules:
	$(MAKE) -f objs/Makefile modules

upgrade:
	/usr/local/nginx/sbin/nginx -t

	kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
	sleep 1
	test -f /usr/local/nginx/logs/nginx.pid.oldbin

	kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
make clean : 重新预编译时,通常执行这条命令删除上次的编译文件 
make build : 编译,默认参数,可省略build参数 
make install : 安装 
make modules : 编译模块 
make  upgrade : 在线升级

1.5 编译并安装

make  &&  make install

1.6 查看版本

[root@test01 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx

1.7 启动 Nginx

  • 绝对路径启动
/usr/local/nginx/sbin/nginx
  • 软连接启动(不推荐)
也可以做个软连接--(在平滑升级时会报错)
ln  -s /usr/local/nginx/sbin/* /usr/local/sbin
然后重新读取下配置文件 
. /etc/profile

nginx
  • 别名启动(推荐)
alias  nginx='/usr/local/nginx/sbin/nginx'
#永久生效
vim ~/.bashrc 
alias nginx='/usr/local/nginx/sbin/nginx'

nginx

1.8 检查进程和端口

[root@test01 nginx-1.16.1]# ps -ef|grep nginx
root      15031      1  0 22:02 ?        00:00:00 nginx: master process nginx
nobody    15032  15031  0 22:02 ?        00:00:00 nginx: worker process
root      15101   9589  0 23:51 pts/0    00:00:00 grep --color=auto nginx
[root@test01 nginx-1.16.1]# netstat -nlpt |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15031/nginx: master

nginx WEB服务已经搭建成功!

原文地址:https://www.cnblogs.com/it-baibai/p/12913658.html