Code前端首页关于Code前端联系我们

Nginx 教程:从源代码构建、编译和安装

terry 2年前 (2023-09-28) 阅读数 56 #未命名

根据您的操作系统,nginx 的安装方式可能有所不同。有几种不同的方法来安装 nginx。

在 Linux 上安装

对于 Linux,您可以使用 nginx.org 中的 nginx 软件包。参考网址:

在FreeBSD上的安装

在FreeBSD上,nginx可以从软件包中安装,也可以通过端口系统安装。门系统提供了更大的灵活性,因此您可以在不同的选项之间进行选择。网关将使用指定的选项编译 nginx 并安装它。

安装在窗户上
先省略,别问为什么!

从源代码构建【推荐】

如果您需要一些特殊功能,当软件包和端口不可用时,您也可以通过源代码编译来安装nginx。虽然源码编译安装比较灵活,但是这种方法对于初学者来说可能会比较复杂(建议初学者本人使用源码编译安装来安装nginx)。

这篇文章我们主要从源码来介绍nginx的安装。本教程基于CentOS7 64位系统。非Centos系统不适用。让我们现在就开始吧! 7 yum -y update - 升级所有软件包,更改软件设置和系统设置,升级系统版本和内核
yum -yum update - 升级所有软件包,不更改软件设置和系统设置,系统版本升级,内核不变

取决于软件包安装

[root@localhost src]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
[root@localhost src]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
Shell

2.下载Nginx安装源文件

源代码下载,可在官网下载地址:http://nginx.org/en/ 下载download.html并上传到服务器(这里选择最新的稳定版本:nginx -1.10.3),如下图-
Nginx教程:从源代码构建、编译安装
或者直接在服务上运行以下命令,为上面的Shel下载上面的-

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
文件。 -
[root@localhost src]# tar zxvf nginx-1.10.3.tar.gz
Shel l

在编译之前,需要做一些前期准备工作,比如:Yilaan包安装、Nginx用户和用户组等。

3.创建新的nginx用户和用户组

以root用户登录系统,执行以下命令创建新用户。

[root@localhost src]# groupadd nginx
[root@localhost src]# useradd -g nginx -M nginx
Shel l

useradd命令的-M参数用于不为❀x 创建❀x 目录
修改 /etc/passwd,导致nginx用户无法bash登录(后来nginx用户由❀/♸更改为❀/ / sbin/nologin) ),

[root@localhost src]# vi /etc/passwd
Shel l

然后找到有nginx的那一行,改成(后来从/bin/bash❀n♼❀nology ): 4。编译配置,编译安装

然后我们进入解压后的nginx源码目录:/usr/local/src/执行以下命令-

[root@localhost ~]# cd /usr/local/src/nginx*
[root@localhost nginx-1.10.3]# pwd
/usr/local/src/nginx-1.10.3
[root@localhost nginx-1.10.3]#
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
 --group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

向后标记:

[root@localhost ~]# cd /usr/local/src/nginx*
[root@localhost nginx-1.10.3]# pwd
/usr/local/src/nginx-1.10.3
[root@localhost nginx-1.10.3]#
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
 --group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

表示换行继续。

--prefix=/usr/local/nginx指定安装到目录/usr/local/nginx

以上配置完成后,继续编译 -

[root@localhost nginx-1.10.3]# make
[root@localhost nginx-1.10.3]# make install
... ...
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/run' \
        || mkdir -p '/usr/local/nginx/run'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/local/src/nginx-1.10.3'
[root@localhost nginx-1.10.3]#
Shel l

以上编译时间与您的电脑配置有关,因此可能需要一些等待时间。

请参阅已安装的程序版本:

[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.10.3
Shell

Changenginx默认端口(可选):

[root@localhost nginx-1.10.3]# vi /usr/local/nginx/conf/nginx.conf

find -Shelleeeeee端口,例如:


修改配置后,检查配置是否合法:

[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Shel l

启动Nginx程序,看到进程-

-

[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.10.3]# ps -ef | grep nginx
root      29151      1  0 22:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     29152  29151  0 22:01 ?        00:00:00 nginx: worker process
root      29154   2302  0 22:01 pts/0    00:00:00 grep --color=auto nginx
[root@localhost nginx-1.10.3]#
x停止并s之前添加nginx服务 nginx的管理只能通过以下方式进行管理:
#  nginx 管理的几种方式 -
# 启动Nginx 
/usr/local/nginx/sbin/nginx 
# 从容停止Nginx:
kill -QUIT 主进程号 # 如上一步中的 ps 命令输出的 29151,就是 Nginx的主进程号
# 快速停止Nginx:
kill -TERM 主进程号
# 强制停止Nginx:
pkill -9 nginx
# 平滑重启nginx
/usr/nginx/sbin/nginx -s reload

现在我们来看看安装好的Nginx的运行结果。您只需使用命令 curl 即可访问 localhost 测试。结果如下 -

[root@localhost nginx-1.10.3]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost nginx-1.10.3]#

或者也可以打开浏览器访问目标服务器的IP。本例中服务器的IP地址为:192.168.0.195,所以打开浏览器访问如下结果——
Nginx教程:从源代码构建、编译安装

温馨提示:如果没有看到以上界面,请确保Nginx已启动的前提下,检查SeLinux和防火墙是否关闭。关闭防火墙命令:systemctl stopfirewalld.service

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

热门