CentOS環(huán)境安裝Nginx

Nginx(engine x) 是一個(gè)高性能的HTTP反向代理web服務(wù)器,同時(shí)也提供了IMAP/POP3/SMTP服務(wù)。
Nginx作為負(fù)載均衡服務(wù):既可以在內(nèi)部直接支持 Rails 和 PHP 程序?qū)ν膺M(jìn)行服務(wù),也可以支持作為 HTTP代理服務(wù)對(duì)外進(jìn)行服務(wù)。

一、下載

  1. 官網(wǎng)下載:http://nginx.org/en/download.html

  2. 源碼下載:

wget http://nginx.org/download/nginx-1.17.7.tar.gz
#解壓
tar -xzf nginx-1.17.7.tar.gz
cd nginx-1.17.7

二、安裝編譯環(huán)境

yum update
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

三、編譯安裝

#添加用戶和組
groupadd www
useradd -g www www
#配置
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads
#編譯
make
#安裝
make install

四、驗(yàn)證是否安裝成功

[root@khunpean ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.17.7
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@khunpean ~]# 
  • http_ssl_module為HTTPS模塊,http_ssl_module并不屬于nginx的基本模塊,需要自己進(jìn)入源碼包目錄重新編譯添加
[root@khunpean ~]# cd /usr/local/nginx-1.17.7
[root@khunpean nginx-1.17.7]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@zabbix nginx-1.17.7]# make
  • 執(zhí)行完編譯后,停止掉nginx進(jìn)程,替換nginx二進(jìn)制文件,備份原來(lái)的啟動(dòng)腳本后重啟Nginx
/etc/init.d/nginx stop
cp /etc/init.d/nginx /etc/init.d/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/
#啟動(dòng)Nginx
/etc/init.d/nginx start
#查看是否正常監(jiān)聽(tīng)80端口
netstat -lnp|grep nginx

五、創(chuàng)建軟鏈接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

六、開(kāi)機(jī)自啟動(dòng)

vim /etc/init.d/nginx
  • 編輯輸入腳本
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# 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
# config:      /etc/sysconfig/nginx
# 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"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
    sleep 1
    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
#賦予腳本可執(zhí)行權(quán)限
chmod a+x /etc/init.d/nginx
#將nginx服務(wù)加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
# 啟動(dòng)
systemctl start nginx

七、常用命令

#測(cè)試訪問(wèn)
curl localhost:80
# 啟動(dòng)
systemctl start nginx
# 查看狀態(tài)
systemctl status nginx
# 停止
systemctl stop nginx

# 重載配置
nginx -s reload
# 測(cè)試配置是否正確
nginx -t
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容