编译安装Nginx

一、下载Nginx的安装包

本次亲测环境为centos7.5

下载一   官网下载:

 http://nginx.org/download/  

寻找要安装的版本进行下载

wget http://nginx.org/download/nginx-1.9.15.tar.gz

下载二   github下载:

 https://github.com/nginx/nginx
git clone git@github.com:nginx/nginx.git

二、编译安装Nginx

 安装Nginx 编译所需所有依赖项

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

下载完毕后进行解压

tar -xzf nginx-1.9.15.tar.gz

进入nginx-1.9.15.tar.gz 文件夹

cd nginx-1.9.15

选择安装的模块

 nginx是非常大的软件,它内部有很多模块。 在这里你必须选择哪个模块对你的网站有用。 您可以查看下面的列表。 您也可以按照以下命令通过自己的系统进行检查:

./configure
--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
--pid-path=/run/nginx.pid
--lock-path=/run/lock/subsys/nginx
--user=nginx --group=nginx
--with-file-aio --with-ipv6
--with-http_auth_request_module
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module=dynamic
--with-http_image_filter_module=dynamic
--with-http_geoip_module=dynamic
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_degradation_module
--with-http_slice_module
--with-http_stub_status_module
--with-http_perl_module=dynamic
--with-mail=dynamic
--with-mail_ssl_module
--with-pcre
--with-pcre-jit
--with-stream=dynamic
--with-stream_ssl_module
--with-google_perftools_module
--with-debug
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
--param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic'
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

然后指定使用几个UPU进行编译安装:

make -j 4 && make install

三、编写Nginx启动脚本

语法检查

/usr/sbin/nginx -t

启动

/usr/sbin/nginx

停止

/usr/sbin/nginx -s stop

平滑启动

/usr/sbin/nginx -s reload

编译完成后,需要在init.d文件夹中创建nginx启动文件。 这样每次服务器重新启动init进程都会自动启动我们的Web服务器

#!/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:      /etc/nginx/nginx.conf
# pidfile:     /var/run/nginx.pid
# user:        nginx

# 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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

lockfile=/var/run/nginx.lock

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

创建一个nginx用户

useradd -r nginx

校验配置文件依次输入下列命令

chkconfig --add nginx
chkconfig --level 345 nginx on

进入init.d 文件夹

cd /etc/init.d/

 当前文件所在目录

给这个文件添加执行权限

chmod +x nginx
设置防火墙
firewall-cmd --permanent --add-port=80/tcp --zone=public
firewall-cmd --reload

启动Nginx服务

service nginx start

 停止Nginx服务

systemctl stop nginx

 附件

#! /bin/sh
# chkconfig: 2345 55 25
NGINX_BIN='/usr/local/nginx/sbin/nginx'
CONFIG='/usr/local/nginx/conf/nginx.conf'
case "$1" in
    start)
        echo -n "Starting nginx... "
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            echo "nginx (pid $PID) already running."
            exit 1
        fi
        $NGINX_BIN -c $CONFIG
        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;
    stop)
        echo -n "Stoping nginx... "
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" = "" ]; then
            echo "nginx is not running."
            exit 1
        fi
        $NGINX_BIN -s stop
        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            $0 force-quit
        else
            echo " done"
        fi
        ;;
    status)
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            echo "nginx (pid $PID) is running..."
        else
            echo "nginx is stopped."
            exit 0
        fi
        ;;
    force-quit|kill)
        echo -n "Terminating nginx... "
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" = "" ]; then
            echo "nginx is is stopped."
            exit 1
        fi
        kill $PID
        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    reload)
        echo -n "Reload nginx... "
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "nginx is not running, can't reload."
            exit 1
        fi
        ;;
    configtest)
        echo -n "Test nginx configure files... "
        $NGINX_BIN -t
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;
esac
原文地址:https://www.cnblogs.com/ling-yu-amen/p/10488494.html