Nginx 编译安装:

1.Nginx 编译安装:

1.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 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的时候需要此模块的⽀持。

1.2安装Nginx:

1.2.1官⽅源码包下载地址:

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_module

1.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 successful

1.2.4启动nginx

[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx

1.2.5测试访问nginx:

在浏览器中输入ip地址来访问nginx

1.2.6停止nginx:

[root@nginx-web-12 nginx-1.20.1]# /apps/nginx/sbin/nginx -s stop

1.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.pid 

1.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.target

1.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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值