安装/升级最新版Nginx
我的VPS上的Nginx记得还是一年前安装的,一直没有升级,最近发现版本都到了1.14了,我的还是1.4,就想做一次升级。
由于使用Nginx用到一些配置文件,所以我预计升级会导致配置文件恢复到默认,所以查询了一些资料后,安全的做了升级。
备份nginx.conf配置文件:
cp nginx.conf nginx.conf.bak
从官方 ppa 源安装
我将自定义server放在单独文件夹内 /etc/nginx/my-server/, 所以配置文件是安全的,升级后需要重新在 nginx.conf 里 include 一下即可。
安装依赖包:
apt-get install curl gnupg2 ca-certificates lsb-release
设置安装稳定版:
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list导入 nginx 官方 key 使 apt 可以鉴定来源:
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
Verify that you now have the proper key:
apt-key fingerprint ABF5BD827BD9BF62
The output should contain the full fingerprint 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62 as follows:
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
uid [ unknown] nginx signing key <[email protected]>安装Nginx,期间会提示有些文件要修改,根据提示覆盖Y或查看不同D,'N'保留原文件:
apt-get update
apt-get install nginx
如果安装失败,提示 error 类似:/var/cache/apt/archives/nginx_1.14.1-1~trusty_amd64.deb,运行下面命令:
apt-get -f install
apt-get remove nginx-common
apt-get install nginx
安装完成后,打开备份的nginx.conf.bak文件,将里面你之前做了自定义修改的字段复制回当前nginx.conf文件内,或者恢复单独的在文件夹的配置文件,并在 nginx.conf 里 include 一下。
检查配置文件是否正确:
service nginx configtest
重启Nginx服务:
service nginx reload
service nginx start
以上就完成了从官方源 Nginx 的升级。
从源码编译安装
此方法适用于需要安装第三方的一些模块的情况,例如需要 fancyindex 等模块。
从此处下载最新源码:http://nginx.org/en/download.html
官方编译教程,里面列出了可用的编译参数:http://nginx.org/en/docs/configure.html
下载源码并解压:
wget http://nginx.org/download/nginx-1.17.6.tar.gz
tar -zxvf nginx-1.17.6.tar.gz
下载第三方模块,如:fancyindex:
git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex
运行配置脚本:
cd nginx-1.17.6
./configure --add-module=../ngx-fancyindex [extra desired options]
extra desired options:
如果不知道该加入那些附加编译参数,通过 nginx -V 可以查看当前官方预编译版本的配置变量。
nginx 官方编译版本默认的附加传递参数 extra desired options 如下:
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --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-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'编译和安装:
make
make install
如果安装失败,提示 error,尝试运行下面命令,然后再次执行上面的 make install:
apt-get -f install
apt-get remove nginx-common
安装完成后的设置和上面 ppa 安装类似。
如果运行 service nginx start 后,nginx 无法运行,查看 80 端口是否被占用,选择卸载或 kill 相关进程:
lsof -i:80