Centos 下安装 Nginx

Centos 下安装 Nginx

今天我们的目标就是在 Centos上搭建好 Nginx 服务,方便后续开展各种测试和学习。这也是学习其他互联网组件的第一步。接下来,我将分步骤描述源码编译的过程,并做部分说明。

1. 官网下载最新源代码,并解压

目前官方最新版本(截止到2019年12月12日)为 1.17.6,我们直接去官网找到下载地址。不推荐直接使用 yum 安装,因为存在如下两个问题:

  • 版本太旧,以 CentOS 为例,直接 yum 安装的版本是 1.12.2 版本,已经严重脱离了时代;

  • 无法自定义安装模块,安装目录等等,不方便后续的实验。

 
 
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@localhost ~]# cd /usr/local/src
# 下载nginx安装包[root@localhost src]# wget https://www.linuxprobe.com/Software/nginx-1.6.0.tar.gz 
# 解压安装包[root@localhost src]# tar -xzf nginx-1.6.0.tar.gz

2. 依赖库提前安装

Nginx 是完全使用 C 语言开发的,所以必须要有 C 编译环境,往往 CentOS 7.6 的环境会预装 gcc 编译器,所以不用额外安装,如果没有使用 yum 直接安装即可。另外,我们使用 Nginx 的压缩功能、正则表达式功能等,需要安装一些额外的依赖库,这是必须要做的,不然在编译阶段就会报错。如下3个是比较 Nginx 中比较常用模块所依赖的库,请在执行 Nginx 源码编译时提前安装好。

下载Nginx安装所需要的依赖库包

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# ls
Python-3.7.0.tar.xz
[root@localhost src]# wget https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
--2020-05-17 20:35:36--  https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
正在解析主机 www.linuxprobe.com (www.linuxprobe.com)... 183.240.224.121, 183.240.224.114, 183.240.224.116, ...
正在连接 www.linuxprobe.com (www.linuxprobe.com)|183.240.224.121|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:571091 (558K) [application/octet-stream]
正在保存至: “zlib-1.2.8.tar.gz”
100%[==============================================================================================================================>] 571,091     1.16MB/s 用时 0.5s   
2020-05-17 20:35:37 (1.16 MB/s) - 已保存 “zlib-1.2.8.tar.gz” [571091/571091])
[root@localhost src]# wget https://www.linuxprobe.com/Software/pcre-8.35.tar.gz
--2020-05-17 20:36:12--  https://www.linuxprobe.com/Software/pcre-8.35.tar.gz
正在解析主机 www.linuxprobe.com (www.linuxprobe.com)... 183.240.224.115, 183.240.224.119, 183.240.224.120, ...
正在连接 www.linuxprobe.com (www.linuxprobe.com)|183.240.224.115|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1996552 (1.9M) [application/octet-stream]
正在保存至: “pcre-8.35.tar.gz”
100%[==============================================================================================================================>] 1,996,552   2.94MB/s 用时 0.6s   
2020-05-17 20:36:13 (2.94 MB/s) - 已保存 “pcre-8.35.tar.gz” [1996552/1996552])
[root@localhost src]# wget https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
--2020-05-17 20:36:54--  https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
正在解析主机 www.linuxprobe.com (www.linuxprobe.com)... 183.240.224.120, 183.240.224.116, 183.240.224.117, ...
正在连接 www.linuxprobe.com (www.linuxprobe.com)|183.240.224.120|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:4475692 (4.3M) [application/octet-stream]
正在保存至: “openssl-1.0.1h.tar.gz”
100%[==============================================================================================================================>] 4,475,692   3.93MB/s 用时 1.1s   
2020-05-17 20:36:57 (3.93 MB/s) - 已保存 “openssl-1.0.1h.tar.gz” [4475692/4475692])
[root@localhost src]# ls
openssl-1.0.1h.tar.gz  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]#

在正式安装Nginx服务程序之前,我们还需要为其解决相关的软件依赖关系,例如用于提供Perl语言兼容的正则表达式库的软件包pcre,就是Nginx服务程序用于实现伪静态功能必不可少的依赖包。下面来解压、编译、生成、安装Nginx服务程序的源码文件:

[root@localhost ~]# cd /usr/local/src

[root@localhost src]# ls
openssl-1.0.1h.tar.gz  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]# tar xzvf pcre-8.35.tar.gz 
[root@localhost src]# cd pcre-8.35
[root@localhost pcre-8.35]# ./configure --prefix=/usr/local/pcre
[root@localhost pcre-8.35]# make
[root@localhost pcre-8.35]# make install

openssl软件包是用于提供网站加密证书服务的程序文件,在安装该程序时需要自定义服务程序的安装目录,以便于稍后调用它们的时候更可控。

[root@localhost pcre-8.35]# cd /usr/local/src
[root@localhost src]# ls
openssl-1.0.1h.tar.gz  pcre-8.35  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]# tar xzvf openssl-1.0.1h.tar.gz
[root@localhost src]# cd openssl-1.0.1h
[root@localhost openssl-1.0.1h]# ./config --prefix=/usr/local/openssl
[root@localhost openssl-1.0.1h]# make
[root@localhost openssl-1.0.1h]# make install

openssl软件包安装后默认会在/usr/local/openssl/bin目录中提供很多的可用命令,我们需要像前面的操作那样,将这个目录添加到PATH环境变量中,并写入到配置文件中,最后执行source命令以便让新的PATH环境变量内容可以立即生效:

[root@linuxprobe pcre-8.35]# vim /etc/profile
………………省略部分输出信息………………
 64 
 65 for i in /etc/profile.d/*.sh ; do
 66 if [ -r "$i" ]; then
 67 if [ "${-#*i}" != "$-" ]; then
 68 . "$i"
 69 else
 70 . "$i" >/dev/null
 71 fi
 72 fi
 73 done
 74 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
 75 unset i
 76 unset -f pathmunge
[root@linuxprobe pcre-8.35]# source /etc/profile

zlib软件包是用于提供压缩功能的函数库文件。其实Nginx服务程序调用的这些服务程序无需深入了解,只要大致了解其作用就已经足够了:

[root@localhost openssl-1.0.1h]# cd /usr/local/src
[root@localhost src]# ls
openssl-1.0.1h  openssl-1.0.1h.tar.gz  pcre-8.35  pcre-8.35.tar.gz  Python-3.7.0.tar.xz  zlib-1.2.8.tar.gz
[root@localhost src]# tar xzvf zlib-1.2.8.tar.gz
[root@localhost src]# cd zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure --prefix=/usr/local/zlib
[root@localhost zlib-1.2.8]# make
[root@localhost zlib-1.2.8]# make install

依赖库安装好后重启系统

[root@localhost nginx-1.17.6]# reboot

编译并安装Nginx

[root@localhost mysql-5.6.19]# cd /usr/local/src
[root@localhost src]# useradd www -s /sbin/nologin
[root@localhost nginx-1.6.0]# make install

特别是在 Nginx 中,可以指定安装某些或者不安装某些模块,默认安装的模块只适合简单的场景,往往在稍微复杂的情况下,就需要额外添加其他模块,或者第三方以及自定义的模块。这高可扩展性正是 Nginx 的一大亮点。想要查看 configure 的可选参数,使用 --help 选项即可:

$ ./configure --help

在所有可选参数中,最常用的有两个:

  • –prefix=PATH:配置 Nginx 安装部署的根目录。类似于在 Windows 下安装软件,我们指定安装目录;

  • –with-xxx_module:–without-xxx_module 其中 xxx 表示 Nginx 一个模块的名称,例如:

    • with-http_ssl_module -> 支持SSL/TLS,即HTTPS

    • with-http_v2_module -> 支持HTTP/2

    • without-http_fastcgi_module -> 不使用fastcgi

为了后续测试功能完善,这里我们编译时候,尽可能多的引入一些模块:


要想启动Nginx服务程序以及将其加入到开机启动项中,也需要有脚本文件。可惜的是,在安装完Nginx软件包之后默认并没有为用户提供脚本文件,因此刘遄老师给各位读者准备了一份可用的启动脚本文件,大家只需在/etc/rc.d/init.d目录中创建脚本文件并直接复制下面的脚本内容即可(相信各位读者在掌握了第4章的内容之后,应该可以顺利看懂这个脚本文件)。

[root@linuxprobe nginx-1.6.0]# vim /etc/rc.d/init.d/nginx#!/bin/bash
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
        if [ -z "`grep $user /etc/passwd`" ]; then
                useradd -M -s /bin/nologin $user
        fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
        if [ `echo $opt | grep '.*-temp-path'` ]; then
                value=`echo $opt | cut -d "=" -f 2`
                if [ ! -d "$value" ]; then
                        # echo "creating" $value
                        mkdir -p $value && chown -R $user $value
                fi
        fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
        rh_status_q && exit 0
        $1
        ;;
stop)
        rh_status_q || exit 0
        $1
        ;;
restart|configtest)
$1
;;
reload)
        rh_status_q || exit 7
        $1
        ;;
force-reload)
        force_reload
        ;;
status)
        rh_status
        ;;
condrestart|try-restart)
        rh_status_q || exit 0
        ;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

保存脚本文件后记得为其赋予755权限,以便能够执行这个脚本。然后以绝对路径的方式执行这个脚本,通过restart参数重启Nginx服务程序,最后再使用chkconfig命令将Nginx服务程序添加至开机启动项中。大功告成!

[root@linuxprobe nginx-1.6.0]# chmod 755 /etc/rc.d/init.d/nginx
[root@linuxprobe nginx-1.6.0]# /etc/rc.d/init.d/nginx restart
Restarting nginx (via systemctl):                          [  OK  ]
[root@linuxprobe nginx-1.6.0]# chkconfig nginx on

启动 Nginx 后,首先使用ps -ef | grep nginx可以查看 Nginx 进程是否已经启动,基于默认的配置,我们将看到 2 个 Nginx 的启动进程:master 进程和 worker 进程。

这是我们在前面讲到的 Nginx 的 Master-Worker 机制,后面会进行详细讲解。另外,我们可以用命令 netstat -anltp | grep 80,看到 CentOS 上已经在监听 80 端口,而这个监听服务正是 Nginx。最后可以用浏览器或者 curl 命令直接在 CentOS 机器上检查 Nginx 服务:


[root@localhost ~]# curl http://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 ~]# ps -ef |grep nginx
root     37071     1  0 22:46 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www      37073 37071  0 22:46 ?        00:00:00 nginx: worker process
root     37105  2397  0 22:49 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

当出现 “Welcome to Nginx!" 这样的欢迎语句,表明我们的 Nginx 已经正常运行了。


猜你喜欢

转载自blog.51cto.com/14035933/2496034