IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> CentOS-8x+Linux-8中源码编译安装Nginx-1.20详解版 -> 正文阅读

[系统运维]CentOS-8x+Linux-8中源码编译安装Nginx-1.20详解版

CentOS-8x+Linux-8中源码编译安装Nginx-1.20*详解版


=目录索引=

01)系统 &环境

运行环境:实验室虚拟环境
系统型号:CtenOS-8.4-x86_64
Linux版本:linux-8-x86_64
CPU&内存:4H + 4GB
系统内核:4.18.0-305.3.1.el8.x86_64

=主题内容=

02)安装前准备

  1. 1 更新 CentOS-8 软件包 :
dnf update
  1. 2 关闭系统防火墙:
systemctl stop firewalld
  1. 3 关闭防火墙开机自启动:
systemctl disable firewalld
  1. 4 关闭 SELINUX
sed -ri '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
setenforce 0

03)编译器 &【dev】库安装

  1. 1 安装 make & wget
yum -y install gcc automake autoconf libtool make wget
  1. 2 安装 gcc 编译器:
    • 系统内置 gcc 版本查询:
gcc --version
gcc (GCC) 10.2.1 20201112 (Red Hat 10.2.1-8)   //版本够高够新,不用升级....
yum -y install gcc gcc-c++
  1. 2 安装 OpenSSL 开发库:
    • ssl 模块,服务器使用 SSL 安全证书需要,【–http_ssl_module】用于开启 https 安全加密功能,依赖 OpenSSL 开发库 :
yum -y install openssl openssl-devel
  1. 3 安装 PCRE 正则式表达库:
    • nginx 必装模块【–http_rewrite_module】,基于正则匹配来实现重定向,依赖PCRE 库 :
yum -y install pcre pcre-devel
  1. 4 安装 zlib 压缩库:
    • nginx 必装模块【–http_gzip_module】,用于针对 HTTP 包中内容进行 gzip 格式的压缩,依赖 zlib 压缩库:
yum -y install zlib zlib-devel

04)编译安装【Nginx】

  1. 1 创建 nginxs 用户 & 用户属组:
useradd -s /sbin/nologin -M nginx
  1. 2 官网下载 nginx 最新稳定版:
mkdir lanmpp
cd /root/lanmpp
wget http://nginx.org/download/nginx-1.20.1.tar.gz
  1. 3 解压 & 删除 nginx 压缩包:
tar -xvf nginx-1.20.1.tar.gz
rm -rf nginx-1.20.1.tar.gz
  1. 4 预编译 & 配置 nginx 源码,使之生成可执行的二进制文件:
cd nginx-1.20.1/
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-pcre \
--with-file-aio \
--with-threads \
--with-mail \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module 
      
  • 打印出现如下结果,则证明预编译 & 生成 makefile 二进制可执行文件成功…
Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx"
  nginx configuration file: "/usr/local/nginx/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
  1. 5 开始编译 & 安装 nginx
make && make install
//nginx短小精悍,就不用加速编译了...

05)配置【Nginx】

  1. 1 将 nginx 添加至系统环境变量中:
echo export PATH=$PATH:/usr/local/nginx/sbin >>/etc/profile
//让设置生效
source /etc/profile
  • 或者创建 nginx 的映射连接:
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
  1. 2 查看 nginx 安装结果 & 版本号:
nginx -v
//打印结果...
nginx version: nginx/1.20.1
  • 查看nginx的配置编译参数
nginx -V
//打印结果...
nginx version: nginx/1.20.1
built by gcc 10.2.1 20201112 (Red Hat 10.2.1-8) (GCC)   //将(GCC)编译器的版本也给同时查印出来...
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: //打印的是预编译的配置内容...省略...
  1. 3 修改 nginx 运行端口:
  • 由于在服务器中,要让 apachenginx 同时运行,且【80】端口已经分配给了 apache ,这里将 nginx 修改为【8088】:
vim /usr/local/nginx/nginx.conf 
//原件内容如下...
listen       80;
server_name  localhost;
//修改内容如下...
listen       8088;
server_name  mhy.com;
  1. 4 安装 iptables 防火墙:
yum install iptables-services
  • 编辑防火墙配置文件,开启 & 放行【8088】端口:
vim /etc/sysconfig/iptables
//将如下字段添加到相应的端口位置中...    
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8088 -j ACCEPT
//:wq 保存退出。
  • 重启【iptables】生效配置:
systemctl restart iptables.service                //重启【iptables】生效配置
systemctl enable iptables.service                 //设置【iptables】开机自启动
/usr/libexec/iptables/iptables.init restart       //重置【iptables】
  1. 5 查看 & 确认是否开启【8088】监听端口:
netstat -tnl
tcp        0      0 127.0.0.1:4330          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8088            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:44321         0.0.0.0:*               LISTEN     
tcp6       0      0 :::33060                :::*                    LISTEN     
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 ::1:4330                :::*                    LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:44321               :::*                    LISTEN     
  • OK!【8088】端口开启成功!

06)启动【Nginx】

/usr/local/nginx/sbin/nginx
  • 启动 Nginx 报错…
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
  • 解决方案如下:
mkdir -p /var/cache/nginx/client_temp
  • 再次启动 Nginx ,成功!
/usr/local/nginx/sbin/nginx
  • 查看 Nginx 运行进程…
ps aux | grep nginx
========================================================================================================
root        9253  0.0  0.0  42604   860 ?        Ss   02:14   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx       9254  0.0  0.1  77336  5012 ?        S    02:14   0:00 nginx: worker process
root       11840  0.0  0.0  12352  1132 pts/0    S+   02:58   0:00 grep --color=auto nginx

07)【Nginx】启动 & 停止和重启服务

  1. 1 启动代码&路径格式:nginx 安装目录路径+ -c nginx 配置文件 nginx.conf 的路径,示例如下:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
//常规启动nginx服务...
/usr/local/nginx/sbin/nginx
  1. 2 停止 nginx 服务:
//常规停止nginx服务...
/usr/local/nginx/sbin/nginx -s stop
//优雅的停止nginx服务...
/usr/local/nginx/sbin/nginx -s quit
  • 1、从容停止:
    • 查看 nginxmaster process 主进程号…
    • 注:worker process 为工作进程…
ps -ef|grep nginx
root        9253       1  0 02:14 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12594    9253  0 03:19 ?        00:00:00 nginx: worker process
root       12761    8788  0 03:23 pts/0    00:00:00 grep --color=auto nginx
//master process主进程号则为:9235
  • 干掉主进程号…
kill -QUIT 9235
  • 2、快速停止:
    • 查看进程号【方法同 (1、)】…
  • 干掉主进程号…
kill -TERM 9235
    //或者
kill -INT 9235
  • 3、强行停止:
pkill -9 nginx
  1. 3 重启 Nginx 服务:
  • 首先验证 nginx 配置文件 nginx.conf 是否正确…
===方法-1===
//任何目录下...
nginx -t
===方法-2=== 
cd /usr/local/nginx/sbin
./nginx -t
===方法-3===//注意:该方法,配置文件的绝对路径要正确...
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/nginx.conf
    
=================================打印信息==================================
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
=================================简明释文==================================
Nginx:配置文件/usr/local/nginx/nginx.conf //语法没问题
Nginx:配置文件/usr/local/nginx/nginx.conf test is successful  //测试成功    
  • 1、 进入 nginx 可执行 sbin 目录下,输入如下命令…
cd /usr/local/nginx/sbin
./nginx -s reload
  • 2、重新加载配置文件 & 平滑重启 nginx
/usr/local/nginx/sbin/nginx -s reload
  • 3、查找当前 nginx 进程号,输入如下命令…,也可实现重启 nginx 服务:
kill -HUP 9235
//注:9235为前nginx的主进程号...

08)设置【Nginx】开机自动启动

  1. 1 创建 & 配置 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
  • 赋予 nginx 脚本可执行权限:
chmod a+x /etc/init.d/nginx
  1. 2 将 nginx 服务加入chkconfig 系统管理序列中…
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
chkconfig --list nginx
  1. 3 执行启动 nginx 服务脚本:
systemctl start nginx
  1. 4 终端命令行测试:
curl localhost:8088
  • 打印输出如下结果…
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

09)【Nginx】 重定向常用启停命令

  • 启动 nginx 服务:
systemctl start nginx
  • 查看 nginx 服务状态:
systemctl status nginx
● nginx.service - SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP>
   Loaded: loaded (/etc/rc.d/init.d/nginx; generated)
   Active: active (running) since Sun 2021-08-08 04:50:18 CST; 19min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 17347 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUC>
 Main PID: 9253 (nginx)
    Tasks: 0 (limit: 23376)
   Memory: 276.0K
   CGroup: /system.slice/nginx.service
           ? 9253 nginx: master process /usr/local/nginx/sbin/nginx
//省略启动日志......
  • 停止 nginx 服务:
systemctl stop nginx
  • 重新加载服务配置:
nginx -s reload
  • 验证 & 测试配置是否正确:
nginx -t

10)【Nginx】 配置优化

  • 以下配置在 nginx.conf 中与 http 属同级节点…
vim /usr/local/nginx/nginx.conf
user  nginx nginx;
worker_processes auto;
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
  • 说明:同级 http 节点下,加入以下配置,将会从指定目录加载 nginx.conf 配置文件…
include /etc/nginx/*.conf;

11)浏览器访问 & 测试【Nginx】安装结果

  • 说明:本地服务器 IP 已绑定虚拟域名…
    Nginx安装浏览器中浏览测试
    Nginx安装浏览器中浏览测试
  • 至此!VM虚拟机【CentOS-8x+Linux-8】中,源码编译安装【Nginx-1.20*】完美成功!
  • 内容相当全面,有点难度,耗时也很长,发文辛苦!如果对你有帮助,就请点个赞!留下你的高见呗…
  • 更多【IT & 编程】知识内容,请关注本人【sngee】的博客…欢迎大家交流 & 点评!!!

学如人生,砥砺前行…
互勉互励,志之所趋…

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 13:44:36  更:2021-08-09 13:44:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 19:23:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码