Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update nginx in ubuntu 14.04

In my server, nginx -v returns nginx version: nginx/1.4.6 (Ubuntu), it is very old, so I decided to update it to the latest stable version.

Then, I followed this answer:

sudo apt-get update
sudo apt-get install nginx

It gives:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc
The following packages will be upgraded:
  nginx nginx-common nginx-core
3 upgraded, 0 newly installed, 0 to remove and 311 not upgraded.
Need to get 349 kB of archives.
After this operation, 3,072 B of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://mirrors.digitalocean.com/ubuntu/ trusty-updates/main nginx-common all 1.4.6-1ubuntu3.7 [19.0 kB]
Get:2 http://mirrors.digitalocean.com/ubuntu/ trusty-updates/main nginx all 1.4.6-1ubuntu3.7 [5,352 B]
Get:3 http://mirrors.digitalocean.com/ubuntu/ trusty-updates/main nginx-core amd64 1.4.6-1ubuntu3.7 [325 kB]
Fetched 349 kB in 0s (431 kB/s)
Preconfiguring packages ...
(Reading database ... 111751 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3.7_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3.7) over (1.4.6-1ubuntu3.5) ...
Preparing to unpack .../nginx_1.4.6-1ubuntu3.7_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3.7) over (1.4.6-1ubuntu3.5) ...
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3.7_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3.7) over (1.4.6-1ubuntu3.5) ...
Processing triggers for ufw (0.34~rc-0ubuntu2) ...
Processing triggers for ureadahead (0.100.0-16) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up nginx-common (1.4.6-1ubuntu3.7) ...
Setting up nginx-core (1.4.6-1ubuntu3.7) ...
Setting up nginx (1.4.6-1ubuntu3.7) ...

However, nginx -v still returns nginx version: nginx/1.4.6 (Ubuntu).

Does anyone know a safe way to update nginx, without modifying configuration files? Because it is in the server in production, I would like to be very careful...

like image 533
SoftTimur Avatar asked May 22 '17 14:05

SoftTimur


3 Answers

Update in 2019: the ppa solution can be used to upgrade Nginx to 1.14.x. In order to upgrade Nginx to the latest stable or mainline version, you need to uninstall Nginx and then re-install it following the document:

// Uninstall Nginx
sudo apt-get remove nginx

//Then, install Nginx following document: http://nginx.org/en/linux_packages.html#Ubuntu
sudo apt 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
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
sudo apt update
sudo apt install nginx
like image 75
shaochuancs Avatar answered Nov 11 '22 01:11

shaochuancs


First, run

sudo apt-get install software-properties-common python-software-properties

Then, add the nginx stable repo:

sudo add-apt-repository ppa:nginx/stable

then run

sudo apt-get update

and

sudo apt-get install nginx

like image 21
Oliver Avatar answered Nov 11 '22 03:11

Oliver


The latest Ubuntu 14.04 comes with nginx-1.4.6 and its quite a good idea to upgrade your nginx to the latest stable version. One valid reason to upgrade might be support for http/2 which is officially available after on and after nginx - 1.9.5

Posting answer here for a safer upgrade so that it can help someone in future.

The most recommended way to install nginx is to use its ppa. If possible stop nginx before installing the new one . Also its always a good idea to take backup of any configuration you might have done. Best case backup /etc/nginx somewhere safe. However the most important backup file you will need is nginx.conf which for sure will override with new config. Also don't forget to add the nginx signing key. More about it here

cd /tmp
sudo add-apt-repository ppa:nginx/stable
wget sudo add-apt-repository ppa:nginx/stable
sudo apt-key add nginx_signing.key
rm nginx_signing.key
sudo apt-get update
sudo cp -r /etc/nginx nginx-bkp
sudo service stop nginx 
sudo apt-get install nginx

While upgrading nginx it will ask you the below question. First be sure you have taken backup of nginx.conf then Answer Y. I would first like to check difference with D then use Y. enter image description here

In my case I had custom log format defined in nginx.conf because of which nginx failed to start after upgrade. All I had to do was open /etc/nginx/nginx.conf and provide my custom logformat to make everything work again.

After that check your nginx version and start it

> sudo nginx -v 
nginx version: nginx/1.12.2
> sudo service nginx start
like image 25
Ankit Kulkarni Avatar answered Nov 11 '22 01:11

Ankit Kulkarni