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 nfsutils automake libxml2 libxml2-devel libxslt libxslt-devel perl perlExtUtils-Embed
gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他
很多种语⾔的编译器(最早的时候只能编译C,后来很快进化成⼀个编译多种语⾔的集合,如Fortran、
Pascal、Objective-C、Java、Ada、 Go等。)
gcc 在编译C++源代码的阶段,只能编译 C++ 源⽂件,⽽不能⾃动和 C++ 程序使⽤的库链接(编译
过程分为编译、链接两个阶段,注意不要和可执⾏⽂件这个概念搞混,相对可执⾏⽂件来说有三个重要的概
念:编译(compile)、链接(link)、加载(load)。源程序⽂件被编译成⽬标⽂件,多个⽬标⽂件连
同库被链接成⼀个最终的可执⾏⽂件,可执⾏⽂件被加载到内存中运⾏)。因此,通常使⽤ g++ 命令来完
成 C++ 程序的编译和连接,该程序会⾃动调⽤ gcc 实现编译。
gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,⽽
g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语
法的要求是有区别的。
automake是⼀个从Makefile.am⽂件⾃动⽣成Makefile.in的⼯具。为了⽣成Makefile.in,
automake还需⽤到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。
libtool是⼀款⽅便⽣成各种程序库的⼯具。
pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular expression),因
为Nginx 的Rewrite模块和HTTP 核⼼模块会使⽤到PCRE正则表达式语法。
zlip zlib-devel:nginx启⽤压缩功能的时候,需要此模块的⽀持。
openssl openssl-devel:开启SSL的时候需要此模块的⽀持。
https://nginx.org/en/download.html
[root@nginx-web-12 ~]# cd /usr/local/src/ [root@nginx-web-12 src]# wget https://nginx.org/download/nginx-1.20.1.tar.gz [root@nginx-web-12 src]# tar xf nginx-1.20.1.tar.gz [root@nginx-web-12 src]# cd nginx-1.20.1
编译是为了检查系统环境是否符合编译安装的要求,⽐如是否有gcc编译⼯具,是否⽀持编译参数当中的模
块,并根据开启的参数等⽣成Makefile⽂件为下⼀步做准备:
[root@nginx-web-12 nginx-1.20.1]# ./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 [root@nginx-web-12 nginx-1.20.1]# make #编译步骤,根据Makefile⽂件⽣成相应的模块 [root@nginx-web-12 nginx-1.20.1]# make install #创建⽬录,并将⽣成的模块和⽂件复制到相应的⽬ 录: [root@nginx-web-12 nginx-1.20.1]# useradd nginx -s /sbin/nologin -u 2000 #以普通⽤户启动nginx [root@nginx-web-12 nginx-1.20.1]# chown nginx.nginx -R /apps/nginx/ #添加所有者所属组权限
备注:nginx完成安装以后,有四个主要的⽬录:
conf:该⽬录中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服务器的最核⼼最主要的配置 ⽂件,其他的.conf则是⽤来配置nginx相关的功能的,例如fastcgi功能使⽤的是fastcgi.conf和 fastcgi_params两个⽂件,配置⽂件⼀般都有个样板配置⽂件,是⽂件名.default结尾,使⽤的使⽤将 其复制为并将default去掉即可。 html:该⽬录中保存了nginx服务器的web⽂件,但是可以更改为其他⽬录保存web⽂件,另外还有⼀个 50x的web⽂件是默认的错误⻚⾯提示⻚⾯。 logs:该⽬录⽤来保存nginx服务器的访问⽇志错误⽇志等⽇志,logs⽬录可以放在其他路径,⽐ 如/var/logs/nginx⾥⾯。 sbin:该⽬录⽤来保存nginx⼆进制启动脚本,可以接受不同的参数以实现不同的功能。1.2.2验证版本及编译参数:
[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx -V nginx version: nginx/1.20.1 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_module1.2.3检测nginx:
[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful1.2.4启动nginx
[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx1.2.5测试访问nginx:
在浏览器中输入ip地址来访问nginx
1.2.6停止nginx:[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx -s stop1.2.6查看pid文件路径:
[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx [root@nginx-web-12 nginx-1.20.1]# ll /apps/nginx/logs/nginx.pid1.2.7创建Nginx⾃启动脚本:
#nginx 1.20.1
[root@nginx-web-12 nginx-1.20.1]# cat /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.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=/usr/sbin/nginx -s reload KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target1.2.8验证Nginx⾃启动脚本:
[root@nginx-web-12 nginx-1.20.1]# systemctl daemon-reload
[root@nginx-web-12 nginx-1.20.1]# systemctl start nginx
[root@nginx-web-12 nginx-1.20.1]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@nginx-web-12 nginx-1.20.1]# 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 Sat 2021-11-06 10:40:04 CST; 4min 55s ago
Main PID: 6413 (nginx)
CGroup: /system.slice/nginx.service
├─6413 nginx: master process /apps/nginx/sbin/nginx
└─6414 nginx: worker process
Nov 06 10:40:04 nginx-web-12 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 06 10:40:04 nginx-web-12 nginx[6407]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Nov 06 10:40:04 nginx-web-12 nginx[6407]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Nov 06 10:40:04 nginx-web-12 systemd[1]: Started The nginx HTTP and reverse proxy server.



