参考资料

Nginx安装:http://www.nginx.cn/install

阿铭:http://www.apelearn.com/study_v2/chapter18.html#nginx

nginx-编译安装、动态添加模块以及平滑升级:https://blog.csdn.net/u014761412/article/details/83753974

环境说明

Nginx源码下载在/root目录

Nginx安装在/usr/local/src/nginx-1.17.1,然后建立软连接/usr/local/src/nginx

系统:CentOS(7.6)

nginx-1.17.1

基础环境准备

添加用户

[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -M -s /sbin/nologin -g nginx nginx

安装openssh 

[root@localhost ~]# yum -y install openssh bash

编译环境gcc g++等软件

[root@localhost ~]# yum -y install gcc gcc-c++ automake autoconf libtool make openssh bash

安装openssl
        Nginx在使用HTTPS服务的时候要用到此模块,如果不安装openssl相关包,安装Nginx的过程会报错。

[root@localhost ~]# yum -y install openssl openssl-devel 

安装PCRE库
        pcre的全称为perl compatible regular expressions,中文译为“prel兼容正则表达式”,官方的站点为http://www.pcre.prg/,安装pcre库是为了使Nginx支持具备URL重写功能的rewrite模块,如果不安装pcre库,则Nginx无法使用rewrite模块功能,Nginx的rewrite模块功能几乎是企业应用必须的。

[root@localhost ~]# yum -y install pcre pcre-devel

安装zlib
zlib是提供数据压缩用的函式库

[root@localhost ~]# yum -y install zlib zlib-devel

安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

[root@localhost ~]# mkdir /home/q
[root@localhost ~]# cd /home/q
[root@localhost q]# wget -c http://nginx.org/download/nginx-1.17.1.tar.gz
[root@localhost q]# tar xf nginx-1.17.1.tar.gz
[root@localhost q]# cd nginx-1.17.1

[root@localhost nginx-1.17.1]# 
./configure \
--prefix=/usr/local/src/nginx-1.17.1 \
--user=nginx \
--group=nginx \
--with-http_stub_status_module  \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-pcre

[root@localhost nginx-1.17.1]# make
[root@localhost nginx-1.17.1]# make install
[root@localhost nginx-1.17.1]# make clean
[root@localhost nginx-1.17.1]# rm -rf Makefile objs
[root@localhost nginx-1.17.1]# ln -s /usr/local/src/nginx-1.17.1 /usr/local/src/nginx

检查安装结果

[root@localhost ~]# /usr/local/src/nginx/sbin/nginx -t

安装完成后可以看到配置如下:

nginx path prefix: "/usr/local/src/nginx-1.17.1"
nginx binary file: "/usr/local/src/nginx-1.17.1/sbin/nginx"
nginx modules path: "/usr/local/src/nginx-1.17.1/modules"
nginx configuration prefix: "/usr/local/src/nginx-1.17.1/conf"
nginx configuration file: "/usr/local/src/nginx-1.17.1/conf/nginx.conf"
nginx pid file: "/usr/local/src/nginx-1.17.1/logs/nginx.pid"
nginx error log file: "/usr/local/src/nginx-1.17.1/logs/error.log"
nginx http access log file: "/usr/local/src/nginx-1.17.1/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

加入系统服务

执行命令

[root@localhost ~]# vim /etc/init.d/nginx

脚本内容

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

保存后,更改权限:


[root@localhost ~]# chmod 755 /etc/init.d/nginx 
[root@localhost ~]# chkconfig --add nginx

如果想开机启动,请执行:

[root@localhost ~]# chkconfig nginx on

开启防火墙端口

# 开启服务器的80端口
[root@localhost ~]# firewall-cmd --permanent --add-port=80/tcp
# 重启防火墙
[root@localhost ~]# firewall-cmd --reload

Nginx服务常用命令

# 启动Nginx
/usr/local/src/nginx/sbin/nginx
# 重新载入配置文件
/usr/local/src/nginx/sbin/nginx -s reload
# 重启 Nginx
/usr/local/src/nginx/sbin/nginx -s reopen
# 停止 Nginx
/usr/local/src/nginx/sbin/nginx -s stop

查看Nginx服务对应的端口是否成功启动

lsof -i :80
或
netstat -lnt | grep 80
或
ps -ef | grep nginx

检查Nginx启动的实际效果

wget 127.0.0.1
或
curl 127.0.0.1

修改首页文件测试
Nginx默认映射/usr/local/src/nginx/html文件夹下的文件,它的首页配置是index.html和index.htm,配置完成后再次访问即可看到新页面。

[root@localhost ~]# vi /usr/local/src/nginx/html/index.html 

文件内容

[root@localhost ~]# vi /usr/local/src/nginx/html/index.html 

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>测试页面</title>
    </head>
    
    <body>
        <center>
            </h1>这是一个HTML实例</h1>
        </center>
        <hr width="50%" />
        <p>本页显示白色背景,黑色的文本</p>
    </body>
</html>