大型网站分布式架构(六)—— Nginx的安装

我们可以使用Nginx做什么:

  • web服务器
  • 负载均衡和反向代理

编译Nginx所需依赖

依赖 功用
pcre 解决C语言中使用正则表达式的问题,rewrite模块需要 pcre 库
zlib 通用的压缩库,gzip模块需要 zlib 库
openssl 提供了丰富的加密解密的相关功能,ssl 功能需要openssl库
perl openssl的编译需要perl
gcc nginx的编译需要gcc

安装依赖

如果主机上没有以上依赖可以执行以下命令:

yum -y install gcc perl zlib pcre-devel openssl openssl-devel

获得Nginx安装包

[root@centos6-1 ~]# cd /export/servers/
[root@centos6-1 servers]# wget http://nginx.org/download/nginx-1.14.0.tar.gz

###解压安装包

[root@centos6-1 servers]# tar -zvxf nginx-1.14.0.tar.gz

编译安装

执行configure

[root@centos6-1 servers]# cd nginx-1.14.0
[root@centos6-1 nginx-1.14.0]# ./configure

博主报了以下错误

./configure: error: the HTTP rewrite module requires the PCRE library.

博主主机缺少PCRE依赖,现在安装

[root@centos6-1 nginx-1.14.0]# yum install pcre-devel

再执行configure,结果如下

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/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@centos6-1 nginx-1.14.0]# make & make install

启动Nginx

默认安装路径为:/usr/logcal/nginx

[root@centos6-1 ~]# cd /usr/local/
[root@centos6-1 local]# ll
total 44
drwxr-xr-x. 2 root root 4096 Jul  3 21:56 bin
drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
drwxr-xr-x. 3 root root 4096 Jul  3 21:56 include
drwxr-xr-x. 3 root root 4096 Jul  3 21:56 lib
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x. 6 root root 4096 Aug 26 17:01 nginx
drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x. 5 root root 4096 Jun 26 22:25 share
drwxr-xr-x. 2 root root 4096 Jul  8 13:33 src
[root@centos6-1 local]# cd nginx/
[root@centos6-1 nginx]# ll
total 16
drwxr-xr-x. 2 root root 4096 Aug 26 17:01 conf
drwxr-xr-x. 2 root root 4096 Aug 26 17:01 html
drwxr-xr-x. 2 root root 4096 Aug 26 17:01 logs
drwxr-xr-x. 2 root root 4096 Aug 26 17:01 sbin
[root@centos6-1 nginx]# cd sbin/
[root@centos6-1 sbin]# ll
total 3596
-rwxr-xr-x. 1 root root 3679027 Aug 26 17:01 nginx
[root@centos6-1 sbin]# ./nginx 
[root@centos6-1 sbin]# ps -ef|grep nginx
root     101575      1  0 17:04 ?        00:00:00 nginx: master process ./nginx
nobody   101576 101575  0 17:04 ?        00:00:00 nginx: worker process
root     101578  96782  0 17:04 pts/1    00:00:00 grep nginx

可以看到nginx已经启动了,一个master进程,一个worker进程

web访问

这里写图片描述

总结

nginx已经安装成功。除了通过源码编译安装,还可以通过其他方式安装,但是源码编译可以安装最新版的nginx。在安装过程中会出现一些bug,只需要根据错误提示解决即可。当然博主这里只是介绍了最简单暴力的默认编译安装方式,如果想了解Nginx的编译安装的其他配置细节可以进入Nginx官网获取相关信息。博主后面将使用Nginx与Tomcat集成,优化web的业务处理。