系统版本:centos7.7

Nginx版本:nginx-1.16.0

环境准备

  先安装准备环境

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

注释:

  gcc为GUN Compiler Collection的缩写,可以编译C和C++源代码等, 它是GUN开发的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创建的发布完全遵循GUN标准,所以在创建中不需要Perl。libtool是一款方便生成各种程序库的工具。

  pcre pcre-devel:在Nginx编译需要PCRE(Perl Compatible Regular Expression),因为Nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法。

  zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持。

  openssl openssl-devel:开启SSL的时候需要此模块的支持。

安装Nginx

  操作命令如下:

mkdir -p /home/vast/tools                                    --> -p 递归创建目录
cd /home/vast/tools/                                         --> 跳转至/home/vast/tools目录
wget -q http://nginx.org/download/nginx-1.16.0.tar.gz        -->下载Nginx软件包
ls -l nginx-1.16.0.tar.gz
useradd nginx -s /sbin/nologin -M                            -->创建Nginx用户
tar zxvf nginx-1.16.0.tar.gz                                 -->解压Nginx软件包
cd nginx-1.16.0
./configure --user=nginx --group=nginx                       -->进程用户或组权限
            --prefix=/application/nginx-1.16.0/              -->设置安装路径
            --with-http_ssl_module                           -->激活ssl功能
            --with-http_stub_status_module                   -->激活状态信息
make && make install
ln -s /application/nginx-1.16.0/ /application/nginx
 将Nginx安装路径通过软连接的方式更改为/application/nginx/,方便使用
 安装时指定版本号路径为了便于查看区分当前使用的Nginx版本,方便升级
 内部人员使用路径/application/nginx/。
 当Nginx软件升级编译成带新版本号的版本后,删除原软链接,重新建立到/application/nginx/的软链接就好

安装完成后启动并检查安装结果

  1、启动前检查配置文件语法

[root@vast ~]# /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

  2、启动Nginx服务

/application/nginx/sbin/nginx

  1)查看Nginx服务对应的端口是否成功启动

[root@vast ~]# lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   23363  root    6u  IPv4  53122      0t0  TCP *:http (LISTEN)
nginx   23364 nginx    6u  IPv4  53122      0t0  TCP *:http (LISTEN)

  2)也可以通过netstat -lnt|grep 80查看

[root@vast ~]# netstat -lnt|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN

  3、检查Nginx启动的实际效果

  1)在Linux系统下可使用wget命令检测

[root@vast ~]# wget 127.0.0.1
--2019-12-12 16:01:20--  http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: ‘index.html’

100%[=========================================================================================>] 612         --.-K/s   in 0s

2019-12-12 16:01:20 (87.9 MB/s) - ‘index.html’ saved [612/612]

  2)也可以使用curl命令检测

[root@vast ~]# curl 127.0.0.1
<!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>

将Nginx添加到systemctl

新增nginx.service文件

vim /usr/lib/systemd/system/nginx.service

添加以下内容

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/application/nginx/logs/nginx.pid
ExecStartPre=/application/nginx/sbin/nginx -t -c /application/nginx/conf/nginx.conf
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别

[Service]部分是服务的关键,是服务的一些具体运行参数的设置,这里Type=forking是后台运行的形式,PIDFile为存放PID的文件路径,ExecStart为服务的具体运行命令,ExecReload为重启命令,ExecStop为停止命令,PrivateTmp=True表示给服务分配独立的临时空间,注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!

[Install]部分是服务安装的相关设置,可设置为多用户的

将Nginx设置为开机启动

systemctl enable nginx.service

测试启动Nginx

systemctl start nginx

其他命令
systemctl reload nginx.service //重启
systemctl stop nginx.service //停止
systemctl stop nginx.service //停止
systemctl status nginx.service //查看状态

 至此,centos7编译安装Nginx-1.16.0安装完成。

12-26 23:59
查看更多