CentOS环境安装Nginx

Nginx(engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。
Nginx作为负载均衡服务:既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。

一、下载

  1. 官网下载: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

二、安装编译环境

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

四、验证是否安装成功

[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的基本模块,需要自己进入源码包目录重新编译添加
[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
  • 执行完编译后,停止掉nginx进程,替换nginx二进制文件,备份原来的启动脚本后重启Nginx
/etc/init.d/nginx stop
cp /etc/init.d/nginx /etc/init.d/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/
#启动Nginx
/etc/init.d/nginx start
#查看是否正常监听80端口
netstat -lnp|grep nginx

五、创建软链接

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

六、开机自启动

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
#赋予脚本可执行权限
chmod a+x /etc/init.d/nginx
#将nginx服务加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
# 启动
systemctl start nginx

七、常用命令

#测试访问
curl localhost:80
# 启动
systemctl start nginx
# 查看状态
systemctl status nginx
# 停止
systemctl stop nginx

# 重载配置
nginx -s reload
# 测试配置是否正确
nginx -t

你可能感兴趣的:(CentOS环境安装Nginx)