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

4 篇文章 0 订阅

=目录索引=

01)系统 &环境

运行环境:实验室虚拟环境
系统型号:CtenOS-8

=主题内容=

02)安装前准备

1 更新 CentOS-8 软件包 :

dnf update

2 关闭系统防火墙:

systemctl stop firewalld

3 关闭防火墙开机自启动:

systemctl disable firewalld

4 关闭 SELINUX :

sed -ri '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
setenforce 0

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

1 安装 make & wget :

yum -y install gcc automake autoconf libtool make wget

2 安装 gcc 编译器

   系统内置 gcc 版本查询   

gcc --version
gcc (GCC) 10.2.1 20201112 (Red Hat 10.2.1-8)   //版本够高够新,不用升级....
yum -y install gcc gcc-c++

3 安装 OpenSSL 开发库

ssl 模块,服务器使用 SSL 安全证书需要,【–http_ssl_module】用于开启 https 安全加密功能,依赖 OpenSSL 开发库

yum -y install openssl openssl-devel

4 安装 PCRE 正则式表达库:

nginx 必装模块【–http_rewrite_module】,基于正则匹配来实现重定向,依赖PCRE 库 

yum -y install pcre pcre-devel

5 安装 zlib 压缩库:

nginx 必装模块【–http_gzip_module】,用于针对 HTTP 包中内容进行 gzip 格式的压缩,依赖 zlib 压缩库:

yum -y install zlib zlib-devel

04)编译安装【Nginx】

1 创建 nginxs 用户 & 用户属组:

useradd -s /sbin/nologin -M nginx

2 官网下载 nginx 最新稳定版:

mkdir lanmpp
cd /root/lanmpp
wget http://nginx.org/download/nginx-1.20.1.tar.gz

3  解压 & 删除 nginx 压缩包

tar -xvf nginx-1.20.1.tar.gz
rm -rf nginx-1.20.1.tar.gz

4 预编译 & 配置 nginx 源码,使之生成可执行的二进制文件

./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-pcre

打印出现如下结果,则证明预编译 & 生成 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"

5 开始编译 & 安装 nginx 
        //nginx短小精悍,就不用加速编译了...

make && make install

05)配置【Nginx】

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

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: //打印的是预编译的配置内容...省略...

3 修改 nginx 运行端口:

由于在服务器中,要让 apache 和 nginx 同时运行,且【80】端口已经分配给了 apache ,这里将 nginx 修改为【8088】

vim /usr/local/nginx/conf/nginx.conf 
//原件内容如下...
listen       80;
server_name  localhost;
//修改内容如下...
listen       8088;
server_name  mhy.com;

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 status iptables.service                 //查看【iptables】状态
systemctl restart iptables.service                //重启【iptables】生效配置
systemctl enable iptables.service                 //设置【iptables】开机自启动
/usr/libexec/iptables/iptables.init restart       //重置【iptables】

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 启动代码&路径格式s:nginx 安装目录路径+ -c nginx 配置文件 nginx.conf 的路径,示例如下:

#使之生成【nginx.pid】文件...

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

//常规启动nginx服务...

/usr/local/nginx/sbin/nginx

停止 nginx 服务:

//常规停止nginx服务...
/usr/local/nginx/sbin/nginx -s stop
//优雅的停止nginx服务...
/usr/local/nginx/sbin/nginx -s quit

(1)从容停止:

  • 查看 nginx 的 master 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

(2) 强行停止:

pkill -9 nginx

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 创建 & 配置 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

2 将 nginx 服务加入chkconfig 系统管理序列中…

chkconfig --add /etc/init.d/nginx
chkconfig nginx on
chkconfig --list nginx

3 执行启动 nginx 服务脚本:

systemctl start nginx

遇到问题,未解决!

当杀掉原来的nginx进程后,nginx.pid文件会消失,就算不消失, nginx.pid中所记录的进程号也会失效,于是报错(下图),未解决!!!

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

或者

service nginx start

 查看 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+post(教程为8088端口) 

至此!阿里云服务器【CentOS-8x】中,源码编译安装【Nginx-1.20*】完美成功!

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 在 CentOS 7 源码编译安装 TiDB 步骤如下: 1. 下载 TiDB 源码: ``` $ git clone https://github.com/pingcap/tidb.git $ cd tidb ``` 2. 编译安装 TiDB: ``` $ make $ make install ``` 3. 配置并启动 TiDB: - 在 `tidb/cmd/tidb-server` 目录下创建一个 `tidb-server.toml` 配置文件,然后配置需要的参数,具体参数可以参考文档。 - 启动 TiDB 服务: ``` $ tidb-server -config=tidb-server.toml ``` 注意:在安装 TiDB 之前,你需要安装 Go 语言的开发环境。 参考文档:https://docs.pingcap.com/zh/tidb/stable/install-tidb-from-source ### 回答2: 在CentOS 7,要编译安装一个TiDB,需要执行以下步骤: 1. 首先,确保系统已经安装编译和构建所需的软件包。使用以下命令安装这些软件包: ``` sudo yum install -y git gcc make cmake autoconf automake libtool pkgconfig bison flex ``` 2. 下载TiDB的源代码。通过Git克隆TiDB代码仓库到本地文件夹: ``` git clone https://github.com/pingcap/tidb.git ``` 3. 进入TiDB源代码目录: ``` cd tidb ``` 4. 编译和构建TiDB。执行以下命令编译和构建TiDB: ``` make ``` 这个过程可能需要一些时间,取决于系统配置和网络速度。 5. 安装TiDB。编译成功后,执行以下命令将TiDB安装到系统: ``` make install ``` 安装过程会将编译生成的二进制文件和依赖项复制到系统路径。 6. 配置和启动TiDB。进入TiDB源代码目录,使用以下命令进行配置和启动: ``` cd bin ./tidb-server ``` 这将启动TiDB服务器,并监听默认端口。 通过以上步骤,在CentOS 7成功编译安装了一个TiDB实例。 ### 回答3: 在CentOS 7源码编译安装TiDB,您可以按照以下步骤进行操作: 1. 安装必要的依赖:首先,确保已经安装了Git、Go和GCC等必要的软件包。您可以使用以下命令安装这些依赖: ``` sudo yum install git go gcc ``` 2. 下载源代码:使用Git命令克隆TiDB的源代码库到相应的目录。例如,您可以使用以下命令将源代码克隆到`/opt/tidb`目录: ``` git clone https://github.com/pingcap/tidb.git /opt/tidb ``` 3. 编译安装:进入源代码目录并执行以下命令来编译安装TiDB: ``` cd /opt/tidb make make deploy ``` 这些命令将在编译后生成的`bin`目录输出编译好的可执行文件,并将其安装到指定的目录。 4. 配置和启动:编辑`conf`目录下的配置文件,根据自己的需求进行相应的配置。然后,使用以下命令启动TiDB: ``` cd /opt/tidb ./bin/tidb-server -config=conf/tidb.toml ``` TiDB将在默认端口4000上启动。 以上就是在CentOS 7源码编译安装TiDB的基本步骤。请注意,在执行步骤2之前,请确保已经安装了正确本的Git、Go和GCC等软件包。另外,如果遇到依赖问题,请根据错误提示安装相应的依赖软件包。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wcybaonier

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值