在网上介绍Nginx怎样安装的教程一搜索一大堆,为什么还要写这篇博文呢?一是为了加深自己的印象,二是我安装过程中遇到的问题你可能也会遇到,做一个参考也可以啊。
源码编译安装主要有五个步骤:
(1)下载Nginx源码包,如:nginx-1.16.1.tar.gz
(2)使用 tar 命令解压源码包
(3)使用 ./configure 命令去自定义我们自己的Nginx,指定安装位置等(见 ./configure 详解)
(4)使用 make 命令去编译源码
(5)使用 make install 安装程序,即将程序拷贝到指定的目录
这个你肯定会,命令如下:
[root@S1 ~]# tar -xvzf nginx-1.16.1.tar.gz
这里我就没有给出解压的输出信息了。
这里我们不会做太多的配置,只使用“--prefix=PATH”选项去指定 Nginx 的安装路径。执行命令如下:
[root@S1 nginx-1.16.1]# ./configure --prefix=/root/nginx
很不幸,遇到了“./configure: error: C compiler cc is not found”问题,这是缺少C编译器,安装 gcc 即可。如下:
[root@S1 nginx-1.16.1]# yum install -y gcc
继续执行“configure”。又遇到新问题“./configure: error: the HTTP rewrite module requires the PCRE library. ”这是缺少PCRE库,安装“pcre-devel”即可。如下:
[root@S1 nginx-1.16.1]# yum install -y pcre-devel
继续执行“configure”。运气很差啊!继续遇到新问题“./configure: error: the HTTP gzip module requires the zlib library.”这是缺少HTTP gzip压缩的zlib库,安装“zlib-devel”即可,如下:
[root@S1 nginx-1.16.1]# ./configure --prefix=/root/nginx
这下没有新问题了,执行完后面信息如下:
Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/root/nginx" # 安装Nginx的基础目录 nginx binary file: "/root/nginx/sbin/nginx" # Nginx二进制文件所在目录 nginx modules path: "/root/nginx/modules" # 模块目录 nginx configuration prefix: "/root/nginx/conf" # 配置目录 nginx configuration file: "/root/nginx/conf/nginx.conf" nginx pid file: "/root/nginx/logs/nginx.pid" nginx error log file: "/root/nginx/logs/error.log" nginx http access log file: "/root/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@S1 nginx-1.16.1]#
成功执行完成后,你会发现在Nginx的基础目录下面多出了一个objs目录,该目录信息如下:
root@S1 nginx-1.16.1]# ll objs/ total 84 -rw-r--r--. 1 root root 19575 Jul 2 19:15 autoconf.err -rw-r--r--. 1 root root 39934 Jul 2 19:15 Makefile -rw-r--r--. 1 root root 6598 Jul 2 19:15 ngx_auto_config.h -rw-r--r--. 1 root root 657 Jul 2 19:14 ngx_auto_headers.h -rw-r--r--. 1 root root 5856 Jul 2 19:15 ngx_modules.c drwxr-xr-x. 9 root root 4096 Jul 2 19:15 src
nginx 通过 ngx_auto_headers.h 和 ngx_auto_config.h 动态安装一些模块。
执行“make”命令去编译源码,如下:
[root@S1 nginx-1.16.1]# make
再次遇到问题,遇到了“-bash: make: command not found”这是因为我们安装CentOS是安装的mini最小系统,因为我们需要自己去安装 make。如下:
[root@S1 nginx-1.16.1]# yum -y install automake autoconf libtool make
继续执行 make,然后执行 make install。执行成功后进入到 /root/nginx 查看详情,如下:
[root@S1 ~]# cd nginx [root@S1 nginx]# ll total 16 drwxr-xr-x. 2 root root 4096 Jul 2 19:29 conf drwxr-xr-x. 2 root root 4096 Jul 2 19:29 html drwxr-xr-x. 2 root root 4096 Jul 2 19:29 logs drwxr-xr-x. 2 root root 4096 Jul 2 19:29 sbin
然后进入到“sbin”目录运行“./nginx”去启动Nginx服务。然后使用netstat查看端口情况,如下:
[root@S1 nginx]# cd sbin/ [root@S1 sbin]# ./nginx [root@S1 sbin]# netstat -ano | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN off (0.00/0/0)
也可以通过浏览器访问,浏览器输入“https://localhost”地址即可访问。