安装Nginx
首先安装常规驱动:
apt-get install libxml2 libxslt1-dev libgd2-xpm libgd2-xpm-dev libgeoip-dev libpcre3-dev libssl-dev
为了支持rewrite功能,安装pcre:
yum install pcre*
为了支持ssl,安装openssl:
yum install openssl*
安装nginx:
wget http://nginx.org/download/nginx-1.9.12.tar.gz
./configure --prefix=/usr/local/nginx-1.9.12 \
--with-http_realip_module \
--with-http_ssl_module --with-http_spdy_module \
--with-http_stub_status_module --with-pcre \
--add-module=../ngx_cache_purge-1.9
–prefix=/usr/local/nginx-1.9.12: 安装路径
–with-http_stub_status_module: 支持状态查询
–with-http_realip_module:启用realip模块(将用户IP转发给后端服务器)
–with-http_ssl_module: 支持https
–with-http_spdy_module: 支持google的spdy,前提是必须安装ssl支持
–with-pcre: 为了支持rewrite重写功能,必须定制pcre
–add-module=../ngx_cache_purge-1.9: 添加缓存清除扩展模块
configure完成后:
make && make install
启动,关闭,重置
直接启动:
/usr/local/nginx-1.9.12/sbin/nginx
启动后进行测试:
curl -s http://localhost | grep nginx.com
nginx.com.
关闭:
/usr/local/nginx-1.9.12/sbin/nginx -s stop
测试配置文件是否正确:
nginx -t -c nginx.conf
当有修改配置文件时,进行reload:
/usr/local/nginx-1.9.12/sbin/nginx -s reload
配置日志格式:
log_format mylog '$remote_addr | $remote_user | [$time_local] | "$request" | "$request_body" | $request_time | '
'$status | $body_bytes_sent | "$http_referer" | "$http_user_agent" | $http_x_forwarded_for';
使用Nginx内置模块创建一个空的GIF图:
# 直接在server块中添加一个路由:
location /app/v1/report {
empty_gif;
}
更新现有的版本
使用升级脚本
wget soft.vpser.net/lnmp/upgrade_nginx.sh;sh upgrade_nginx.sh
然后按提示输入要更新的版本号,确认后回车,程序开始安装.
手动升级
首先下载需要升级版本的源码文件,解压后进入对应目录,然后查看当前版本的配置信息:
nginx -V
会看到当前版本的所有配置信息:
--prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug \
--with-http_addition_module --with-http_dav_module --with-http_geoip_module \
--with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module \
--with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module \
--with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module
然后执行configure命令并跟上上面的配置信息,如果上面的配置中有--add-module
部分则需要去掉,因为不同的版本有不同的模块名和路径,如果需要使用可以单独进行安装:
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf \ .....
如果执行中遇到什么错误,进行修复后重新执行以上命令:
配置 –with-http_xslt_module 时提示 the HTTP XSLT module requires the libxml2/libxslt libraries: yum install -y libxml2 libxml2-devel libxslt libxslt-devel
配置 –with-http_image_filter_module 时提示 the HTTP image filter module requires the GD library: yum install -y libgdata libgdata-devel
配置 –with-http_geoip_module 时提示 the GeoIP module requires the GeoIP library: 安装geoip对应的驱动
rewrite需要pcre支持, 错误提示:./configure: error: the HTTP rewrite module requires the PCRE library: yum install -y pcre*
Ubuntu下可能需要安装的驱动:
apt-get install libxml2 libxml2-dev libxslt-dev libgd2-xpm libgd2-xpm-dev geoip-database libgeoip-dev libpcre3 libpcre3-dev
没有提示错误后进行make:
make
编译完成后多出一个nginx文件夹,就是已编译好的nginx程序.
备份旧的nginx程序的名字,并复制新的程序进去,首先cd到旧的nginx目录:
mv /usr/sbin/nginx /usr/sbin/nginx.backends
cp objs/nginx /usr/sbin/nginx
执行检查命令对配置进行检查:
/usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
然后回到新版nginx的解压目录,执行更新:
make install
make upgrade
但是得到一个错误:
make: /etc/nginx/sbin/nginx: Command not found
make: *** [upgrade] Error 127
用文本编辑器打开Makefile文件,将upgrade节点中的/etc/nginx/sbin/nginx改为/usr/sbin/nginx -t,保存后关闭并重新执行:
make upgrade
然后执行命令进行版本检查:
nginx -V
1.9.12