菜鸟教程linux安装nginx教程:Linux系统中编译安装Nginx服务器

161阅读模式

编译安装Nginx

编译安装Nginx,需要依赖pcre-devel和openssl-devel两个基础依赖包。所以在安装Nginx之前,需要先检查该依赖包是否已安装。

安装基础依赖包

安装PCRE库

PCRE(opens new window)(Perl Compatible Regular Expressions)意为"Perl兼容正则表达式",安装PCRE是为了让Nginx支持具备URL重写功能的Rewrite模块,如果不安装,则Nginx无法使用Rewrite模块功能。

使用yum安装方式安装PCRE:

[root@edaa2f912abf /]# yum instal pcre pcre-devel -y

yum安装后检查安装结果:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf /]# rpm -qa pcre pcre-devel
pcre-8.32-17.el7.x86_64
pcre-devel-8.32-17.el7.x86_64

安装openssl-devel

使用yum安装openssl-devel:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf /]# yum install openssl openssl-devel -y

检查yum安装结果:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf /]# rpm -qa openssl openssl-devel
openssl-devel-1.0.2k-25.el7_9.x86_64
openssl-1.0.2k-25.el7_9.x86_64

安装Nginx

在确保Nginx编译安装所依赖的pcre-devel和openssl-devel已经安装好了后,可以下载Nginx源码进行编译安装。 Nginx源码可以从Nginx官方网站(http://nginx.org)找到相应版本源码的下载地址: http://nginx.org/download/nginx-1.16.0.tar.gz文章源自懂站帝-http://www.sfdkj.com/18730.html

下载Nginx源码:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf /]# mkdir -p /home/tools/ && cd /home/tools
[root@edaa2f912abf tools]# wget -q http://nginx.org/download/nginx-1.16.0.tar.gz
[root@edaa2f912abf tools]# ls -lh
total 1012K
-rw-r--r-- 1 root root 1009K Apr 23  2019 nginx-1.16.0.tar.gz

添加nginx用户,服务于Nginx:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf tools]# useradd nginx -u 1111 -s /sbin/nologin -M

解压软件包:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf tools]# tar xf nginx-1.16.0.tar.gz

编译Ngixn源码并安装:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf tools]# cd nginx-1.16.0
[root@edaa2f912abf nginx-1.16.0]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module
[root@edaa2f912abf nginx-1.16.0]# make
[root@edaa2f912abf nginx-1.16.0]# make install
其中--prefix选项就是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr/local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,各种文件比较分散。 为了便于集中管理某个软件的各种文件,可以配置–prefix,如: ./configure –-prefix=/application/nginx-1.16.0 可以把所有资源文件放在/application/nginx-1.16.0的路径中,就不会分散了。 用了--prefix选项的另一个好处是卸载软件或移植软件,当某个安装的软件不再需要时,只须简单地删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。

建立nginx应用程序软链接:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf nginx-1.16.0]# ln -s /application/nginx-1.16.0 /application/nginx
建立软件链接的操作并不是必须要做的,这样做的主要是为了后续nginx升级,用户操作不受影响。 对于普通用户,使用nginx的目录就是/application/nginx目录,不管后续如何升级Nginx版本,只需要将最新的Nginx版本目录与统一的/application/nginx目录建立新的软链接即可。

# 启动并检查安装结果

启动前为了避免报错,先检查配置文件语法:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf tools]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful

启动Nginx服务:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf tools]# /application/nginx/sbin/nginx

查看Nginx服务是否启动成功:文章源自懂站帝-http://www.sfdkj.com/18730.html

[root@edaa2f912abf tools]# netstat -lntup|grpe nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3351/nginx: master

说明已经启动成功,打开浏览器输入http://127.0.0.1/就可以看到nginx网页信息。文章源自懂站帝-http://www.sfdkj.com/18730.html文章源自懂站帝-http://www.sfdkj.com/18730.html

懂站帝
  • 本文由 发表于 2022年7月9日 09:55:34
  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至395045033@qq.com举报,一经查实,本站将立刻删除。
评论  0  访客  0