1.2:Nginx 编译安装
1.2.1:准备编译环境
- 安装编译基础环境以及常用工具:
yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
1.2.2:下载 Nginx 源码包
Nginx 官方下载地址:https://nginx.org/en/download.html
[root@node106 ~]# cd /usr/local/src/
[root@node106 src]# ll
total 1016
-rw-r--r-- 1 root root 1039530 Oct 19 17:33 nginx-1.18.0.tar.gz
1.2.3:解压 Nginx 源码包
[root@node106 src]# tar zxvf nginx-1.18.0.tar.gz
[root@node106 src]# ll nginx-1.18.0
total 768
drwxr-xr-x 6 1001 1001 4096 Dec 2 03:58 auto
-rw-r--r-- 1 1001 1001 302863 Apr 21 2020 CHANGES
-rw-r--r-- 1 1001 1001 462213 Apr 21 2020 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Dec 2 03:58 conf
-rwxr-xr-x 1 1001 1001 2502 Apr 21 2020 configure
drwxr-xr-x 4 1001 1001 68 Dec 2 03:58 contrib
drwxr-xr-x 2 1001 1001 38 Dec 2 03:58 html
-rw-r--r-- 1 1001 1001 1397 Apr 21 2020 LICENSE
drwxr-xr-x 2 1001 1001 20 Dec 2 03:58 man
-rw-r--r-- 1 1001 1001 49 Apr 21 2020 README
drwxr-xr-x 9 1001 1001 84 Dec 2 03:58 src
1.2.4:生成 Makefile
- 这一步是检查系统环境是否符合编译安装的要求,是否支持编译参数中的模块,并根据指定的编译参数生成 Makefile 文件,为接下来的编译安装作好准备:
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
1.2.5:编译并安装
- 编译是根据 Makefile 生成安装所需的相应模块文件:
[root@node106 nginx-1.18.0]# make
- 安装是创建相应目录,并将编译过程生成的模块和文件复制到相应目录:
[root@node106 nginx-1.18.0]# make install
1.2.6:创建 Nginx 用户
- 创建 Nginx 用户,指定 UID 为 2000,shell 为 nologin:
[root@node106 ~]# useradd -u 2000 -s /sbin/nologin nginx
- 验证 Nginx 用户:
[root@node106 ~]# id nginx
uid=2000(nginx) gid=2000(nginx) groups=2000(nginx)
1.2.7:创建额外的配置文件目录
[root@node106 ~]# mkdir /apps/nginx/conf.d
[root@node106 ~]# chown nginx:nginx -R /apps/nginx/conf.d/
1.2.8:创建 nginx 软链接
[root@node106 ~]# ln -sv /apps/nginx/sbin/nginx /usr/bin/nginx
‘/usr/bin/nginx’ -> ‘/apps/nginx/sbin/nginx’
1.2.9:验证编译安装
- 查看安装生成的文件目录:
[root@node106 ~]# ll /apps/nginx/
total 4
drwxr-xr-x 2 root root 4096 Dec 2 04:18 conf
drwxr-xr-x 2 root root 38 Dec 2 04:18 html
drwxr-xr-x 2 root root 6 Dec 2 04:18 logs
drwxr-xr-x 2 root root 18 Dec 2 04:18 sbin
- 查看 Nginx 版本及编译配置参数:
[root@node106 ~]# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
1.2.10:Nginx 默认配置
[root@node106 ~]# grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
1.2.11:创建 Nginx 启动脚本
- 编辑启动脚本:
[root@node106 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /apps/nginx/logs/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 重载 systemd:
[root@node106 ~]# systemctl daemon-reload
1.2.12:启动 Nginx
- 设置开机启动:
[root@node106 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
- 启动 Nginx:
[root@node106 ~]# systemctl start nginx
- 查看 Nginx 运行状态:
[root@node106 ~]# ssytemctl status nginx
-bash: ssytemctl: command not found
[root@node106 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-12-02 04:27:38 CST; 12s ago
Process: 6208 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 6206 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 6205 ExecStartPre=/usr/bin/rm -f /apps/nginx/logs/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 6210 (nginx)
CGroup: /system.slice/nginx.service
├─6210 nginx: master process /apps/nginx/sbin/nginx
└─6211 nginx: worker process
Dec 02 04:27:38 node106.yqc.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 02 04:27:38 node106.yqc.com nginx[6206]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Dec 02 04:27:38 node106.yqc.com nginx[6206]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Dec 02 04:27:38 node106.yqc.com systemd[1]: Failed to parse PID from file /apps/nginx/logs/nginx.pid: Invalid argument
Dec 02 04:27:38 node106.yqc.com systemd[1]: Started The nginx HTTP and reverse proxy server.
- 查看 Nginx 进程:
[root@node106 ~]# ps -ef | grep nginx | grep -v grep
root 6210 1 0 20:27 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 6211 6210 0 20:27 ? 00:00:00 nginx: worker process