nginx是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。nginx占用内存少,并发能力强。nginx是开源的,不过最近nginx也推出了付费版的nginx plus,比开源版的增多或加强了一些模块功能。
下面将介绍一下nginx开源版的安装。
版本选择
开源版的nginx有两个版本:主线版本和稳定版本。
主线版本:主线版本具有一些最新的特性,但同时新引入的特性可能会存在一定的bug。
稳定版本:稳定版本没有一些新特性,但是存在的bug隐患较少。
个人建议:实验用选择主线版本,生产选择稳定版本。
安装方式
nginx的安装可以选择预编译包或者源码安装。
预编译包安装更快捷方便,同时大多数常用的官方模块。而源码安装需要重新编译源码,略微复杂一点,但是更灵活,自己可以定制安装需要的模块,同时可以尝试一些新的特性。
预安装包
虽然使用预安装包安装并不是那么灵活,但是足够简单快捷,几条命令就可以快速安装和使用nginx。
由于个人偏好使用ubuntu,所以就介绍一下在ubuntu下的安装流程。
预安装的配置参数如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 |
最最简单的方式就是使用ubuntu默认的repository。
1 2 3 4 5 6 | #更新ubunturepository sudo apt-get update #安装nginx sudo apt-get install nginx #验证安装 nginx -v |
使用ubuntu的repository安装的nginx版本较低,如果14.04下nginx版本才到1.4.6。
需要安装新版,需要使用nginx的repository。
下载和添加用来apt签名nginx 包和repository的key。
1 2 | $ sudo wget http://nginx.org/keys/nginx_signing.key $ sudo apt-key add nginx_signing.key |
添加apt源
修改/etc/apt/sources.list,并根据不同的系统版本和不同的nginx添加相应的 deb 源。
1 2 | deb http://nginx.org/packages/mainline/ubuntu/ codename nginx deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx |
codename是ubuntu的release名。
| Version | Codename | Supported Platforms |
|---|---|---|
| 12.04 | precise | x86_64, i386 |
| 14.04 | trust | x86_64, i386, aarch64/arm64 |
| 15.10 | wily | x86_64, i386 |
| 16.04 | xenial | x86_64, i386 |
所以,ubuntu14.04 安装mainline如下:
1 2 | deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx |
安装稳定版如下:
1 2 | deb http://nginx.org/packages/ubuntu/ trusty nginx deb-src http://nginx.org/packages/ubuntu/ trusty nginx |
更新apt源并安装nginx
1 2 3 | $ sudo apt-get remove nginx-common $ sudo apt-get update $ sudo apt-get install nginx |
至此,nginx已安装成功。可查询安装的nginx版本验证安装。
1 2 | $ nginx -v nginx version: nginx/1.10.2 |
源码安装
此文暂不介绍,后续补充。
常见命令
安装好后,nginx会按默认的配置文件启动nginx。默认的配置文件位于/etc/nginx/nginx.conf。
常见的一些文件目录如下:
1 2 3 4 | 配置文件: /etc/nginx/ningx.conf error log: /var/log/nginx/error.log http log: /var/log/nginx/access.log 模块: /usr/lib/nginx/modules |
具体的信息,可以查看编译选项。
1 | nginx -V |
可以一下指令启停nginx。
1 2 3 | sudo service nginx start sudo service nginx stop sudo service nginx restart |
启动的nginx有一个master进程和若干的worker进程。主进程负责解析配置文件和调度worker进程。而worker进程负责具体的处理request。
所以控制nginx也可以通过向master进程发送信号来完成。
1 2 3 4 5 6 | nginx -s signal quit: graceful退出,处理完当前请求再退出 reload:重新加载配置文件 reopen:重新打开log文件 stop:立即退出 |