一 什么是nginx
Nginx是一款轻量级的Web 服务器,反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强,能够支持高达 50,000 个并发连接数的响应。事实上nginx的并发能力确实在同类型的网页服务器中表现较好,使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等
二 环境准备
1 准备一台干净的centos7服务器,或者虚拟机或者购买的VPS。Nginx是C开发的,建议在 Linux上运行,当然,也可以安装Windows 版本。
2 安装依赖,安装需要gcc环境,所以需要安装gcc;zlib是用来对http包的内容进行gzip压缩的;openssl则是支持https的SSL协议;pcre库是用来匹配正则的,rewrite规则需要它
yum install gcc-c++ pcre pcre-devel zlib zlib-devel
3.下载最新稳定版的nginx,目前稳定版是1.14.2。官网下载地址:http://nginx.org/en/download.html,下载后将安装包上传到CentOS中。
可以使用wget命令直接下载到CentOS系统中:
wget http://nginx.org/download/nginx-1.14.2.tar.gz
groupadd www
useradd -s /sbin/nologin -g www www
4 编译安装
进入下载的安装包目录,解压nginx,进入nginx解压后的目录,配置安装参数:
tar -zxvf nginx -1.14.2.tar.gz
cd nginx -1.14.2
更改源码修改软件名称和版本号
[root@localhost core]# pwd
/usr/local/src/nginx-1.8.1/src/core
[root@localhost core]# vim nginx.h
#define nginx_version 8008001
#define NGINX_VERSION "8.12.0"
#define NGINX_VER "wg007/" NGINX_VERSION
#ifdef NGX_BUILD
#define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD NGINX_VER
#endif
#define NGINX_VAR "WG007"
#define NGX_OLDPID_EXT ".oldbin"
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
http_stub_status_module 作用:监控nginx运行状态
http_sub_module 作用:用于替换字符串
http_ssl_module 作用:支持https访问
http_gzip_static_module 作用:实现静态压缩功能,节约带宽
进行编译安装
make && make install
5 管理nginx
编辑nginx 启动脚本
# chkconfig: 2345 99 20<br>2345表示系统运行级别是2,3,4或者5时都启动此服务,<br>99,是启动的优先级,<br>20, 是关闭的优先级
#description: nginx-server
nginx=/usr/local/nginx/sbin/nginx
case $1 in
start)
netstat -anptu | grep nginx
if [ $? -eq 0