Linux,  NGINX

如何在CentOS 7上通过编译源码来安装配置NGINX

零 机器信息

[root@gxbigdatadev ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
[root@gxbigdatadev ~]# uptime 
 10:16:48 up 17 days, 22:19,  4 users,  load average: 0.12, 0.04, 0.05
[root@gxbigdatadev ~]#  
[root@gxbigdatadev ~]# uname -rm
3.10.0-693.el7.x86_64 x86_64
[root@gxbigdatadev ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
[root@gxbigdatadev ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7822        5549         188          42        2084        1893
Swap:             0           0           0
[root@gxbigdatadev ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       498G   14G  485G   3% /
devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs                   tmpfs     3.9G   42M  3.8G   2% /run
tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.9G  180M  1.7G  10% /boot
tmpfs                   tmpfs     783M   56K  783M   1% /run/user/1000
/dev/sr0                iso9660   4.3G  4.3G     0 100% /run/media/t1/CentOS 7 x86_64
tmpfs                   tmpfs     783M     0  783M   0% /run/user/0
[root@gxbigdatadev ~]# 

一 下载解压

到NGINX官网,下载最新版本的NGINX 1.24。https://nginx.org/en/download.html

[root@gxbigdatadev ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
--2023-04-28 10:23:58--  https://nginx.org/download/nginx-1.24.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: 'nginx-1.24.0.tar.gz'
​
100%[========================================================================================================================================>] 1,112,471    629KB/s   in 1.7s   
​
2023-04-28 10:24:01 (629 KB/s) - 'nginx-1.24.0.tar.gz' saved [1112471/1112471]
​
[root@gxbigdatadev ~]# tar -zxvf nginx-1.24.0.tar.gz 
nginx-1.24.0/
nginx-1.24.0/auto/
...
nginx-1.24.0/auto/cc/sunc
[root@gxbigdatadev ~]# 

二 编译源码

进入到解压后的源代码路径下,执行./configure –prefix=/etc/nginx –with-http_ssl_module来编译源码。如果遇到因为操作系统缺少必要的软件包,则根据提示先安装系统软件包,再重新执行编译。

[root@gxbigdatadev ~]# cd nginx-1.24.0/
[root@gxbigdatadev nginx-1.24.0]# ll
total 816
drwxr-xr-x. 6 1001 1001   4096 Apr 28 10:24 auto
-rw-r--r--. 1 1001 1001 323312 Apr 11 09:45 CHANGES
-rw-r--r--. 1 1001 1001 494234 Apr 11 09:45 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 Apr 28 10:24 conf
-rwxr-xr-x. 1 1001 1001   2611 Apr 11 09:45 configure
drwxr-xr-x. 4 1001 1001     72 Apr 28 10:24 contrib
drwxr-xr-x. 2 1001 1001     40 Apr 28 10:24 html
-rw-r--r--. 1 1001 1001   1397 Apr 11 09:45 LICENSE
drwxr-xr-x. 2 1001 1001     21 Apr 28 10:24 man
-rw-r--r--. 1 1001 1001     49 Apr 11 09:45 README
drwxr-xr-x. 9 1001 1001     91 Apr 28 10:24 src
[root@gxbigdatadev nginx-1.24.0]# ./configure --prefix=/etc/nginx --with-http_ssl_module
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
...
creating objs/Makefile
​
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library
​
  nginx path prefix: "/etc/nginx"
  nginx binary file: "/etc/nginx/sbin/nginx"
  nginx modules path: "/etc/nginx/modules"
  nginx configuration prefix: "/etc/nginx/conf"
  nginx configuration file: "/etc/nginx/conf/nginx.conf"
  nginx pid file: "/etc/nginx/logs/nginx.pid"
  nginx error log file: "/etc/nginx/logs/error.log"
  nginx http access log file: "/etc/nginx/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@gxbigdatadev nginx-1.24.0]# 

三 安装配置

源码编译之后,如果没有问题,则可以执行make && make install来安装NGINX软件。

[root@gxbigdatadev nginx-1.24.0]# make && make install
...
test -d '/etc/nginx/logs' \
        || mkdir -p '/etc/nginx/logs'
make[1]: Leaving directory `/root/nginx-1.24.0'
[root@gxbigdatadev nginx-1.24.0]# 

四 设置自动启动

通过执行下述配置脚本来设置NGINX服务随着OS的启动而自动启动:

cat <<EOF>/etc/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=/etc/nginx/sbin/nginx
ExecReload=/etc/nginx/sbin/nginx -s reload
ExecStop=/etc/nginx/sbin/nginx -s quit
PrivateTmp=true
​
[Install]
WantedBy=multi-user.target
EOF
​
​
[root@gxbigdatadev nginx-1.24.0]# cat <<EOF>/etc/systemd/system/nginx.service
> [Unit]
> Description=nginx
> After=network.target
>  
> [Service]
> Type=forking
> ExecStart=/etc/nginx/sbin/nginx
> ExecReload=/etc/nginx/sbin/nginx -s reload
> ExecStop=/etc/nginx/sbin/nginx -s quit
> PrivateTmp=true
> 
> [Install]
> WantedBy=multi-user.target
> EOF
[root@gxbigdatadev nginx-1.24.0]# 
[root@gxbigdatadev nginx-1.24.0]# systemctl daemon-reload
systemctl start nginx
systemctl enable nginx     [root@gxbigdatadev nginx-1.24.0]# systemctl start nginx
[root@gxbigdatadev nginx-1.24.0]# systemctl enable nginx     
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /etc/systemd/system/nginx.service.
[root@gxbigdatadev nginx-1.24.0]# systemctl status nginx     
● nginx.service - nginx
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-04-28 10:32:34 CST; 13s ago
 Main PID: 18192 (nginx)
   CGroup: /system.slice/nginx.service
           ├─18192 nginx: master process /etc/nginx/sbin/nginx
           └─18193 nginx: worker process
​
Apr 28 10:32:34 gxbigdatadev.localdomain systemd[1]: Starting nginx...
Apr 28 10:32:34 gxbigdatadev.localdomain systemd[1]: Started nginx.
[root@gxbigdatadev nginx-1.24.0]# 

五 添加配置文件路径

重建/etc/nginx/conf.d,专门用于存放配置文件。然后,再在/etc/nginx/conf/nginx.conf主配置文件中,添加这个路径,并重启NGINX。

[root@gxbigdatadev ~]# cd /etc/nginx/
[root@gxbigdatadev nginx]# mkdir -p conf.d
[root@gxbigdatadev nginx]# pwd
/etc/nginx
[root@gxbigdatadev nginx]# ll
total 4
drwx------. 2 nobody root    6 Apr 28 10:32 client_body_temp
drwxr-xr-x. 2 root   root 4096 Apr 28 10:53 conf
drwxr-xr-x. 2 root   root    6 Apr 28 10:49 conf.d
drwx------. 2 nobody root    6 Apr 28 10:32 fastcgi_temp
drwxr-xr-x. 2 root   root   40 Apr 28 10:28 html
drwxr-xr-x. 2 root   root   58 Apr 28 10:32 logs
drwx------. 2 nobody root    6 Apr 28 10:32 proxy_temp
drwxr-xr-x. 2 root   root   19 Apr 28 10:28 sbin
drwx------. 2 nobody root    6 Apr 28 10:32 scgi_temp
drwx------. 2 nobody root    6 Apr 28 10:32 uwsgi_temp
[root@gxbigdatadev nginx]# vi conf/nginx.conf
​
#user  nobody;
worker_processes  1;
​
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
​
#pid        logs/nginx.pid;
​
​
events {
    worker_connections  1024;
}
​
include conf.d/*;       #添加这一行,保存退出
​
http {
    include       mime.types;
 
[root@gxbigdatadev nginx]# nginx -t
nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/conf/nginx.conf test is successful
[root@gxbigdatadev nginx]# nginx -s reload
[root@gxbigdatadev nginx]# 

六 完整脚本

wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz 
cd nginx-1.24.0
./configure --prefix=/etc/nginx  --with-http_ssl_module
make && make install
​
cat <<EOF>/etc/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=/etc/nginx/sbin/nginx
ExecReload=/etc/nginx/sbin/nginx -s reload
ExecStop=/etc/nginx/sbin/nginx -s quit
PrivateTmp=true
​
[Install]
WantedBy=multi-user.target
EOF
​
###
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx   
systemctl status nginx