手动编译安装 Nginx(附带rtmp模块)

环境:CentOS7.9

安装目录:/usr/local/nginx

解压目录:/usr/local/src/nginx

步骤

cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12.1.tar.gz
mv nginx-1.12.1 nginx
cd nginx
git clone https://github.com/arut/nginx-rtmp-module.git
./configure --prefix=/usr/local/nginx --add-module=./nginx-rtmp-module --with-http_ssl_module
# 编译工程
make && make install

展示

[root@VM-4-3-centos nginx]# cd  /usr/local/nginx/
[root@VM-4-3-centos nginx]# ls
conf html logs sbin
[root@VM-4-3-centos nginx]# cd sbin/
[root@VM-4-3-centos sbin]# ls
nginx
[root@VM-4-3-centos sbin]# ./nginx -v
nginx version: nginx/1.12.1

配置

[root@VM-4-3-centos conf]# cd /usr/local/nginx/conf
[root@VM-4-3-centos conf]# vim nginx.conf
# 添加如下
rtmp {

server {

listen 1935 so_keepalive=2s:1:1; #监听的端口
chunk_size 4000;
application hls { #rtmp推流请求路径
live on;
}
}
}

nginx 相关命令

启动 nginx
  1. 直接启动
  • 进入 nginx 安装目录找到 sbin 文件夹,然后启动 nginx。
    命令:cd /sbin 进入到 sbin 目录下
    命令:./nginx 启动 nginx
  1. 指定配置文件方式启动:
    命令:./nginx -c /usr/local/nginx/conf/nginx.conf
检查 nginx 配置

进入 nginx 下的 sbin 目录下执行命令:
命令:./nginx -t

重启 nginx

./nginx -s reload

自启动 nginx

设置 Linux 重启之后 nginx 自动启动命令:systemctl enable nginx

查看状态,命令:systemctl is-enabled nginx 或者service nginx status

关闭 nginx
关闭 nginx
  1. 进入 nginx 下的 sbin 目录下执行命令:
    快速停止 nginx 命令:./nginx -s stop
    完整有序的停止 nginx:./nginx -s quit;这个命令会等待所有请求结束后再关闭 nginx。
  2. 使用 kill 命令关闭 nginx
  • 先查看 nginx 进程,找到主进程号。然后再使用 kill 命令杀死 nginx 进程。
    查看 nginx 进程号命令:ps -ef | grep nginx
    从容停止 nginx 命令:kill -QUIT 主进程号
    快速停止 nginx 命令:kill -TERM 主进程号
    强制停止 nginx 命令:kill -HUP 主进程号
    平滑重启 nginx 命令:kill -9 nginx
    查找并杀死所有 nginx 进程:ps aux | grep nginx |awk '{print $2}' | xargs kill -9

问题

启动时出现 Failed to start nginx.service:unit not found

方案
vim /etc/init.d/nginx

插入代码

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server

# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

echo -n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

}


stop() {

echo -n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

}



restart() {

configtest || return $?

stop

start

}


reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}


configtest() {

$nginx -t -c $NGINX_CONF_FILE

}



rh_status() {

status $prog

}


rh_status_q() {

rh_status >/dev/null 2>&1

}

case "$1" in

start)

rh_status_q && exit 0
$1
;;

stop)


rh_status_q || exit 0
$1
;;

restart|configtest)
$1
;;

reload)
rh_status_q || exit 7
$1
;;


force-reload)
force_reload
;;
status)
rh_status
;;


condrestart|try-restart)

rh_status_q || exit 0
;;

*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2

esac
依次执行
cd /etc/init.d

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

需要注意的是如果之前没有配置/etc/init.d/nginx之前如果启动了nginx

需要手动关闭(kill 命令),再执行systemctl start nginx.service

正在加载今日诗词....