Linux中编译、安装nginx

Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。 Nginx 是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

作为开源的服务器软件,在Linux系统中安装和其他开源软件的安装方法大同小异,无非就是编译,然后安装。下面介绍我编译安装nginx的过程:

工作机器各项参数如下:

CPU:Intel Xeon 5110

内存:DDR2 1G*4

主机型号:ProLiant DL140 G3

操作系统:Red Hat Enterprise Linux Server release 5.4 x86_64版

内核版本:2.6.18

gcc版本:4.1.2

g++版本:4.1.2

 

1.下载nginx源码:

截止到今天(2011年11月24日),最新的稳定版的nginx服务器版本号为1.0.10,可以从nginx的官方网站http://www.nginx.org/中下载到其源代码。 

这里强烈建议下载tar.gz格式的压缩包。在Linux下,文件访问有着严格的权限限制。一个文件是否允许以二进制或者脚本的形式执行,完全取决于其是否拥有执行缺陷,这与Windows识别文件后缀名(.exe、.bat)的方式不同。zip格式的压缩包中是不保留文件的权限信息的,而tar.gz格式的压缩包是保存有文件的权限信息的。

 

然后对其解压,并执行编译配置脚本:

[root@lxp2 Downloads]# tar -xf nginx-1.0.10.tar.gz 
[root@lxp2 Downloads]# cd nginx-1.0.10
[root@lxp2 nginx-1.0.10]# ./configure 

脚本执行到最后可能会报出如下错误:

checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

[root@lxp2 nginx-1.0.10]# 

这说明系统中缺少PCRE库。该库是实现正则表达式的基础,如果缺少此库,nginx无法支持HTTP中的URL重写功能。如果你不需要此功能,可以在执行编译配置脚本时加入“--without-http_rewrite_module”。但是,这里我们需要这项功能。于是下载PCRE库。

 

2.下载安装PCRE库:

PCRE库是实现Perl式正则表达式的基础。如果系统中缺少此库需要编译安装。可以从著名的开源软件网站sourceforge上下载:http://sourceforge.net/projects/pcre/files/pcre/。目前最新版本是8.20。

仍然是下载后解压、配置、编译和安装:

[root@lxp2 Downloads]# tar -xf pcre-8.20.tar.gz 
[root@lxp2 Downloads]# cd pcre-8.20
[root@lxp2 pcre-8.20]# ./configure
[root@lxp2 pcre-8.20]# make
[root@lxp2 pcre-8.20]# sudo make install

 

3.编译nginx:

2021年5月20日补充:有些情况下,本机内部自带的OpenSSL库、PCRE库或者zlib库版本比较旧,希望编译使用新版本库。只要下载相应的源代码,解压到一个目录并配置编译选项即可(以下代码在Ubuntu 16.04 + gcc 4.8.5 + nginx 1.20.0下测试通过):

./configure --with-cc-opt="-static -static-libgcc" --with-ld-opt="-static" --prefix=/root/nginx-static \
--with-openssl=/root/openssl-1.1.1k \
--with-pcre=/root/pcre-8.44 \
--with-zlib=/root/zlib-1.2.11 \
--with-threads --with-file-aio \
--with-http_ssl_module \
--with-http_auth_request_module \
--with-http_stub_status_module

默认情况下,nginx采用动态编译,使用ldd命令可以查看到编译出的nginx需要依赖系统库。而采用了静态编译方式后,使用ldd命令查看依赖库,提示:not a dynamic executable。此时可以把编译好的nginx任意放置在基于相同架构下的linux发行版中,即可直接使用。

 

如果编译配置脚本正常运行,会在最后报告出nginx将要安装的位置以及其他相关信息。阅读过之后直接编译就好:

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  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@lxp2 nginx-1.0.10]# make

然后进行安装:

[root@lxp2 nginx-1.0.10]# sudo make install

安装到的默认位置是/usr/local/nginx

 

安装完毕之后一般需要对其进行优化配置。在很多时候一台nginx可以承载很多业务,它扮演的是各个子业务系统的入口,通过主机头来区分不同的业务应用。只有一个nginx.conf配置文件就会给今后的运维工作带来很大的麻烦,为此我们将nginx的配置进行了重新的梳理

 

首先我们把默认的server配置全部删除。只保留基本的http配置。默认的访问日志内容不够全面,于是我们对它进行修改,记录更多的字段。

 

log_format  main  '$time_iso8601'
                        '       $request_uri'
                        '       $status'
                        '       $bytes_sent'
                        '       $upstream_cache_status'
                        '       $request_time'
                        '       $upstream_response_time'
                        '       $host'
                        '       $remote_addr'
                        '       $server_addr'
                        '       $upstream_addr'
                        '       $http_referer'
                        '       $http_user_agent'
                        '       $http_x_forwarded_for'
                        '       web'
                        '       -'                                              #$cachezone
                        '       $sent_http_cache_control'
                        '       $http_nagios'
                        '       $upstream_status'
                        '       -'                                              #$cache_process_time


虽然最终要启用gzip压缩,但由于细化配置较多,我们保持nginx.conf中的#gzip on被注释掉的状态,增加如下配置

 

 

#gzip  on;
include  gzip.conf;
include  proxy.conf;
include  vhost/*.conf;

关于gzip.conf的配置详情:

 

 

#找不到预压缩文件,进行动态压缩
gzip on;
gzip_min_length   1k;  #设置最小的压缩值,单位为bytes.超过设置的min_length的值会进行压缩,小于的不压缩.
gzip_comp_level   3;   #压缩等级设置,1-9,1是最小压缩,速度也是最快的;9刚好相反,最大的压缩,速度是最慢的,消耗的CPU资源也多
gzip_buffers      16 64k;   #设置系统的缓存大小,以存储GZIP压缩结果的数据流,它可以避免nginx频烦向系统申请压缩空间大小
gzip_types text/plain application/x-javascript text/css text/javascript;

#关于gzip_types,如果你想让图片也开启gzip压缩,那么用以下这段吧:
#gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;

#gzip公共配置
gzip_http_version 1.1;      #识别http的协议版本(1.0/1.1)
gzip_proxied      any;      #设置使用代理时是否进行压缩,默认是off的
gzip_vary         on;       #和http头有关系,加个vary头,代理判断是否需要压缩
gzip_disable "MSIE [1-6]."; #禁用IE6的gzip压缩


关于proxy.conf的配置详情:

 

 

#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path   /tmp/proxy_temp;

#设置Web缓存区名称为cache_one,内存缓存空间大小为500MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path  /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;

client_body_buffer_size  10m;     #原为512k
proxy_connect_timeout    10;       #代理连接超时
proxy_read_timeout       30;      #代理发送超时
proxy_send_timeout       30;      #代理接收超时
proxy_buffer_size        128k;     #代理缓冲大小,原为32k
proxy_buffers           16 256k;   #代理缓冲,原为4 64k
proxy_busy_buffers_size 512k;      #高负荷下缓冲大小,原为128k
proxy_temp_file_write_size 1024m;  #proxy缓存临时文件的大小原为128k
#proxy_ignore_client_abort  on;    #不允许代理端主动关闭连接
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;


接下来就是针对具体应用进行配置了,首先创建虚拟主机配置专用的目录

 

[root@lxp2 pcre-8.20 conf]# mkdir vhost
[root@lxp2 pcre-8.20 conf]# cd vhost/

 

 

针对每一个主机,分成两个配置文件:XXX_server.conf和upstream_XXX.conf。前者用来配置主机名、转发规则,后者用来配置提供服务的后端服务器。

 

 

例如:admin_web_system_server.conf

 

server {
	listen 80;

	server_name admin.business.com;
	client_max_body_size 35m;

	location ^~ / {
		access_log  /data/log/nginx/logs/admin_web_system/access.log main;
		proxy_next_upstream http_502 http_504 error timeout invalid_header;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://admin_web_system;
		proxy_pass_header Set-Cookie;
	}
	#limit_conn perip 50;
}

后端服务器配置:upstream_admin_web_system.conf

 

upstream admin_web_system {
         server 192.168.254.100:8080 max_fails=2 fail_timeout=30s;
         server 192.168.254.100:8081 max_fails=2 fail_timeout=30s;
}


在上述配置中,还缺少统一存放日志的目录:/data/log/nginx/logs/admin_web_system,创建一下就好了。

 

接下来测试一下配置是否正确:

 

[root@lxp2 vhost]#cd ..
[root@lxp2 conf]# cd ..
[root@lxp2 nginx]# cd sbin/
[root@lxp2 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

 

 

提示一切正常就可以启动了:

 

[root@lxp2 sbin]# ./nginx

 

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值