nginx 编译安装

编辑于 2021-05-11 11:38:35 阅读 1253

下面以CentOS为例

编译安装

下载

wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
wget http://nginx.org/download/nginx-1.20.0.tar.gz

编译

# 创建所属用户和组,不创建家目录,不能ssh登录
useradd -M -s /sbin/nologin www

./configure --user=www --group=www --prefix=/data/apps/nginx --conf-path=/vagrant/apps/nginx/conf/nginx.conf --pid-path=/data/apps/nginx/logs/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-pcre=/vagrant/tgz/pcre-8.44 --with-http_realip_module --with-http_image_filter_module

make && make install

启动脚本

vi /etc/init.d/nginx #详见附件1️⃣
#给执行权限
chmod +x /etc/rc.d/init.d/nginx

开机启动

chkconfig --level 345 nginx on

使用

service nginx start
service nginx reload
service nginx stop
service nginx restart

yum安装

这个就没太多说的了,按照官方文档配置好yum源就行

http://nginx.org/en/linux_packages.html#RHEL-CentOS

附件

1️⃣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:      /data/apps/nginx/conf/nginx.conf
# pidfile:     /data/apps/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="/data/apps/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/data/apps/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

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验